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