xentaur

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