Dibyendu Jana
2020-02-27 33fc99134cf9441f64c8ad17eadc55b88e3fcee8
commit | author | age
16ed8d 1 // -------------- Configuration --------------
DJ 2 // CloudForms
3 def opentlc_creds = 'b93d2da4-c2b7-45b5-bf3b-ee2c08c6368e'
4 def opentlc_admin_creds = '73b84287-8feb-478a-b1f2-345fd0a1af47'
5 def cf_uri = 'https://labs.opentlc.com'
6 def cf_group = 'opentlc-access-cicd'
7 // IMAP
8 def imap_creds = 'd8762f05-ca66-4364-adf2-bc3ce1dca16c'
9 def imap_server = 'imap.gmail.com'
10 // Notifications
33fc99 11 def notification_email = 'gpteinfrasev3@redhat.com'
16ed8d 12 def rocketchat_hook = '5d28935e-f7ca-4b11-8b8e-d7a7161a013a'
DJ 13
14 // SSH key
15 def ssh_creds = '15e1788b-ed3c-4b18-8115-574045f32ce4'
16
17 // Admin host ssh location is in a credential too
18 def ssh_admin_host = 'admin-host-na'
19
20 // state variables
21 def guid=''
22 def ssh_location = ''
23
24
25 // Catalog items
26 def choices = [
27     'OPENTLC Automation / Ansible Implementation',
28 ].join("\n")
29
30 def region_choice = [
31     'na',
32     'apac',
33     'emea',
34 ].join("\n")
35
36 pipeline {
37     agent any
38
39     options {
40         buildDiscarder(logRotator(daysToKeepStr: '30'))
41     }
42
43     parameters {
44         booleanParam(
45             defaultValue: false,
46             description: 'wait for user input before deleting the environment',
47                 name: 'confirm_before_delete'
48         )
49         choice(
50             choices: choices,
51             description: 'Catalog item',
52             name: 'catalog_item',
53         )
54         choice(
55             choices: region_choice,
56             description: 'Catalog item',
57             name: 'region',
58         )
59     }
60
61     stages {
62         stage('order from CF') {
63             environment {
64                 uri = "${cf_uri}"
65                 credentials = credentials("${opentlc_creds}")
66                 DEBUG = 'true'
67             }
68             /* This step use the order_svc_guid.sh script to order
69              a service from CloudForms */
70             steps {
71                 git url: 'https://github.com/redhat-gpte-devopsautomation/cloudforms-oob'
72
73                 script {
74                     def catalog = params.catalog_item.split(' / ')[0].trim()
75                     def item = params.catalog_item.split(' / ')[1].trim()
76                     def region = params.region.trim()
77                     def cfparams = [
78                         'expiration=7',
79                         'runtime=8',
80                         "region=${region}",
81                     ].join(',').trim()
82
83                     echo "'${catalog}' '${item}'"
84                     guid = sh(
85                         returnStdout: true,
86                         script: """
87                           ./opentlc/order_svc_guid.sh \
88                           -c '${catalog}' \
89                           -i '${item}' \
90                           -G '${cf_group}' \
91                           -d '${cfparams}' \
92                         """
93                     ).trim()
94
95                     echo "GUID is '${guid}'"
96                 }
97             }
98         }
99
100         stage('Wait for first email') {
101             environment {
102                 credentials=credentials("${imap_creds}")
103             }
104             steps {
105                 git url: 'https://github.com/sborenst/ansible_agnostic_deployer',
106                     branch: 'development'
107
108
109                 sh """./tests/jenkins/downstream/poll_email.py \
110                     --server '${imap_server}' \
111                     --guid ${guid} \
112                     --timeout 20 \
113                     --filter 'has started'"""
114             }
115         }
116
117         stage('Wait for last email and parse OpenShift and App location') {
118             environment {
119                 credentials=credentials("${imap_creds}")
120             }
121             steps {
122                 git url: 'https://github.com/sborenst/ansible_agnostic_deployer',
123                     branch: 'development'
124
125                 script {
126                     email = sh(
127                         returnStdout: true,
128                         script: """
129                           ./tests/jenkins/downstream/poll_email.py \
130                           --server '${imap_server}' \
131                           --guid ${guid} \
132                           --timeout 40 \
133                           --filter 'has completed'
134                         """
135                     ).trim()
136
137                     try {
138                         def m = email =~ /<pre>. *ssh -i [^ ]+ *([^ <]+?) *<\/pre>/
139                         ssh_location = m[0][1]
140                         echo "ssh_location = ${ssh_location}"
141                     } catch(Exception ex) {
142                         echo "Could not parse email:"
143                         echo email
144                         echo ex.toString()
145                         throw ex
146                     }
147                 }
148             }
149         }
150         stage('SSH') {
151             steps {
152                 withCredentials([
153                     sshUserPrivateKey(
154                         credentialsId: ssh_creds,
155                         keyFileVariable: 'ssh_key',
156                         usernameVariable: 'ssh_username')
157                 ]) {
158                     sh "ssh -o StrictHostKeyChecking=no -i ${ssh_key} ${ssh_location} w"
159                 }
160             }
161         }
162
163         stage('Confirm before retiring') {
164             when {
165                 expression {
166                     return params.confirm_before_delete
167                 }
168             }
169             steps {
170                 input "Continue ?"
171             }
172         }
173         stage('Retire service from CF') {
174             environment {
175                 uri = "${cf_uri}"
176                 credentials = credentials("${opentlc_creds}")
177                 admin_credentials = credentials("${opentlc_admin_creds}")
178                 DEBUG = 'true'
179             }
180             /* This step uses the delete_svc_guid.sh script to retire
181              the service from CloudForms */
182             steps {
183                 git 'https://github.com/redhat-gpte-devopsautomation/cloudforms-oob'
184
185                 sh "./opentlc/delete_svc_guid.sh '${guid}'"
186             }
187             post {
188                 failure {
189                     withCredentials([usernameColonPassword(credentialsId: imap_creds, variable: 'credentials')]) {
190                         mail(
191                             subject: "${env.JOB_NAME} (${env.BUILD_NUMBER}) failed retiring for GUID=${guid}",
192                             body: "It appears that ${env.BUILD_URL} is failing, somebody should do something about that.\nMake sure GUID ${guid} is destroyed.",
193                             to: "${notification_email}",
194                             replyTo: "${notification_email}",
195                             from: credentials.split(':')[0]
196                         )
197                     }
198                     withCredentials([string(credentialsId: rocketchat_hook, variable: 'HOOK_URL')]) {
199                         sh(
200                             """
201                             curl -H 'Content-Type: application/json' \
202                             -X POST '${HOOK_URL}' \
203                             -d '{\"username\": \"jenkins\", \"icon_url\": \"https://dev-sfo01.opentlc.com/static/81c91982/images/headshot.png\", \"text\": \"@here :rage: ${env.JOB_NAME} (${env.BUILD_NUMBER}) failed retiring ${guid}.\"}'\
204                             """.trim()
205                         )
206                     }
207                 }
208             }
209         }
210         stage('Wait for deletion email') {
211             steps {
212                 git url: 'https://github.com/sborenst/ansible_agnostic_deployer',
213                     branch: 'development'
214
215                 withCredentials([usernameColonPassword(credentialsId: imap_creds, variable: 'credentials')]) {
216                     sh """./tests/jenkins/downstream/poll_email.py \
217                         --guid ${guid} \
218                         --timeout 20 \
219                         --server '${imap_server}' \
220                         --filter 'has been deleted'"""
221                 }
222             }
223         }
224     }
225
226     post {
227         failure {
228             git 'https://github.com/redhat-gpte-devopsautomation/cloudforms-oob'
229             /* retire in case of failure */
230             withCredentials(
231                 [
232                     usernameColonPassword(credentialsId: opentlc_creds, variable: 'credentials'),
233                     usernameColonPassword(credentialsId: opentlc_admin_creds, variable: 'admin_credentials')
234                 ]
235             ) {
236                 sh """
237                 export uri="${cf_uri}"
238                 export DEBUG=true
239                 ./opentlc/delete_svc_guid.sh '${guid}'
240                 """
241             }
242
243             /* Print ansible logs */
244             withCredentials([
245                 string(credentialsId: ssh_admin_host, variable: 'ssh_admin'),
246                 sshUserPrivateKey(
247                     credentialsId: ssh_creds,
248                     keyFileVariable: 'ssh_key',
249                     usernameVariable: 'ssh_username')
250             ]) {
251                 sh("""
252                     ssh -o StrictHostKeyChecking=no -i ${ssh_key} ${ssh_admin} \
253                     "bin/logs.sh ${guid}" || true
254                 """.trim()
255                 )
256             }
257
258             withCredentials([usernameColonPassword(credentialsId: imap_creds, variable: 'credentials')]) {
259                 mail(
260                     subject: "${env.JOB_NAME} (${env.BUILD_NUMBER}) failed GUID=${guid}",
261                     body: "It appears that ${env.BUILD_URL} is failing, somebody should do something about that.",
262                     to: "${notification_email}",
263                     replyTo: "${notification_email}",
264                     from: credentials.split(':')[0]
265               )
266             }
267             withCredentials([string(credentialsId: rocketchat_hook, variable: 'HOOK_URL')]) {
268                 sh(
269                     """
270                       curl -H 'Content-Type: application/json' \
271                       -X POST '${HOOK_URL}' \
272                       -d '{\"username\": \"jenkins\", \"icon_url\": \"https://dev-sfo01.opentlc.com/static/81c91982/images/headshot.png\", \"text\": \"@here :rage: ${env.JOB_NAME} (${env.BUILD_NUMBER}) failed GUID=${guid}. It appears that ${env.BUILD_URL}/console is failing, somebody should do something about that.\"}'\
273                     """.trim()
274                 )
275             }
276         }
277         fixed {
278             withCredentials([string(credentialsId: rocketchat_hook, variable: 'HOOK_URL')]) {
279                 sh(
280                     """
281                       curl -H 'Content-Type: application/json' \
282                       -X POST '${HOOK_URL}' \
283                       -d '{\"username\": \"jenkins\", \"icon_url\": \"https://dev-sfo01.opentlc.com/static/81c91982/images/headshot.png\", \"text\": \"@here :smile: ${env.JOB_NAME} is now FIXED, see ${env.BUILD_URL}/console\"}'\
284                     """.trim()
285                 )
286             }
287         }
288     }
289 }