Files
ubicloud/rhizome/host/bin/setup-vm
Daniel Farina fcb93202c3 Make setup-vm fail earlier
I saw a case of a nil-expansion of `dns_ipv4` causing this error in code that looks like this:

    r "ip -n #{q_vm} addr replace #{dns_ipv4} dev #{nic.tap}"

And has errors like:

    ---STDERR---
    RTNETLINK answers: Operation not supported
            from /home/rhizome/host/lib/vm_setup.rb:271:in `block in setup_taps_6'
            from /home/rhizome/host/lib/vm_setup.rb:268:in `each'
            from /home/rhizome/host/lib/vm_setup.rb:268:in `setup_taps_6'
            from /home/rhizome/host/lib/vm_setup.rb:96:in `setup_networking'
            from /home/rhizome/host/lib/vm_setup.rb:49:in `block in prep'
    /home/rhizome/common/lib/util.rb:29:in `r': command failed: ip -n vmg1x0hy addr replace  dev nc9g0we9s2 (CommandFail)

While this was probably caused by an old running code image, it seems
like we can check for the existence of obliged fields in `params.json`
as we do all the others, for some reason, a couple got square brackets
rather than `.fetch`.
2024-10-17 01:35:20 -07:00

101 lines
2.7 KiB
Ruby
Executable File

#!/bin/env ruby
# frozen_string_literal: true
require "json"
require_relative "../lib/vm_setup"
unless (action = ARGV.shift)
puts "expected action as argument"
exit 1
end
unless (vm_name = ARGV.shift)
puts "expected path to vm_name as argument"
exit 1
end
vm_setup = VmSetup.new(vm_name)
if action == "delete"
vm_setup.purge
exit 0
end
if action == "delete_keep_net"
vm_setup.purge_without_network
exit 0
end
if action == "delete_net"
vm_setup.purge_network
vm_setup.purge_user
exit 0
end
params = JSON.parse(File.read(VmPath.new(vm_name).prep_json))
begin
# "Global Unicast" subnet, i.e. a subnet for exchanging packets with
# the internet.
gua = params.fetch("public_ipv6")
ip4 = params.fetch("public_ipv4")
local_ip4 = params.fetch("local_ipv4")
unix_user = params.fetch("unix_user")
ssh_public_keys = params.fetch("ssh_public_keys")
nics = params.fetch("nics").map { |args| VmSetup::Nic.new(*args) }.freeze
max_vcpus = params.fetch("max_vcpus")
cpu_topology = params.fetch("cpu_topology")
mem_gib = params.fetch("mem_gib")
ndp_needed = params.fetch("ndp_needed", false)
storage_volumes = params.fetch("storage_volumes")
swap_size_bytes = params["swap_size_bytes"]
pci_devices = params.fetch("pci_devices", [])
boot_image = params.fetch("boot_image")
dns_ipv4 = params.fetch("dns_ipv4")
rescue KeyError => e
puts "Needed #{e.key} in parameters json"
exit 1
end
case action
when "prep"
secrets = JSON.parse($stdin.read)
unless (storage_secrets = secrets["storage"])
puts "need storage secrets in secrets json"
exit 1
end
vm_setup.prep(unix_user, ssh_public_keys, nics, gua, ip4,
local_ip4, max_vcpus, cpu_topology, mem_gib,
ndp_needed, storage_volumes, storage_secrets, swap_size_bytes,
pci_devices, boot_image, dns_ipv4)
when "recreate-unpersisted"
secrets = JSON.parse($stdin.read)
unless (storage_secrets = secrets["storage"])
puts "need storage secrets in secrets json"
exit 1
end
vm_setup.recreate_unpersisted(
gua, ip4, local_ip4, nics, mem_gib,
ndp_needed, storage_volumes, storage_secrets, dns_ipv4,
pci_devices, multiqueue: max_vcpus > 1
)
when "reinstall-systemd-units"
vm_setup.install_systemd_unit(
max_vcpus, cpu_topology, mem_gib, storage_volumes, nics
)
when "reassign-ip6"
secrets = JSON.parse($stdin.read)
unless (storage_secrets = secrets["storage"])
puts "need storage secrets in secrets json"
exit 1
end
vm_setup.reassign_ip6(unix_user, ssh_public_keys, nics, gua, ip4,
local_ip4, max_vcpus, cpu_topology, mem_gib,
ndp_needed, storage_volumes, storage_secrets, swap_size_bytes, pci_devices, boot_image, dns_ipv4)
else
puts "Invalid action #{action}"
exit 1
end