-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
executable file
·155 lines (131 loc) · 5.11 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.net.SocketTimeoutException
import java.lang.RuntimeException
import java.net.InetSocketAddress
import com.xerox.amazonws.ec2.*
import org.jets3t.service.model.S3Object
import org.jets3t.service.security.AWSCredentials
import org.jets3t.service.impl.rest.httpclient.RestS3Service
usePlugin('scala')
usePlugin('war')
usePlugin('jetty')
targetCompatibility = 1.5
version = 1
awsAccessKeyId=""
awsSecretAccessKey=""
ec2keyname = "" // Name of the EC2 key used to start instance
ec2KeyFile = "" // Path to EC2 key file
s3Bucket = "" // The S3 bucket to use for uploading cookbooks & war file
imageId = "ami-2c9f7845" // Rightscale Ubuntu 8.04
ec2 = new Jec2(awsAccessKeyId, awsSecretAccessKey);
dependencies {
addMavenRepo()
addMavenStyleRepo('scala-releases', 'http://scala-tools.org/repo-releases/')
scalaTools 'org.scala-lang:scala-compiler:2.7.3', 'org.scala-lang:scala-library:2.7.3'
compile 'org.scala-lang:scala-library:2.7.3', 'net.liftweb:lift-webkit:1.0', 'net.liftweb:lift-util:1.0'
testCompile 'junit:junit:4.4', 'org.scala-lang:scala-compiler:2.7.3','org.mortbay.jetty:jetty:6.1.6'
testCompile 'org.mortbay.jetty:jetty-util:6.1.6'
providedCompile 'javax.servlet:servlet-api:2.5'
}
createTask('createInstance', dependsOn: 'libs') {
def config = new LaunchConfiguration(imageId)
config.keyName = ec2keyname
def instances = ec2.runInstances(config)
def instance = instances.instances.get(0)
createInstance.instanceId = instance.instanceId
println "Started instance with id $createInstance.instanceId"
}
def sshExec(cmd) {
ant.sshexec(host: createInstance.dnsName, trust: true, keyfile: ec2KeyFile, username:"root", command:cmd)
}
def waitFor(name, timeOutSecs, Closure c) {
while (timeOutSecs > 0) {
def val = c()
if (val)
break;
Thread.sleep(1000)
timeOutSecs--
println "Waiting for $name, $timeOutSecs seconds left until timeout"
}
if (timeOutSecs == 0)
throw new RuntimeException("Timeout waiting for $name")
}
def bootstrap(cookbookURL, dnaURL) {
// Wait for SSH service to be ready on the instance
def id = createInstance.instanceId
def instance
waitFor("instance to boot", 120, {
instances = ec2.describeInstances([id])
instance = instances.get(0).instances.get(0)
return instance.running
})
def hostname = instance.dnsName
createInstance.dnsName = hostname
println "Instance boot complete, hostname is $hostname"
waitFor("SSH service", 180, {
Socket socket = null;
try {
socket = new Socket();
socket.bind(null);
socket.connect(new InetSocketAddress(hostname, 22), 1000)
return true
} catch (SocketTimeoutException e) {
return false;
} catch (java.net.ConnectException e) {
return false;
} finally {
socket.close()
}
})
sshExec("gem install --no-ri --no-rdoc ohai chef --source http://gems.opscode.com --source http://gems.rubyforge.org");
ant.scp(passphrase:"", todir: "root@$createInstance.dnsName:", trust: true, keyfile: ec2KeyFile) {
fileset(dir: "deployment") {
include(name: "solo.rb")
}
}
sshExec("chef-solo -c solo.rb -j '$dnaURL' -l info -r '$cookbookURL'");
}
def uploadCookbooksToS3() {
// Make & upload cookbooks
ant.tar(destfile:"$buildDir/cookbooks.tgz", compression:"gzip", basedir: "deployment/", includes:"cookbooks/**/*", excludes:"cookbooks/.git/**")
def credentials = new AWSCredentials(awsAccessKeyId, awsSecretAccessKey)
s3 = new RestS3Service(credentials)
bucket = s3.getBucket(s3Bucket)
s3obj = new S3Object(bucket, new File(buildDir, "cookbooks.tgz"))
s3.putObject(bucket,s3obj)
expiry = new GregorianCalendar()
expiry.add(Calendar.MINUTE,10)
cookbookURL = s3.createSignedGetUrl(s3obj.bucketName, s3obj.key, credentials, expiry.time)
s3obj = new S3Object(bucket,archive_war.getArchivePath())
// Need to make war file public readable. Workaround until http://tickets.opscode.com/browse/CHEF-269 is fixed
s3obj.acl = org.jets3t.service.acl.AccessControlList.REST_CANNED_PUBLIC_READ
s3.putObject(bucket,s3obj)
// warURL = s3.createSignedGetUrl(s3obj.bucketName, s3obj.key, credentials, expiry.time)
warURL = "http://" + s3.generateS3HostnameForBucket(s3obj.bucketName) + "/" + s3obj.key
// Make & upload the DNA file
dna = new File(buildDir, "dna.json")
dna.delete()
dna << '{"war": "' + warURL + '", "recipes": "helloworld"}'
s3obj = new S3Object(bucket,dna)
s3.putObject(bucket,s3obj)
dnaURL = s3.createSignedGetUrl(s3obj.bucketName, s3obj.key, credentials, expiry.time)
return [cookbookURL, dnaURL]
}
createTask('testDeploy', dependsOn:['libs','createInstance']) {
urls = uploadCookbooksToS3()
bootstrap(urls[0], urls[1])
}
// Very simple integration test :-)
createTask('integrationTest', dependsOn: 'testDeploy') {
try {
client = new org.apache.commons.httpclient.HttpClient()
method = new org.apache.commons.httpclient.methods.GetMethod("http://$createInstance.dnsName/")
client.executeMethod(method)
def s = method.responseBodyAsString
if (s.indexOf("Welcome to the final helloworld project!") < 0) {
println s
throw new RuntimeException("Test failed")
}
} finally {
ec2.terminateInstances([createInstance.instanceId])
}
}