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