-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
310 lines (285 loc) · 11.3 KB
/
Jenkinsfile
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
properties([
parameters([
string(name: 'ssh_creds', defaultValue: 'ubuntu_ssh', description: 'ssh credentials id for target node'),
string(name: 'rhost_ip_mon', defaultValue: '', description: 'target mon node ip address'),
string(name: 'git_branch_mon', defaultValue: 'SO-MON-integration', description: 'mon git branch/tag name'),
string(name: 'ci_branch_mon', defaultValue: 'master', description: 'mon ci git branch/tag name'),
string(name: 'rhost_ip_so', defaultValue: '', description: 'target node ip address'),
string(name: 'git_branch_so', defaultValue: 'master', description: 'git branch/tag name'),
string(name: 'ci_branch_so', defaultValue: 'renaming', description: 'ci git branch/tag name'),
string(name: 'git_branch_vs', defaultValue: 'master', description: 'git branch/tag name'),
string(name: 'ci_branch_vs', defaultValue: 'master', description: 'ci git branch/tag name'),
string(name: 'git_branch_mtp', defaultValue: 'master', description: '5g git branch/tag name'),
// string(name: 'ci_branch_mtp', defaultValue: 'mtp_dummy', description: 'ci git branch/tag name'),
string(name: 'ci_branch_mtp', defaultValue: 'master', description: 'ci git branch/tag name'),
// instantiate paramas
choice(choices: ['m1.medium', 'm1.small2', 'm1.large'], description: 'What flavor to use?', name: 'flavor'),
string(name: 'ssh_cfy', defaultValue: 'centos_ssh', description: 'ssh credentials id for target node'),
string(name: 'cfy_node_ip', description: 'Cloudify ip address', defaultValue: '10.5.1.68'),
string(name: 'depl_id', defaultValue: 'so-test', description: 'deployment name'),
])
])
//{
def String nodeIp
def remote = [:]
remote.name = "${params.cfy_node_ip}"
remote.host = "${params.cfy_node_ip}"
remote.allowAnyHosts = true
//}
node {
withCredentials([sshUserPrivateKey(credentialsId: "${params.ssh_cfy}", keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName'),
]) {
remote.user = userName
remote.identityFile = identity
try {
stage('cleanup') {
sh '''
rm -rf Dockerfile || true
rm -rf *.yml || true
sudo docker system prune -a -f || true
'''
}
stage('config') {
parallel dockerfile: {
sh '''
cat > Dockerfile << EOF
FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y wget
RUN wget http://repository.cloudifysource.org/cloudify/19.07.18/community-release/cloudify-cli-community-19.07.18.deb
RUN dpkg -i *.deb
RUN mkdir infraDev
COPY infra.yml infraDev
RUN cfy profiles use 10.5.1.68 -u admin -p admin -t default_tenant
RUN cfy install infraDev/infra.yml -b myinfra
RUN cfy deployments outputs myinfra | grep -i value | awk '{print $2}' | tee myinfra.IP
EOF
'''
},
compose: {
sh """
cat > docker-compose.yml << EOF
version: '3'
services:
cfydev:
container_name: cfydev
image: ubuntu:latest
build:
context: .
dockerfile: ./Dockerfile
EOF
"""
},
blueprint: {
sh """
cat > infra.yml << EOF
tosca_definitions_version: cloudify_dsl_1_3
imports:
- http://www.getcloudify.org/spec/cloudify/4.3/types.yaml
- plugin:cloudify-openstack-plugin
- plugin:cloudify-utilities-plugin
inputs:
username: {default: 5gr-test}
password: {default: secret-test}
project_id: {default: 114f57f1565745f1801bd6fcb6811c52}
tenant_name: {default: 5gr-test}
auth_url: {default: 'http://10.5.1.95:5000/v3'}
user_domain_name: {default: Default}
region: {default: "RegionOne"}
ssh_key:
default: jenkins-key
img_my:
default: xenial-server-cloudimg-amd64-disk1
flavor_name:
default: 'm1.small2'
public_network_name1:
default: provider1
public_subnet_name1:
default: provider1-v4
dsl_definitions:
openstack_config: &openstack_config
username: { get_input: username }
password: { get_input: password }
project_id: { get_input: project_id }
auth_url: { get_input: auth_url }
region: { get_input: region }
user_domain_name: { get_input: user_domain_name }
node_templates:
my-openstack-keypair:
type: cloudify.openstack.nodes.KeyPair
properties:
use_external_resource: true
resource_id: { get_input: ssh_key }
private_key_path: '/etc/cloudify/key/jenkins-key'
openstack_config: *openstack_config
public_network:
type: cloudify.openstack.nodes.Network
properties:
openstack_config: *openstack_config
use_external_resource: true
resource_id: { get_input: public_network_name1 }
public_subnet:
type: cloudify.openstack.nodes.Subnet
properties:
openstack_config: *openstack_config
use_external_resource: true
resource_id: { get_input: public_subnet_name1 }
node_dev_port:
type: cloudify.openstack.nodes.Port
properties:
resource_id: 'node_dev_port'
openstack_config: *openstack_config
relationships:
- type: cloudify.relationships.contained_in
target: public_network
- type: cloudify.relationships.depends_on
target: public_subnet
node_dev:
type: cloudify.openstack.nodes.Server
properties:
agent_config:
install_method: none
resource_id: 'node_dev'
openstack_config: *openstack_config
image: { get_input: img_my }
flavor: { get_input: flavor_name }
agent_config:
install_method: none
relationships:
- target: node_dev_port
type: cloudify.openstack.server_connected_to_port
- target: my-openstack-keypair
type: cloudify.openstack.server_connected_to_keypair
outputs:
endpoint:
description: ip provider network
value: { get_attribute: [node_dev, ip]}
EOF
"""
}
}
stage('instantiate VM'){
sh """
sed -i 's/10.5.1.68/${params.cfy_node_ip}/g' Dockerfile
sed -i 's/myinfra/${params.depl_id}/g' Dockerfile
sed -i 's/cfydev/${params.depl_id}/g' docker-compose.yml
sed -i 's/node_dev/${params.depl_id}/g' infra.yml
sed -i 's/m1.small2/${params.flavor}/g' infra.yml
sudo docker-compose up --build
sudo docker system prune -a -f
"""
sshCommand remote: remote, command: "cfy deployments outputs ${params.depl_id} | grep -i value | awk '{print \$2}' | tee ${params.depl_id}.IP"
sshCommand remote: remote, command: "echo 'private key is:' | tee -a ${params.depl_id}.IP"
sshCommand remote: remote, command: "cat /etc/cloudify/key/jenkins-key | tee -a ${params.depl_id}.IP"
sshGet remote: remote, from: "${params.depl_id}.IP", into: "${params.depl_id}.IP", override: true
sh "cat ${params.depl_id}.IP"
nodeIp = sh(script: "head -n 1 ${params.depl_id}.IP", returnStdout: true)
// sh "exit 1"
}
} catch(er) {
//clear Jenkins
sh 'sudo docker system prune -a -f'
sshCommand remote: remote, command: "rm -rf ${params.depl_id}.IP"
currentBuild.result = 'FAILURE'
//destroy VM
sshCommand remote: remote, command: "cfy uninstall -f ${params.depl_id} -p 'ignore_failure=true' || true"
//mail
emailext attachLog: true, //attachmentsPattern: 'generatedFile.txt',
body: "Failed instantiate VM stage\n ${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}",
recipientProviders: [brokenBuildSuspects()],
subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}"
throw er
}}}
//string vars{
string d_path_mon = "mon1"
string s_path_mon = "repo/containerization/monitoring_platform"
string d_path_so = "so1"
string s_path_so = "repo/containerization/so"
string d_path_vs = "vs1"
string s_path_vs = "repo/containerization/vertical_slicer"
string d_path_mtp = "rl1"
string s_path_mtp = "repo/containerization/rl"
//}
//libs{
def dschr = new com.libs.dockerCheckRemote()
def br = new com.libs.buildRemote()
def dr = new com.libs.deployRemote()
def vrfr = new com.libs.verify()
//}
node {
try {
//try build & deploy so
stage('so') {
dschr.dockerScriptRemote("$nodeIp", params.ssh_creds)
br.remoteBuild("$d_path_so", "$s_path_so", 'so_', params.ci_branch_so, params.git_branch_so, params.ssh_creds, "$nodeIp")
dr.remoteDeploy("$d_path_so", 'so_', params.ssh_creds, "$nodeIp")
vrfr.remoteVerify("$d_path_so", "$s_path_so", 'so', params.ssh_creds, "$nodeIp")
}
} catch(er) {
//ERR
//mail
emailext attachLog: true, //attachmentsPattern: 'generatedFile.txt',
body: "Failed deploy Monitoring Platform stage\n ${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}",
recipientProviders: [brokenBuildSuspects()],
subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}"
//destroy VM
sshCommand remote: remote, command: "cfy uninstall -f ${params.depl_id} -p 'ignore_failure=true' || true"
//clear Jenkins
sh 'sudo docker system prune -a -f'
sshCommand remote: remote, command: "rm -rf ${params.depl_id}.IP"
currentBuild.result = 'FAILURE'
throw er
}
}
node {
try {
// try to deploy ready/tested other components
stage('mon') {
dschr.dockerScriptRemote("$nodeIp", params.ssh_creds)
br.remoteBuild("$d_path_mon", "$s_path_mon", 'mon_', params.ci_branch_mon, params.git_branch_mon, params.ssh_creds, "$nodeIp")
dr.remoteDeploy("$d_path_mon", 'mon_', params.ssh_creds, "$nodeIp")
}
stage('vs') {
dschr.dockerScriptRemote("$nodeIp", params.ssh_creds)
br.remoteBuild("$d_path_vs", "$s_path_vs", 'vs_', params.ci_branch_vs, params.git_branch_vs, params.ssh_creds, "$nodeIp")
dr.remoteDeploy("$d_path_vs", 'vs_', params.ssh_creds, "$nodeIp")
vrfr.remoteVerify("$d_path_vs", "$s_path_vs", 'vs', params.ssh_creds, "$nodeIp")
}
stage('mtp') {
dschr.dockerScriptRemote("$nodeIp", params.ssh_creds)
br.remoteBuild("$d_path_mtp", "$s_path_mtp", 'rl_', params.ci_branch_mtp, params.git_branch_mtp, params.ssh_creds, "$nodeIp")
dr.remoteDeploy("$d_path_mtp", 'rl_', params.ssh_creds, "$nodeIp")
vrfr.remoteVerify("$d_path_mtp", "$s_path_mtp", 'rl', params.ssh_creds, "$nodeIp")
}
} catch(er) {
//ERR
currentBuild.result = 'FAILURE'
//mail
emailext attachLog: true, //attachmentsPattern: 'generatedFile.txt',
body: "Failed deploy Other stack stage\n ${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}",
recipientProviders: [brokenBuildSuspects()],
subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}"
//destroy VM
node {
withCredentials([sshUserPrivateKey(credentialsId: "${params.ssh_cfy}", keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName'),
]) {
remote.user = userName
remote.identityFile = identity
sshCommand remote: remote, command: "cfy uninstall -f ${params.depl_id} -p 'ignore_failure=true' || true"
sshCommand remote: remote, command: "rm -rf ${params.depl_id}.IP"
throw er
}}
//clear Jenkins
sh 'sudo docker system prune -a -f'
}}
node {
withCredentials([sshUserPrivateKey(credentialsId: "${params.ssh_cfy}", keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName'),
]) {
remote.user = userName
remote.identityFile = identity
stage('deinstantiate'){
sshCommand remote: remote, command: "cfy uninstall -f ${params.depl_id} -p 'ignore_failure=true' || true"
sshCommand remote: remote, command: "rm -rf ${params.depl_id}.IP"
}
}
}