Class: ConfCtl::Cli::Configuration

Inherits:
Command
  • Object
show all
Defined in:
lib/confctl/cli/configuration.rb

Constant Summary collapse

DIR_MODE =
0o755
FILE_MODE =
0o644

Instance Attribute Summary

Attributes inherited from Command

#args, #gopts, #opts

Instance Method Summary collapse

Methods inherited from Command

#ask_action, #ask_confirmation, #ask_confirmation!, #initialize, #require_args!, run, #run_method, #use_color?, #use_pager?

Constructor Details

This class inherits a constructor from ConfCtl::Cli::Command

Instance Method Details

#addObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/confctl/cli/configuration.rb', line 161

def add
  require_args!('name')

  name = args[0]
  dir = File.join('cluster', name)
  depth = name.count('/')

  raise "#{dir} already exists" if Dir.exist?(dir)

  mkdir_p(dir)

  mkfile(File.join(dir, 'module.nix')) do |f|
    f.puts(<<~END
      { config, ... }:
      {
        cluster."#{name}" = {
          spin = "nixos";
          swpins.channels = [ "nixos-unstable" ];
          host = { name = "machine"; domain = "example.com"; };
        };
      }
    END
          )
  end

  mkfile(File.join(dir, 'config.nix')) do |f|
    f.puts(<<~END
      { config, pkgs, lib, ... }:
      {
        imports = [
          #{'../' * (depth + 2)}environments/base.nix
        ];

        # ... standard NixOS configuration ...

        networking.hostName = "#{name.gsub('/', '-')}";
      }
    END
          )
  end

  rediscover
end

#initObject



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
157
158
159
# File 'lib/confctl/cli/configuration.rb', line 12

def init
  dir = File.realpath(Dir.pwd)

  Dir.entries(dir).each do |v|
    raise 'init must be called in an empty directory' unless %w[. .. shell.nix .confctl .gems
                                                                .gitignore].include?(v)
  end

  mkdir('cluster')

  mkfile('cluster/module-list.nix') do |f|
    f.puts(<<~END
      (import ./cluster.nix) ++ [
        # Place for custom modules
      ]
    END
          )
  end

  mkfile('cluster/cluster.nix') do |f|
    f.puts(<<~END
      # This file is generated by confctl, changes will be lost
      []
    END
          )
  end

  mkdir('configs')
  mkfile('configs/confctl.nix') do |f|
    f.puts(<<~END
      { config, ... }:
      {
        confctl = {
          # listColumns = {
          #   "name"
          #   "spin"
          #   "host.fqdn"
          # };
        };
      }
    END
          )
  end

  mkfile('configs/swpins.nix') do |f|
    f.puts(<<~END
      { config, ... }:
      let
        nixpkgsBranch = branch: {
          type = "git-rev";

          git-rev = {
            url = "https://github.com/NixOS/nixpkgs";
            update.ref = "refs/heads/${branch}";
          };
        };

        vpsadminosBranch = branch: {
          type = "git-rev";

          git-rev = {
            url = "https://github.com/vpsfreecz/vpsadminos";
            update.ref = "refs/heads/${branch}";
          };
        };
      in {
        confctl.swpins.channels = {
          nixos-unstable = { nixpkgs = nixpkgsBranch "nixos-unstable"; };

          # nixos-stable = { nixpkgs = nixpkgsBranch "nixos-20.09"; };

          # vpsadminos-staging = { vpsadminos = vpsadminosBranch "staging"; };
        };
      }
    END
          )
  end

  mkdir('data')

  mkfile('data/default.nix') do |f|
    f.puts(<<~END
      { lib }:
      {
        # Place to load custom data sets
        sshKeys = import ./ssh-keys.nix;
      }
    END
          )
  end

  mkfile('data/ssh-keys.nix') do |f|
    f.puts(<<~END
      rec {
        admins = [
          # someone
        ];

        someone = "...ssh public key...";
      }
    END
          )
  end

  mkdir('environments')

  mkfile('environments/base.nix') do |f|
    f.puts(<<~END
      { config, pkgs, confData, ... }:
      {
        time.timeZone = "Europe/Amsterdam";

        services.openssh.enable = true;

        environment.systemPackages = with pkgs; [
          vim
          screen
        ];

        users.users.root.openssh.authorizedKeys.keys = with confData.sshKeys; admins;
      }
    END
          )
  end

  mkdir('modules')
  mkfile('modules/module-list.nix') do |f|
    f.puts(<<~END
      rec {
        shared = [
          # Modules not dependent on spin
        ];

        nixos = shared ++ [
          # Modules only for NixOS
        ];

        vpsadminos = shared ++ [
          # Modules only for vpsAdminOS
        ];
      }
    END
          )
  end

  mkdir('swpins')
  mkdir('swpins/channels')
end

#rediscoverObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/confctl/cli/configuration.rb', line 231

def rediscover
  hosts = discover_dir('cluster').sort

  replace_file('cluster/cluster.nix') do |f|
    f.puts('# This file is generated by confctl, changes will be lost')
    f.puts('[')

    hosts.each do |host|
      f.puts("  ./#{host}/module.nix")
    end

    f.puts(']')
  end

  ConfCtl::Hook.call(:configuration_rediscover)
end

#renameObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/confctl/cli/configuration.rb', line 205

def rename
  require_args!('old-name', 'new-name')

  src = args[0]
  dst = args[1]

  src_path = File.join('cluster', src)
  dst_path = File.join('cluster', dst)

  if !Dir.exist?(src_path)
    raise "'#{src}' not found"
  elsif Dir.exist?(dst_path)
    raise "'#{dst}' already exists"
  end

  dst_dir = File.dirname(dst_path)
  mkdir_p(dst_dir)
  mv(src_path, dst_path)

  src_swpins = File.join('swpins/cluster', "#{src.gsub('/', ':')}.json")
  dst_swpins = File.join('swpins/cluster', "#{dst.gsub('/', ':')}.json")
  mv(src_swpins, dst_swpins) if File.exist?(src_swpins)

  rediscover
end