diff options
author | Aaron Ball <nullspoon@iohq.net> | 2016-02-22 10:17:14 -0700 |
---|---|---|
committer | Aaron Ball <nullspoon@iohq.net> | 2016-02-22 10:17:14 -0700 |
commit | 07aaaead85c7f0f66a6d697f15e9c5c4aea48798 (patch) | |
tree | 7b70c081e90d35ef586be3cb32e7e68109491d1a | |
parent | ff3b57174c71c0d284d624a044810144c059ec2e (diff) | |
download | vmgr-07aaaead85c7f0f66a6d697f15e9c5c4aea48798.tar.gz vmgr-07aaaead85c7f0f66a6d697f15e9c5c4aea48798.tar.xz |
Wrote env_setup and converted to bins assoc. array
The old way tracked each binary's absolute path, hardcoded in a variable
bin${binary_name}.
This introduces the env_setup function, which will find the absolute
path (using which) to each specified binary, updating the global bins
associative array with its path. This allows for binaries to be placed
in other locations and the script will still work, provided the binary
can be found somewhere in PATH. This also allows for binary overriding.
This change updates all references to the previous way of finding
binaries with the new env_setup way.
Note that this function will also exit code 1 with an error message if a
binary can't be found, ensuring the script exits early if the
environment isn't set up to run it.
-rwxr-xr-x | vmgr | 60 |
1 files changed, 46 insertions, 14 deletions
@@ -26,10 +26,9 @@ prefixmac='52:54:00:7e:70:' prefixip='192.168.122.' -binvirsh='/usr/bin/virsh' -binvirt_install='/usr/bin/virt-install' -binqemu_img='/usr/bin/qemu-img' -bincp='/bin/cp' +# Hash to contain direct paths to binaries +# Populated by env_setup +declare -A bins # @@ -70,6 +69,35 @@ function log { # +# Searches PATH for all binaries listed as function arguments. Each found +# binary is then put into the global "bins" associative array, using the +# binary's name as the key to its path. +# +# This function also servers to ensure all specified binaries are present and +# in PATH. It will exit code 1 with an error message if any binaries are not +# found. +# +# Note: To use this function, insert "declare -A bins" at top of script). +# +# @param bins All function arguments are the names of binaries to locate. +# +function env_setup { + local args=${@} + + for i in ${args[@]}; do + local path=$(which ${i} 2>/dev/null) + + if [[ $? -gt 0 ]]; then + echo "Could not find binary ${i}. Is it installed?" + exit 1 + fi + + bins[$i]="${path}" + done +} + + +# # Executes virt-install with the specified arguments. # # @param name Name of the virtual machine (what displays in libvirt utils) @@ -84,7 +112,7 @@ function install { local mac=${3} local mem=4096 - out=$(${binvirt_install} \ + out=$(${bins['virt-install']} \ --name ${name} \ --ram=${mem} \ --virt-type=kvm \ @@ -126,9 +154,9 @@ function clone_disk { [[ -z ${3} ]] && echo "ERROR: Clone speed required (arg 3)." && exit 1 if [[ ${speed} == 'fast' ]]; then - ${bincp} ${src} ${dest} + ${bins['cp']} ${src} ${dest} elif [[ ${speed} == 'small' ]]; then - ${binqemu_img} convert -O qcow2 -p -c ${src} ${dest} + ${bins['qemu-img']} convert -O qcow2 -p -c ${src} ${dest} else log error "Unknown speed: ${speed}" exit 1 @@ -153,7 +181,7 @@ function net_reserve_ip { xml="<host mac='${mac}' name='${name}' ip='${ip}'/>" # Perform reservation command - out=$(${binvirsh} net-update ${network} add ip-dhcp-host "${xml}" 2>&1) + out=$(${bins['virsh']} net-update ${network} add ip-dhcp-host "${xml}" 2>&1) # Log the output of ip reservation operation if [[ $? -gt 0 ]]; then @@ -182,7 +210,7 @@ function net_rm_reservation { xml="<host mac='${mac}' name='${name}' ip='${ip}'/>" # Perform reservation command - ${binvirsh} net-update ${network} delete ip-dhcp-host "${xml}" + ${bins['virsh']} net-update ${network} delete ip-dhcp-host "${xml}" if [[ $? -gt 0 ]]; then log error "A problem occured deleting ip reservation (${ip}) for mac ${mac}." @@ -205,7 +233,7 @@ function get_next_index { index=10 # Get list of hosts - hostxml="$(${binvirsh} net-dumpxml ${network} | grep '<host' )" + hostxml="$(${bins['virsh']} net-dumpxml ${network} | grep '<host' )" # For each index, check if the mac address exists while [ $(echo ${hostxml} | grep "mac='${prefixmac}${index}'" -c ) -ne 0 ]; do @@ -299,14 +327,14 @@ function vm_rm { # Shut the VM down forcibly (it's about to be deleted, so we don't care about # a friendly shutdown here) - ${binvirsh} destroy ${name} + ${bins['virsh']} destroy ${name} # Refresh the pool after VM destruction. If we don't do this, virsh # intermittently fails on storage removal, with a permission denied error. # This is because it doesn't know that the disk is no longer in use, so a # pool-refresh is required. log info "Refreshing storage pool ${pool} to ensure disk removal success." - poolout=$(${binvirsh} pool-refresh ${pool} 2>&1) + poolout=$(${bins['virsh']} pool-refresh ${pool} 2>&1) if [[ $? -gt 0 ]]; then log error "${poolout}" @@ -316,7 +344,7 @@ function vm_rm { # Delete the VM and remove all of its storage log info -n "Removing VM ${name} and all storage." - undefout=$(${binvirsh} undefine --remove-all-storage ${name}) + undefout=$(${bins['virsh']} undefine --remove-all-storage ${name}) if [[ $? -gt 0 ]]; then log error "${undefout}" @@ -334,7 +362,8 @@ function vm_rm { # Lists all VMs currently running, their expected ips, and mac addresses. # function vm_ls { - names=$(${binvirsh} list --name) + names=$(${bins['virsh']} list --name) + echo ${bins['virsh']} list --name output='Host IP MAC\n---- -- ---\n' for i in ${names}; do @@ -352,6 +381,9 @@ function main { action=${1} shift; + # Set up paths to all binary variables + env_setup virsh virt-install qemu-img cp + args=(${@}) if [[ ${action} == 'new' ]]; then |