Merge lp:~mpontillo/maas/maas-enlist-wget-and-curl into lp:~maas-maintainers/maas/maas-enlist

Proposed by Mike Pontillo
Status: Merged
Merged at revision: 42
Proposed branch: lp:~mpontillo/maas/maas-enlist-wget-and-curl
Merge into: lp:~maas-maintainers/maas/maas-enlist
Prerequisite: lp:~cyphermox/maas/maas-enlist-wget-not-curl
Diff against target: 343 lines (+156/-111)
1 file modified
bin/maas-enlist (+156/-111)
To merge this branch: bzr merge lp:~mpontillo/maas/maas-enlist-wget-and-curl
Reviewer Review Type Date Requested Status
Mike Pontillo (community) Approve
Mathieu Trudel-Lapierre (community) Approve
Review via email: mp+270895@code.launchpad.net

This proposal supersedes a proposal from 2015-09-13.

Commit message

Support wget and curl in maas-enlist.

Description of the change

Support both wget and curl in maas-enlist.

Based on a branch proposed by Mathieu Trudel-Lapierre on 2015-09-03.

To post a comment you must log in.
Revision history for this message
Mathieu Trudel-Lapierre (cyphermox) wrote :

Looks fine to me as long as 'which' is available. It might not be the case in d-i (but it likely is there).

review: Needs Information
Revision history for this message
Mathieu Trudel-Lapierre (cyphermox) :
review: Approve
Revision history for this message
Mike Pontillo (mpontillo) wrote :

Since $(which) wasn't there, I updated it to use $(ls) instead.

