~ubuntu-branches/ubuntu/quantal/maas-enlist/quantal

« back to all changes in this revision

Viewing changes to bin/maas-enlist

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-02-28 10:22:26 UTC
  • Revision ID: package-import@ubuntu.com-20120228102226-810vtwlm2wpbdvdr
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
 
 
3
get_mac_addresses() {
 
4
        macs=`ip addr | egrep 'link/ether' | cut -d' ' -f6`
 
5
        for i in $macs;
 
6
        do
 
7
                if [ -z "$mac_address" ]; then
 
8
                        mac_address=$i;
 
9
                else
 
10
                        mac_address=$mac_address,$i
 
11
                fi
 
12
        done
 
13
        echo $mac_address
 
14
}
 
15
 
 
16
get_mac_address_by_interface() {
 
17
        iface=$1
 
18
        mac=`ip addr sh $iface | egrep -m1 'link/ether' | cut -d' ' -f6`
 
19
        echo $mac
 
20
}
 
21
 
 
22
get_mac_address_data() {
 
23
        input_string=$1
 
24
        OIFS=$IFS; IFS=","; set -- $input_string; IFS=$OIFS
 
25
        for i in "$@";
 
26
        do
 
27
                mac_address=$mac_address"&mac_addresses=""${i}";
 
28
        done
 
29
        echo $mac_address
 
30
}
 
31
 
 
32
get_host_architecture() {
 
33
        arch=`archdetect | cut -d'/' -f1`
 
34
        echo $arch
 
35
}
 
36
 
 
37
enlist_node() {
 
38
        serverurl=${1}
 
39
        port=${2}
 
40
        mac=${3}
 
41
        arch=${4}
 
42
        hostname=${5}
 
43
 
 
44
        curl \
 
45
            --data "op=new${mac}&hostname=${hostname}&architecture=${arch}&after_commissioning_action=2" \
 
46
            http://${serverurl}:${port}/api/1.0/nodes/
 
47
}
 
48
 
 
49
Error () {
 
50
        echo "ERROR: $1"
 
51
        exit 1
 
52
}
 
53
 
 
54
Usage() {
 
55
        cat <<EOF
 
56
Usage: ${0##*/} [ options ]
 
57
 
 
58
   node enlistment into the MaaS server
 
59
 
 
60
   options:
 
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
 
66
 
 
67
   Example:
 
68
    - ${0##*/} --serverurl 127.0.0.1 --serverport 8000 --interface eth0
 
69
 
 
70
EOF
 
71
}
 
72
 
 
73
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }
 
74
 
 
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}" ||
 
80
        bad_Usage
 
81
 
 
82
while [ $# -ne 0 ]; do
 
83
        cur=${1}; next=${2};
 
84
        case "$cur" in
 
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;;
 
91
                --) shift; break;;
 
92
        esac
 
93
        shift;
 
94
done
 
95
 
 
96
## check arguments here
 
97
#[ $# -eq 0 ] && bad_Usage
 
98
 
 
99
# If no interface is specified. obtain the MAC from all interfaces
 
100
if [ -z "$iface" ]; then
 
101
        mac_addrs=$(get_mac_addresses)
 
102
else
 
103
        mac_addrs=$(get_mac_address_by_interface $iface)
 
104
fi
 
105
 
 
106
# If no port is specified, assume the default.
 
107
if [ -z "$port" ]; then
 
108
        port=8000
 
109
fi
 
110
 
 
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
 
115
        continue
 
116
# TODO: Check hostname URL is valid
 
117
#else
 
118
#       Error "The server address is incorrect"
 
119
fi
 
120
 
 
121
#TODO: Auto-detect hostname?
 
122
if [ -z "$hostname" ]; then
 
123
        continue
 
124
        #Error "No hostname has been provided"
 
125
fi
 
126
 
 
127
if [ -z "$arch" ]; then
 
128
        arch=$(get_host_architecture)
 
129
fi
 
130
 
 
131
# Obtain the MAC addresses data
 
132
mac=$(get_mac_address_data $mac_addrs)
 
133
 
 
134
enlist_node $serverurl $port $mac $arch $hostname