xentaur
view files/ec2-instances @ 68:f652fab38c7a
ec2 parameters small fixes
author | Igor Chubin <igor@chub.in> |
---|---|
date | Mon Jan 11 19:36:56 2010 +0200 (2010-01-11) |
parents | 6c145935ece5 |
children |
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 unlock_root_account()
104 {
105 for i in `seq 0 $((INSTANCES_NUMBER-1))`
106 do
107 host=$NETWORK-node$i.$DOMAIN
108 ssh -i $SSH_SECRET_KEY ubuntu@$host "sudo sh -c 'cat .ssh/authorized_keys > /root/.ssh/authorized_keys'"
109 done
110 }
112 configure_instances()
113 {
114 #FIXME: Only for managed hosts! Not for all!
115 cat <<EOF > ~/.ssh/config
116 Host *.$DOMAIN
117 User root
118 IdentityFile $SSH_SECRET_KEY
119 EOF
120 > $NETWORK-preparation.log
121 message "* Preparing network. Detailed log messages are in $NETWORK-preparation.log"
122 for i in `seq 0 $((INSTANCES_NUMBER-1))`
123 do
124 host=$NETWORK-node$i.$DOMAIN
125 message "* Preparing $host"
126 message "** Copying configuration files"
127 ssh $host 'mkdir /etc/xgurulla' >> $NETWORK-preparation.log 2>&1
128 scp ssh-keys-$NETWORK $host:~/.ssh/known_hosts >> $NETWORK-preparation.log 2>&1
129 scp $SSH_SECRET_KEY $host:~/.ssh/id_dsa >> $NETWORK-preparation.log 2>&1
130 cat <<EOF | ssh $host 'cat > /etc/xgurulla/config'
131 NETWORK=$NETWORK
132 N=$i
133 SERVER=$NETWORK-node0.$DOMAIN
134 HOSTNAME=$host
135 EOF
136 scp $SCRIPTS_PATH/prepare-ec2-instance $host:/etc/xgurulla/ >> $NETWORK-preparation.log 2>&1
137 message "** Installing software"
138 ssh $host sh /etc/xgurulla/prepare-ec2-instance >> $NETWORK-preparation.log 2>&1
139 done
140 }
142 start_all() {
143 start_instances
144 wait_for_loading
145 update_dns
146 ssh_keys
147 attach_block_device
148 unlock_root_account
149 configure_instances
150 message "* Starting emulators"
151 start_emulators
152 message "* Networking building completed"
153 }
155 stop_instances()
156 {
157 [ -s "$NETWORK-instances" ] && ec2-terminate-instances `cat $NETWORK-instances`
158 ec2-describe-instances
159 }
162 stop_all()
163 {
164 stop_instances
165 }
167 if [ "$1" = start ]
168 then
169 start_all
170 elif [ "$1" = stop ]
171 then
172 stop_all
173 else
174 cat <<USAGE
175 Usage:
177 $0 start | stop
179 USAGE
180 fi