updated on Thu Jan 5 13:17:10 UTC 2012
[aur-mirror.git] / ec2arch / ec2
blob36b8c78e178e9550c260e5bc3f012533afbae6d7
1 #/bin/bash
3 URL="http://169.254.169.254/latest/"
4 NEWUSER="arch"
5 CURL="curl --retry 3  -f -s"
7 . /etc/rc.conf
8 . /etc/rc.d/functions
10 case "$1" in
11   start)
12     stat_busy "Starting ec2"
13     chmod 1777 /tmp
14     ## Set hostname and static ip
15     if ! grep -q ".internal" /etc/hosts ; then
16       cp /etc/hosts /etc/hosts.orig
17       HOSTNAME=$($CURL "${URL}meta-data/local-hostname")
18       IPV4=$($CURL "${URL}meta-data/local-ipv4")
19       if [[ -n "$HOSTNAME" && -n "$IPV4" && -n "${HOSTNAME%.*.internal}" ]]; then
20         echo "$IPV4 $HOSTNAME ${HOSTNAME%.*.internal}" >> /etc/hosts
21       fi
22       echo "domain $(hostname -d)" >> /etc/resolv.conf
23     fi
24     ## Set root key
25     if [ ! -f /root/.ssh/authorized_keys ] ; then
26        mkdir -p /root/.ssh
27        $CURL -o /root/.ssh/authorized_keys "${URL}meta-data/public-keys/0/openssh-key"
28     fi
29     ## Create arch user
30     if useradd -m -g adm -s /bin/zsh -p x -G wheel,log $NEWUSER 2>/dev/null; then
31        cp -r /root/. /home/$NEWUSER
32        chown -R ${NEWUSER}:adm /home/${NEWUSER}/.
33     fi
34     if [ -f /root/firstboot ]; then
35        if $CURL -o /root/user-data "${URL}user-data"; then
36          /bin/bash /root/user-data
37          rm -f /root/user-data
38        fi
39        rm -f /root/firstboot
40     fi
41     if [ $? -gt 0 ]; then
42       stat_fail
43     else
44       stat_done
45     fi
46     ;;
47   stop)
48     stat_busy "Stopping ec2"
49     sed -i "/domain/d" /etc/resolv.conf
50     sed -i "/$(hostname)/d" /etc/hosts
51     if [ $? -gt 0 ]; then
52       stat_fail
53     else
54       stat_done
55     fi
56     ;;
57   restart)
58     $0 stop
59     sleep 1
60     $0 start
61     ;;
62   *)
63     echo "usage: $0 {start|stop|restart}"
64 esac
65 exit 0