4
macs=`ip addr | egrep 'link/ether' | cut -d' ' -f6`
7
if [ -z "$mac_address" ]; then
10
mac_address=$mac_address,$i
16
get_mac_address_by_interface() {
18
mac=`ip addr sh $iface | egrep -m1 'link/ether' | cut -d' ' -f6`
22
get_mac_address_data() {
24
OIFS=$IFS; IFS=","; set -- $input_string; IFS=$OIFS
27
mac_address=$mac_address"&mac_addresses=""${i}";
32
get_host_architecture() {
33
arch=`archdetect | cut -d'/' -f1`
45
--data "op=new${mac}&hostname=${hostname}&architecture=${arch}&after_commissioning_action=2" \
46
http://${serverurl}:${port}/api/1.0/nodes/
56
Usage: ${0##*/} [ options ]
58
node enlistment into the MaaS server
61
-s | --serverurl resolvable MaaS server API URL (maas.local if not specified)
62
-o | --serverport server port number (8000 if not specified)
63
-n | --hostname hostname of the node to register
64
-i | --interface interface address to register (obtains MAC address)
65
-a | --arch architecture of the node to register
68
- ${0##*/} --serverurl 127.0.0.1 --serverport 8000 --interface eth0
73
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }
75
short_opts="hs:o:n:i:a:"
76
long_opts="help,serverurl:,serverport:,hostname:,interface:,arch:"
77
getopt_out=$(getopt --name "${0##*/}" \
78
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
79
eval set -- "${getopt_out}" ||
82
while [ $# -ne 0 ]; do
85
-h|--help) Usage ; exit 0;;
86
-s|--serverurl) serverurl=${2}; shift;;
87
-o|--serverport) port=${2}; shift;;
88
-n|--hostname) hostname=${2}; shift;;
89
-i|--interface) iface=${2}; shift;;
90
-a|--arch) iface=${2}; shift;;
96
## check arguments here
97
#[ $# -eq 0 ] && bad_Usage
99
# If no interface is specified. obtain the MAC from all interfaces
100
if [ -z "$iface" ]; then
101
mac_addrs=$(get_mac_addresses)
103
mac_addrs=$(get_mac_address_by_interface $iface)
106
# If no port is specified, assume the default.
107
if [ -z "$port" ]; then
111
#TODO: Verify that it's a valid hostname (sanitize it)
112
if [ -z "$serverurl" ]; then
113
serverurl="maas.local"
114
elif echo $serverurl | grep -qs '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$'; then
116
# TODO: Check hostname URL is valid
118
# Error "The server address is incorrect"
121
#TODO: Auto-detect hostname?
122
if [ -z "$hostname" ]; then
124
#Error "No hostname has been provided"
127
if [ -z "$arch" ]; then
128
arch=$(get_host_architecture)
131
# Obtain the MAC addresses data
132
mac=$(get_mac_address_data $mac_addrs)
134
enlist_node $serverurl $port $mac $arch $hostname