#!/bin/sh # should be run from ~/.xentaur/$NETWORK/ # and variable NETWORK set set -e source ./start-instances-$NETWORK # variables is here # start_emulators is here message() { printf "\033[1;33m[`date +"%T.%N"|cut -c1-12`] $*\033[0;39m\n" } start_instances() { message "* Starting instances" > $NETWORK-instances for i in `seq 0 $((INSTANCES_NUMBER-1))` do > /tmp/$NETWORK-ec2-run-instances ec2-run-instances $INSTANCE_AMI -z $EC2_ZONE -k $SSH_KEYPAIR > /tmp/$NETWORK-ec2-run-instances cat /tmp/$NETWORK-ec2-run-instances | grep INSTANCE | awk '{print $2}' >> $NETWORK-instances message Instance `tail -1 $NETWORK-instances` started done message "* All $INSTANCES_NUMBER instances started" cat $NETWORK-instances } wait_for_loading() { message "* Waiting for the instances finish loading" > $NETWORK-instances-ready exit=no while [ "$exit" != yes ] do ec2-describe-instances | grep INSTANCE | egrep "`cat $NETWORK-instances|tr '\n' '|'`"NNNN | grep -q pending || exit=yes ec2-describe-instances | grep INSTANCE | egrep "`cat $NETWORK-instances|tr '\n' '|'`"NNNN | grep -v pending \ | egrep -v "`cat $NETWORK-instances-ready|tr '\n' '|'`"XXXX | awk '{print $2}'> /tmp/$NETWORK-instances-new if [ -s "/tmp/$NETWORK-instances-new" ] then cat /tmp/$NETWORK-instances-new >> $NETWORK-instances-ready echo -n " "`cat /tmp/$NETWORK-instances-new` rm /tmp/$NETWORK-instances-new else echo -n . fi sleep 5 done echo message "* Loading finished" } update_dns() { message "* Updating DNS records" i=0 cat $NETWORK-instances | while read instance do echo $NETWORK-node$i A $(host $(ec2-describe-instances $instance | grep INS | awk '{print $4}') | awk '{print $3}') i=$((i+1)) done > /tmp/zone-$NETWORK message "New records:" cat /tmp/zone-$NETWORK cat /tmp/zone-$NETWORK | awk '{print $1}' > $NETWORK-hostnames #FIXME: The file should be not overwritten, but merged! sudo mv /tmp/zone-$NETWORK /etc/bind/ec2-include sudo rndc reload message "* Updating DNS records finished" } clear_old_ssh_keys() { #FIXME! # rm ~/.ssh/known_hosts for i in `seq 0 $((INSTANCES_NUMBER-1))` do ssh-keygen -R $NETWORK-node$i.$DOMAIN ssh-keygen -R $NETWORK-node$i done } ssh_keys() { message "* Doing SSH keyscan" for i in `seq 0 $((INSTANCES_NUMBER-1))` do ssh-keyscan $NETWORK-node$i.$DOMAIN ssh-keyscan $NETWORK-node$i done > ssh-keys-$NETWORK clear_old_ssh_keys cat ssh-keys-$NETWORK >> ~/.ssh/known_hosts grep ^# ssh-keys-$NETWORK || true message "* SSH keyscan finished" } attach_block_device() { ec2-attach-volume -d /dev/sdb -i `head -1 $NETWORK-instances` $VOLUME_NAME } unlock_root_account() { for i in `seq 0 $((INSTANCES_NUMBER-1))` do host=$NETWORK-node$i.$DOMAIN ssh -i $SSH_SECRET_KEY ubuntu@$host "sudo sh -c 'cat .ssh/authorized_keys > /root/.ssh/authorized_keys'" done } configure_instances() { #FIXME: Only for managed hosts! Not for all! cat < ~/.ssh/config Host *.$DOMAIN User root IdentityFile $SSH_SECRET_KEY EOF > $NETWORK-preparation.log message "* Preparing network. Detailed log messages are in $NETWORK-preparation.log" for i in `seq 0 $((INSTANCES_NUMBER-1))` do host=$NETWORK-node$i.$DOMAIN message "* Preparing $host" message "** Copying configuration files" ssh $host 'mkdir /etc/xgurulla' >> $NETWORK-preparation.log 2>&1 scp ssh-keys-$NETWORK $host:~/.ssh/known_hosts >> $NETWORK-preparation.log 2>&1 scp $SSH_SECRET_KEY $host:~/.ssh/id_dsa >> $NETWORK-preparation.log 2>&1 cat < /etc/xgurulla/config' NETWORK=$NETWORK N=$i SERVER=$NETWORK-node0.$DOMAIN HOSTNAME=$host EOF scp $SCRIPTS_PATH/prepare-ec2-instance $host:/etc/xgurulla/ >> $NETWORK-preparation.log 2>&1 message "** Installing software" ssh $host sh /etc/xgurulla/prepare-ec2-instance >> $NETWORK-preparation.log 2>&1 done } start_all() { start_instances wait_for_loading update_dns ssh_keys attach_block_device unlock_root_account configure_instances message "* Starting emulators" start_emulators message "* Networking building completed" } stop_instances() { [ -s "$NETWORK-instances" ] && ec2-terminate-instances `cat $NETWORK-instances` ec2-describe-instances } stop_all() { stop_instances } if [ "$1" = start ] then start_all elif [ "$1" = stop ] then stop_all else cat <