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