Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | function xapi_plugin_location { |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 4 | for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/"; do |
| 5 | if [ -d $PLUGIN_DIR ]; then |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 6 | echo $PLUGIN_DIR |
| 7 | return 0 |
| 8 | fi |
| 9 | done |
| 10 | return 1 |
| 11 | } |
| 12 | |
| 13 | function zip_snapshot_location { |
| 14 | echo $1 | sed "s:\.git$::;s:$:/zipball/$2:g" |
| 15 | } |
| 16 | |
| 17 | function create_directory_for_kernels { |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 18 | if [ -d "/boot/guest" ]; then |
| 19 | echo "INFO: /boot/guest directory already exists, using that" >&2 |
| 20 | else |
| 21 | local LOCALPATH="$(get_local_sr_path)/os-guest-kernels" |
| 22 | mkdir -p $LOCALPATH |
| 23 | ln -s $LOCALPATH /boot/guest |
| 24 | fi |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | function extract_remote_zipball { |
| 28 | local ZIPBALL_URL=$1 |
| 29 | |
| 30 | local LOCAL_ZIPBALL=$(mktemp) |
| 31 | local EXTRACTED_FILES=$(mktemp -d) |
| 32 | |
| 33 | ( |
| 34 | wget -nv $ZIPBALL_URL -O $LOCAL_ZIPBALL --no-check-certificate |
| 35 | unzip -q -o $LOCAL_ZIPBALL -d $EXTRACTED_FILES |
| 36 | rm -f $LOCAL_ZIPBALL |
| 37 | ) >&2 |
| 38 | |
| 39 | echo "$EXTRACTED_FILES" |
| 40 | } |
| 41 | |
| 42 | function find_xapi_plugins_dir { |
| 43 | find $1 -path '*/xapi.d/plugins' -type d -print |
| 44 | } |
| 45 | |
| 46 | function install_xapi_plugins_from_zipball { |
| 47 | local XAPI_PLUGIN_DIR |
| 48 | local EXTRACTED_FILES |
| 49 | local EXTRACTED_PLUGINS_DIR |
| 50 | |
| 51 | XAPI_PLUGIN_DIR=$(xapi_plugin_location) |
| 52 | |
| 53 | EXTRACTED_FILES=$(extract_remote_zipball $1) |
| 54 | EXTRACTED_PLUGINS_DIR=$(find_xapi_plugins_dir $EXTRACTED_FILES) |
| 55 | |
| 56 | cp -pr $EXTRACTED_PLUGINS_DIR/* $XAPI_PLUGIN_DIR |
| 57 | rm -rf $EXTRACTED_FILES |
| 58 | chmod a+x ${XAPI_PLUGIN_DIR}* |
| 59 | } |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 60 | |
| 61 | function get_local_sr { |
| 62 | xe sr-list name-label="Local storage" --minimal |
| 63 | } |
| 64 | |
| 65 | function get_local_sr_path { |
| 66 | echo "/var/run/sr-mount/$(get_local_sr)" |
| 67 | } |
Mate Lakat | 8644676 | 2013-05-12 18:34:29 +0100 | [diff] [blame] | 68 | |
| 69 | function find_ip_by_name() { |
| 70 | local guest_name="$1" |
| 71 | local interface="$2" |
| 72 | |
| 73 | local period=10 |
| 74 | local max_tries=10 |
| 75 | local i=0 |
| 76 | |
| 77 | while true; do |
| 78 | if [ $i -ge $max_tries ]; then |
| 79 | echo "Timeout: ip address for interface $interface of $guest_name" |
| 80 | exit 11 |
| 81 | fi |
| 82 | |
| 83 | ipaddress=$(xe vm-list --minimal \ |
| 84 | name-label=$guest_name \ |
| 85 | params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p") |
| 86 | |
| 87 | if [ -z "$ipaddress" ]; then |
| 88 | sleep $period |
| 89 | ((i++)) |
| 90 | else |
| 91 | echo $ipaddress |
| 92 | break |
| 93 | fi |
| 94 | done |
| 95 | } |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 96 | |
| 97 | function _create_new_network() { |
| 98 | local name_label |
| 99 | name_label=$1 |
| 100 | |
| 101 | xe network-create name-label="$name_label" |
| 102 | } |
| 103 | |
| 104 | function _multiple_networks_with_name() { |
| 105 | local name_label |
| 106 | name_label=$1 |
| 107 | |
| 108 | # A comma indicates multiple matches |
| 109 | xe network-list name-label="$name_label" --minimal | grep -q "," |
| 110 | } |
| 111 | |
| 112 | function _network_exists() { |
| 113 | local name_label |
| 114 | name_label=$1 |
| 115 | |
| 116 | ! [ -z $(xe network-list name-label="$name_label" --minimal) ] |
| 117 | } |
| 118 | |
| 119 | function _bridge_exists() { |
| 120 | local bridge |
| 121 | bridge=$1 |
| 122 | |
| 123 | ! [ -z $(xe network-list bridge="$bridge" --minimal) ] |
| 124 | } |
| 125 | |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame^] | 126 | function _network_uuid() { |
| 127 | local bridge_or_net_name |
| 128 | bridge_or_net_name=$1 |
| 129 | |
| 130 | if _bridge_exists "$bridge_or_net_name"; then |
| 131 | xe network-list bridge="$bridge_or_net_name" --minimal |
| 132 | else |
| 133 | xe network-list name-label="$bridge_or_net_name" --minimal |
| 134 | fi |
| 135 | } |
| 136 | |
| 137 | function add_interface() { |
| 138 | local vm_name |
| 139 | local bridge_or_network_name |
| 140 | |
| 141 | vm_name="$1" |
| 142 | bridge_or_network_name="$2" |
| 143 | device_number="$3" |
| 144 | |
| 145 | local vm |
| 146 | local net |
| 147 | |
| 148 | vm=$(xe vm-list name-label="$vm_name" --minimal) |
| 149 | net=$(_network_uuid "$bridge_or_network_name") |
| 150 | xe vif-create network-uuid=$net vm-uuid=$vm device=$device_number |
| 151 | } |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 152 | |
| 153 | function setup_network() { |
| 154 | local bridge_or_net_name |
| 155 | bridge_or_net_name=$1 |
| 156 | |
| 157 | if ! _bridge_exists "$bridge_or_net_name"; then |
| 158 | if _network_exists "$bridge_or_net_name"; then |
| 159 | if _multiple_networks_with_name "$bridge_or_net_name"; then |
| 160 | cat >&2 << EOF |
| 161 | ERROR: Multiple networks found matching name-label to "$bridge_or_net_name" |
| 162 | please review your XenServer network configuration / localrc file. |
| 163 | EOF |
| 164 | exit 1 |
| 165 | fi |
| 166 | else |
| 167 | _create_new_network "$bridge_or_net_name" |
| 168 | fi |
| 169 | fi |
| 170 | } |
| 171 | |
| 172 | function bridge_for() { |
| 173 | local bridge_or_net_name |
| 174 | bridge_or_net_name=$1 |
| 175 | |
| 176 | if _bridge_exists "$bridge_or_net_name"; then |
| 177 | echo "$bridge_or_net_name" |
| 178 | else |
| 179 | xe network-list name-label="$bridge_or_net_name" params=bridge --minimal |
| 180 | fi |
| 181 | } |
| 182 | |
| 183 | function xenapi_ip_on() { |
| 184 | local bridge_or_net_name |
| 185 | bridge_or_net_name=$1 |
| 186 | |
| 187 | ifconfig $(bridge_for "$bridge_or_net_name") | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//" |
| 188 | } |
| 189 | |
| 190 | function xenapi_is_listening_on() { |
| 191 | local bridge_or_net_name |
| 192 | bridge_or_net_name=$1 |
| 193 | |
| 194 | ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ] |
| 195 | } |
| 196 | |
| 197 | function parameter_is_specified() { |
| 198 | local parameter_name |
| 199 | parameter_name=$1 |
| 200 | |
| 201 | compgen -v | grep "$parameter_name" |
| 202 | } |