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