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