summaryrefslogtreecommitdiff
path: root/ec2-init
blob: 85dd58edafa8db8d87ffc09e58ef1646022c97c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/sh
### BEGIN INIT INFO
# Provides:          ec2-init
# Required-Start:    $network $local_fs
# Required-Stop:
# Should-Start:      $named
# Should-Stop:
# Default-Start:     S
# Default-Stop:      1
# Short-Description: Initialises system for use on Amazon EC2
# Description:       Fetches login credentials and handles various quirks
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=ec2-init

. /lib/lsb/init-functions

if [ ! -d /var/ec2 ]; then
    mkdir /var/ec2
fi

run_once() {
    per_id=$1
    action_id=$2

    semaphore="/var/lib/ec2/$action_id.$per_id"

    if ! [ -e "$semaphore" ]
    then
        touch "$semaphore"
        return 0
    fi
    return 1
}

run_once_per_ami() {
    action_id=$1
    ami=`ec2-get-info --ami-id | cut -f2 -d\ `
    run_once $ami $action_id
}

run_once_ever() {
    action_id=$1
    run_once ever $action_id
}

regenerate_ssh_host_keys() {
    rm -f /etc/ssh/ssh_host_*_key*

    ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N '' | logger -s -t "ec2"
    ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N '' | logger -s -t "ec2"

    # This allows user to get host keys securely through console log
    echo | logger -s -t "ec2"
    echo | logger -s -t "ec2"
    echo "#############################################################" | logger -s -t "ec2"
    echo "-----BEGIN SSH HOST KEY FINGERPRINTS-----" | logger -s -t "ec2"
    ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key.pub | logger -s -t "ec2"
    ssh-keygen -l -f /etc/ssh/ssh_host_dsa_key.pub | logger -s -t "ec2"
    echo "-----END SSH HOST KEY FINGERPRINTS-----" | logger -s -t "ec2"
    echo "#############################################################" | logger -s -t "ec2"
}

case "$1" in
    start)
        log_daemon_msg "Waiting for EC2 meta-data service"
        if ec2-wait-for-meta-data-service
        then
            log_end_msg 0
        else
            log_end_msg 1
            exit 1
        fi

        if run_once_per_ami ssh_host_key_regeneration
        then
            regenerate_ssh_host_keys
        fi

        if run_once_ever ec2-defaults
        then
            log_daemon_msg "Setting EC2 defaults"
            if ec2-set-defaults 2> /dev/null
            then
                log_end_msg 0
            else
                log_end_msg 1
            fi
        fi

        if run_once_per_ami ssh_authorized_keys
        then
            log_daemon_msg "Fetching EC2 SSH keys"
            if ec2-fetch-credentials 2> /dev/null
            then
                log_end_msg 0
            else
                log_end_msg 1
            fi
        fi

        log_daemon_msg "Setting hostname to EC2 localhostname"
        if ec2-set-hostname 2> /dev/null
        then
            log_end_msg 0
        else
            log_end_msg 1
        fi

        if run_once_per_ami user-data
        then
            log_daemon_msg "Running EC2 user data"
            if ec2-run-user-data 2>&1 | logger -t "user-data"
            then
                log_end_msg 0
            else
                log_end_msg 1
            fi
        fi
        ;;
    stop)
        exit 0
        ;;
    restart|force-reload)
        exec $0 start
        ;;
    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0