0f9efe2ae850d938fb7f948286498b8c7810d36c
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / lib / cirros / ds / ec2
1 #!/bin/sh
2
3 VERBOSITY=1
4 TEMP_D=""
5 CONFIG=/etc/cirros-init/ds-ec2
6 MDURL="http://169.254.169.254/2009-04-04"
7 NAME="${0##*/}"
8
9 . ${CIRROS_SHLIB:=/lib/cirros/shlib} ||
10         { echo "failed to read ${CIRROS_SHLIB}" 1>&2; exit 1; }
11
12 error() { echo "$@" 1>&2; }
13 fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
14
15 Usage() {
16         cat <<EOF
17 Usage: ${0##*/} mode output_d
18
19    Datasource for EC2 metadata service
20    Requires network.
21 EOF
22 }
23
24 mdget() {
25         ec2metadata "--url=$MDURL" "$@"
26 }
27
28 cleanup() {
29         [ -z "${TMPF}" -o ! -f "$TMPF" ] || rm -f "${TMPF}"
30 }
31
32 search_local() {
33         local out_d="$1"
34         local i="" max="" iid="" uptime=""
35         [ -d "$out_d" ] || mkdir -p "$out_d" ||
36                 { error "failed to create output dir"; return 1; }
37         TMPF="${out_d}/tmpf"
38
39         trap cleanup EXIT
40
41         debug 1 "checking $MDURL/instance-id"
42         i=0
43         MAX_TRIES=${MAX_TRIES:-20}
44         SLEEP_TIME=${SLEEP_TIME:-2}
45
46         max=${MAX_TRIES}
47         while [ $i -lt ${max} ] && i=$(($i+1)); do
48                 read uptime idle < /proc/uptime
49                 if iid=$(mdget --instance-id 2>/dev/null); then
50                         [ "${iid#i-}" != "${iid}" ] && break
51                         debug 1 "failed $i/${max}: up ${uptime}. iid had '${iid}'"
52                 else
53                         debug 1 "failed $i/${max}: up ${uptime}. request failed"
54                 fi
55                 sleep $SLEEP_TIME
56         done
57
58         if [ -n "${iid}" ]; then
59                 debug 1 "successful after ${i}/${max} tries: up ${uptime}. iid=${iid}"
60         else
61                 error "failed to read iid from metadata. tried ${max}";
62                 return 0
63         fi
64
65         local keys="" key=""
66         keys="public-keys instance-id ami-launch-index instance-type local-ipv4
67                 public-ipv4 hostname local-hostname user-data
68                 block-device-mappings public-hostname availability-zone"
69         mkdir -p "${out_d}/data"
70         for key in $keys; do
71                 mdget "--${key}" > "${out_d}/data/${key}" ||
72                         debug 1 "warning: no ec2 metadata for $key"
73         done
74
75         local fix="" t="" f=""
76         for fix in ami-launch-index:launch-index; do
77                 f="${fix%:*}"
78                 t="${fix#*:}"
79                 [ -f "${out_d}/data/$f" -a ! -f "${out_d}/data/$t" ] || continue
80                 ln -sf "$f" "${out_d}/data/$t" ||
81                         fail "failed to link $f to ${out_d}/data/t"
82         done
83
84         echo 0 > "$out_d/result"
85         return
86 }
87
88 apply() {
89         # ec2 datasource does nothing for apply
90         local mode="$1" data_d="$2"
91         return 0
92 }
93
94 short_opts="hv"
95 long_opts="help,verbose"
96 getopt_out=$(getopt --name "${0##*/}" \
97         --options "${short_opts}" --long "${long_opts}" -- "$@") &&
98         eval set -- "${getopt_out}" ||
99         bad_Usage
100
101 output=""
102
103 while [ $# -ne 0 ]; do
104         cur=${1}; next=${2};
105         case "$cur" in
106                 -h|--help) Usage ; exit 0;;
107                 -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
108                 --) shift; break;;
109         esac
110         shift;
111 done
112
113 [ $# -eq 2 ] || bad_Usage "must provide mode and data dir"
114 mode="$1"
115 out_d="$2"
116
117 [ "$mode" = "net" -o "$mode" = "apply-net" ] || {
118         debug 2 "only supported in mode 'net' or 'apply-net'";
119         exit 0;
120 }
121
122 [ ! -e "$CONFIG" ] || . "$CONFIG" ||
123         fail "failed to read $CONFIG"
124
125 if [ "$mode" = "net" ]; then
126         search_local "$out_d"
127 elif [ "$mode" = "apply-net" ]; then
128         apply "$mode" "$out_d"
129 else
130         fail "error, unexpected input"
131 fi
132
133 exit
134 # vi: ts=4 noexpandtab