xentaur

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/files/ec2-instances	Sat Jan 09 20:20:08 2010 +0200
     1.3 @@ -0,0 +1,183 @@
     1.4 +#!/bin/sh
     1.5 +
     1.6 +DOMAIN=ec2.xgu.ru
     1.7 +NETWORK=net1
     1.8 +INSTANCES_NUMBER=2
     1.9 +INSTANCE_AMI=ami-b21ff8db
    1.10 +INSTANCE_AMI=ami-7cfd1a15
    1.11 +SSH_SECRET_KEY=~/.ec2/id_rsa-pstam-keypair
    1.12 +SSH_KEYPAIR=pstam-keypair
    1.13 +VOLUME_NAME=vol-28d13141
    1.14 +EC2_ZONE=us-east-1a
    1.15 +DOMAIN=ec2.xgu.ru
    1.16 +SCRIPTS_PATH=~/hg/xentaur/files
    1.17 +
    1.18 +XGURULLA_DIR=~/.xgurulla/
    1.19 +WORK_DIR=${XGURULLA_DIR}/${NETWORK}
    1.20 +mkdir -p ${WORK_DIR}
    1.21 +
    1.22 +set -e -x
    1.23 +source ./start-instances-$NETWORK
    1.24 +# start_emulators is here
    1.25 +
    1.26 +message()
    1.27 +{
    1.28 +    printf "\033[1;33m[`date +"%T.%N"|cut -c1-12`] $*\033[0;39m\n"
    1.29 +}
    1.30 +
    1.31 +start_instances()
    1.32 +{
    1.33 +    message "* Starting instances"
    1.34 +    > $NETWORK-instances
    1.35 +    for i in `seq 0 $((INSTANCES_NUMBER-1))`
    1.36 +    do
    1.37 +          > /tmp/$NETWORK-ec2-run-instances
    1.38 +          ec2-run-instances $INSTANCE_AMI -z $EC2_ZONE -k $SSH_KEYPAIR > /tmp/$NETWORK-ec2-run-instances
    1.39 +          cat /tmp/$NETWORK-ec2-run-instances | grep INSTANCE | awk '{print $2}' >> $NETWORK-instances
    1.40 +          message Instance `tail -1 $NETWORK-instances` started
    1.41 +    done
    1.42 +    message "* All $INSTANCES_NUMBER instances started"
    1.43 +    cat $NETWORK-instances
    1.44 +}
    1.45 +
    1.46 +wait_for_loading()
    1.47 +{
    1.48 +    message "* Waiting for the instances finish loading"
    1.49 +    > $NETWORK-instances-ready
    1.50 +    exit=no
    1.51 +    while [ "$exit" != yes ] 
    1.52 +    do
    1.53 +           ec2-describe-instances | grep INSTANCE | egrep "`cat $NETWORK-instances|tr '\n' '|'`"NNNN | grep -q pending || exit=yes
    1.54 +           ec2-describe-instances | grep INSTANCE | egrep "`cat $NETWORK-instances|tr '\n' '|'`"NNNN | grep -v pending \
    1.55 +           | egrep -v "`cat $NETWORK-instances-ready|tr '\n' '|'`"XXXX | awk '{print $2}'> /tmp/$NETWORK-instances-new
    1.56 +	   if [ -s "/tmp/$NETWORK-instances-new" ]
    1.57 +           then 
    1.58 +              cat /tmp/$NETWORK-instances-new >> $NETWORK-instances-ready
    1.59 +              echo -n " "`cat /tmp/$NETWORK-instances-new`
    1.60 +              rm /tmp/$NETWORK-instances-new
    1.61 +           else 
    1.62 +              echo -n .
    1.63 +	   fi
    1.64 +           sleep 5
    1.65 +    done
    1.66 +    echo
    1.67 +    message "* Loading finished"
    1.68 +}
    1.69 +
    1.70 +update_dns()
    1.71 +{
    1.72 +    message "* Updating DNS records"
    1.73 +    i=0
    1.74 +    cat $NETWORK-instances | while read instance
    1.75 +    do
    1.76 +        echo $NETWORK-node$i A $(host $(ec2-describe-instances $instance | grep INS | awk '{print $4}') | awk '{print $3}')
    1.77 +        i=$((i+1))
    1.78 +    done > /tmp/zone-$NETWORK
    1.79 +    message "New records:"
    1.80 +    cat /tmp/zone-$NETWORK
    1.81 +    cat /tmp/zone-$NETWORK | awk '{print $1}' > $NETWORK-hostnames
    1.82 +
    1.83 +#FIXME: The file should be not overwritten, but merged!
    1.84 +    sudo mv /tmp/zone-$NETWORK /etc/bind/ec2-include 
    1.85 +    sudo rndc reload
    1.86 +    message "* Updating DNS records finished"
    1.87 +}
    1.88 +
    1.89 +clear_old_ssh_keys()
    1.90 +{
    1.91 +#FIXME!
    1.92 +#    rm ~/.ssh/known_hosts
    1.93 +    for i in `seq 0 $((INSTANCES_NUMBER-1))`
    1.94 +    do
    1.95 +        ssh-keygen -R $NETWORK-node$i.$DOMAIN
    1.96 +        ssh-keygen -R $NETWORK-node$i
    1.97 +    done
    1.98 +}
    1.99 +
   1.100 +ssh_keys()
   1.101 +{
   1.102 +    message "* Doing SSH keyscan"
   1.103 +    for i in `seq 0 $((INSTANCES_NUMBER-1))`
   1.104 +    do
   1.105 +        ssh-keyscan $NETWORK-node$i.$DOMAIN
   1.106 +        ssh-keyscan $NETWORK-node$i
   1.107 +    done > ssh-keys-$NETWORK
   1.108 +    clear_old_ssh_keys
   1.109 +    cat ssh-keys-$NETWORK >> ~/.ssh/known_hosts
   1.110 +    grep ^# ssh-keys-$NETWORK || true
   1.111 +    message "* SSH keyscan finished"
   1.112 +}
   1.113 +
   1.114 +attach_block_device()
   1.115 +{
   1.116 +    ec2-attach-volume -d /dev/sdb -i `head -1 $NETWORK-instances` $VOLUME_NAME 
   1.117 +}
   1.118 +
   1.119 +configure_instances()
   1.120 +{
   1.121 +#FIXME: Only for managed hosts! Not for all!
   1.122 +    cat <<EOF > ~/.ssh/config
   1.123 +Host *.$DOMAIN
   1.124 +	User root
   1.125 +	IdentityFile $SSH_SECRET_KEY
   1.126 +EOF
   1.127 +    > $NETWORK-preparation.log
   1.128 +    message "* Preparing network. Detailed log messages are in $NETWORK-preparation.log"
   1.129 +    for i in `seq 0 $((INSTANCES_NUMBER-1))`
   1.130 +    do
   1.131 +        host=$NETWORK-node$i.$DOMAIN
   1.132 +	message "* Preparing $host"
   1.133 +	message "** Copying configuration files"
   1.134 +        ssh $host 'mkdir /etc/xgurulla' >> $NETWORK-preparation.log 2>&1
   1.135 +        scp ssh-keys-$NETWORK $host:~/.ssh/known_hosts >> $NETWORK-preparation.log 2>&1
   1.136 +        scp $SSH_SECRET_KEY $host:~/.ssh/id_dsa >> $NETWORK-preparation.log 2>&1
   1.137 +        cat <<EOF | ssh $host 'cat > /etc/xgurulla/config'
   1.138 +NETWORK=$NETWORK
   1.139 +N=$i
   1.140 +SERVER=$NETWORK-node0.$DOMAIN
   1.141 +HOSTNAME=$host
   1.142 +EOF
   1.143 +        scp $SCRIPTS_PATH/prepare-ec2-instance $host:/etc/xgurulla/ >> $NETWORK-preparation.log 2>&1
   1.144 +	message "** Installing software"
   1.145 +        ssh $host sh /etc/xgurulla/prepare-ec2-instance >> $NETWORK-preparation.log 2>&1
   1.146 +    done
   1.147 +}
   1.148 +
   1.149 +start_all() {
   1.150 +start_instances
   1.151 +wait_for_loading
   1.152 +update_dns
   1.153 +ssh_keys
   1.154 +attach_block_device
   1.155 +configure_instances
   1.156 +message "* Starting emulators"
   1.157 +start_emulators
   1.158 +message "* Networking building completed"
   1.159 +}
   1.160 +
   1.161 +stop_instances()
   1.162 +{
   1.163 +[ -s "$NETWORK-instances" ] && ec2-terminate-instances `cat $NETWORK-instances`
   1.164 +ec2-describe-instances
   1.165 +}
   1.166 +
   1.167 +
   1.168 +stop_all()
   1.169 +{
   1.170 +    stop_instances
   1.171 +}
   1.172 +
   1.173 +if [ "$1" = start ]
   1.174 +then
   1.175 +    start_all
   1.176 +elif [ "$1" = stop ]
   1.177 +then
   1.178 +    stop_all
   1.179 +else
   1.180 +    cat <<USAGE
   1.181 +Usage:
   1.182 +
   1.183 +    $0 start | stop
   1.184 +
   1.185 +USAGE
   1.186 +fi