blob: c6e484d03edfbd7fda8b124010a9adef84d6f90f [file] [log] [blame]
Mate Lakat57e3da92013-03-22 16:34:05 +00001#!/bin/bash
2
3function xapi_plugin_location {
Mate Lakatfe586b12013-03-28 15:02:27 +00004 for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/"; do
5 if [ -d $PLUGIN_DIR ]; then
Mate Lakat57e3da92013-03-22 16:34:05 +00006 echo $PLUGIN_DIR
7 return 0
8 fi
9 done
10 return 1
11}
12
13function zip_snapshot_location {
14 echo $1 | sed "s:\.git$::;s:$:/zipball/$2:g"
15}
16
17function create_directory_for_kernels {
Mate Lakatfe586b12013-03-28 15:02:27 +000018 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 Lakat57e3da92013-03-22 16:34:05 +000025}
26
27function 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
42function find_xapi_plugins_dir {
43 find $1 -path '*/xapi.d/plugins' -type d -print
44}
45
46function 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 Lakatfe586b12013-03-28 15:02:27 +000060
61function get_local_sr {
62 xe sr-list name-label="Local storage" --minimal
63}
64
65function get_local_sr_path {
66 echo "/var/run/sr-mount/$(get_local_sr)"
67}
Mate Lakat86446762013-05-12 18:34:29 +010068
69function 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 Lakat9e326772013-05-08 16:42:22 +010096
97function _create_new_network() {
98 local name_label
99 name_label=$1
100
101 xe network-create name-label="$name_label"
102}
103
104function _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
112function _network_exists() {
113 local name_label
114 name_label=$1
115
116 ! [ -z $(xe network-list name-label="$name_label" --minimal) ]
117}
118
119function _bridge_exists() {
120 local bridge
121 bridge=$1
122
123 ! [ -z $(xe network-list bridge="$bridge" --minimal) ]
124}
125
126
127function setup_network() {
128 local bridge_or_net_name
129 bridge_or_net_name=$1
130
131 if ! _bridge_exists "$bridge_or_net_name"; then
132 if _network_exists "$bridge_or_net_name"; then
133 if _multiple_networks_with_name "$bridge_or_net_name"; then
134 cat >&2 << EOF
135ERROR: Multiple networks found matching name-label to "$bridge_or_net_name"
136please review your XenServer network configuration / localrc file.
137EOF
138 exit 1
139 fi
140 else
141 _create_new_network "$bridge_or_net_name"
142 fi
143 fi
144}
145
146function bridge_for() {
147 local bridge_or_net_name
148 bridge_or_net_name=$1
149
150 if _bridge_exists "$bridge_or_net_name"; then
151 echo "$bridge_or_net_name"
152 else
153 xe network-list name-label="$bridge_or_net_name" params=bridge --minimal
154 fi
155}
156
157function xenapi_ip_on() {
158 local bridge_or_net_name
159 bridge_or_net_name=$1
160
161 ifconfig $(bridge_for "$bridge_or_net_name") | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"
162}
163
164function xenapi_is_listening_on() {
165 local bridge_or_net_name
166 bridge_or_net_name=$1
167
168 ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ]
169}
170
171function parameter_is_specified() {
172 local parameter_name
173 parameter_name=$1
174
175 compgen -v | grep "$parameter_name"
176}