ubicloud/rhizome/host/bin/setup-vm
2025-09-04 14:15:35 +02:00

124 lines
3.6 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
params = begin
JSON.parse(File.read(VmPath.new(vm_name).prep_json))
rescue Errno::ENOENT
if action.start_with?("delete")
# Params were not historically needed for the delete actions,
# so handle case where prep file has already been deleted.
{}
else
raise
end
end
vm_setup = VmSetup.new(vm_name,
hugepages: params.fetch("hugepages", true),
ch_version: params["ch_version"],
firmware_version: params["firmware_version"])
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
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")
slice_name = params.fetch("slice_name", "system.slice")
cpu_percent_limit = params.fetch("cpu_percent_limit", 0)
cpu_burst_percent_limit = params.fetch("cpu_burst_percent_limit", 0)
ipv6_disabled = params.fetch("ipv6_disabled", false)
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,
slice_name, cpu_percent_limit, cpu_burst_percent_limit, ipv6_disabled)
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, slice_name, cpu_burst_percent_limit,
multiqueue: max_vcpus > 1
)
when "reinstall-systemd-units"
vm_setup.install_systemd_unit(
max_vcpus, cpu_topology, mem_gib, storage_volumes, nics,
pci_devices, slice_name, cpu_percent_limit, cpu_burst_percent_limit
)
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,
slice_name, cpu_percent_limit, cpu_burst_percent_limit)
when "restart"
vm_setup.restart(slice_name, cpu_burst_percent_limit)
else
puts "Invalid action #{action}"
exit 1
end