blob: b6910109f7a3beb855ca4a553fac06c2d5acbf72 [file] [log] [blame]
David K. Bainbridge6ea57c12016-06-06 23:29:12 -07001#!/bin/ash
2
3USERNAME=$1; shift
4HOSTIP=$1; shift
5SSH_KEY="-i /etc/maas/virtualbox/id_rsa"
6
7LIST=$(echo $* | awk '{print toupper($0)}')
8MATCH=
9
10best() {
11 local i
12 local BEST=
13 local BEST_CNT=0
14 for i in $MATCH; do
15 local CNT=$(echo $i | cut -d- -f2)
16 if [ $CNT > $BEST_CNT ]; then
17 BEST=$(echo $i | cut -d- -f1)
18 BEST_CNT=$CNT
19 fi
20 done
21 echo $BEST
22}
23
24inc() {
25 FOUND=$(echo $MATCH | grep "$1" | wc -l)
26 if [ $FOUND -eq 0 ]; then
27 MATCH="$MATCH $1-1"
28 else
29 NEXT=
30 local i=
31 for i in $MATCH; do
32 FOUND=$(echo $i | grep "$1" | wc -l)
33 if [ $FOUND -eq 1 ]; then
34 COUNT=$(echo $i | cut -d- -f2)
35 COUNT=$(expr $COUNT + 1)
36 NEXT="$NEXT $1-$COUNT"
37 else
38 NEXT="$NEXT $i"
39 fi
40 done
41 MATCH=$NEXT
42 fi
43}
44
45for i in $(ssh $SSH_KEY $USERNAME@$HOSTIP /usr/local/bin/vboxmanage list vms | awk '{print $NF}' | sed -e 's/[{}]//g'); do
46 for m in $(ssh $SSH_KEY $USERNAME@$HOSTIP /usr/local/bin/vboxmanage showvminfo --machinereadable $i | grep -i macaddress | cut -d= -f2 | sed -e 's/"//g' -e 's/\(..\)/\1:/g' -e 's/:$//g'); do
47 p=$(echo $i | cut -d- -f5)
48 for t in $LIST; do
49 if [ "$t" == "$m" ]; then
50 inc $p
51 fi
52 done
53 done
54done
55
56BEST=$(best)
57if [ "$BEST x" == " x" ]; then
58 echo "{}"
59else
60 MAC=$(echo $BEST | cut -d- -f5 | sed -e 's/"//g' -e 's/\(..\)/\1:/g' -e 's/:$//g')
61 echo "{\"name\":\"amt\",\"mac_address\":\"$MAC\",\"power_password\":\"$USERNAME\",\"power_address\":\"$HOSTIP\"}"
62fi