~j-harbott/cirros/dev

« back to all changes in this revision

Viewing changes to src/etc/rc.d/init.d/cloud-setup

  • Committer: Scott Moser
  • Date: 2011-09-21 00:58:21 UTC
  • Revision ID: smoser@ubuntu.com-20110921005821-fxtb4o4xaqktm1dm
add 'src' from old ttylinux-uec (revno 55).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# vi: ts=4 noexpandtab
 
3
 
 
4
XN=cloud-setup
 
5
. /etc/rc.d/init.d/cloud-functions
 
6
 
 
7
start() {
 
8
        local i=0 max=${MD_MAX_TRIES} iid="" uptime="" toks="" n="" cmdline=""
 
9
        is_nocloud &&
 
10
                { msg "nocloud specified, not using metadata"; return 0; }
 
11
        read cmdline < /proc/cmdline
 
12
        [ -f "${STATE_D}/cloud_tries" ] && read max < "${STATE_D}/cloud_tries"
 
13
        if ! lxc-is-container; then
 
14
                for n in ${cmdline}; do
 
15
                        case "${n}" in
 
16
                                cloud_tries=*) max=${n#cloud_tries=}; break;;
 
17
                                ds=nocloud*)
 
18
                                        msg "kernel option nocloud specified. not using md"; return 0;;
 
19
                        esac
 
20
                done
 
21
        fi
 
22
        [ ${max} -lt 0 ] && max=$((60*60*24*3)) ; # 3 days, is plenty
 
23
        
 
24
        msg "checking ${MDURL}/instance-id"
 
25
        i=0
 
26
        while [ $i -lt ${max} ] && i=$(($i+1)); do
 
27
                read uptime cputime < /proc/uptime
 
28
                iid=""
 
29
                if mdget instance-id; then
 
30
                        iid=${_RET}
 
31
                        [ "${iid#i-}" != "${iid}" ] && break
 
32
                        msg "failed $i/${max}: up ${uptime}. iid had ${iid}"
 
33
                else
 
34
                        msg "failed $i/${max}: up ${uptime}. request failed"
 
35
                fi
 
36
                sleep 1
 
37
                if [ $i -eq "${MD_DEBUG_COUNT}" ]; then
 
38
                        msg "after ${MD_DEBUG_COUNT} fails, debugging"
 
39
                        mddebug
 
40
                fi
 
41
        done
 
42
 
 
43
        if [ -n "${iid}" ]; then
 
44
                msg "successful after ${i}/${max} tries: up ${uptime}. iid=${iid}"
 
45
        else
 
46
                msg "failed to read iid from metadata. tried ${max}"; return 1;
 
47
        fi
 
48
 
 
49
        marked ${iid} &&
 
50
                { msg "previously ran for ${iid}"; return 0; }
 
51
        mark ${iid}
 
52
 
 
53
        mkdir -p /root/.ssh
 
54
        chmod 700 /root/.ssh
 
55
 
 
56
        mdget public-keys/ "${TMPF}"
 
57
        echo >> "${TMPF}" # while loop will fail if no trailing newline
 
58
        keys=""
 
59
        while read line; do
 
60
                keys="${keys} ${line}"
 
61
        done < "${TMPF}"
 
62
        keys=${keys# }
 
63
        # keys is list of [0-9]=name
 
64
        if [ -n "${keys}" ]; then
 
65
                msg "cloudinit: getting ssh keys: [${keys}]"
 
66
                for toks in ${keys}; do
 
67
                        n=${toks%%=*}
 
68
                        mdget public-keys/${n}/openssh-key - >> /root/.ssh/authorized_keys
 
69
                done
 
70
        fi
 
71
        return 0
 
72
}
 
73
 
 
74
case "$1" in
 
75
        start) start;;
 
76
        *) msg "unknown argument ${1}";;
 
77
esac