xentaur

annotate files/ec2-instances @ 66:aaf034af3a35

Merge of Xgurulla into Xentaur code. Not completed yet!!!

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