~julian-edwards/maas/maas-enlist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/sh
#
#    maas-enlist: MAAS Enlistment Tool
#
#    Copyright (C) 2012 Canonical Ltd.
#
#    Authors: Andres Rodriguez <andres.rodriguez@canonical.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

get_mac_addresses() {
	macs=`ip addr | egrep 'link/ether' | cut -d' ' -f6`
	for i in $macs;
	do
		if [ -z "$mac_address" ]; then
			mac_address="$i";
		else
			mac_address="$mac_address,$i"
		fi
	done
	echo "$mac_address"
}

get_mac_address_by_interface() {
	iface="$1"
	mac=`ip addr sh "$iface" | egrep -m1 'link/ether' | cut -d' ' -f6`
	echo "$mac"
}

get_mac_address_data() {
	input_string="$1"
	OIFS=$IFS; IFS=","; set -- $input_string; IFS=$OIFS
	for i in "$@";
	do
		mac_address="$mac_address""&mac_addresses=""${i}";
	done
	echo "$mac_address"
}

get_host_architecture() {
	if grep "flags" /proc/cpuinfo | grep -qs "\ lm\ "; then
		# if /proc/cpuinfo Flags has 'lm', it is x86_64
		arch="amd64"
	else
		arch=`archdetect | cut -d'/' -f1`
	fi
	echo "$arch"
}

get_server_name() {
	local servername="$1";
	_RET=${servername#*://};
	_RET=${_RET%%/*};
	echo "$_RET";
}
enlist_node() {
	serverurl="${1}"
	mac="${2}"
	arch="${3}"
	hostname="${4}"

	curl \
	    --data "op=new${mac}&hostname=${hostname}&architecture=${arch}&after_commissioning_action=0" \
	    "${serverurl}"
}

Error () {
	echo "ERROR: $1"
	exit 1
}

Usage() {
	cat <<EOF
Usage: ${0##*/} [ options ]

   node enlistment into the MAAS server

   options:
      -s | --serverurl        resolvable MAAS server API URL (maas.local if not specified)
      -n | --hostname         hostname of the node to register
      -i | --interface        interface address to register (obtains MAC address)
      -a | --arch             architecture of the node to register

   Example:
    - ${0##*/} --serverurl 127.0.0.1 --interface eth0

EOF
}

bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || Error "$@"; exit 1; }

short_opts="hs:n:i:a:"
long_opts="help,serverurl:,hostname:,interface:,arch:"
getopt_out=$(getopt --name "${0##*/}" \
	--options "${short_opts}" --long "${long_opts}" -- "$@") &&
	eval set -- "${getopt_out}" ||
	bad_Usage

while [ $# -ne 0 ]; do
	cur=${1}; next=${2};
	case "$cur" in
		-h|--help) Usage ; exit 0;;
		-s|--serverurl) serverurl=${2}; shift;;
		-n|--hostname) hostname=${2}; shift;;
		-i|--interface) iface=${2}; shift;;
		-a|--arch) arch=${2}; shift;;
		--) shift; break;;
	esac
	shift;
done

## check arguments here
#[ $# -eq 0 ] && bad_Usage

# If no interface is specified. obtain the MAC from all interfaces
if [ -z "$iface" ]; then
	mac_addrs=$(get_mac_addresses)
else
	mac_addrs=$(get_mac_address_by_interface "$iface")
fi

protocol=
servername=$(get_server_name "$serverurl")
if echo "$serverurl" | egrep -q '^[a-z]+://' ; then
	protocol=`echo "$serverurl" | sed 's#^\([a-z]\+\)://.*#\\1#'`
else
	protocol="http"
fi

if [ "$protocol" != "http" ] && [ "$protocol" != "https" ]; then
	Error "Invalid protocol '$protocol'"
fi

if [ -z "$servername" ]; then
	serverurl="maas.local"
	servername="$serverurl"
fi
if echo "$serverurl" | egrep -q '(^[a-z]+://|^)[a-z0-9\.]+($|/$)'; then
	api_url="MAAS/api/1.0/nodes/"
else
	api_url=`echo $serverurl | sed 's#^\(\|[a-z]\+://\)[a-zA-Z0-9\.]\+/##'`
fi

#TODO: Auto-detect hostname?
if [ -z "$hostname" ]; then
	continue
	#Error "No hostname has been provided"
fi

if [ -z "$arch" ]; then
	arch=$(get_host_architecture)
fi

# Obtain the MAC addresses data
mac=$(get_mac_address_data "$mac_addrs")

enlist_node "$protocol://$servername/$api_url" "$mac" "$arch" "$hostname"