Class: ConfCtl::Cli::Generation

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

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

#collect_garbageObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/confctl/cli/generation.rb', line 108

def collect_garbage
  machines = select_machines(args[0]).runnable

  raise 'No machines to collect garbage on' if machines.empty?

  ask_confirmation! do
    puts 'Collect garbage on the following machines:'
    list_machines(machines)
  end

  run_gc(machines)
end

#listObject



5
6
7
8
9
# File 'lib/confctl/cli/generation.rb', line 5

def list
  machines = select_machines(args[0])
  gens = select_generations(machines, args[1])
  list_generations(gens)
end

#removeObject



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
# File 'lib/confctl/cli/generation.rb', line 11

def remove
  machines = select_machines(args[0])
  gens = select_generations(machines, args[1])
  changed_hosts = []

  if gens.empty?
    puts 'No generations found'
    return
  end

  ask_confirmation! do
    puts 'The following generations will be removed:'
    list_generations(gens)
    puts
    puts "Garbage collection: #{opts[:remote] && opts[:gc] ? 'yes' : 'no'}"
  end

  gens.each do |gen|
    puts "Removing #{gen.presence_str} generation #{gen.host}@#{gen.name}"
    gen.destroy

    changed_hosts << gen.host unless changed_hosts.include?(gen.host)
  end

  return unless opts[:remote] && opts[:gc]

  machines_gc = {}

  machines.each do |host, machine|
    next unless changed_hosts.include?(host)

    m =
      if machine.carried?
        machine.carrier_machine
      else
        machine
      end

    machines_gc[m.name] = m if m.target_host
  end

  run_gc(ConfCtl::MachineList.new(machines: machines_gc))
end

#rotateObject



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
# File 'lib/confctl/cli/generation.rb', line 55

def rotate
  machines = select_machines(args[0])

  to_delete = []
  changed_hosts = []
  enable_gc = opts[:remote] && opts[:gc]

  to_delete.concat(host_generations_rotate(machines)) if opts[:remote]

  to_delete.concat(build_generations_rotate(machines)) if opts[:local] || (!opts[:local] && !opts[:remote])

  if to_delete.empty?
    puts 'No generations to delete'
    return
  end

  ask_confirmation! do
    puts 'The following generations will be removed:'
    OutputFormatter.print(to_delete, i[host name type id], layout: :columns)
    puts
    puts "Garbage collection: #{enable_gc ? 'when enabled in configuration' : 'no'}"
  end

  to_delete.each do |gen|
    puts "Removing #{gen[:type]} generation #{gen[:host]}@#{gen[:name]}"
    gen[:generation].destroy

    changed_hosts << gen[:host] unless changed_hosts.include?(gen[:host])
  end

  return unless enable_gc

  global = ConfCtl::Settings.instance.host_generations
  machines_gc = {}

  machines.each do |host, machine|
    next unless changed_hosts.include?(host)

    m =
      if machine.carried?
        machine.carrier_machine
      else
        machine
      end

    next if !m.target_host || (!m['buildGenerations']['collectGarbage'] && !global['collectGarbage'])

    machines_gc[m.name] = m
  end

  run_gc(ConfCtl::MachineList.new(machines: machines_gc)) if machines_gc.any?
end