-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
212 lines (198 loc) · 7.11 KB
/
Copy pathJenkinsfile
File metadata and controls
212 lines (198 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!groovy
import groovy.transform.Field
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
@Field
def dataRules = ""
@Field
def controlRules = ""
def dataPath = 'runbooks/newrules/tidb-cloud/data-plane/data-platform/'
def controlPath = 'runbooks/newrules/tidb-cloud/control-plane/cloud-platform/'
def globalDomainDev = 'k8s-observab-globalin-22824fa614-584301555.us-west-2.elb.amazonaws.com'
def globalDomainStaging = 'www.gs.us-west-2.aws.observability-staging.pingcap.cloud/'
def globalDomainProd = 'www.gs.us-west-2.aws.observability.tidbcloud.com'
def dataPlaneRulesID = '95072d4e-5676-4084-bdf1-09aca85881f2'
def controlPlaneRulesID = 'd5025c09-d861-417c-9cfe-797bd97ce0c3'
pipeline {
agent any
options {
disableConcurrentBuilds()
}
stages {
stage('Pull rules') {
when {
beforeAgent true
not { changeRequest() }
changeset
}
steps {
script {
try {
retry(3) {
echo 'pulling rules...'
dir("runbooks"){
git branch: 'NewRuleFolder', credentialsId: 'jenkins-user-for-github', url: 'https://github.com/tidbcloud/runbooks.git'
}
}
} catch (err) {
echo "Failed: ${err}"
}
}
}
}
stage('Read yaml file'){
stages {
stage('read dev rules') {
when {
changeset "test/**" // "newrules/dev/tidb-cloud/**"
not { changeRequest() }
}
stages {
stage('read data-plane rules') {
when {
changeset "test/data-plane/**" //"newrules/dev/tidb-cloud/data-plane/data-platform/**"
}
steps{
script{
getDataRules(dataPath)
}
}
}
stage('read control-plane rules') {
when {
changeset "test/control-plane/**" //"newrules/dev/tidb-cloud/control-plane/cloud-platform/**"
}
steps{
script{
getControlRules(controlPath)
}
}
}
}
}
stage('read staging rules') {
steps{
echo 'read staging rules'
}
}
stage('read prod rules') {
steps{
echo 'read prod rules'
}
}
}
}
stage('Delivery'){
stages {
stage('Apply rules to dev') {
when {
changeset "test/data-plane/**" // "newrules/dev/tidb-cloud/**"
}
stages {
stage('Call API to update data-plane rules') {
when {
changeset "test/data-plane/**" // "newrules/dev/tidb-cloud/data-plane/data-platform/**"
}
steps {
script{
token = getToken()
writeRules("${dataPlaneRulesID}", "${dataRules}", "${globalDomainDev}", "${token}")
}
}
}
stage('Call API to update control-plane rules') {
when {
changeset "test/control-plane/**" // "newrules/dev/tidb-cloud/control-plane/cloud-platform/**"
}
steps {
script{
token = getToken()
writeRules("${controlPlaneRulesID}", "${controlRules}", "${globalDomainDev}", "${token}")
}
}
}
}
}
stage('Apply rules to staging') {
steps{
echo 'Apply rules to staging'
}
}
stage('Apply rules to prod') {
steps{
echo 'Apply rules to prod'
}
}
}
}
}
post {
always {
echo 'get rules done'
}
success {
echo 'the result is success'
}
failure {
echo 'the result is failure'
}
unstable {
echo 'the result is unstable'
}
changed {
echo 'the state of the Pipeline has changed'
}
}
}
def getDataRules(PATH) {
def files
dir(PATH) {
files = findFiles(glob: '*.yaml')
}
for(file in files) {
content = readYaml file : "${PATH}${file.path}"
json = JsonOutput.toJson(content)
dataRules = "${dataRules}${json.substring(11, json.length()-2)},"
}
dataRules = "{\"groups\":[${dataRules.substring(0, dataRules.length()-1)}]}"
}
def getControlRules(PATH) {
def files
dir(PATH) {
files = findFiles(glob: '*.yaml')
}
for(file in files) {
content = readYaml file : "${PATH}${file.path}"
json = JsonOutput.toJson(content)
controlRules = "${controlRules}${json.substring(11, json.length()-2)},"
}
controlRules = "{\"groups\":[${controlRules.substring(0, controlRules.length()-1)}]}"
}
def getToken() {
def response = sh returnStdout: true, script: """
curl --location --request POST 'https://tidb-soc2.us.auth0.com/oauth/token' \
--header 'content-type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=Sd4aekPCa5nMUSQBIvf8eGSL895WtV4o' \
--data-urlencode 'client_secret=-SLP5UvxJdwGbVaKupxgv53T6otXguApPoG0fBsWCIqDG8_TuvlYnlkhPQHcWaKT' \
--data-urlencode 'audience=https://tidb-soc2.us.auth0.com/api/v2/'
"""
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(response)
echo "${object}"
echo object.access_token
return object.access_token
}
def writeRules(RULES_ID, RULES, DOMAIN, TOKEN) {
sh """
curl --location --request POST 'http://${DOMAIN}/api/v1/alertrule-template' \
--header 'Authorization: Bearer ${TOKEN}' \
--header 'Content-Type: application/json' \
--data-raw '{
"vendor": "aws",
"region": "us-west-2",
"templateID": "${RULES_ID}",
"monitorPromRules": ${RULES}
}'
"""
}