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