· 5 years ago · Nov 15, 2019, 11:18 PM
1/**
2 * pipeline for alerting about AWS budget quotas
3 * CREDENTIALS_ID - gerrit credentials id
4 * GERRIT_HOST - Gerrit host
5 * KAAS_PIPELINE_REFSPEC - Refspec for kaas related pipelines, for testing additional pipeline changes, generally using GERRIT_REFSPEC
6 * GERRIT_REFSPEC - Parameter also used for fetching groovy pipeline from kaas repo [KAAS_PIPELINE_REFSPEC],
7 * default value used in case of non-triggered run, otherwise gerrit trigger will overwrite it
8 *
9 * KAAS_AWS_CREDENTIALS - AWSAccessKeyId/AWSSecretKey credentials
10 * KAAS_AWS_REGION - AWS region
11
12 * SLACK_CHANNEL_NOTIFY - send reports in slack channel
13 *
14**/
15
16common = new com.mirantis.mk.Common()
17slack = new com.mirantis.mcp.SlackNotification()
18gerrit = new com.mirantis.mk.Gerrit()
19jobCatchedErrors = 'No build errors'
20
21def awsCliImage = 'mikesir87/aws-cli:1.16.277'
22
23node('docker') {
24
25 // Define Jenkins user id's for docker operations
26 def jenkinsUID = common.getJenkinsUid()
27 def jenkinsGID = common.getJenkinsGid()
28 jenkinsUser = "${jenkinsUID}:${jenkinsGID}"
29 def workspace = common.getWorkspace()
30 //avoid root-owned docker artifacts before kaasLibrary scm
31 sh(script: """\
32 docker run --rm \
33 -v ${workspace}:/code \
34 busybox sh -c 'chown -R ${jenkinsUser} /code'
35 """
36 )
37
38 try {
39 timeout(time: 1, unit: 'HOURS') {
40
41 stage('Checkout') {
42 gerrit.gerritPatchsetCheckout('ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/kaas/cluster-api-provider-openstack', 'mirantis', 'HEAD', CREDENTIALS_ID)
43 }
44
45 stage('Getting monthly budget metrics') {
46 withCredentials([
47 [$class: 'UsernamePasswordMultiBinding',
48 credentialsId: KAAS_AWS_CREDENTIALS,
49 passwordVariable: 'SECRET_KEY',
50 usernameVariable: 'SECRET_ID'],
51 ],) {
52 def meta = sh(script: """make --silent DEST=${workspace} \
53 GOOS=linux \
54 AWS_ACCESS_KEY_ID=${SECRET_ID} \
55 AWS_ACCESS_KEY_SECRET=${SECRET_KEY} \
56 AWS_CLI_IMAGE=${awsCliImage} \
57 AWS_BUDGET_REPORT_REGION=${KAAS_AWS_REGION} \
58 aws-monthly-budget-report""", returnStdout: true)
59 parsedMeta = readJSON text: meta
60 }
61 }
62 }
63 currentBuild.result = 'SUCCESS'
64 } catch (e) {
65 // If there was an error or exception thrown, the build failed
66 currentBuild.result = 'FAILURE'
67 jobCatchedErrors = e.message ?: 'Failed to get error msg'
68 throw e
69 } finally {
70 stage('Reporting') {
71 // Shared doc with aws quotas calculations
72 def infoLink = '<https://docs.google.com/document/d/1K8FDKm8R_1cVaIG2gJwK5UPr6rWw8qWg4vp4ahw85Ko/edit?usp=sharing| KaaS AWS Budget Quotas>'
73 def tsStarts = parsedMeta['ResultsByTime'][0]['TimePeriod']['Start']
74 def tsEnds = parsedMeta['ResultsByTime'][0]['TimePeriod']['End']
75 def costs = parsedMeta['ResultsByTime'][0]['Total']
76
77 def report = "Budget configured for account: ${KAAS_AWS_CREDENTIALS}\n" +
78 "Starting from: ${tsStarts}\n" +
79 "Spend blended: ${costs['BlendedCost']['Amount']} ${costs['BlendedCost']['Unit']}\n" +
80 "Spend unblended: ${costs['UnblendedCost']['Amount']} ${costs['UnblendedCost']['Unit']}\n" +
81 "Budget reset: ${tsEnds}\n" +
82 "Quota information: ${infoLink}
83 common.infoMsg(report)
84 slack.jobResultNotification(currentBuild.result, SLACK_CHANNEL_NOTIFY, '', null, '', 'slack_webhook_url', report)
85 }
86
87 currentBuild.description = """
88 <p>
89 <b>Errors</b>: ${jobCatchedErrors}<br/>
90 </p>
91 """
92
93 stage('Cleanup'){
94 // cleanup workspace
95 deleteDir()
96 }
97 }
98}