# -*- mode: ruby -*- # vi: set ft=ruby : # >>> VARS =begin # 变量文件 require "yaml" settings = YAML.load_file "settings.yml" # 默认 PROVIDER VAGRANT_DEFAULT_PROVIDER = "vmware_desktop" # CLONE value. if true SIZE< 1.3GB, if false SIZE> 13GB CLONE_VALUE = settings["vmware"]["clone"] # 取出网段 IP_SECTIONS = settings["network"]["control_ip"].match(/^([0-9.]+\.)([^.]+)$/) # 网络地址 IP_NW = IP_SECTIONS.captures[0] # 起始 IP IP_START = Integer(IP_SECTIONS.captures[1]) # 取出 工作节点个数 NUM_NODES = settings["nodes"]["control"]["count"] # 虚拟机 NAME VB_NODE = settings["nodes"]["control"]["name"] # 虚拟机 DIRECTORY VMWARE_DIRECTORY = settings["vmware"]["directory"] + "/VM-" + VB_NODE # <<< VARS =end # >>> Configuration Version =begin Vagrant.configure("2") do |config| # This configures what box the machine will be brought up against # The value here should be the name of an installed box or a shorthand name of a box in HashiCorp's Vagrant Cloud if `uname -m`.strip == "arm64" vm.box = settings["vmware"]["box"]["arm64"] APT_MIRROR = settings["software"]["apt_mirror"]["arm64"] GUEST_OS = "arm-ubuntu-64" else config.vm.box = settings["vmware"]["box"]["amd64"] APT_MIRROR = settings["software"]["apt_mirror"]["amd64"] GUEST_OS = "ubuntu-64" end # If true, Vagrant will check for updates to the configured box on every vagrant up # If an update is found, Vagrant will tell the user config.vm.box_check_update = true # Configures synced folders on the machine, # so that folders on your host machine can be synced to and from the guest machine config.vm.synced_folder ".", "/vagrant", disabled: true (1..NUM_NODES).each do |i| # >>> Machine Settings =begin config.vm.define VB_NODE + "#{i}" do |node| # >> Configures networks on the machine node.vm.network "private_network", ip: IP_NW + "#{IP_START + i}" ## The hostname the machine should have # If set to a string, the hostname will be set on boot # If set, Vagrant will update /etc/hosts on the guest with the configured hostname node.vm.hostname = VB_NODE + "#{i}" # >>> Provider settings =begin node.vm.provider VAGRANT_DEFAULT_PROVIDER do |v| ## HGFS is functional within the guest # This defaults to detected capability of the guest v.functional_hgfs = false ## Use linked clones instead of full copy clones v.linked_clone = CLONE_VALUE ## Launch guest with a GUI # This defaults to false v.gui = true ## Path for storing VMware clones # This defaults to ./.vagrant v.clone_directory = VMWARE_DIRECTORY ## VMX key/value pairs to set or unset # If the value is nit, the key will be deleted v.vmx["displayName"] = VB_NODE + "#{i}" v.vmx["guestOS"] = GUEST_OS v.vmx["memsize"] = settings["nodes"]["control"]["memory"] v.vmx["numvcpus"] = settings["nodes"]["control"]["cpu"] v.vmx["annotation"] = settings["nodes"]["annotation"] v.vmx["cpuid.coresPerSocket"] = "1" end # <<< Provider settings =end end # <<< Machine Settings =end end # >>> The Vagrant Shell provisioner =begin # allows you to upload and execute a script within the guest machine config.vm.provision "shell", env: { "DNS_SERVERS" => settings["network"]["dns_servers"].join(" "), "APT_MIRROR" => APT_MIRROR }, inline: <<-SHELL # dns sed -i /etc/systemd/resolved.conf \ -e '/^#DNS=/{s/=.*/='"${DNS_SERVERS}"'/;s/#//}' systemctl restart systemd-resolved.service ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf # apt_mirror if ! curl --connect-timeout 2 google.com &>/dev/null; then # C. 国内 CODE_NAME=$(lsb_release -cs) COMPONENT="main restricted universe multiverse" tee /etc/apt/sources.list >/dev/null<<-EOF deb ${APT_MIRROR} ${CODE_NAME} ${COMPONENT} deb ${APT_MIRROR} ${CODE_NAME}-updates ${COMPONENT} deb ${APT_MIRROR} ${CODE_NAME}-backports ${COMPONENT} deb ${APT_MIRROR} ${CODE_NAME}-security ${COMPONENT} EOF fi # reset_tty1 if pgrep -l agetty &>/dev/null; then kill -9 $(pgrep -l agetty | awk '{print $1}') fi SHELL # <<< The Vagrant Shell provisioner =end end # <<< Configuration Version =end