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
=== modified file 'bin/maas-enlist'
--- bin/maas-enlist 2015-09-15 20:09:15 +0000
+++ bin/maas-enlist 2015-09-15 20:09:15 +0000
@@ -19,99 +19,132 @@
19# along with this program. If not, see <http://www.gnu.org/licenses/>.19# along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
21get_mac_addresses() {21get_mac_addresses() {
22 macs=`ip addr | egrep 'link/ether' | cut -d' ' -f6`22 macs=`ip addr | egrep 'link/ether' | cut -d' ' -f6`
23 for i in $macs;23 for i in $macs;
24 do24 do
25 if [ -z "$mac_address" ]; then25 if [ -z "$mac_address" ]; then
26 mac_address="$i";26 mac_address="$i";
27 else27 else
28 mac_address="$mac_address,$i"28 mac_address="$mac_address,$i"
29 fi29 fi
30 done30 done
31 echo "$mac_address"31 echo "$mac_address"
32}32}
3333
34get_mac_address_by_interface() {34get_mac_address_by_interface() {
35 iface="$1"35 iface="$1"
36 mac=`ip addr sh "$iface" | egrep -m1 'link/ether' | cut -d' ' -f6`36 mac=`ip addr sh "$iface" | egrep -m1 'link/ether' | cut -d' ' -f6`
37 echo "$mac"37 echo "$mac"
38}38}
3939
40get_mac_address_parms() {40get_mac_address_curl_parms() {
41 local args="" input_string="$1"41 local args="" input_string="$1"
42 OIFS=$IFS; IFS=","; set -- $input_string; IFS=$OIFS42 OIFS=$IFS; IFS=","; set -- $input_string; IFS=$OIFS
43 for i in "$@";43 for i in "$@";
44 do44 do
45 args="${args}&mac_addresses=${i}"45 args="${args} --data-urlencode mac_addresses=${i}"
46 #mac_address="$mac_address""&mac_addresses=""${i}";46 done
47 done47 echo "${args# }"
48 echo "${args# }"48}
49
50get_mac_address_wget_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}&mac_addresses=${i}"
56 done
57 echo "${args# }"
49}58}
5059
51get_host_architecture() {60get_host_architecture() {
52 if grep "flags" /proc/cpuinfo | grep -qs "\ lm\ "; then61 if grep "flags" /proc/cpuinfo | grep -qs "\ lm\ "; then
53 # if /proc/cpuinfo Flags has 'lm', it is x86_6462 # if /proc/cpuinfo Flags has 'lm', it is x86_64
54 arch="amd64"63 arch="amd64"
55 else64 else
56 arch=`archdetect | cut -d'/' -f1`65 arch=`archdetect | cut -d'/' -f1`
57 fi66 fi
58 echo "$arch"67 echo "$arch"
59}68}
6069
61get_host_subarchitecture() {70get_host_subarchitecture() {
62 local arch=$171 local arch=$1
63 case $arch in72 case $arch in
64 i386|amd64)73 i386|amd64)
65 # Skip the call to archdetect as that's what74 # Skip the call to archdetect as that's what
66 # get_host_architecture does75 # get_host_architecture does
67 echo generic76 echo generic
68 ;;77 ;;
69 *)78 *)
70 archdetect | cut -d'/' -f279 archdetect | cut -d'/' -f2
71 ;;80 ;;
72 esac81 esac
73}82}
7483
75get_server_name() {84get_server_name() {
76 local servername="$1";85 local servername="$1";
77 _RET=${servername#*://};86 _RET=${servername#*://};
78 _RET=${_RET%%/*};87 _RET=${_RET%%/*};
79 echo "$_RET";88 echo "$_RET";
80}89}
8190
82enlist_node() {91enlist_node_curl() {
83 serverurl="${1}"92 serverurl="${1}"
84 mac="${2}"93 mac="${2}"
85 arch="${3}"94 arch="${3}"
86 subarch="${4}"95 subarch="${4}"
87 hostname="${5}"96 hostname="${5}"
88 power_type="${6}"97 power_type="${6}"
89 power_params="${7}"98 power_params="${7}"
9099
91 local macparms=""100 local macparms=""
92 macparms=$(get_mac_address_parms "$mac")101 macparms=$(get_mac_address_curl_parms "$mac")
102
103 curl \
104 --data-urlencode "op=new" \
105 --data-urlencode "autodetect_nodegroup=1" \
106 --data-urlencode "hostname=${hostname}" \
107 --data-urlencode "architecture=${arch}" \
108 --data-urlencode "subarchitecture=${subarch}" \
109 --data-urlencode "power_type=${power_type}" \
110 --data-urlencode "power_parameters=${power_params}" \
111 ${macparms} \
112 "${serverurl}"
113}
114
115enlist_node_wget() {
116 serverurl="${1}"
117 mac="${2}"
118 arch="${3}"
119 subarch="${4}"
120 hostname="${5}"
121 power_type="${6}"
122 power_params="${7}"
123
124 local macparms=""
125 macparms=$(get_mac_address_wget_parms "$mac")
93 126
94 local params="op=new"127 local params="op=new"
95 params="${params}&autodetect_nodegroup=1"128 params="${params}&autodetect_nodegroup=1"
96 params="${params}&hostname=${hostname}"129 params="${params}&hostname=${hostname}"
97 params="${params}&architecture=${arch}"130 params="${params}&architecture=${arch}"
98 params="${params}&subarchitecture=${subarch}"131 params="${params}&subarchitecture=${subarch}"
99 params="${params}&power_type=${power_type}"132 params="${params}&power_type=${power_type}"
100 params="${params}&power_parameters=${power_params}"133 params="${params}&power_parameters=${power_params}"
101 params="${params}${macparms}"134 params="${params}${macparms}"
102135
103 wget -q -O/dev/null \136 wget -q -O/dev/null \
104 "${serverurl}" \137 "${serverurl}" \
105 --post-data="$params"138 --post-data="$params"
106}139}
107140
108Error () {141Error () {
109 echo "ERROR: $1"142 echo "ERROR: $1"
110 exit 1143 exit 1
111}144}
112145
113Usage() {146Usage() {
114 cat <<EOF147 cat <<EOF
115Usage: ${0##*/} [ options ]148Usage: ${0##*/} [ options ]
116149
117 node enlistment into the MAAS server150 node enlistment into the MAAS server
@@ -134,83 +167,95 @@
134167
135bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || Error "$@"; exit 1; }168bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || Error "$@"; exit 1; }
136169
170CURL=$(ls -1 /usr/bin/curl 2> /dev/null)
171WGET=$(ls -1 /usr/bin/wget 2> /dev/null)
172
137short_opts="hs:n:i:a:t:p:"173short_opts="hs:n:i:a:t:p:"
138long_opts="help,serverurl:,hostname:,interface:,arch:,subarch:,power-type:,power-params:"174long_opts="help,serverurl:,hostname:,interface:,arch:,subarch:,power-type:,power-params:,wget,curl"
139getopt_out=$(getopt --name "${0##*/}" \175getopt_out=$(getopt --name "${0##*/}" \
140 --options "${short_opts}" --long "${long_opts}" -- "$@") &&176 --options "${short_opts}" --long "${long_opts}" -- "$@") &&
141 eval set -- "${getopt_out}" ||177 eval set -- "${getopt_out}" ||
142 bad_Usage178 bad_Usage
143179
144while [ $# -ne 0 ]; do180while [ $# -ne 0 ]; do
145 cur=${1}; next=${2};181 cur=${1}; next=${2};
146 case "$cur" in182 case "$cur" in
147 -h|--help) Usage ; exit 0;;183 -h|--help) Usage ; exit 0;;
148 -s|--serverurl) serverurl=${2}; shift;;184 -s|--serverurl) serverurl=${2}; shift;;
149 -n|--hostname) hostname=${2}; shift;;185 -n|--hostname) hostname=${2}; shift;;
150 -i|--interface) iface=${2}; shift;;186 -i|--interface) iface=${2}; shift;;
151 -a|--arch) arch=${2}; shift;;187 -a|--arch) arch=${2}; shift;;
152 --subarch) subarch=${2}; shift;;188 --subarch) subarch=${2}; shift;;
153 -t|--power-type) power_type=${2}; shift;;189 -t|--power-type) power_type=${2}; shift;;
154 -p|--power-params) power_parameters=${2}; shift;;190 -p|--power-params) power_parameters=${2}; shift;;
155 --) shift; break;;191 --wget) CURL=""; shift;;
156 esac192 --curl) WGET=""; shift;;
157 shift;193 --) shift; break;;
194 esac
195 shift;
158done196done
159197
160## check arguments here198## check arguments here
161#[ $# -eq 0 ] && bad_Usage199#[ $# -eq 0 ] && bad_Usage
162200
201
163# If no interface is specified. obtain the MAC from all interfaces202# If no interface is specified. obtain the MAC from all interfaces
164if [ -z "$iface" ]; then203if [ -z "$iface" ]; then
165 mac_addrs=$(get_mac_addresses)204 mac_addrs=$(get_mac_addresses)
166else205else
167 mac_addrs=$(get_mac_address_by_interface "$iface")206 mac_addrs=$(get_mac_address_by_interface "$iface")
168fi207fi
169208
170protocol=209protocol=
171servername=$(get_server_name "$serverurl")210servername=$(get_server_name "$serverurl")
172if echo "$serverurl" | egrep -q '^[a-z]+://' ; then211if echo "$serverurl" | egrep -q '^[a-z]+://' ; then
173 protocol=`echo "$serverurl" | sed 's#^\([a-z]\+\)://.*#\\1#'`212 protocol=`echo "$serverurl" | sed 's#^\([a-z]\+\)://.*#\\1#'`
174else213else
175 protocol="http"214 protocol="http"
176fi215fi
177216
178if [ "$protocol" != "http" ] && [ "$protocol" != "https" ]; then217if [ "$protocol" != "http" ] && [ "$protocol" != "https" ]; then
179 Error "Invalid protocol '$protocol'"218 Error "Invalid protocol '$protocol'"
180fi219fi
181220
182if [ -z "$servername" ]; then221if [ -z "$servername" ]; then
183 serverurl="maas.local"222 serverurl="maas.local"
184 servername="$serverurl"223 servername="$serverurl"
185fi224fi
186if echo "$serverurl" | egrep -q '(^[a-z]+://|^)[a-z0-9\.]+($|/$)'; then225if echo "$serverurl" | egrep -q '(^[a-z]+://|^)[a-z0-9\.]+($|/$)'; then
187 api_url="MAAS/api/1.0/nodes/"226 api_url="MAAS/api/1.0/nodes/"
188else227else
189 api_url=`echo $serverurl | sed 's#^\(\|[a-z]\+://\)[a-zA-Z0-9\.]\+\(\|\:[0-9]\+\)/##'`228 api_url=`echo $serverurl | sed 's#^\(\|[a-z]\+://\)[a-zA-Z0-9\.]\+\(\|\:[0-9]\+\)/##'`
190fi229fi
191230
192#TODO: Auto-detect hostname?231#TODO: Auto-detect hostname?
193if [ -z "$hostname" ]; then232if [ -z "$hostname" ]; then
194 continue233 continue
195 #Error "No hostname has been provided"234 #Error "No hostname has been provided"
196fi235fi
197236
198if [ -z "$arch" ]; then237if [ -z "$arch" ]; then
199 arch=$(get_host_architecture)238 arch=$(get_host_architecture)
200fi239fi
201240
202if [ -z "$subarch" ]; then241if [ -z "$subarch" ]; then
203 subarch=$(get_host_subarchitecture $arch)242 subarch=$(get_host_subarchitecture $arch)
204fi243fi
205244
206if [ -n "$power_type" ]; then245if [ -n "$power_type" ]; then
207 case $power_type in246 case $power_type in
208 ipmi) continue ;;247 ipmi) continue ;;
209 virsh) continue ;;248 virsh) continue ;;
210 etherwake) continue ;;249 etherwake) continue ;;
211 moonshot) continue ;;250 moonshot) continue ;;
212 *) Error "Invalid power type: [$power_type]"251 *) Error "Invalid power type: [$power_type]"
213 esac252 esac
214fi253fi
215254
216enlist_node "$protocol://$servername/$api_url" "${mac_addrs}" "$arch" "$subarch" "$hostname" "$power_type" "$power_parameters"255if [ "$CURL" != "" ]; then
256 enlist_node_curl "$protocol://$servername/$api_url" "${mac_addrs}" "$arch" "$subarch" "$hostname" "$power_type" "$power_parameters"
257elif [ "$WGET" != "" ]; then
258 enlist_node_wget "$protocol://$servername/$api_url" "${mac_addrs}" "$arch" "$subarch" "$hostname" "$power_type" "$power_parameters"
259else
260 echo "Must install 'curl' or 'wget' to use this script."
261fi

Subscribers

People subscribed via source and target branches