-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbeanstalkUpload.groovy
133 lines (105 loc) · 5.25 KB
/
beanstalkUpload.groovy
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
@GrabResolver(name='central', root='http://repo1.maven.org/maven2')
@GrabResolver(name='javanet', root='http://download.java.net/maven/2')
@Grab(group='com.amazonaws', module='aws-java-sdk', version='1.1.6')
@Grab(group='commons-logging', module='commons-logging', version='1.1.1')
@Grab(group='commons-codec', module='commons-codec', version='1.4')
@Grab(group='commons-httpclient', module='commons-httpclient', version='3.1')
@Grab(group='javax.mail', module='mail', version='1.4.3')
@Grab(group='org.codehaus.jackson', module='jackson-core-asl', version='1.4.3')
@Grab(group='stax', module='stax', version='1.2.0')
@Grab(group='stax', module='stax-api', version='1.0.1')
import org.apache.commons.logging.LogFactory
import com.amazonaws.auth.AWSCredentials
import com.amazonaws.auth.PropertiesCredentials
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.Bucket
import com.amazonaws.services.s3.model.StorageClass
import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.model.PutObjectResult
import com.amazonaws.services.s3.model.PutObjectRequest
import com.amazonaws.services.s3.model.ProgressEvent
import com.amazonaws.services.s3.model.ProgressListener
import com.amazonaws.services.s3.transfer.Upload
import com.amazonaws.services.s3.transfer.TransferManager
import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalk
import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalkClient
import com.amazonaws.services.elasticbeanstalk.model.S3Location
import com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionRequest
import com.amazonaws.services.elasticbeanstalk.model.ApplicationVersionDescription
void upload(appName, appVersion, fileToUpload) {
def fileSize = fileToUpload.size()
def config = new Properties()
def credentialsFile = new File("credentials.properties")
if (!credentialsFile.exists()) {
log(appName, "File '${credentialsFile}' does not exist, you have to have one in the same")
log(appName, "directory from where you're executing this script. It should have two keys")
log(appName, "with names 'accessKey' and 'secretKey' with respective content.")
System.exit(1)
} else {
log(appName, "Loading 'credentials.propeties' file")
}
def credentials = new PropertiesCredentials(credentialsFile)
log(appName, "Loaded AWS credentials")
def s3client = new AmazonS3Client(credentials)
def bucketName = "${appName}-${UUID.randomUUID()}"
log(appName, "Creating s3 bucket '${bucketName}' to hold application file")
def bucket = s3client.createBucket(bucketName)
def objectKey = "${new Date().format('yyyyMMddHHmmss')}-${fileToUpload.name}"
def metadata = new ObjectMetadata()
metadata.setContentLength(fileSize)
metadata.addUserMetadata("app-name", appName)
metadata.addUserMetadata("app-version", appVersion)
def transferManager = new TransferManager(credentials)
fileToUpload.withInputStream { fileToUploadInputStream ->
def decimalFormat = new java.text.DecimalFormat("##.#")
def warUpload = transferManager.upload(bucketName, objectKey, fileToUploadInputStream, metadata)
while (!warUpload.done) {
def percent = (warUpload.progress.bytesTransfered * 100) / warUpload.progress.totalBytesToTransfer
def percentToShow = decimalFormat.format(percent)
log(true, appName, "${warUpload.description}: [${warUpload.state}] - ${warUpload.progress.bytesTransfered} of ${warUpload.progress.totalBytesToTransfer} (${percentToShow}%) ")
Thread.sleep(1000)
}
}
def beanstalk = new AWSElasticBeanstalkClient(credentials)
def applicationVersionRequest = new CreateApplicationVersionRequest(appName, appVersion)
applicationVersionRequest.setAutoCreateApplication(true)
applicationVersionRequest.setSourceBundle(new S3Location(bucketName, objectKey))
println ""
log(appName, "Creating application version...")
def applicationVersionResult = beanstalk.createApplicationVersion(applicationVersionRequest)
def applicationVersionDescription = applicationVersionResult.applicationVersion
log(appName, "Done!")
log(appName, "App: ${applicationVersionDescription.applicationName}")
log(appName, "Version: ${applicationVersionDescription.versionLabel}")
log(appName, "S3 Bucket: ${bucketName}")
log(appName, "War file: ${objectKey}")
log(appName, "Version created at: ${applicationVersionDescription.dateCreated.format('yyyy/MM/dd HH:mm:ss')}")
transferManager.shutdownNow()
}
void log(message) {
println "[${new Date().format('yyyy/MM/dd HH:mm:ss')}] ${message}"
}
void log(returnLine = false, appName, message) {
def msg = "[${new Date().format('yyyy/MM/dd HH:mm:ss')}] [${appName}] ${message}"
if (returnLine)
print "\r${msg}"
else
println msg
}
//disable output messages for aws sdk
def logAttribute = "org.apache.commons.logging.Log"
def logValue = "org.apache.commons.logging.impl.NoOpLog"
LogFactory.getFactory().setAttribute(logAttribute, logValue)
//script
if (args.size() != 3) {
log("Usage: groovy beanstalkUpload.groovy <path_to_war> <application_name> <application_version_label>")
System.exit(1)
}
def fileToUpload = new File(args[0])
def appName = args[1].toLowerCase()
def appVersion = args[2]
if (!fileToUpload.exists()) {
log("File '${fileToUpload}' does not exist")
System.exit(1)
}
upload(appName, appVersion, fileToUpload)