Thanks for taking a look! Self-approving.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/maas-enlist'
2--- bin/maas-enlist 2015-09-15 20:09:15 +0000
3+++ bin/maas-enlist 2015-09-15 20:09:15 +0000
4@@ -19,99 +19,132 @@
5 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6
7 get_mac_addresses() {
8- macs=`ip addr | egrep 'link/ether' | cut -d' ' -f6`
9- for i in $macs;
10- do
11- if [ -z "$mac_address" ]; then
12- mac_address="$i";
13- else
14- mac_address="$mac_address,$i"
15- fi
16- done
17- echo "$mac_address"
18+ macs=`ip addr | egrep 'link/ether' | cut -d' ' -f6`
19+ for i in $macs;
20+ do
21+ if [ -z "$mac_address" ]; then
22+ mac_address="$i";
23+ else
24+ mac_address="$mac_address,$i"
25+ fi
26+ done
27+ echo "$mac_address"
28 }
29
30 get_mac_address_by_interface() {
31- iface="$1"
32- mac=`ip addr sh "$iface" | egrep -m1 'link/ether' | cut -d' ' -f6`
33- echo "$mac"
34-}
35-
36-get_mac_address_parms() {
37- local args="" input_string="$1"
38- OIFS=$IFS; IFS=","; set -- $input_string; IFS=$OIFS
39- for i in "$@";
40- do
41- args="${args}&mac_addresses=${i}"
42- #mac_address="$mac_address""&mac_addresses=""${i}";
43- done
44- echo "${args# }"
45+ iface="$1"
46+ mac=`ip addr sh "$iface" | egrep -m1 'link/ether' | cut -d' ' -f6`
47+ echo "$mac"
48+}
49+
50+get_mac_address_curl_parms() {
51+ local args="" input_string="$1"
52+ OIFS=$IFS; IFS=","; set -- $input_string; IFS=$OIFS
53+ for i in "$@";
54+ do
55+ args="${args} --data-urlencode mac_addresses=${i}"
56+ done
57+ echo "${args# }"
58+}
59+
60+get_mac_address_wget_parms() {
61+ local args="" input_string="$1"
62+ OIFS=$IFS; IFS=","; set -- $input_string; IFS=$OIFS
63+ for i in "$@";
64+ do
65+ args="${args}&mac_addresses=${i}"
66+ done
67+ echo "${args# }"
68 }
69
70 get_host_architecture() {
71- if grep "flags" /proc/cpuinfo | grep -qs "\ lm\ "; then
72- # if /proc/cpuinfo Flags has 'lm', it is x86_64
73- arch="amd64"
74- else
75- arch=`archdetect | cut -d'/' -f1`
76- fi
77- echo "$arch"
78+ if grep "flags" /proc/cpuinfo | grep -qs "\ lm\ "; then
79+ # if /proc/cpuinfo Flags has 'lm', it is x86_64
80+ arch="amd64"
81+ else
82+ arch=`archdetect | cut -d'/' -f1`
83+ fi
84+ echo "$arch"
85 }
86
87 get_host_subarchitecture() {
88- local arch=$1
89- case $arch in
90- i386|amd64)
91- # Skip the call to archdetect as that's what
92- # get_host_architecture does
93- echo generic
94- ;;
95- *)
96- archdetect | cut -d'/' -f2
97- ;;
98- esac
99+ local arch=$1
100+ case $arch in
101+ i386|amd64)
102+ # Skip the call to archdetect as that's what
103+ # get_host_architecture does
104+ echo generic
105+ ;;
106+ *)
107+ archdetect | cut -d'/' -f2
108+ ;;
109+ esac
110 }
111
112 get_server_name() {
113- local servername="$1";
114- _RET=${servername#*://};
115- _RET=${_RET%%/*};
116- echo "$_RET";
117-}
118-
119-enlist_node() {
120- serverurl="${1}"
121- mac="${2}"
122- arch="${3}"
123- subarch="${4}"
124- hostname="${5}"
125- power_type="${6}"
126- power_params="${7}"
127-
128- local macparms=""
129- macparms=$(get_mac_address_parms "$mac")
130+ local servername="$1";
131+ _RET=${servername#*://};
132+ _RET=${_RET%%/*};
133+ echo "$_RET";
134+}
135+
136+enlist_node_curl() {
137+ serverurl="${1}"
138+ mac="${2}"
139+ arch="${3}"
140+ subarch="${4}"
141+ hostname="${5}"
142+ power_type="${6}"
143+ power_params="${7}"
144+
145+ local macparms=""
146+ macparms=$(get_mac_address_curl_parms "$mac")
147+
148+ curl \
149+ --data-urlencode "op=new" \
150+ --data-urlencode "autodetect_nodegroup=1" \
151+ --data-urlencode "hostname=${hostname}" \
152+ --data-urlencode "architecture=${arch}" \
153+ --data-urlencode "subarchitecture=${subarch}" \
154+ --data-urlencode "power_type=${power_type}" \
155+ --data-urlencode "power_parameters=${power_params}" \
156+ ${macparms} \
157+ "${serverurl}"
158+}
159+
160+enlist_node_wget() {
161+ serverurl="${1}"
162+ mac="${2}"
163+ arch="${3}"
164+ subarch="${4}"
165+ hostname="${5}"
166+ power_type="${6}"
167+ power_params="${7}"
168+
169+ local macparms=""
170+ macparms=$(get_mac_address_wget_parms "$mac")
171
172- local params="op=new"
173- params="${params}&autodetect_nodegroup=1"
174- params="${params}&hostname=${hostname}"
175- params="${params}&architecture=${arch}"
176- params="${params}&subarchitecture=${subarch}"
177- params="${params}&power_type=${power_type}"
178- params="${params}&power_parameters=${power_params}"
179- params="${params}${macparms}"
180+ local params="op=new"
181+ params="${params}&autodetect_nodegroup=1"
182+ params="${params}&hostname=${hostname}"
183+ params="${params}&architecture=${arch}"
184+ params="${params}&subarchitecture=${subarch}"
185+ params="${params}&power_type=${power_type}"
186+ params="${params}&power_parameters=${power_params}"
187+ params="${params}${macparms}"
188
189- wget -q -O/dev/null \
190- "${serverurl}" \
191- --post-data="$params"
192+ wget -q -O/dev/null \
193+ "${serverurl}" \
194+ --post-data="$params"
195 }
196
197 Error () {
198- echo "ERROR: $1"
199- exit 1
200+ echo "ERROR: $1"
201+ exit 1
202 }
203
204 Usage() {
205- cat <<EOF
206+ cat <<EOF
207 Usage: ${0##*/} [ options ]
208
209 node enlistment into the MAAS server
210@@ -134,83 +167,95 @@
211
212 bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || Error "$@"; exit 1; }
213
214+CURL=$(ls -1 /usr/bin/curl 2> /dev/null)
215+WGET=$(ls -1 /usr/bin/wget 2> /dev/null)
216+
217 short_opts="hs:n:i:a:t:p:"
218-long_opts="help,serverurl:,hostname:,interface:,arch:,subarch:,power-type:,power-params:"
219+long_opts="help,serverurl:,hostname:,interface:,arch:,subarch:,power-type:,power-params:,wget,curl"
220 getopt_out=$(getopt --name "${0##*/}" \
221- --options "${short_opts}" --long "${long_opts}" -- "$@") &&
222- eval set -- "${getopt_out}" ||
223- bad_Usage
224+ --options "${short_opts}" --long "${long_opts}" -- "$@") &&
225+ eval set -- "${getopt_out}" ||
226+ bad_Usage
227
228 while [ $# -ne 0 ]; do
229- cur=${1}; next=${2};
230- case "$cur" in
231- -h|--help) Usage ; exit 0;;
232- -s|--serverurl) serverurl=${2}; shift;;
233- -n|--hostname) hostname=${2}; shift;;
234- -i|--interface) iface=${2}; shift;;
235- -a|--arch) arch=${2}; shift;;
236- --subarch) subarch=${2}; shift;;
237- -t|--power-type) power_type=${2}; shift;;
238- -p|--power-params) power_parameters=${2}; shift;;
239- --) shift; break;;
240- esac
241- shift;
242+ cur=${1}; next=${2};
243+ case "$cur" in
244+ -h|--help) Usage ; exit 0;;
245+ -s|--serverurl) serverurl=${2}; shift;;
246+ -n|--hostname) hostname=${2}; shift;;
247+ -i|--interface) iface=${2}; shift;;
248+ -a|--arch) arch=${2}; shift;;
249+ --subarch) subarch=${2}; shift;;
250+ -t|--power-type) power_type=${2}; shift;;
251+ -p|--power-params) power_parameters=${2}; shift;;
252+ --wget) CURL=""; shift;;
253+ --curl) WGET=""; shift;;
254+ --) shift; break;;
255+ esac
256+ shift;
257 done
258
259 ## check arguments here
260 #[ $# -eq 0 ] && bad_Usage
261
262+
263 # If no interface is specified. obtain the MAC from all interfaces
264 if [ -z "$iface" ]; then
265- mac_addrs=$(get_mac_addresses)
266+ mac_addrs=$(get_mac_addresses)
267 else
268- mac_addrs=$(get_mac_address_by_interface "$iface")
269+ mac_addrs=$(get_mac_address_by_interface "$iface")
270 fi
271
272 protocol=
273 servername=$(get_server_name "$serverurl")
274 if echo "$serverurl" | egrep -q '^[a-z]+://' ; then
275- protocol=`echo "$serverurl" | sed 's#^\([a-z]\+\)://.*#\\1#'`
276+ protocol=`echo "$serverurl" | sed 's#^\([a-z]\+\)://.*#\\1#'`
277 else
278- protocol="http"
279+ protocol="http"
280 fi
281
282 if [ "$protocol" != "http" ] && [ "$protocol" != "https" ]; then
283- Error "Invalid protocol '$protocol'"
284+ Error "Invalid protocol '$protocol'"
285 fi
286
287 if [ -z "$servername" ]; then
288- serverurl="maas.local"
289- servername="$serverurl"
290+ serverurl="maas.local"
291+ servername="$serverurl"
292 fi
293 if echo "$serverurl" | egrep -q '(^[a-z]+://|^)[a-z0-9\.]+($|/$)'; then
294- api_url="MAAS/api/1.0/nodes/"
295+ api_url="MAAS/api/1.0/nodes/"
296 else
297- api_url=`echo $serverurl | sed 's#^\(\|[a-z]\+://\)[a-zA-Z0-9\.]\+\(\|\:[0-9]\+\)/##'`
298+ api_url=`echo $serverurl | sed 's#^\(\|[a-z]\+://\)[a-zA-Z0-9\.]\+\(\|\:[0-9]\+\)/##'`
299 fi
300
301 #TODO: Auto-detect hostname?
302 if [ -z "$hostname" ]; then
303- continue
304- #Error "No hostname has been provided"
305+ continue
306+ #Error "No hostname has been provided"
307 fi
308
309 if [ -z "$arch" ]; then
310- arch=$(get_host_architecture)
311+ arch=$(get_host_architecture)
312 fi
313
314 if [ -z "$subarch" ]; then
315- subarch=$(get_host_subarchitecture $arch)
316+ subarch=$(get_host_subarchitecture $arch)
317 fi
318
319 if [ -n "$power_type" ]; then
320- case $power_type in
321- ipmi) continue ;;
322- virsh) continue ;;
323- etherwake) continue ;;
324- moonshot) continue ;;
325- *) Error "Invalid power type: [$power_type]"
326- esac
327+ case $power_type in
328+ ipmi) continue ;;
329+ virsh) continue ;;
330+ etherwake) continue ;;
331+ moonshot) continue ;;
332+ *) Error "Invalid power type: [$power_type]"
333+ esac
334 fi
335
336-enlist_node "$protocol://$servername/$api_url" "${mac_addrs}" "$arch" "$subarch" "$hostname" "$power_type" "$power_parameters"
337+if [ "$CURL" != "" ]; then
338+ enlist_node_curl "$protocol://$servername/$api_url" "${mac_addrs}" "$arch" "$subarch" "$hostname" "$power_type" "$power_parameters"
339+elif [ "$WGET" != "" ]; then
340+ enlist_node_wget "$protocol://$servername/$api_url" "${mac_addrs}" "$arch" "$subarch" "$hostname" "$power_type" "$power_parameters"
341+else
342+ echo "Must install 'curl' or 'wget' to use this script."
343+fi

Subscribers

People subscribed via source and target branches