forked from electric-cloud/DSL-Samples
-
Notifications
You must be signed in to change notification settings - Fork 6
/
basicDeployModel.groovy
executable file
·347 lines (301 loc) · 11.7 KB
/
basicDeployModel.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
Format: Electric Flow DSL
File: basicDeployModel.groovy
Description: Electric Flow Deploy model with configuration adaptation based on an environment property
- Creates environment model (with resources all pointing to localhost by default)
- Creates application model
- Creates and runs a procedure that creates template and "installer" files and publish them as an artifact version
Set up Instructions
-------------------
ectool --format json evalDsl --dslFile basicDeployModel.groovy
Running Instructions
--------------------
Once the procedure to create the artifact version has completed, deploy the application to one of its environments
Examine the job details to see the log outputs showing the adapted configuration file
*/
// Customizable values ------------------
// Application Name
def appName = "Application with config adaptation"
// Environment names ["env1", "env2" ...]
def envs = ["sample-dev","sample-qa"]
// Application-Environment tier mapping ["apptier1":"envtier1", "apptier2":"envtier2" ...]
def appEnvTiers = ["app":"app"]
// Artifact group id
def artifactRoot = "com.sample"
// Project name - currently only "Default" is supported by the Electric Flow Deploy UI
def projectName = "Default"
// Host(s) to use for the environment resources ["desired base resource name #1":"ip #1", "desired base resource name #2":"ip #2" ...]
def hosts = ["local":"localhost"]
// ---------------------------------------
def envTiers = appEnvTiers.values()
def appTiers = appEnvTiers.keySet()
// Remove old application model
deleteApplication (projectName: projectName, applicationName: appName)
// Remove old Environment models
envs.each { env ->
appTiers.each() { tier ->
hosts.each { name, ip ->
deleteResource resourceName: "${name}_${env}_${tier}"
}
}
deleteEnvironment(projectName: projectName, environmentName: env)
}
// Create new -------------------------------
def artifactVersions = []
project projectName, {
// Create Environments, Tiers and Resources
envs.each { env ->
environment environmentName: env, {
property "db_port", value: (String) Math.round(Math.random()*1000+1000)
envTiers.each() { tier ->
environmentTier tier, {
// create and add resource to the Tier
hosts.each { name, ip ->
resource resourceName: "${name}_${env}_${tier}", hostName : ip
}
}
}
}
} // Environments
application applicationName: appName, {
process processName: "Deploy"
appTiers.each() { tier ->
applicationTier tier, {
def compName = "${tier}_comp"
def artifactVersion = "1.35"
def artifactName_ = artifactRoot + ':' + compName
artifactVersions << [artifactName: artifactName_, artifactVersion: artifactVersion]
// Create artifact
artifact groupId: artifactRoot, artifactKey: compName
//def artifactName = "com.ec.test:test"
component componentName: compName, pluginKey: "EC-Artifact", {
ec_content_details.with {
pluginProjectName = "EC-Artifact"
pluginProcedure = "Retrieve"
artifactName = artifactName_
filterList = ""
overwrite = "update"
versionRange = ""
artifactVersionLocationProperty = "/myJob/retrievedArtifactVersions/\$" + "[assignedResourceName]"
}
process processName: "Install",
processType: "DEPLOY",
componentApplicationName: appName,
applicationName: null,
{
processStep processStepName: "Retrieve Artifact",
processStepType: "component",
subprocedure: "Retrieve",
errorHandling: "failProcedure",
subproject: "/plugins/EC-Artifact/project",
applicationName: null,
applicationTierName: null,
actualParameter: [
artifactName : "\$" + "[/myComponent/ec_content_details/artifactName]",
artifactVersionLocationProperty : "\$" + "[/myComponent/ec_content_details/artifactVersionLocationProperty]",
filterList : "\$" + "[/myComponent/ec_content_details/filterList]",
overwrite : "\$" + "[/myComponent/ec_content_details/overwrite]",
versionRange : "\$" + "[/myJob/ec_" + compName + "-version]"
]
processStep processStepName: "Deploy Artifact",
applicationName: null,
applicationTierName: null,
componentApplicationName: appName,
command: "echo testing $compName..."
processStep processStepName: "Deploy Artifact",
processStepType: 'command',
subproject: '/plugins/EC-Core/project',
subprocedure: 'RunCommand',
actualParameter: [
shellToUse: '$[/javascript getProperty("/resources/$[assignedResourceName]/hostPlatform")=="windows"?"":"sh"]',
commandToRun: '$[/javascript getProperty("/resources/$[assignedResourceName]/hostPlatform")=="windows"?"":"sh "]' +
'"' +
'$[/myJob/retrievedArtifactVersions/$[assignedResourceName]/$[/myRetrievedArtifact/artifactName]/cacheLocation]' +
'/app.' +
'$[/javascript getProperty("/resources/$[assignedResourceName]/hostPlatform")=="windows"?"bat":"sh"]' +
'"'
],
applicationName: null,
applicationTierName: null,
componentApplicationName: appName
createProcessDependency componentApplicationName: appName,
processStepName: "Retrieve Artifact",
targetProcessStepName: "Deploy Artifact"
processStep "Adapt configuration file", {
applicationTierName = null
dependencyJoinType = null
errorHandling = 'failProcedure'
processStepType = 'procedure'
projectName = 'Default'
subcomponent = null
subcomponentApplicationName = null
subcomponentProcess = null
subprocedure = 'AdaptFile'
subproject = "Deploy Utilities"
timeLimitUnits = null
workspaceName = null
actualParameter 'inputFile', '$[/myJob/retrievedArtifactVersions/$[assignedResourceName]/$[/myRetrievedArtifact/artifactName]/cacheLocation]/app.template'
actualParameter 'outputFile', 'app.cfg'
}
createProcessDependency componentApplicationName: appName,
processStepName: "Deploy Artifact",
targetProcessStepName: "Adapt configuration file"
processStep processStepName: "Show template content",
processStepType: 'command',
subproject: '/plugins/EC-Core/project',
subprocedure: 'RunCommand',
actualParameter: [
commandToRun: '$[/javascript getProperty("/resources/$[assignedResourceName]/hostPlatform")=="windows"?"type ":"cat "]' +
'app.cfg'
],
applicationName: null,
applicationTierName: null,
componentApplicationName: appName
createProcessDependency componentApplicationName: appName,
processStepName: "Adapt configuration file",
targetProcessStepName: "Show template content"
}
processStep processStepName: "Install $compName",
processName: "Deploy",
processStepType: 'process',
componentName: null,
applicationName: appName,
componentApplicationName: appName,
errorHandling: 'failProcedure',
subcomponent: compName,
subcomponentApplicationName: appName,
subcomponentProcess: "Install",
applicationTierName: tier
} // Components
}
} // Application Tiers
envs.each { env ->
tierMap tierMapName: "$appName-$env",
environmentProjectName: projectName, // Replace with projectName reference
environmentName: env,
tierMapping: appEnvTiers
}
} // Applications
} // project
// Create publishArtifact procedure
project "Deploy Utilities",{
procedure 'AdaptFile', {
description = 'Helper procedure to take an input file, run property path expansions on it, and save it to an output file.'
formalParameter 'inputFile', required: 'true'
formalParameter 'outputFile', required: 'true'
step 'Adapt File', {
command = '''\
use strict;
use ElectricCommander ();
$| = 1;
local $/;
my $ec = new ElectricCommander->new();
my $inputFile = $ec->getProperty("inputFile")->find("//value")->string_value;
unless(open INFILE, $inputFile) {
# Die with error message
# if we can't open it.
die "\\nUnable to open $inputFile\\n";
}
print "Input file: ", $inputFile, "\\n";
my $inContent = <INFILE>;
print "Input Content______________________\\n";
print $inContent;
print "\\n\\n";
close INFILE;
my $outputFile = $ec->getProperty("outputFile")->find("//value")->string_value;
$outputFile =~ s#\\\\#/#g;
unless(open OUTFILE, '>'.$outputFile) {
# Die with error message
# if we can't open it.
die "\\nUnable to open $outputFile\\n";
}
print "Output file: ",$outputFile,"\\n";
my $outContent = $ec->expandString($inContent)->find("//value")->string_value;
print "Output Content______________________\\n";
print $outContent;
print OUTFILE $outContent;
close OUTFILE;'''.stripIndent()
shell = 'ec-perl'
}
}
procedure "Create template file",{
formalParameter 'fileName', required: 'true'
formalParameter 'fileContent', expansionDeferred: 'true', type: 'textarea'
step 'Generate File', {
command = '''\
use strict;
use ElectricCommander ();
#use Data::Dumper;
$| = 1;
my $ec = new ElectricCommander->new();
my $fileName = $ec->getProperty("fileName")->find("//value")->string_value;
#$fileName =~ s#\\\\#/#g;
open OUTFILE, '>'.$fileName;
local $/;
my $fileContent = $ec->getProperty('fileContent',{expand=>'false'})->find("//value")->string_value;
print $fileContent;
print OUTFILE $fileContent;
close OUTFILE;
'''.stripIndent()
shell = 'ec-perl'
}
}
procedure "publishArtifact", {
formalParameter "artifactName", type: "textentry", required: "1"
formalParameter "artifactVersion", type: "textentry", required: "1"
formalParameter "fileName", type: "textentry", required: "1"
formalParameter "fileContent", type: "textarea", required: "1"
step "Create installer Windows",
subproject: "/plugins/EC-FileOps/project",
subprocedure: "AddTextToFile",
actualParameter: [
Path: '$' + "[fileName].bat",
Content: '$' + "[fileContent]",
AddNewLine: "0",
Append: "0"
]
step "Create installer Linux",
subproject: "/plugins/EC-FileOps/project",
subprocedure: "AddTextToFile",
actualParameter: [
Path: '$' + "[fileName].sh",
Content: '$' + "[fileContent]",
AddNewLine: "0",
Append: "0"
]
step "Create Config template",
subproject: '$[/myProject/projectName]',
subprocedure: "Create template file",
actualParameter: [
fileName: '$' + "[fileName].template",
//fileContent: '# App config file\ndb_port = $[/myEnvironment/db_port]'
fileContent: '''\
# App config file
# Environment: $[/myEnvironment/environmentName]
db_port = $[/myEnvironment/db_port]
'''.stripIndent()
]
step "Publish Artifact",
subproject: "/plugins/EC-Artifact/project",
subprocedure: "Publish",
actualParameter: [
artifactName: '$' + "[artifactName]",
artifactVersionVersion: '$' + "[artifactVersion]",
includePatterns: '$' + "[fileName].*",
repositoryName: "Default"
//fromLocation:
]
}
}
artifactVersions.each { ar ->
// Create artifact version
transaction {
runProcedure procedureName: "publishArtifact", projectName: "Deploy Utilities",
actualParameter: [
artifactName: ar.artifactName,
fileContent: "echo Installing " + ar.artifactName,
fileName: "app",
artifactVersion: ar.artifactVersion + '-$[/increment /myProject/artifactIndex]'
]
}
}