xentaur

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