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