This repository has been archived by the owner on Jan 31, 2019. It is now read-only.
/
play-service.groovy
228 lines (187 loc) · 6.51 KB
/
play-service.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
* Copyright (c) 2012 GigaSpaces Technologies Ltd. All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
import java.util.concurrent.TimeUnit
service {
name "play"
icon "playIcon.png"
type "WEB_SERVER"
elastic true
numInstances 1
minAllowedInstances 1
maxAllowedInstances 2
compute {
template "SMALL_LINUX"
}
lifecycle {
details {
def currPublicIP
if ( context.isLocalCloud() ) {
currPublicIP = InetAddress.localHost.hostAddress
}
else {
currPublicIP =context.getPublicAddress()
}
def baseURL = "http://${currPublicIP}:${httpPort}"
def applicationURL = "${baseURL}/${applicationCtxPath}"
return [
"Application URL":"<a href=\"${applicationURL}\" target=\"_blank\">${applicationURL}</a>"
]
}
install "play_install.groovy"
preStart "play_preStart.groovy"
start "play_start.groovy"
startDetectionTimeoutSecs 400
startDetection {
!ServiceUtils.isPortFree(httpPort)
}
def instanceID = context.instanceId
postStart {
if ( useLoadBalancer ) {
println "play-service.groovy: play Post-start ..."
def apacheService = context.waitForService(loadBalancerServiceName, 180, TimeUnit.SECONDS)
println "play-service.groovy: invoking add-node of apacheLB ..."
def privateIP
if ( context.isLocalCloud() ) {
privateIP=InetAddress.getLocalHost().getHostAddress()
}
else {
privateIP =System.getenv()["CLOUDIFY_AGENT_ENV_PRIVATE_IP"]
}
println "play-service.groovy: privateIP is ${privateIP} ..."
def currURL="http://${privateIP}:${httpPort}"
println "play-service.groovy: About to add ${currURL} to apacheLB ..."
apacheService.invoke("addNode", currURL as String, instanceID as String)
println "play-service.groovy: play Post-start ended"
}
}
postStop {
if ( useLoadBalancer ) {
println "play-service.groovy: play Post-stop ..."
try {
def apacheService = context.waitForService(loadBalancerServiceName, 180, TimeUnit.SECONDS)
if ( apacheService != null ) {
def privateIP
if ( context.isLocalCloud() ) {
privateIP=InetAddress.localHost.hostAddress
}
else {
privateIP =System.getenv()["CLOUDIFY_AGENT_ENV_PRIVATE_IP"]
}
println "play-service.groovy: privateIP is ${privateIP} ..."
def currURL="http://${privateIP}:${httpPort}"
println "play-service.groovy: About to remove ${currURL} from apacheLB ..."
apacheService.invoke("removeNode", currURL as String, instanceID as String)
}
else {
println "play-service.groovy: waitForService returned ${loadBalancerServiceName} null"
}
}
catch (all) {
println "play-service.groovy: Exception in Post-stop: " + all
}
println "play-service.groovy: play Post-stop ended"
}
}
}
customCommands ([
/*
This custom command enables users to update their application
Usage:
invoke play updateApp http://www.mynewapplication.zip
*/
"updateApp" : {appZipUrl ->
println "play-service.groovy(updateApp custom command): AppZipUrl is ${appZipUrl}..."
installDir = System.properties["user.home"]+ "/.cloudify/${serviceName}" + context.instanceId
applicationZip = "${installDir}/${applicationZipName}"
def builder = new AntBuilder()
println "Getting updated application ${appZipUrl}..."
builder.get(src:"${appZipUrl}", dest:"${applicationZip}", skipexisting:false)
builder.unzip(src:"${applicationZip}", dest:"${installDir}", overwrite:true)
println "Copying ${applicationName} to " + context.serviceDirectory + "/${name}/playApps ..."
builder.move(file:"${installDir}/${applicationName}", tofile:context.serviceDirectory +"/${name}/playApps/${applicationName}", overwrite:true)
return true
},
/*
This custom command enables users to replace a string in a file (relative to play home folder
Usage : invoke play replace all|first origString newString relativePath
Examples:
1. The following replaces all the occurrences of DEBUG with ERROR
in PLAY_HOME_FOLDER/framework/src/play/src/main/resources/reference.conf :
invoke play replace all "DEBUG" "ERROR" /framework/src/play/src/main/resources/reference.conf
2. The following replaces the 1st occurrence of DEBUG with ERROR
in PLAY_HOME_FOLDER/framework/src/play/src/main/resources/reference.conf :
invoke play replace first "DEBUG" "ERROR" /framework/src/play/src/main/resources/reference.conf
*/
"replace" : "play_relpacer.groovy" ,
/* The following invokes a play command line and up to 3 arguments.
Here are some examples :
Usage invoke play cmd nameOfTheCommandLine arg1 arg2 ...
1. invoke play cmd compile
2. invoke play cmd clean
3. invoke play cmd clean-all
4. invoke play cmd test
5. invoke play cmd package
6. invoke play cmd update
*/
"cmd" : "play_cmd.groovy"
])
userInterface {
metricGroups = ([
metricGroup {
name "process"
metrics([
"Total Process Cpu Time"
])
}
])
widgetGroups = ([
widgetGroup {
name "Total Process Cpu Time"
widgets([
balanceGauge{metric = "Total Process Cpu Time"},
barLineChart {
metric "Total Process Cpu Time"
axisYUnit Unit.REGULAR
}
])
}
])
}
network {
port = httpPort
protocolDescription ="HTTP"
}
scaleCooldownInSeconds 20
samplingPeriodInSeconds 1
scalingRules ([
scalingRule {
serviceStatistics {
metric "Total Process Cpu Time"
timeStatistics Statistics.averageCpuPercentage
instancesStatistics Statistics.maximum
movingTimeRangeInSeconds 20
}
highThreshold {
value 40
instancesIncrease 1
}
lowThreshold {
value 25
instancesDecrease 1
}
}
])
}