#!/bin/sh ### BEGIN INIT INFO # Provides: ec2-init # Required-Start: $network $local_fs $ssh # 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