training-guides/labs/lib/osbash/virtualbox.install_node
Roger Luethi 946114eb30 labs: pass vm_name as arg to _vbox_configure_ifs
_vbox_configure_ifs should no longer use the environment variable
NODE_NAME as the name of the node VM. It gets replaced by the correct
value passed as an argument.

Change-Id: I9d36420bcf56bbabe2f47cee2d7eb2ad824bf5d1
2014-09-01 17:43:27 +02:00

87 lines
2.7 KiB
Bash

# This bash library contains the main function that creates a node VM.
# Configure VirtualBox network interfaces
function _vbox_configure_ifs {
local vm_name=$1
# Iterate over all NET_IF_? variables
local net_ifs=( "${!NET_IF_@}" )
local net_if=""
for net_if in "${net_ifs[@]}"; do
local if_num=${net_if##*_}
if [ "${!net_if}" = "nat" ]; then
echo "interface $if_num: NAT"
vm_nic_nat "$vm_name" "$if_num"
else
# Host-only network: net_if is net name (e.g. API_NET)
# Use corresponding VirtualBox interface (e.g. API_NET_IF)
local host_if="${!net_if}_IF"
echo "interface $if_num: host-only ${!host_if}"
vm_nic_hostonly "$vm_name" "$if_num" "${!host_if}"
fi
done
}
# Boot node VM; wait until autostart files are processed and VM is shut down
function _vbox_boot_with_autostart {
local vm_name=$1
local ssh_port=$2
vbox_boot "$vm_name"
# Wait for ssh connection and execute scripts in autostart directory
# (for wbatch, osbashauto does the processing instead)
${WBATCH:+:} ssh_process_autostart "$ssh_port" &
wait_for_autofiles
echo >&2 "VM \"$vm_name\": autostart files executed"
}
# Create a new node VM and run basic configuration scripts
function vm_init_node {
vm_name=$1
vm_create "$vm_name"
# Set VM_MEM in config/config.NODE_NAME to override
vm_mem "$vm_name" "${VM_MEM:-512}"
# Set VM_CPUS in config/config.NODE_NAME to override
vm_cpus "$vm_name" "${VM_CPUS:-1}"
_vbox_configure_ifs "$vm_name"
# Port forwarding
if [ -n "${VM_SSH_PORT:-}" ]; then
vm_port "$vm_name" ssh "$VM_SSH_PORT" 22
fi
if [ -n "${VM_WWW_PORT:-}" ]; then
vm_port "$vm_name" http "$VM_WWW_PORT" 80
fi
vm_add_share "$vm_name" "$SHARE_DIR" "$SHARE_NAME"
vm_attach_disk_multi "$vm_name" "$BASE_DISK"
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Rename to pass the node name to the script
autostart_and_rename osbash init_xxx_node.sh "init_${NODE_NAME}_node.sh"
}
function vm_build_node {
# XXX Run this function in sub-shell to protect our caller's environment
# (which might be _our_ enviroment if we get called again)
(
NODE_NAME=$1
source "$CONFIG_DIR/config.$NODE_NAME"
${WBATCH:-:} wbatch_begin_node "$NODE_NAME"
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
autostart_reset
autostart_from_config "scripts.$NODE_NAME"
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
${WBATCH:-:} wbatch_end_file
)
}
# vim: set ai ts=4 sw=4 et ft=sh: