xentaur

annotate files/ec2-instances @ 67:6c145935ece5

Fixed path scripts and configuration templates moved to a single file.
author Igor Chubin <igor@chub.in>
date Mon Jan 11 13:01:35 2010 +0200 (2010-01-11)
parents aaf034af3a35
children f652fab38c7a
rev   line source
igor@66 1 #!/bin/sh
igor@66 2
igor@67 3 # should be run from ~/.xentaur/$NETWORK/
igor@67 4 # and variable NETWORK set
igor@67 5 set -e
igor@66 6 source ./start-instances-$NETWORK
igor@67 7 # variables is here
igor@66 8 # start_emulators is here
igor@66 9
igor@66 10 message()
igor@66 11 {
igor@66 12 printf "\033[1;33m[`date +"%T.%N"|cut -c1-12`] $*\033[0;39m\n"
igor@66 13 }
igor@66 14
igor@66 15 start_instances()
igor@66 16 {
igor@66 17 message "* Starting instances"
igor@66 18 > $NETWORK-instances
igor@66 19 for i in `seq 0 $((INSTANCES_NUMBER-1))`
igor@66 20 do
igor@66 21 > /tmp/$NETWORK-ec2-run-instances
igor@66 22 ec2-run-instances $INSTANCE_AMI -z $EC2_ZONE -k $SSH_KEYPAIR > /tmp/$NETWORK-ec2-run-instances
igor@66 23 cat /tmp/$NETWORK-ec2-run-instances | grep INSTANCE | awk '{print $2}' >> $NETWORK-instances
igor@66 24 message Instance `tail -1 $NETWORK-instances` started
igor@66 25 done
igor@66 26 message "* All $INSTANCES_NUMBER instances started"
igor@66 27 cat $NETWORK-instances
igor@66 28 }
igor@66 29
igor@66 30 wait_for_loading()
igor@66 31 {
igor@66 32 message "* Waiting for the instances finish loading"
igor@66 33 > $NETWORK-instances-ready
igor@66 34 exit=no
igor@66 35 while [ "$exit" != yes ]
igor@66 36 do
igor@66 37 ec2-describe-instances | grep INSTANCE | egrep "`cat $NETWORK-instances|tr '\n' '|'`"NNNN | grep -q pending || exit=yes
igor@66 38 ec2-describe-instances | grep INSTANCE | egrep "`cat $NETWORK-instances|tr '\n' '|'`"NNNN | grep -v pending \
igor@66 39 | egrep -v "`cat $NETWORK-instances-ready|tr '\n' '|'`"XXXX | awk '{print $2}'> /tmp/$NETWORK-instances-new
igor@66 40 if [ -s "/tmp/$NETWORK-instances-new" ]
igor@66 41 then
igor@66 42 cat /tmp/$NETWORK-instances-new >> $NETWORK-instances-ready
igor@66 43 echo -n " "`cat /tmp/$NETWORK-instances-new`
igor@66 44 rm /tmp/$NETWORK-instances-new
igor@66 45 else
igor@66 46 echo -n .
igor@66 47 fi
igor@66 48 sleep 5
igor@66 49 done
igor@66 50 echo
igor@66 51 message "* Loading finished"
igor@66 52 }
igor@66 53
igor@66 54 update_dns()
igor@66 55 {
igor@66 56 message "* Updating DNS records"
igor@66 57 i=0
igor@66 58 cat $NETWORK-instances | while read instance
igor@66 59 do
igor@66 60 echo $NETWORK-node$i A $(host $(ec2-describe-instances $instance | grep INS | awk '{print $4}') | awk '{print $3}')
igor@66 61 i=$((i+1))
igor@66 62 done > /tmp/zone-$NETWORK
igor@66 63 message "New records:"
igor@66 64 cat /tmp/zone-$NETWORK
igor@66 65 cat /tmp/zone-$NETWORK | awk '{print $1}' > $NETWORK-hostnames
igor@66 66
igor@66 67 #FIXME: The file should be not overwritten, but merged!
igor@66 68 sudo mv /tmp/zone-$NETWORK /etc/bind/ec2-include
igor@66 69 sudo rndc reload
igor@66 70 message "* Updating DNS records finished"
igor@66 71 }
igor@66 72
igor@66 73 clear_old_ssh_keys()
igor@66 74 {
igor@66 75 #FIXME!
igor@66 76 # rm ~/.ssh/known_hosts
igor@66 77 for i in `seq 0 $((INSTANCES_NUMBER-1))`
igor@66 78 do
igor@66 79 ssh-keygen -R $NETWORK-node$i.$DOMAIN
igor@66 80 ssh-keygen -R $NETWORK-node$i
igor@66 81 done
igor@66 82 }
igor@66 83
igor@66 84 ssh_keys()
igor@66 85 {
igor@66 86 message "* Doing SSH keyscan"
igor@66 87 for i in `seq 0 $((INSTANCES_NUMBER-1))`
igor@66 88 do
igor@66 89 ssh-keyscan $NETWORK-node$i.$DOMAIN
igor@66 90 ssh-keyscan $NETWORK-node$i
igor@66 91 done > ssh-keys-$NETWORK
igor@66 92 clear_old_ssh_keys
igor@66 93 cat ssh-keys-$NETWORK >> ~/.ssh/known_hosts
igor@66 94 grep ^# ssh-keys-$NETWORK || true
igor@66 95 message "* SSH keyscan finished"
igor@66 96 }
igor@66 97
igor@66 98 attach_block_device()
igor@66 99 {
igor@66 100 ec2-attach-volume -d /dev/sdb -i `head -1 $NETWORK-instances` $VOLUME_NAME
igor@66 101 }
igor@66 102
igor@66 103 configure_instances()
igor@66 104 {
igor@66 105 #FIXME: Only for managed hosts! Not for all!
igor@66 106 cat <<EOF > ~/.ssh/config
igor@66 107 Host *.$DOMAIN
igor@66 108 User root
igor@66 109 IdentityFile $SSH_SECRET_KEY
igor@66 110 EOF
igor@66 111 > $NETWORK-preparation.log
igor@66 112 message "* Preparing network. Detailed log messages are in $NETWORK-preparation.log"
igor@66 113 for i in `seq 0 $((INSTANCES_NUMBER-1))`
igor@66 114 do
igor@66 115 host=$NETWORK-node$i.$DOMAIN
igor@66 116 message "* Preparing $host"
igor@66 117 message "** Copying configuration files"
igor@66 118 ssh $host 'mkdir /etc/xgurulla' >> $NETWORK-preparation.log 2>&1
igor@66 119 scp ssh-keys-$NETWORK $host:~/.ssh/known_hosts >> $NETWORK-preparation.log 2>&1
igor@66 120 scp $SSH_SECRET_KEY $host:~/.ssh/id_dsa >> $NETWORK-preparation.log 2>&1
igor@66 121 cat <<EOF | ssh $host 'cat > /etc/xgurulla/config'
igor@66 122 NETWORK=$NETWORK
igor@66 123 N=$i
igor@66 124 SERVER=$NETWORK-node0.$DOMAIN
igor@66 125 HOSTNAME=$host
igor@66 126 EOF
igor@66 127 scp $SCRIPTS_PATH/prepare-ec2-instance $host:/etc/xgurulla/ >> $NETWORK-preparation.log 2>&1
igor@66 128 message "** Installing software"
igor@66 129 ssh $host sh /etc/xgurulla/prepare-ec2-instance >> $NETWORK-preparation.log 2>&1
igor@66 130 done
igor@66 131 }
igor@66 132
igor@66 133 start_all() {
igor@66 134 start_instances
igor@66 135 wait_for_loading
igor@66 136 update_dns
igor@66 137 ssh_keys
igor@66 138 attach_block_device
igor@66 139 configure_instances
igor@66 140 message "* Starting emulators"
igor@66 141 start_emulators
igor@66 142 message "* Networking building completed"
igor@66 143 }
igor@66 144
igor@66 145 stop_instances()
igor@66 146 {
igor@66 147 [ -s "$NETWORK-instances" ] && ec2-terminate-instances `cat $NETWORK-instances`
igor@66 148 ec2-describe-instances
igor@66 149 }
igor@66 150
igor@66 151
igor@66 152 stop_all()
igor@66 153 {
igor@66 154 stop_instances
igor@66 155 }
igor@66 156
igor@66 157 if [ "$1" = start ]
igor@66 158 then
igor@66 159 start_all
igor@66 160 elif [ "$1" = stop ]
igor@66 161 then
igor@66 162 stop_all
igor@66 163 else
igor@66 164 cat <<USAGE
igor@66 165 Usage:
igor@66 166
igor@66 167 $0 start | stop
igor@66 168
igor@66 169 USAGE
igor@66 170 fi