lib/confctl/cli/cluster.rb in confctl-1.0.0 vs lib/confctl/cli/cluster.rb in confctl-2.0.0
- old
+ new
@@ -108,11 +108,14 @@
raise 'Health checks failed'
end
def status
- machines = select_machines(args[0]).managed
+ machines = select_machines(args[0]).managed.select do |_host, machine|
+ machine.target_host || machine.carried?
+ end
+
raise 'No machines to check' if machines.empty?
ask_confirmation! do
if opts[:generation]
puts 'The following machines will be checked:'
@@ -242,11 +245,11 @@
io.puts(s || 'no changes')
end
end
def test_connection
- machines = select_machines_with_managed(args[0])
+ machines = select_machines_with_managed(args[0]).runnable
raise 'No machines to test' if machines.empty?
ask_confirmation! do
puts 'Test SSH connection to the following machines:'
list_machines(machines)
@@ -274,11 +277,11 @@
puts 'Failed machines:'
failed.each { |host| puts " #{host}" }
end
def ssh
- machines = select_machines_with_managed(args[0])
+ machines = select_machines_with_managed(args[0]).runnable
raise 'No machines to ssh to' if machines.empty?
if opts['input-string'] && opts['input-file']
raise GLI::BadCommandLine, 'use one of --input-string or --input-file'
end
@@ -293,11 +296,11 @@
run_ssh_command(machines, args[1..])
end
def cssh
- machines = select_machines_with_managed(args[0])
+ machines = select_machines_with_managed(args[0]).runnable
raise 'No machines to open cssh to' if machines.empty?
ask_confirmation! do
puts 'Open cssh to the following machines:'
list_machines(machines)
@@ -506,42 +509,71 @@
header: "#{Rainbow('Deploying to').bright} #{Rainbow(host).yellow}\n",
title: Rainbow('Live view').bright,
size: :auto,
reserved_lines: 10
) do |lw|
- if opts['dry-activate-first']
- lw.sync_console do
- puts Rainbow(
- "Trying to activate configuration on #{host} " \
- "(#{machine.target_host})"
- ).yellow
- end
-
- raise "Error while activating configuration on #{host}" unless nix.activate(machine, toplevel, 'dry-activate')
+ if machine.carried?
+ deploy_carried_to_host(lw, nix, host, machine, toplevel, action)
+ else
+ deploy_standalone_to_host(lw, nix, host, machine, toplevel, action)
end
+ lw.flush
+ end
+ end
+
+ def deploy_standalone_to_host(lw, nix, host, machine, toplevel, action)
+ if opts['dry-activate-first']
lw.sync_console do
puts Rainbow(
- "Activating configuration on #{host} (#{machine.target_host}): " \
- "#{action}"
+ "Trying to activate configuration on #{host} " \
+ "(#{machine.target_host})"
).yellow
end
- return :skip if opts[:interactive] && !ask_confirmation(always: true)
+ raise "Error while activating configuration on #{host}" unless nix.activate(machine, toplevel, 'dry-activate')
+ end
- raise "Error while activating configuration on #{host}" unless nix.activate(machine, toplevel, action)
+ lw.sync_console do
+ puts Rainbow(
+ "Activating configuration on #{host} (#{machine.target_host}): " \
+ "#{action}"
+ ).yellow
+ end
- if %w[boot switch].include?(action) && !nix.set_profile(machine, toplevel)
- raise "Error while setting profile on #{host}"
- end
+ return :skip if opts[:interactive] && !ask_confirmation(always: true)
+
+ # rubocop:disable Style/GuardClause
+
+ unless nix.activate(machine, toplevel, action)
+ raise "Error while activating configuration on #{host}"
end
+
+ if %w[boot switch].include?(action) && !nix.set_profile(machine, toplevel)
+ raise "Error while setting profile on #{host}"
+ end
+
+ # rubocop:enable Style/GuardClause
end
+ def deploy_carried_to_host(lw, nix, host, machine, toplevel, action)
+ return if action != 'switch'
+
+ # rubocop:disable Style/GuardClause
+ unless nix.set_carried_profile(machine, toplevel)
+ raise "Error while setting carried profile for #{host} on #{machine.carrier_machine}"
+ end
+ # rubocop:enable Style/GuardClause
+ end
+
def reboot_host(host, machine)
if machine.localhost?
puts Rainbow("Skipping reboot of #{host} as it is localhost").yellow
return :skip
+ elsif machine.carried?
+ puts Rainbow("Skipping reboot of carried machine #{host}").yellow
+ return :skip
end
puts Rainbow("Rebooting #{host} (#{machine.target_host})").yellow
return :skip if opts[:interactive] && !ask_confirmation(always: true)
@@ -759,26 +791,20 @@
results = {}
machines.each do |host, machine|
mc = ConfCtl::MachineControl.new(machine)
- begin
- puts "#{host}:" unless aggregate
+ puts "#{host}:" unless aggregate
- result = run_ssh_command_on_machine(mc, cmd)
+ result = run_ssh_command_on_machine(mc, cmd)
- if aggregate
- results[host] = result
- else
- puts result.out
- end
- rescue TTY::Command::ExitError => e
- if aggregate
- results[host] = e
- else
- puts e.message
- end
+ if aggregate
+ results[host] = result
+ elsif result.success?
+ puts result.out
+ else
+ puts result.err
end
puts unless aggregate
end
@@ -806,16 +832,12 @@
machines.each do |host, machine|
tw.add do
mc = ConfCtl::MachineControl.new(machine)
- begin
- result = run_ssh_command_on_machine(mc, cmd)
- results[host] = result
- rescue TTY::Command::ExitError => e
- results[host] = e
- end
+ result = run_ssh_command_on_machine(mc, cmd)
+ results[host] = result
lw.sync_console { pb.advance }
end
end
@@ -828,11 +850,17 @@
return
end
results.each do |host, result|
puts "#{host}:"
- puts result.out
+
+ if result.success?
+ puts result.out
+ else
+ puts result.err
+ end
+
puts
end
end
def run_ssh_command_on_machine(mc, cmd)
@@ -842,11 +870,11 @@
cmd_opts[:input] = opts['input-string']
elsif opts['input-file']
cmd_opts[:in] = opts['input-file']
end
- mc.execute(*cmd, **cmd_opts)
+ mc.execute!(*cmd, **cmd_opts)
end
def process_aggregated_results(results)
groups = {}
@@ -901,11 +929,12 @@
end
def do_build(machines)
nix = ConfCtl::Nix.new(
show_trace: opts['show-trace'],
- max_jobs: opts['max-jobs']
+ max_jobs: opts['max-jobs'],
+ cores: opts['cores']
)
hosts_swpin_paths = {}
autoupdate_swpins(machines)
host_swpin_specs = check_swpins(machines)
@@ -994,10 +1023,10 @@
fetch_pb = multibar.register(
'Fetching [:bar] :current/:total (:percent)'
)
- built_generations = nix.build_toplevels(
+ built_generations = nix.build_attributes(
hosts:,
swpin_paths:,
time:,
host_swpin_specs:
) do |type, _progress, total, _path|