|  | #!/bin/bash | 
|  |  | 
|  | function xapi_plugin_location { | 
|  | for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/"; do | 
|  | if [ -d $PLUGIN_DIR ]; then | 
|  | echo $PLUGIN_DIR | 
|  | return 0 | 
|  | fi | 
|  | done | 
|  | return 1 | 
|  | } | 
|  |  | 
|  | function zip_snapshot_location { | 
|  | echo $1 | sed "s:\.git$::;s:$:/zipball/$2:g" | 
|  | } | 
|  |  | 
|  | function create_directory_for_kernels { | 
|  | if [ -d "/boot/guest" ]; then | 
|  | echo "INFO: /boot/guest directory already exists, using that" >&2 | 
|  | else | 
|  | local LOCALPATH="$(get_local_sr_path)/os-guest-kernels" | 
|  | mkdir -p $LOCALPATH | 
|  | ln -s $LOCALPATH /boot/guest | 
|  | fi | 
|  | } | 
|  |  | 
|  | function extract_remote_zipball { | 
|  | local ZIPBALL_URL=$1 | 
|  |  | 
|  | local LOCAL_ZIPBALL=$(mktemp) | 
|  | local EXTRACTED_FILES=$(mktemp -d) | 
|  |  | 
|  | ( | 
|  | wget -nv $ZIPBALL_URL -O $LOCAL_ZIPBALL --no-check-certificate | 
|  | unzip -q -o $LOCAL_ZIPBALL -d $EXTRACTED_FILES | 
|  | rm -f $LOCAL_ZIPBALL | 
|  | ) >&2 | 
|  |  | 
|  | echo "$EXTRACTED_FILES" | 
|  | } | 
|  |  | 
|  | function find_xapi_plugins_dir { | 
|  | find $1 -path '*/xapi.d/plugins' -type d -print | 
|  | } | 
|  |  | 
|  | function install_xapi_plugins_from_zipball { | 
|  | local XAPI_PLUGIN_DIR | 
|  | local EXTRACTED_FILES | 
|  | local EXTRACTED_PLUGINS_DIR | 
|  |  | 
|  | XAPI_PLUGIN_DIR=$(xapi_plugin_location) | 
|  |  | 
|  | EXTRACTED_FILES=$(extract_remote_zipball $1) | 
|  | EXTRACTED_PLUGINS_DIR=$(find_xapi_plugins_dir $EXTRACTED_FILES) | 
|  |  | 
|  | cp -pr $EXTRACTED_PLUGINS_DIR/* $XAPI_PLUGIN_DIR | 
|  | rm -rf $EXTRACTED_FILES | 
|  | chmod a+x ${XAPI_PLUGIN_DIR}* | 
|  | } | 
|  |  | 
|  | function get_local_sr { | 
|  | xe sr-list name-label="Local storage" --minimal | 
|  | } | 
|  |  | 
|  | function get_local_sr_path { | 
|  | echo "/var/run/sr-mount/$(get_local_sr)" | 
|  | } | 
|  |  | 
|  | function find_ip_by_name() { | 
|  | local guest_name="$1" | 
|  | local interface="$2" | 
|  |  | 
|  | local period=10 | 
|  | local max_tries=10 | 
|  | local i=0 | 
|  |  | 
|  | while true; do | 
|  | if [ $i -ge $max_tries ]; then | 
|  | echo "Timeout: ip address for interface $interface of $guest_name" | 
|  | exit 11 | 
|  | fi | 
|  |  | 
|  | ipaddress=$(xe vm-list --minimal \ | 
|  | name-label=$guest_name \ | 
|  | params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p") | 
|  |  | 
|  | if [ -z "$ipaddress" ]; then | 
|  | sleep $period | 
|  | ((i++)) | 
|  | else | 
|  | echo $ipaddress | 
|  | break | 
|  | fi | 
|  | done | 
|  | } | 
|  |  | 
|  | function _create_new_network() { | 
|  | local name_label | 
|  | name_label=$1 | 
|  |  | 
|  | xe network-create name-label="$name_label" | 
|  | } | 
|  |  | 
|  | function _multiple_networks_with_name() { | 
|  | local name_label | 
|  | name_label=$1 | 
|  |  | 
|  | # A comma indicates multiple matches | 
|  | xe network-list name-label="$name_label" --minimal | grep -q "," | 
|  | } | 
|  |  | 
|  | function _network_exists() { | 
|  | local name_label | 
|  | name_label=$1 | 
|  |  | 
|  | ! [ -z $(xe network-list name-label="$name_label" --minimal) ] | 
|  | } | 
|  |  | 
|  | function _bridge_exists() { | 
|  | local bridge | 
|  | bridge=$1 | 
|  |  | 
|  | ! [ -z $(xe network-list bridge="$bridge" --minimal) ] | 
|  | } | 
|  |  | 
|  |  | 
|  | function setup_network() { | 
|  | local bridge_or_net_name | 
|  | bridge_or_net_name=$1 | 
|  |  | 
|  | if ! _bridge_exists "$bridge_or_net_name"; then | 
|  | if _network_exists "$bridge_or_net_name"; then | 
|  | if _multiple_networks_with_name "$bridge_or_net_name"; then | 
|  | cat >&2 << EOF | 
|  | ERROR: Multiple networks found matching name-label to "$bridge_or_net_name" | 
|  | please review your XenServer network configuration / localrc file. | 
|  | EOF | 
|  | exit 1 | 
|  | fi | 
|  | else | 
|  | _create_new_network "$bridge_or_net_name" | 
|  | fi | 
|  | fi | 
|  | } | 
|  |  | 
|  | function bridge_for() { | 
|  | local bridge_or_net_name | 
|  | bridge_or_net_name=$1 | 
|  |  | 
|  | if _bridge_exists "$bridge_or_net_name"; then | 
|  | echo "$bridge_or_net_name" | 
|  | else | 
|  | xe network-list name-label="$bridge_or_net_name" params=bridge --minimal | 
|  | fi | 
|  | } | 
|  |  | 
|  | function xenapi_ip_on() { | 
|  | local bridge_or_net_name | 
|  | bridge_or_net_name=$1 | 
|  |  | 
|  | ifconfig $(bridge_for "$bridge_or_net_name") | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//" | 
|  | } | 
|  |  | 
|  | function xenapi_is_listening_on() { | 
|  | local bridge_or_net_name | 
|  | bridge_or_net_name=$1 | 
|  |  | 
|  | ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ] | 
|  | } | 
|  |  | 
|  | function parameter_is_specified() { | 
|  | local parameter_name | 
|  | parameter_name=$1 | 
|  |  | 
|  | compgen -v | grep "$parameter_name" | 
|  | } |