about summary refs log tree commit diff
path: root/nixos/modules/services/cluster/panamax.nix
blob: b47ff744fc27be6adda8b147945506068e74a5fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.panamax;

  panamax_api = pkgs.panamax_api.override { dataDir = cfg.dataDir + "/api"; };
  panamax_ui = pkgs.panamax_ui.override { dataDir = cfg.dataDir + "/ui"; };

in {

  ##### Interface
  options.services.panamax = {
    enable = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Whether to enable Panamax service.
      '';
    };

    UIPort = mkOption {
      type = types.int;
      default = 8888;
      description = ''
        Panamax UI listening port.
      '';
    };

    APIPort = mkOption {
      type = types.int;
      default = 3000;
      description = ''
        Panamax UI listening port.
      '';
    };

    dataDir = mkOption {
      type = types.str;
      default = "/var/lib/panamax";
      description = ''
        Data dir for Panamax.
      '';
    };

    fleetctlEndpoint = mkOption {
      type = types.str;
      default = "http://127.0.0.1:4001";
      description = ''
        Panamax fleetctl endpoint.
      '';
    };

    journalEndpoint = mkOption {
      type = types.str;
      default = "http://127.0.0.1:19531";
      description = ''
        Panamax journal endpoint.
      '';
    };

    secretKey = mkOption {
      type = types.str;
      default = "SomethingVeryLong.";
      description = ''
        Panamax secret key (do change this).
      '';
    };

  };

  ##### Implementation
  config = mkIf cfg.enable {
    systemd.services.panamax-api = {
      description = "Panamax API";

      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" "fleet.service" "etcd.service" "docker.service" ];

      path = [ panamax_api ];
      environment = {
        RAILS_ENV = "production";
        JOURNAL_ENDPOINT = cfg.journalEndpoint;
        FLEETCTL_ENDPOINT = cfg.fleetctlEndpoint;
        PANAMAX_DATABASE_PATH = "${cfg.dataDir}/api/db/mnt/db.sqlite3";
      };

      preStart = ''
        rm -rf ${cfg.dataDir}/state/tmp
        mkdir -p ${cfg.dataDir}/api/{db/mnt,state/log,state/tmp}
        ln -sf ${panamax_api}/share/panamax-api/_db/{schema.rb,seeds.rb,migrate} ${cfg.dataDir}/api/db/

        if [ ! -f ${cfg.dataDir}/.created ]; then
          bundle exec rake db:setup
          bundle exec rake db:seed
          bundle exec rake panamax:templates:load || true
          touch ${cfg.dataDir}/.created
        else
          bundle exec rake db:migrate
        fi
      '';

      serviceConfig = {
        ExecStart = "${panamax_api}/bin/bundle exec rails server --binding 127.0.0.1 --port ${toString cfg.APIPort}";
        User = "panamax";
        Group = "panamax";
      };
    };

    systemd.services.panamax-ui = {
      description = "Panamax UI";

      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" "panamax_api.service" ];

      path = [ panamax_ui ];
      environment = {
        RAILS_ENV = "production";
        JOURNAL_ENDPOINT = cfg.journalEndpoint;
        PMX_API_PORT_3000_TCP_ADDR = "localhost";
        PMX_API_PORT_3000_TCP_PORT = toString cfg.APIPort;
        SECRET_KEY_BASE = cfg.secretKey;
      };

      preStart = ''
        mkdir -p ${cfg.dataDir}/ui/state/{log,tmp}
        chown -R panamax:panamax ${cfg.dataDir}
      '';

      serviceConfig = {
        ExecStart = "${panamax_ui}/bin/bundle exec rails server --binding 127.0.0.1 --port ${toString cfg.UIPort}";
        User = "panamax";
        Group = "panamax";
        PermissionsStartOnly = true;
      };
    };

    users.extraUsers.panamax =
    { uid = config.ids.uids.panamax;
      description = "Panamax user";
      createHome = true;
      home = cfg.dataDir;
      extraGroups = [ "docker" ];
    };

    services.journald.enableHttpGateway = mkDefault true;
    services.fleet.enable = mkDefault true;
    services.cadvisor.enable = mkDefault true;
    services.cadvisor.port = mkDefault 3002;
    virtualisation.docker.enable = mkDefault true;

    environment.systemPackages = [ panamax_api panamax_ui ];
    users.extraGroups.panamax.gid = config.ids.gids.panamax;
  };
}