The cirros image was rebuilt against the 3.13.0-83 kernel, drivers e1000e, igbvf...
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / usr / bin / ec2metadata
1 #!/bin/sh
2
3 MD_URL="http://169.254.169.254/latest"
4 OIFS="$IFS"
5 TIMEOUT=10
6 CR="
7 "
8
9 Usage() {
10         cat <<EOF
11 Usage: ${0##*/} [options]
12   -u | --url URL    use URL (default: $MD_URL)
13   -h | --help       display usage
14
15        --user-data  dump user data
16        --<item>     dump 'item' from the metadata service
17
18   Special items:
19    --block-device-mappings   dump the block device mappings, space delimited
20    --public-keys             dump the public keys
21
22   with no options given, metadata service will be crawled and
23   dumped.  This will not include user-data.
24 EOF
25 }
26
27 error() { echo "$@" 1>&2; }
28 fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
29
30 get_field_list() {
31         local under="$1" x="" out="" found=""
32         mdget "$under"
33         out="$_RET"
34         for x in $out; do
35                 case "$x" in
36                         */) get_field_list "${under:+${under}/}${x%/}"
37                                 found="${found} $_RET";;
38                         *) found="${found} ${under:+$under/}$x";;
39                 esac
40         done
41         _RET="${found# }"
42 }
43
44 caturl() {
45         curl --fail --silent --max-time "${TIMEOUT}" "$1"
46 }
47
48 mdget() {
49         local u="$1" t1="" t2="" out=""
50         case "$u" in
51                 user-data) :;;
52                 public-keys|public-keys/)
53                         out=$(caturl "$MD_URL/meta-data/$u") ||
54                                 fail "failed to get $MD_URL/meta-data/$u";
55                         # output of public-keys is lines of 'X=keyname'
56                         # so we return a carriage return list of the tokens before =
57                         _RET=""
58                         IFS="$CR"
59                         for t1 in $out; do
60                                 t1="${t1%%=*}"
61                                 _RET="${_RET:+${_RET}${CR}}${t1%/}"
62                         done
63                         IFS="$OIFS"
64                         return
65                         ;;
66                 public-keys/*)
67                         t1=${u#public-keys/[0-9]} 
68                         # t2 will now have 'public-keys/[0-9]'
69                         t2=${u%${t1}}
70                         # if t1 has a / (ie, u=public-keys/0=brickies/openssh-keys)
71                         # then set t1 to just "/openssh-keys". if not, set it to ""
72                         [ "${t1#*/}" = "${t1}" ] && t1="" || t1="/${t1#*/}"
73                         u="meta-data/${t2}${t1}"
74                         ;;
75                 *) u="meta-data/$u";;
76         esac
77         _RET=$(caturl "$MD_URL/$u") ||
78                 fail "failed to get ${MD_URL}/$u"
79 }
80 special() {
81         local out="" x="" t="" ret=""
82         case "$1" in
83                 availability-zone)
84                         mdget "placement/availability-zone";;
85                 public-keys)
86                         mdget public-keys
87                         out="$_RET"
88                         IFS="$CR"
89                         for x in ${out}; do
90                                 IFS="$OIFS" mdget \
91                                         "public-keys/${x}/openssh-key" &&
92                                 ret="${ret:+${ret}${CR}}${_RET}"
93                         done
94                         IFS="$OIFS"
95                         _RET="$ret"
96                         ;;
97                 block-device-mappings)
98                         mdget block-device-mapping
99                         out="$_RET"
100                         for x in ${out}; do
101                                 mdget "block-device-mapping/$x"
102                                 ret="${ret:+${ret} }${x}=$_RET";
103                         done
104                         _RET="$ret"
105         esac
106 }
107
108 fields=""
109 while [ $# -ne 0 ]; do
110         cur=${1}; next=${2};
111         case "$cur" in
112                 -h|--help) Usage ; exit 0;;
113                 --url=*) MD_URL=${cur#*=}; MD_URL=${MD_URL%/};;
114                 --url|-u) MD_URL="${next%/}"; shift;;
115                 --availability-zone|--public-keys|--block-device-mappings)
116                         fields="$fields special_${cur#--}";;
117                 --*) fields="$fields ${cur#--}";;
118         esac
119         shift;
120 done
121
122 fields="${fields# }"
123 if [ -z "$fields" ]; then
124         get_field_list ""
125         fields="${_RET}"
126         prefix=true
127 else
128         prefix=false
129 fi
130
131 for f in $fields; do
132         case "$f" in
133                 special_*) special "${f#special_}";;
134                 *) mdget "$f";;
135         esac
136         ret=$?
137         [ $ret -eq 0 ] || fail "failed to get $f"
138         if $prefix; then
139                 if [ "${_RET#*${CR}}" != "${_RET}" ]; then
140                         _RET=$(echo "$_RET" | sed 's,^,|,g')
141                         _RET="${_RET#|}"
142                 fi
143                 echo "${f#special_}: $_RET"
144         else
145                 echo "$_RET"
146         fi
147 done
148
149 exit 0
150
151 # vi: ts=4 noexpandtab