5 CONFIG=/etc/cirros-init/ds-ec2
6 MDURL="http://169.254.169.254/2009-04-04"
9 . ${CIRROS_SHLIB:=/lib/cirros/shlib} ||
10 { echo "failed to read ${CIRROS_SHLIB}" 1>&2; exit 1; }
12 error() { echo "$@" 1>&2; }
13 fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
17 Usage: ${0##*/} mode output_d
19 Datasource for EC2 metadata service
25 ec2metadata "--url=$MDURL" "$@"
29 [ -z "${TMPF}" -o ! -f "$TMPF" ] || rm -f "${TMPF}"
34 local i="" max="" iid="" uptime=""
35 [ -d "$out_d" ] || mkdir -p "$out_d" ||
36 { error "failed to create output dir"; return 1; }
41 debug 1 "checking $MDURL/instance-id"
43 MAX_TRIES=${MAX_TRIES:-20}
44 SLEEP_TIME=${SLEEP_TIME:-2}
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}'"
53 debug 1 "failed $i/${max}: up ${uptime}. request failed"
58 if [ -n "${iid}" ]; then
59 debug 1 "successful after ${i}/${max} tries: up ${uptime}. iid=${iid}"
61 error "failed to read iid from metadata. tried ${max}";
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"
71 mdget "--${key}" > "${out_d}/data/${key}" ||
72 debug 1 "warning: no ec2 metadata for $key"
75 local fix="" t="" f=""
76 for fix in ami-launch-index:launch-index; do
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"
84 echo 0 > "$out_d/result"
89 # ec2 datasource does nothing for apply
90 local mode="$1" data_d="$2"
95 long_opts="help,verbose"
96 getopt_out=$(getopt --name "${0##*/}" \
97 --options "${short_opts}" --long "${long_opts}" -- "$@") &&
98 eval set -- "${getopt_out}" ||
103 while [ $# -ne 0 ]; do
106 -h|--help) Usage ; exit 0;;
107 -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
113 [ $# -eq 2 ] || bad_Usage "must provide mode and data dir"
117 [ "$mode" = "net" -o "$mode" = "apply-net" ] || {
118 debug 2 "only supported in mode 'net' or 'apply-net'";
122 [ ! -e "$CONFIG" ] || . "$CONFIG" ||
123 fail "failed to read $CONFIG"
125 if [ "$mode" = "net" ]; then
126 search_local "$out_d"
127 elif [ "$mode" = "apply-net" ]; then
128 apply "$mode" "$out_d"
130 fail "error, unexpected input"
134 # vi: ts=4 noexpandtab