Class: ConfCtl::Cli::App

Inherits:
Object
  • Object
show all
Includes:
GLI::App
Defined in:
lib/confctl/cli/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.getObject



7
8
9
10
11
# File 'lib/confctl/cli/app.rb', line 7

def self.get
  cli = new
  cli.setup
  cli
end

.runObject



13
14
15
16
# File 'lib/confctl/cli/app.rb', line 13

def self.run
  cli = get
  exit(cli.run(ARGV))
end

Instance Method Details

#setupObject



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
160
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
204
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/confctl/cli/app.rb', line 18

def setup
  Thread.abort_on_exception = true

  program_desc 'Nix deployment configuration management tool'
  subcommand_option_handling :normal
  preserve_argv true
  arguments :strict
  hide_commands_without_desc true

  desc 'Toggle color mode'
  flag %i[c color], must_match: %w[always never auto], default_value: 'auto'

  desc 'Create a new configuration'
  command :init do |c|
    c.action(&Command.run(c, Configuration, :init))
  end

  desc 'Add a new machine'
  arg_name '<name>'
  command :add do |c|
    c.action(&Command.run(c, Configuration, :add))
  end

  desc 'Rename an existing machine'
  arg_name '<old-name> <new-name>'
  command :rename do |c|
    c.action(&Command.run(c, Configuration, :rename))
  end

  desc 'Update cluster machine list with contents of cluster/'
  command :rediscover do |c|
    c.action(&Command.run(c, Configuration, :rediscover))
  end

  desc 'Manage software pins'
  command :swpins do |pins|
    pins.desc 'Manage software pins channels'
    pins.command :channel do |ch|
      ch.desc 'List configured sw pins'
      ch.arg_name '[channel [sw]]'
      ch.command :ls do |c|
        c.action(&Command.run(c, Swpins::Channel, :list))
      end

      swpins_commands(ch, Swpins::Channel, 'channel')
    end

    pins.desc 'Manage cluster software pins'
    pins.command :cluster do |cl|
      cl.desc 'List configured sw pins'
      cl.arg_name '[cluster-name [sw]]'
      cl.command :ls do |c|
        c.action(&Command.run(c, Swpins::Cluster, :list))
      end

      swpins_commands(cl, Swpins::Cluster, 'name')
    end

    pins.desc 'Manage core software pins'
    pins.command :core do |core|
      core.desc 'List configured sw pins'
      core.arg_name '[sw]'
      core.command :ls do |c|
        c.action(&Command.run(c, Swpins::Core, :list))
      end

      core.desc 'Set to specific version'
      core.arg_name '<sw> <ref>'
      core.command :set do |c|
        c.desc 'Commit changes to git'
        c.switch :commit, default_value: false

        c.desc 'Include changelog in the commit message'
        c.switch :changelog, default_value: true

        c.desc 'Generate changelog for downgrade'
        c.switch %i[d downgrade], default_value: false

        c.action(&Command.run(c, Swpins::Core, :set))
      end

      core.desc 'Update to newest version'
      core.arg_name '[<sw> [<version...>]]]'
      core.command :update do |c|
        c.desc 'Commit changes to git'
        c.switch :commit, default_value: false

        c.desc 'Include changelog in the commit message'
        c.switch :changelog, default_value: true

        c.desc 'Generate changelog for downgrade'
        c.switch %i[d downgrade], default_value: false

        c.action(&Command.run(c, Swpins::Core, :update))
      end
    end

    pins.desc 'Update all swpins'
    pins.command :update do |c|
      c.desc 'Commit changes to git'
      c.switch :commit, default_value: false

      c.desc 'Include changelog in the commit message'
      c.switch :changelog, default_value: true

      c.desc 'Generate changelog for downgrade'
      c.switch %i[d downgrade], default_value: false

      c.action(&Command.run(c, Swpins::Base, :update))
    end

    pins.desc 'Generate confctl-managed JSON files for configured swpins'
    pins.command :reconfigure do |c|
      c.action(&Command.run(c, Swpins::Base, :reconfigure))
    end
  end

  desc 'List configured machines'
  arg_name '[machine-pattern]'
  command :ls do |c|
    c.desc 'Enable traces in Nix'
    c.switch 'show-trace'

    c.desc 'Filter (un)managed machines'
    c.flag :managed, must_match: %w[y yes n no a all]

    c.desc 'List possible attributes to output'
    c.switch %i[L list], negatable: false

    c.desc 'Select attributes to output'
    c.flag %i[o output]

    c.desc 'Do not show the header'
    c.switch %i[H hide-header]

    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.action(&Command.run(c, Cluster, :list))
  end

  desc 'Build target systems'
  arg_name '[machine-pattern]'
  command :build do |c|
    c.desc 'Enable traces in Nix'
    c.switch 'show-trace'

    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.desc 'Assume the answer to confirmations is yes'
    c.switch %w[y yes]

    nix_build_options(c)

    c.action(&Command.run(c, Cluster, :build))
  end

  desc 'Deploy target systems'
  arg_name '[machine-pattern [switch-action]]'
  command :deploy do |c|
    c.desc 'Enable traces in Nix'
    c.switch 'show-trace'

    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.desc 'Assume the answer to confirmations is yes'
    c.switch %w[y yes]

    c.desc 'Deploy selected generation'
    c.flag %i[g generation]

    c.desc 'Ask for confirmation before activation'
    c.switch %w[i interactive]

    c.desc 'Try to dry-activate before the real switch'
    c.switch 'dry-activate-first'

    c.desc 'Copy and deploy machines one by one'
    c.switch 'one-by-one'

    c.desc 'Max number of concurrent nix-copy-closure processes'
    c.flag 'max-concurrent-copy', arg_name: 'n', type: Integer,
                                  default_value: 5

    c.desc 'Do not activate copied closures'
    c.switch 'copy-only', negatable: false

    c.desc 'Reboot target systems after deployment'
    c.switch :reboot

    c.desc 'Wait for the machine to boot'
    c.flag 'wait-online', default_value: '600'

    nix_build_options(c)

    c.desc 'Toggle health checks'
    c.switch 'health-checks', default_value: true

    c.desc 'Do not abourt on failed health checks'
    c.switch 'keep-going', default_value: false

    c.action(&Command.run(c, Cluster, :deploy))
  end

  desc 'Run machine health-checks'
  arg_name '[machine-pattern]'
  command :'health-check' do |c|
    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.desc 'Assume the answer to confirmations is yes'
    c.switch %w[y yes]

    c.desc 'Maximum number of health-check jobs'
    c.flag %w[j max-jobs], arg_name: 'number', type: Integer, default_value: 5

    c.action(&Command.run(c, Cluster, :health_check))
  end

  desc 'Check machine status'
  arg_name '[machine-pattern]'
  command :status do |c|
    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.desc 'Assume the answer to confirmations is yes'
    c.switch %w[y yes]

    c.desc 'Check status against selected generation'
    c.flag %i[g generation]

    c.action(&Command.run(c, Cluster, :status))
  end

  desc 'Changelog between deployed and configured swpins'
  arg_name '[machine-pattern [sw-pattern]]'
  command :changelog do |c|
    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.desc 'Assume the answer to confirmations is yes'
    c.switch %w[y yes]

    c.desc 'Show changelog against swpins from selected generation'
    c.flag %i[g generation]

    c.desc 'Show a changelog for downgrade'
    c.switch %i[d downgrade]

    c.desc 'Show full-length changelog descriptions'
    c.switch %i[v verbose]

    c.desc 'Show patches'
    c.switch %i[p patch]

    nix_build_options(c)

    c.action(&Command.run(c, Cluster, :changelog))
  end

  desc 'Diff between deployed and configured swpins'
  arg_name '[machine-pattern [sw-pattern]]'
  command :diff do |c|
    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.desc 'Assume the answer to confirmations is yes'
    c.switch %w[y yes]

    c.desc 'Show diff against swpins from selected generation'
    c.flag %i[g generation]

    c.desc 'Show a changelog for downgrade'
    c.switch %i[d downgrade]

    nix_build_options(c)

    c.action(&Command.run(c, Cluster, :diff))
  end

  desc 'Test SSH connection'
  arg_name '[machine-pattern]'
  command :'test-connection' do |c|
    c.desc 'Filter (un)managed machines'
    c.flag :managed, must_match: %w[y yes n no a all]

    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.desc 'Assume the answer to confirmations is yes'
    c.switch %w[y yes]

    c.action(&Command.run(c, Cluster, :test_connection))
  end

  desc 'Run command over SSH'
  arg_name '[machine-pattern [command [arguments...]]]'
  command :ssh do |c|
    c.desc 'Filter (un)managed machines'
    c.flag :managed, must_match: %w[y yes n no a all]

    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.desc 'Assume the answer to confirmations is yes'
    c.switch %w[y yes]

    c.desc 'Run command in parallel on all machines at once'
    c.switch %i[p parallel]

    c.desc 'Aggregate identical command output'
    c.switch %i[g aggregate]

    c.desc 'Data passed to standard input'
    c.flag %i[i input-string]

    c.desc 'File passed to standard input'
    c.flag %i[f input-file]

    c.action(&Command.run(c, Cluster, :ssh))
  end

  desc 'Open ClusterSSH'
  arg_name '[machine-pattern]'
  command :cssh do |c|
    c.desc 'Filter (un)managed machines'
    c.flag :managed, must_match: %w[y yes n no a all]

    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.desc 'Assume the answer to confirmations is yes'
    c.switch %w[y yes]

    c.action(&Command.run(c, Cluster, :cssh))
  end

  desc 'Manage built machine generations'
  command :generation do |gen|
    gen.desc 'List machine generations'
    gen.arg_name '[machine-pattern [generation-pattern]]'
    gen.command :ls do |c|
      c.desc 'Filter by attribute'
      c.flag %i[a attr], multiple: true

      c.desc 'Filter by tag'
      c.flag %i[t tag], multiple: true

      c.desc 'List local build generations'
      c.switch %i[l local]

      c.desc 'List remote machine generations'
      c.switch %i[r remote]

      c.action(&Command.run(c, Generation, :list))
    end

    gen.desc 'Remove machine generations'
    gen.arg_name '[machine-pattern [generation-pattern|old]]'
    gen.command :rm do |c|
      c.desc 'Filter by attribute'
      c.flag %i[a attr], multiple: true

      c.desc 'Filter by tag'
      c.flag %i[t tag], multiple: true

      c.desc 'List local build generations'
      c.switch %i[l local]

      c.desc 'List remote machine generations'
      c.switch %i[r remote]

      c.desc 'Run nix-collect-garbage to delete unreachable store paths'
      c.switch %i[gc collect-garbage], default_value: true

      c.desc 'Max number of concurrent nix-collect-garbage processes'
      c.flag 'max-concurrent-gc', arg_name: 'n', type: Integer,
                                  default_value: 5

      c.desc 'Assume the answer to confirmations is yes'
      c.switch %w[y yes]

      c.action(&Command.run(c, Generation, :remove))
    end

    gen.desc 'Auto-remove old machine generations'
    gen.arg_name '[machine-pattern]'
    gen.command :rotate do |c|
      c.desc 'Filter by attribute'
      c.flag %i[a attr], multiple: true

      c.desc 'Filter by tag'
      c.flag %i[t tag], multiple: true

      c.desc 'List local build generations'
      c.switch %i[l local]

      c.desc 'List remote machine generations'
      c.switch %i[r remote]

      c.desc 'Max number of concurrent nix-collect-garbage processes'
      c.flag 'max-concurrent-gc', arg_name: 'n', type: Integer,
                                  default_value: 5

      c.desc 'Assume the answer to confirmations is yes'
      c.switch %w[y yes]

      c.action(&Command.run(c, Generation, :rotate))
    end
  end

  desc 'Run nix-collect-garbage to deleted unreachable store paths'
  arg_name '[machine-pattern]'
  command 'collect-garbage' do |c|
    c.desc 'Filter by attribute'
    c.flag %i[a attr], multiple: true

    c.desc 'Filter by tag'
    c.flag %i[t tag], multiple: true

    c.desc 'Max number of concurrent nix-collect-garbage processes'
    c.flag 'max-concurrent-gc', arg_name: 'n', type: Integer,
                                default_value: 5

    c.desc 'Assume the answer to confirmations is yes'
    c.switch %w[y yes]

    c.action(&Command.run(c, Generation, :collect_garbage))
  end

  desc 'Generate data files'
  command 'gen-data' do |gen|
    gen.desc 'Fetch data from vpsAdmin'
    gen.command :vpsadmin do |vpsa|
      vpsa.desc 'Generate all data files'
      vpsa.command :all do |c|
        c.action(&Command.run(c, GenData, :vpsadmin_all))
      end

      vpsa.desc 'Generate container data files'
      vpsa.command :containers do |c|
        c.action(&Command.run(c, GenData, :vpsadmin_containers))
      end

      vpsa.desc 'Generate network data files'
      vpsa.command :network do |c|
        c.action(&Command.run(c, GenData, :vpsadmin_network))
      end
    end
  end

  ConfCtl::UserScripts.each do |script|
    script.setup_cli(self)
  end

  on_error do |_exception|
    log = ConfCtl::Logger.instance
    warn "\nLog file: #{log.path}" if log.open?
    true
  end
end