Files
ubicloud/rhizome/host/bin/setup-vm
Maciek Sarnowicz 8f5f357317 Add Slice and CPU limit parameters to VmSetup
Defaults are applied so recreate-unpersisted run on old prep.json
without crashing.  This patch does apply any of the parameters to the
systemd units, though, that's next.

Add the following parameters to `VmSetup`'s methods:

* `slice_name`: cgroup slice name the VM should be a member of. Default
  is `system.slice`, which will be used by default until we migrate to a
  system with stricter resource management. Note that this works also on
  Ubuntu 22.04.
* `cpu_percent_limit`: cpu quota the VM can use.
* `cpu_burst_percent_limit`: additional quota that a VM can burst into
  temporarily if it has build up enough credit.
2025-01-10 23:23:05 -08:00

109 lines
3.1 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")
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)
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)
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)
else
puts "Invalid action #{action}"
exit 1
end