Files
ubicloud/rhizome/host/lib/slice_setup.rb
Jeremy Evans e59d71e2f6 Restore Ruby 3.2 syntax to rhizome
Ubuntu 22.04 VmHosts use Ruby 3.0, and Ubunutu 24.04 use Ruby 3.2,
so 3.4 syntax would break either.  This was theoretically an issue
before, but I'm guessing there were no significant syntax changes
between 3.0 and 3.2 that Rubocop's rewriting would break.

This commits a rhizome/.rubocop.yml file that inherits the default
configuration, and overrides TargetRubyVersion.
2025-04-26 08:34:47 +09:00

65 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require_relative "../../common/lib/util"
require "fileutils"
class SliceSetup
def initialize(slice_name)
@slice_name = slice_name
end
def systemd_service
@systemd_service ||= File.join("/etc/systemd/system", @slice_name)
end
def prep(allowed_cpus)
fail "BUG: invalid cpuset" unless valid_cpuset?(allowed_cpus)
install_systemd_unit(allowed_cpus)
start_systemd_unit
end
def purge
r("systemctl stop #{@slice_name.shellescape}", expect: [0, 5])
rm_if_exists systemd_service
r "systemctl daemon-reload"
end
def install_systemd_unit(allowed_cpus)
fail "BUG: unit name must not be empty" if @slice_name.empty?
fail "BUG: we cannot create system units" if @slice_name == "system.slice" || @slice_name == "user.slice"
fail "BUG: unit name cannot contain a dash" if @slice_name.include?("-")
fail "BUG: invalid allowed_cpus" if !valid_cpuset?(allowed_cpus)
# Only proceed if the slice has not yet been setup
unless File.exist? systemd_service
safe_write_to_file(systemd_service, <<SLICE_CONFIG)
[Unit]
Description=Restricting resouces for virtual machines
Before=slices.target
[Slice]
AllowedCPUs=#{allowed_cpus}
SLICE_CONFIG
r "systemctl daemon-reload"
end
end
def start_systemd_unit
r "systemctl start #{@slice_name}"
cpuset_path = File.join("/sys/fs/cgroup", @slice_name, "cpuset.cpus.partition")
File.write(cpuset_path, "root")
end
def valid_cpuset?(str)
return false if str.nil? || str.empty?
str.split(",").all? do |part|
if part.include?("-")
r = part.split("-")
r.size == 2 && r.all? { _1.to_i.to_s == _1 } && r[0].to_i <= r[1].to_i
else
part.to_i.to_s == part
end
end
end
end