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'
33fc99 5 def cf_uri = 'https://rhpds.redhat.com'
DJ 6 def cf_group = 'rhpds-access-cicd'
16ed8d 7 // IMAP
DJ 8 def imap_creds = 'd8762f05-ca66-4364-adf2-bc3ce1dca16c'
9 def imap_server = 'imap.gmail.com'
10 // Notifications
33fc99 11 def notification_email = 'gpteinfrasev2@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 openshift_location = ''
23
24 // Catalog items
25 def choices = [
33fc99 26     'Workshops / CCN Roadshow for Dev Track',
DJ 27     'DevOps Team Development Catalog / DEV - CCN Roadshow for Dev Track',
16ed8d 28 ].join("\n")
DJ 29
30 def region_choice = [
33fc99 31     'na_gpte',
DJ 32     'apac_gpte',
33     'emea_gpte',
34 ].join("\n")
35
36 def course_module_list_choice = [
37     'm1,m2,m3,m4',
16ed8d 38 ].join("\n")
DJ 39
40 pipeline {
41     agent any
42
43     options {
44         buildDiscarder(logRotator(daysToKeepStr: '30'))
45     }
46
47     parameters {
48         booleanParam(
49             defaultValue: false,
50             description: 'wait for user input before deleting the environment',
51                 name: 'confirm_before_delete'
52         )
53         choice(
54             choices: choices,
55             description: 'Catalog item',
56             name: 'catalog_item',
57         )
58         choice(
59             choices: region_choice,
33fc99 60             description: 'Catalog item',
16ed8d 61             name: 'region',
33fc99 62         )
DJ 63         choice(
64             choices: course_module_list_choice,
65             description: 'Catalog item',
66             name: 'course_module_list',
16ed8d 67         )
DJ 68     }
69
70     stages {
71         stage('order from CF') {
72             environment {
73                 uri = "${cf_uri}"
74                 credentials = credentials("${opentlc_creds}")
75                 DEBUG = 'true'
76             }
77             /* This step use the order_svc_guid.sh script to order
78              a service from CloudForms */
79             steps {
80                 git url: 'https://github.com/redhat-gpte-devopsautomation/cloudforms-oob'
81
82                 script {
83                     def catalog = params.catalog_item.split(' / ')[0].trim()
84                     def item = params.catalog_item.split(' / ')[1].trim()
85                     def region = params.region.trim()
33fc99 86                     def course_module_list = params.course_module_list.trim()
DJ 87                     def cfparams = [
88                         'check=t',
89                         'salesforce=gptejen',
90                         'city=jenkins',
91                         'notes=devops_automation_jenkins',
92                         'use_letsencrypt=f',
93                         'expiration=2',
94                         'runtime=24',
95                         'quotacheck=t',
96                         'users=2',
97                         "region=${region}",
98                         "course_module_list=${course_module_list}",
99                     ].join(',').trim()
16ed8d 100                     echo "'${catalog}' '${item}'"
DJ 101                     guid = sh(
102                         returnStdout: true,
103                         script: """
104                           ./opentlc/order_svc_guid.sh \
105                           -c '${catalog}' \
106                           -i '${item}' \
107                           -G '${cf_group}' \
33fc99 108                           -d '${cfparams}' \
16ed8d 109                         """
DJ 110                     ).trim()
111
112                     echo "GUID is '${guid}'"
113                 }
114             }
115         }
33fc99 116         // Skip this step because sometimes the completed email arrives
DJ 117         // before the 'has started' email
16ed8d 118         stage('Wait for first email') {
DJ 119             environment {
120                 credentials=credentials("${imap_creds}")
121             }
122             steps {
33fc99 123                 git url: 'https://github.com/sborenst/ansible_agnostic_deployer',
16ed8d 124                     branch: 'development'
DJ 125
126                 sh """./tests/jenkins/downstream/poll_email.py \
127                     --server '${imap_server}' \
128                     --guid ${guid} \
33fc99 129                     --timeout 30 \
16ed8d 130                     --filter 'has started'"""
DJ 131             }
132         }
33fc99 133
DJ 134         stage('Wait for last email and parse OpenShift and App location') {
16ed8d 135             environment {
DJ 136                 credentials=credentials("${imap_creds}")
137             }
138             steps {
33fc99 139                 git url: 'https://github.com/sborenst/ansible_agnostic_deployer',
16ed8d 140                     branch: 'development'
DJ 141
142                 script {
143                     email = sh(
144                         returnStdout: true,
145                         script: """
146                           ./tests/jenkins/downstream/poll_email.py \
147                           --server '${imap_server}' \
148                           --guid ${guid} \
33fc99 149                           --timeout 150 \
16ed8d 150                           --filter 'has completed'
DJ 151                         """
152                     ).trim()
153
154                     try {
33fc99 155                         def m = email =~ /Openshift Master Console: (http:\/\/[^ \n]+)/
DJ 156                         openshift_location = m[0][1]
157                         echo "openshift_location = '${openshift_location}'"
16ed8d 158                     } catch(Exception ex) {
DJ 159                         echo "Could not parse email:"
160                         echo email
161                         echo ex.toString()
162                         throw ex
163                     }
164                 }
165             }
166         }
167
168         stage('Confirm before retiring') {
169             when {
170                 expression {
171                     return params.confirm_before_delete
172                 }
173             }
174             steps {
175                 input "Continue ?"
176             }
177         }
178         stage('Retire service from CF') {
179             environment {
180                 uri = "${cf_uri}"
181                 credentials = credentials("${opentlc_creds}")
182                 admin_credentials = credentials("${opentlc_admin_creds}")
183                 DEBUG = 'true'
184             }
185             /* This step uses the delete_svc_guid.sh script to retire
186              the service from CloudForms */
187             steps {
188                 git 'https://github.com/redhat-gpte-devopsautomation/cloudforms-oob'
189
190                 sh "./opentlc/delete_svc_guid.sh '${guid}'"
191             }
192             post {
193                 failure {
194                     withCredentials([usernameColonPassword(credentialsId: imap_creds, variable: 'credentials')]) {
195                         mail(
196                             subject: "${env.JOB_NAME} (${env.BUILD_NUMBER}) failed retiring for GUID=${guid}",
197                             body: "It appears that ${env.BUILD_URL} is failing, somebody should do something about that.\nMake sure GUID ${guid} is destroyed.",
198                             to: "${notification_email}",
199                             replyTo: "${notification_email}",
200                             from: credentials.split(':')[0]
201                         )
202                     }
203                     withCredentials([string(credentialsId: rocketchat_hook, variable: 'HOOK_URL')]) {
204                         sh(
205                             """
206                             curl -H 'Content-Type: application/json' \
207                             -X POST '${HOOK_URL}' \
208                             -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}.\"}'\
209                             """.trim()
210                         )
211                     }
212                 }
213             }
214         }
215         stage('Wait for deletion email') {
216             steps {
33fc99 217                 git url: 'https://github.com/sborenst/ansible_agnostic_deployer',
16ed8d 218                     branch: 'development'
DJ 219
220                 withCredentials([usernameColonPassword(credentialsId: imap_creds, variable: 'credentials')]) {
221                     sh """./tests/jenkins/downstream/poll_email.py \
222                         --guid ${guid} \
223                         --timeout 20 \
224                         --server '${imap_server}' \
225                         --filter 'has been deleted'"""
226                 }
227             }
228         }
229     }
230
231     post {
232         failure {
233             git 'https://github.com/redhat-gpte-devopsautomation/cloudforms-oob'
234             /* retire in case of failure */
235             withCredentials(
236                 [
237                     usernameColonPassword(credentialsId: opentlc_creds, variable: 'credentials'),
238                     usernameColonPassword(credentialsId: opentlc_admin_creds, variable: 'admin_credentials')
239                 ]
240             ) {
241                 sh """
242                 export uri="${cf_uri}"
243                 export DEBUG=true
244                 ./opentlc/delete_svc_guid.sh '${guid}'
245                 """
33fc99 246             }
DJ 247
248             /* Print ansible logs */
249             withCredentials([
250                 string(credentialsId: ssh_admin_host, variable: 'ssh_admin'),
251                 sshUserPrivateKey(
252                     credentialsId: ssh_creds,
253                     keyFileVariable: 'ssh_key',
254                     usernameVariable: 'ssh_username')
255             ]) {
256                 sh("""
257                     ssh -o StrictHostKeyChecking=no -i ${ssh_key} ${ssh_admin} \
258                     "bin/logs.sh ${guid}" || true
259                 """.trim()
260                 )
16ed8d 261             }
DJ 262
263             withCredentials([usernameColonPassword(credentialsId: imap_creds, variable: 'credentials')]) {
264                 mail(
265                     subject: "${env.JOB_NAME} (${env.BUILD_NUMBER}) failed GUID=${guid}",
266                     body: "It appears that ${env.BUILD_URL} is failing, somebody should do something about that.",
267                     to: "${notification_email}",
268                     replyTo: "${notification_email}",
269                     from: credentials.split(':')[0]
270               )
271             }
272             withCredentials([string(credentialsId: rocketchat_hook, variable: 'HOOK_URL')]) {
273                 sh(
274                     """
275                       curl -H 'Content-Type: application/json' \
276                       -X POST '${HOOK_URL}' \
277                       -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.\"}'\
278                     """.trim()
279                 )
280             }
281         }
282         fixed {
283             withCredentials([string(credentialsId: rocketchat_hook, variable: 'HOOK_URL')]) {
284                 sh(
285                     """
286                       curl -H 'Content-Type: application/json' \
287                       -X POST '${HOOK_URL}' \
288                       -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\"}'\
289                     """.trim()
290                 )
291             }
292         }
293     }
33fc99 294 }