-
Notifications
You must be signed in to change notification settings - Fork 174
/
cf-deploy.gradle
197 lines (178 loc) · 7.41 KB
/
cf-deploy.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
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
file("cf-deploy.gradle.properties").withInputStream { input ->
def p = new Properties()
p.load(input)
p.each { k, v -> project.ext.set(k, v) }
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.cloudfoundry', name: 'cf-gradle-plugin', version: '1.1.3'
}
}
apply plugin: 'cloudfoundry'
ext {
productionEnv = project.hasProperty('prod')
cfEnvLabel = productionEnv ? 'prod' : 'dev'
cfDefaultDomain = 'cfapps.io'
// used for setting Server.Service.Connector.proxyName system property
connectorProxyName = productionEnv ? 'grails.org' : 'staging.grails.org'
def cfConfigFile = file("cf-deployment-${productionEnv ? 'production' : 'development'}/cf_config.groovy")
if(cfConfigFile.exists()) {
cfConfig = new ConfigSlurper().parse(cfConfigFile.toURI().toURL())
} else {
cfConfig = new ConfigObject()
}
}
cloudfoundry {
stack = "cflinuxfs3"
application = "plugins-grails-org-${cfEnvLabel}"
target = 'https://api.run.pivotal.io'
organization = 'grails-org'
space = productionEnv ? 'production' : 'development'
file = file("target/grails-website.war")
buildpack = "https://github.com/cloudfoundry/java-buildpack.git"
env = [
"JAVA_OPTS": (
"-Xverify:none "+
"-XX:+UseCompressedClassPointers -XX:CompressedClassSpaceSize=256M "+
"-XX:+UseCompressedOops "+
"-XX:InitialCodeCacheSize=64M -XX:CodeCacheExpansionSize=1M -XX:CodeCacheMinimumFreeSpace=1M -XX:ReservedCodeCacheSize=200M "+
"-XX:MinMetaspaceExpansion=1M -XX:MaxMetaspaceExpansion=8M "+
"-XX:MaxDirectMemorySize=96M "+
"-Dfile.encoding=UTF-8 -Duser.language=en -Duser.country=US "+
"-Dsun.net.client.defaultConnectTimeout=10000 -Dsun.net.client.defaultReadTimeout=10000 "+
"-Djava.net.preferIPv4Stack=true "+
"-XX:+PrintGCDetails -XX:+PrintHeapAtGC -XX:+PrintGCDateStamps -verbose:gc -Xloggc:/home/vcap/logs/gc.log "+
"-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=10M "+
"-Dgrails.env=production -DServer.Service.Connector.proxyName=${connectorProxyName} "
),
"MALLOC_ARENA_MAX": "2",
"MALLOC_MMAP_THRESHOLD_": "131072",
"MALLOC_TRIM_THRESHOLD_": "131072",
"MALLOC_TOP_PAD_": "131072",
"MALLOC_MMAP_MAX_": "65536"
]
if(cfConfig.containsKey('env') && cfConfig.env instanceof Map) {
env.putAll(cfConfig.env)
}
mergeEnv = true
healthCheckTimeout = 180
appStartupTimeout = 5
memory = productionEnv ? 2048 : 1500
diskQuota = 2048
instances = 1
variants = productionEnv ? ['-blue', '-green'] : []
domain = 'grails.org'
host = productionEnv ? 'prod' : 'staging'
services {
"db-grails-org-${cfEnvLabel}" {
label = 'cleardb'
plan = productionEnv ? 'amp' : 'boost'
bind = true
}
"sendgrid-grails-org-${cfEnvLabel}" {
label = 'sendgrid'
plan = 'free'
bind = true
}
"newrelic-grails-org-${cfEnvLabel}" {
label = 'newrelic'
plan = 'standard'
bind = true
}
}
}
// current cfDeploy task doesn't allow 2 domains, this is a workaround for that limitation
class CustomBlueGreenDeploymentTask extends org.cloudfoundry.gradle.tasks.DeployCloudFoundryTask {
List<String> getAllUris() {
List<String> customAllUris = []
customAllUris.addAll(super.getAllUris())
if(project.cloudfoundry.domain && project.cloudfoundry.host && project.cloudfoundry.currentVariant) {
customAllUris << "${project.cloudfoundry.application}${project.cloudfoundry.currentVariant}.${project.ext.cfDefaultDomain}".toString()
}
customAllUris
}
}
// replace cfDeploy task with our custom one
tasks.replace("cfDeploy", CustomBlueGreenDeploymentTask)
import org.cloudfoundry.client.lib.domain.CloudApplication
@Mixin(org.cloudfoundry.gradle.tasks.DeployCloudFoundryHelper)
class AliasMappingsTask extends org.cloudfoundry.gradle.tasks.AbstractMapCloudFoundryTask {
List<String> aliasRoutes = []
boolean returnAliasRoutes = false
AliasMappingsTask() {
super()
description = 'Adds route aliases to active route'
}
List<String> getAllUris() {
returnAliasRoutes ? aliasRoutes : super.getAllUris()
}
@TaskAction
void updateAliasUris() {
if(!aliasRoutes) {
return
}
returnAliasRoutes = false
withCloudFoundryClient {
validateVariantsForDeploy()
List<CloudApplication> apps = client.applications
List<String> mappedAppVariants = findMappedVariants(application, apps)
List<String> unmappedAppVariants = findUnmappedVariants(application, apps)
returnAliasRoutes = true
if (mappedAppVariants) {
log "Mapping URIs ${allUris} for ${mappedAppVariants}"
}
if (unmappedAppVariants) {
log "Unmapping URIs ${allUris} for ${unmappedAppVariants}"
}
withApplication {
mappedAppVariants.each { appName ->
project.cloudfoundry.application = appName
mapUrisToApplication()
}
unmappedAppVariants.each { appName ->
project.cloudfoundry.application = appName
unmapUrisFromApplication()
}
}
}
}
}
tasks.create("cfUpdateAliases", AliasMappingsTask) {
def plainAppNameRoute = "${project.cloudfoundry.application}.${project.ext.cfDefaultDomain}".toString()
aliasRoutes = productionEnv ? ['www.grails.org', 'grails.org', 'plugins.grails.org', plainAppNameRoute] : ['www-dev.grails.org', 'dev.grails.org', plainAppNameRoute]
}
// Custom cfScale task that works with blue-green deployment
@Mixin(org.cloudfoundry.gradle.tasks.DeployCloudFoundryHelper)
class CustomScaleCloudFoundryTask extends org.cloudfoundry.gradle.tasks.AbstractMapCloudFoundryTask {
CustomScaleCloudFoundryTask() {
super()
description = 'Scales application instances up or down'
}
@TaskAction
void scale() {
withCloudFoundryClient {
validateVariantsForDeploy()
List<CloudApplication> apps = client.applications
List<String> mappedAppVariants = findMappedVariants(application, apps)
List<String> unmappedAppVariants = findUnmappedVariants(application, apps)
withApplication {
// reduce instances to default value for unmapped applications
unmappedAppVariants.each { appName ->
project.cloudfoundry.application = appName
log "Setting number of instances of application ${application} to ${instances}"
client.updateApplicationInstances(application, instances)
}
// increase instances for mapped applications
mappedAppVariants.each { appName ->
project.cloudfoundry.application = appName
log "Setting number of instances of application ${application} to ${instances + 1}"
client.updateApplicationInstances(application, instances + 1)
}
}
}
}
}
tasks.replace("cfScale", CustomScaleCloudFoundryTask)