-
Notifications
You must be signed in to change notification settings - Fork 119
/
Jenkinsfile-windows-integration
298 lines (262 loc) · 10.2 KB
/
Jenkinsfile-windows-integration
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
#!/usr/bin/env groovy
/**
* This Jenkinsfile runs a set of parallel builders for the dcos-cli across
* multiple platforms (linux/mac/windows).
*
* One set of builders builds the CLI into a binary on each platform. The other
* set of builders runs integration tests on each platform. Under the hood, the
* integration test builders use `dcos_launch` to create the DC/OS clusters for
* each platform to run their tests against. Unfortunately, `dcos_launch` only
* works reliably on Linux, so we use a single linux instance to create all of
* the clusters and separate linux/mac/windows instances to run the actual
* tests.
*/
/**
* These are the platforms we are building against.
*/
def platforms = ["windows"]
/**
* This generates the `dcos_launch` config for a particular build.
*/
def generateConfig(deploymentName, installerUrl, sshKeyPath) {
return """
---
launch_config_version: 1
deployment_name: ${deploymentName}
installer_url: ${installerUrl}
platform: aws
provider: onprem
aws_region: us-west-2
aws_key_name: default
ssh_private_key_filename: ${sshKeyPath}
os_name: cent-os-7
instance_type: m4.xlarge
num_masters: 1
num_private_agents: 1
num_public_agents: 1
dcos_config:
cluster_name: DC/OS CLI Integration Tests
resolvers:
- 10.10.0.2
dns_search: us-west-2.compute.internal
master_discovery: static
cosmos_config:
staged_package_storage_uri: file:///var/lib/dcos/cosmos/staged-packages
package_storage_uri: file:///var/lib/dcos/cosmos/packages
"""
}
/**
* This class abstracts away the functions required to create a test cluster
* for each of our platforms.
*/
class TestCluster implements Serializable {
WorkflowScript script
String platform
TestCluster(WorkflowScript script, String platform) {
this.script = script
this.platform = platform
}
/**
* Creates a new test cluster for the given platform using dcos-launch.
* Waits (blocks) for the cluster to be ready to use.
* Exits if interrupted by user.
* Retries 3 times.
* Destroys cluster on failure.
*/
def create() {
def createAttempts = 0
while (true) {
try {
createAttempts++
script.sh "rm -rf ${platform}_config.yaml"
script.sh "rm -rf ${platform}_cluster_info.json"
script.writeFile([
"file": "${platform}_config.yaml",
"text" : script.generateConfig(
"dcos-cli-${platform}-${script.env.ghprbPullId}-${script.env.BUILD_ID}-${createAttempts}",
"${script.env.DCOS_INSTALLER_URL}",
"${script.env.CLI_TEST_SSH_KEY_PATH}")])
script.sh "./dcos-launch create -c ${platform}_config.yaml -i ${platform}_cluster_info.json"
script.sh "./dcos-launch wait -i ${platform}_cluster_info.json"
break
} catch(InterruptedException | hudson.AbortException e) {
script.echo("Build interrupted. Destroying cluster....")
destroy()
script.echo("Cluster destroyed. Exiting...")
throw e
} catch(Exception e) {
script.echo("Exception:" + e.toString());
script.echo("Cluster creation failed. Destroying cluster....");
destroy()
if (createAttempts >= 3) {
script.echo("Cluster destroyed. Maximum number of creation attempts exceeded. Exiting...")
throw e
}
script.echo("Cluster destroyed. Retrying...")
}
}
}
/**
* Destroys a test cluster previously created using `create()`.
*/
def destroy() {
script.sh "./dcos-launch delete -i ${platform}_cluster_info.json"
}
/**
* Retrieves the URL of a cluster previously created using `create()`.
*/
def getDcosIp() {
/* In the future, consider doing the following with jq instead of
inline python (however, jq is not installed on our windows machines
at the moment). */
script.sh """
./dcos-launch describe -i ${platform}_cluster_info.json \
| python -c \
'import sys, json; \
contents = json.load(sys.stdin); \
print(contents["masters"][0]["public_ip"], end="")' \
> ${platform}_dcos_url"""
return script.readFile("${platform}_dcos_url")
}
}
/**
* This function returns a closure that prepares a test environment for a
* specific platform on a specific node in a specific workspace.
*/
def testBuilder(String platform, String nodeId, String workspace = null) {
return { Closure _body ->
def body = _body
return {
def destroyCluster = true
def cluster = new TestCluster(this, platform)
stage ("Create ${platform} cluster") {
cluster.create()
}
try {
def dcosIp = cluster.getDcosIp()
node(nodeId) {
if (!workspace) {
workspace = "${env.WORKSPACE}"
}
ws (workspace) {
stage ('Cleanup workspace') {
deleteDir()
}
stage ("Unstash dcos-cli repository") {
unstash('dcos-cli')
}
withCredentials(
[[$class: 'FileBinding',
credentialsId: '1c206779-acc0-4844-97f6-7b3ed081a456',
variable: 'DCOS_SNAKEOIL_CRT_PATH'],
[$class: 'FileBinding',
credentialsId: '23743034-1ac4-49f7-b2e6-a661aee2d11b',
variable: 'CLI_TEST_SSH_KEY_PATH']]) {
withEnv(["DCOS_IP=${dcosIp}"]) {
try {
body()
} catch(InterruptedException | hudson.AbortException e) {
echo(
"Build interrupted. The DC/OS cluster at" +
" ${dcosIp} will be destroyed.")
destroyCluster = true
throw e
} catch (Exception e) {
echo(
"Build failed. The DC/OS cluster at" +
" ${dcosIp} will remain temporarily" +
" active so you can debug what went" +
" wrong.")
destroyCluster = false
throw e
}
}
}
}
}
} finally {
if (destroyCluster) {
stage ("Destroy ${platform} cluster") {
try { cluster.destroy() }
catch (Exception e) {}
}
}
}
}
}
}
/**
* These are the builds that can be run in parallel.
*/
def builders = [:]
builders['windows-tests'] = testBuilder('windows', 'windows', 'C:\\windows\\workspace')({
stage ("Run dcos-cli tests") {
bat '''
bash -c " \
rm -rf ${HOME}/.dcos && \
sed \'/^.* dcos.snakeoil.mesosphere.com$/d\' /windows/system32/drivers/etc/hosts >/windows/system32/drivers/etc/hosts && \
echo ${DCOS_IP} dcos.snakeoil.mesosphere.com >> /windows/system32/drivers/etc/hosts"'''
bat 'bash -c "cat /windows/system32/drivers/etc/hosts"'
dir('dcos-cli') {
bat 'make test'
}
dir('dcos-cli/cli') {
bat '''
bash -c " \
export PYTHONIOENCODING=utf-8; \
make binary; \
export CLI_TEST_SSH_USER=centos; \
export CLI_TEST_MASTER_PROXY=true; \
dist/dcos cluster setup dcos.snakeoil.mesosphere.com \
--insecure --username=${DCOS_ADMIN_USERNAME} \
--password-env=DCOS_ADMIN_PASSWORD; \
dist/dcos config set core.reporting false; \
dist/dcos config set core.timeout 5; \
make test-binary"'''
}
}
})
/**
* This node bootstraps everything including creating all the test clusters,
* starting the builders, and finally destroying all the clusters once they
* are done.
*/
throttle(['dcos-cli']) {
node('py35') {
stage('Cleanup workspace') {
deleteDir()
}
stage ('Update node') {
sh 'pip install requests'
}
stage ('Download dcos-launch') {
sh 'wget https://downloads.dcos.io/dcos-test-utils/bin/linux/dcos-launch'
sh 'chmod a+x dcos-launch'
}
stage ('Pull dcos-cli repository') {
dir('dcos-cli') {
checkout scm
}
}
stage ('Stash dcos-cli repository') {
stash(['includes': 'dcos-cli/**', name: 'dcos-cli'])
}
withCredentials(
[[$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: '7155bd15-767d-4ae3-a375-e0d74c90a2c4',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'],
[$class: 'FileBinding',
credentialsId: '23743034-1ac4-49f7-b2e6-a661aee2d11b',
variable: 'CLI_TEST_SSH_KEY_PATH'],
[$class: 'StringBinding',
credentialsId: '84658f14-3944-4597-bca1-a00b89fcf938',
variable: 'DCOS_INSTALLER_URL'],
[$class: 'UsernamePasswordMultiBinding',
credentialsId: '323df884-742b-4099-b8b7-d764e5eb9674',
usernameVariable: 'DCOS_ADMIN_USERNAME',
passwordVariable: 'DCOS_ADMIN_PASSWORD']]) {
parallel builders
}
}
}