Friday, April 29, 2011

script to spin up an amazon ec2 instance and deploy a tomcat war

It requires ec2-api-tools ssh scp curl and optionally xml2 to be installed somewhere on your machine.

This one is a real gem:


    1 export EC2_HOME=/location/of/ec2-api-tools-1.3-42584
2 export PATH=$PATH:$EC2_HOME/bin
3 export EC2_PRIVATE_KEY=~/.ec2/secretkeyhere.pem
4 export EC2_CERT=~/.ec2/certhere.pem
5 export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
6
7 export curlflags="-L -v -b cookiejar.txt -c cookiejar.txt"
8 export appname=war_you_want_deployed_without_extension
9
10 my_instance=`ec2-run-instances ami-1515f67c -k gsg-keypair | grep INSTANCE | awk '{print $2}'`
11 echo $my_instance
12
13 remotehost=`ec2-describe-instances $my_instance | grep INSTANCE | awk '{print $4}'`
14 echo $remotehost
15
16 while [ "$remotehost" == "pending" ]
17 do
18 remotehost=`ec2-describe-instances $my_instance | grep INSTANCE | awk '{print $4}'`
19 echo $remotehost
20 done
21
22
23 ec2-authorize default -p 22
24 ec2-authorize default -p 80
25 ec2-authorize default -p 1194 -P udp
26 ec2-authorize default -p 1194 -P tcp
27 ec2-authorize default -p 8080
28 ssh -i ~/.ssh/id_rsa-gsg-keypair ubuntu@$remotehost -o StrictHostKeyChecking=no 'sudo apt-get update -y'
29 ssh -i ~/.ssh/id_rsa-gsg-keypair ubuntu@$remotehost 'sudo apt-get install openjdk-6-jdk -y'
30 ssh -i ~/.ssh/id_rsa-gsg-keypair ubuntu@$remotehost 'java -version'
31 ssh -i ~/.ssh/id_rsa-gsg-keypair ubuntu@$remotehost 'wget http://apache.mirror.nexicom.net/tomcat/tomcat-6/v6.0.32/bin/apache-tomcat-6.0.32.tar.gz'
32 ssh -i ~/.ssh/id_rsa-gsg-keypair ubuntu@$remotehost 'tar -zxvf apache-tomcat-6.0.32.tar.gz'
33 ssh -i ~/.ssh/id_rsa-gsg-keypair ubuntu@$remotehost 'apache-tomcat-6.0.32/bin/startup.sh'
34
35 echo "my_instance=$my_instance"
36 echo "remotehost=$remotehost"
37
38 echo "curl $curlflags http://$remotehost:8080 | html2"
39 curl $curlflags http://$remotehost:8080 | html2
40
41 scp -i ~/.ssh/id_rsa-gsg-keypair $appname.war ubuntu@$remotehost:apache-tomcat-6.0.32/webapps/
42
43 echo "curl $curlflags http://$remotehost:8080/$appname | html2"
44 curl $curlflags http://$remotehost:8080/$appname | html2
45
46
47 echo "my_instance=$my_instance"
48 echo "remotehost=$remotehost"
49 echo "server should be up at: http://$remotehost:8080/$appname"
50
51
52 #
53 echo 'dont forget to shut it down when youre done it costs money!'
54 echo "ec2-terminate-instances ${my_instance}"
55

No comments:

Post a Comment