-
Notifications
You must be signed in to change notification settings - Fork 34
/
Jenkinsfile
246 lines (210 loc) · 9.57 KB
/
Jenkinsfile
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!groovy
notifyBuildDetails = ""
cdnHost = ""
jumpServer = ""
aptRepo = ""
try {
notifyBuild('STARTED')
String debFileName = "management-${env.BRANCH_NAME}.deb"
switch (env.BRANCH_NAME) {
case ~/master/: cdnHost = "masterbazaar.subutai.io"; break;
case ~/dev/: cdnHost = "devbazaar.subutai.io"; break;
default: cdnHost = "bazaar.subutai.io"
}
switch (env.BRANCH_NAME) {
case ~/master/: jumpServer = "mastertun.subutai.io"; break;
case ~/dev/: jumpServer = "devtun.subutai.io"; break;
default: jumpServer = "tun.subutai.io"
}
switch (env.BRANCH_NAME) {
case ~/master/: aptRepo = "master"; break;
case ~/dev/: aptRepo = "dev"; break;
default: aptRepo = "prod"
}
node("management") {
deleteDir()
def mvnHome = tool 'M3'
def workspace = pwd()
stage("Build management deb package")
// Use maven to to build deb and template files of management
notifyBuildDetails = "\nFailed Step - Build management deb package"
checkout scm
def artifactVersion = getVersion("management/pom.xml")
// build deb
sh """
cd management
set +x
set -e
git checkout ${env.BRANCH_NAME}
sed 's/export BAZAAR_IP=.*/export BAZAAR_IP=${cdnHost}/g' -i server/server-karaf/src/main/assembly/bin/setenv
if [[ "${env.BRANCH_NAME}" == "dev" ]]; then
${mvnHome}/bin/mvn clean install -X -Dmaven.test.skip=false -P deb -Dgit.branch=${env.BRANCH_NAME}
else
${mvnHome}/bin/mvn clean install -Dmaven.test.skip=true -P deb -Dgit.branch=${env.BRANCH_NAME}
fi
branch=`git symbolic-ref --short HEAD` && echo "Branch is \$branch"
find ${workspace}/management/server/server-karaf/target/ -name *.deb | xargs -I {} cp {} ${workspace}/${debFileName}
"""
stash includes: "management-*.deb", name: 'deb'
// CDN auth credentials
String user = "[email protected]"
String fingerprint = "877B586E74F170BC4CF6ECABB971E2AC63D23DC9"
def authId = sh(script: """
curl -s https://${cdnHost}/rest/v1/cdn/token?fingerprint=${fingerprint}
""", returnStdout: true)
authId = authId.trim()
def sign = sh(script: """
echo ${authId} | gpg1 --clearsign -u ${user}
""", returnStdout: true)
sign = sign.trim()
String token = sh(script: """
curl -s --data-urlencode "request=${sign}" https://${cdnHost}/rest/v1/cdn/token
""", returnStdout: true)
token = token.trim()
stage("Build management template")
notifyBuildDetails = "\nFailed Step - Build management template"
// create management template
sh """
set +x
set -e
sudo sed 's/URL =.*/URL = ${cdnHost}/gI' -i /etc/subutai/agent.conf
sudo sed 's/SshJumpServer =.*/SshJumpServer = ${jumpServer}/gI' -i /etc/subutai/agent.conf
set +e
sudo subutai destroy management
set -e
sudo subutai clone debian-buster management
/bin/sleep 20
cp ${workspace}/${debFileName} /var/lib/lxc/management/rootfs/tmp/
sudo subutai attach management "apt-get update && apt-get install dirmngr curl -y"
sudo subutai attach management "apt-key adv --recv-keys --keyserver keyserver.ubuntu.com C6B2AC7FBEB649F1"
sudo subutai attach management "apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 8AC3B29174885C03"
sudo subutai attach management "curl https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | apt-key add -"
sudo subutai attach management "echo 'deb https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/ buster main' > /etc/apt/sources.list.d/adoptopenjdk.list"
sudo subutai attach management "echo 'deb http://deb.subutai.io/subutai ${aptRepo} main' > /etc/apt/sources.list.d/subutai-repo.list"
sudo subutai attach management "apt-get update && apt upgrade -y"
sudo subutai attach management "sync"
sudo subutai attach management "apt-get -y install influxdb influxdb-certs adoptopenjdk-8-hotspot adoptopenjdk-8-hotspot-jre"
sudo cp ~/influxdb.conf /var/lib/lxc/management/rootfs/etc/influxdb/influxdb.conf
sudo subutai attach management "dpkg -i /tmp/${debFileName}"
sudo subutai attach management "systemctl enable management"
sudo subutai attach management "systemctl stop management"
sudo subutai attach management "rm -rf /opt/subutai-mng/keystores/"
sudo subutai attach management "rm -rf /opt/subutai-mng/db"
sudo subutai attach management "apt-get clean"
sudo subutai attach management "sync"
sudo subutai attach management "sed -i "s/weekly/dayly/g" /etc/logrotate.d/rsyslog"
sudo subutai attach management "sed -i "/delaycompress/d" /etc/logrotate.d/rsyslog"
sudo subutai attach management "sed -i "s/7/3/g" /etc/logrotate.d/rsyslog"
sudo subutai attach management "sed -i "s/4/3/g" /etc/logrotate.d/rsyslog"
sudo rm /var/lib/lxc/management/rootfs/tmp/${debFileName}
echo "Using CDN token ${token}"
echo "Template version is ${artifactVersion}"
"""
//remove existing template metadata
String OLD_ID = sh(script: """
var=\$(curl -s https://${cdnHost}/rest/v1/cdn/template?name=management) ; if [[ \$var != "Template not found" ]]; then echo \$var | jq -r \'.id\' ; else echo \$var; fi
""", returnStdout: true)
OLD_ID = OLD_ID.trim()
sh """
echo "OLD ID: ${OLD_ID}"
if [[ "${OLD_ID}" != "Template not found" ]]; then
curl -X DELETE "https://${cdnHost}/rest/v1/cdn/template?token=${token}&id=${OLD_ID}"
fi
"""
if (env.BRANCH_NAME == 'dev') {
// Exporting template
sh """
set -e
sudo subutai -d export management -r "${artifactVersion}" --local --token "${token}"
"""
}
else {
sh """
set -e
sudo subutai export management -r "${artifactVersion}" --local --token "${token}"
"""
}
stage("Upload management template to IPFS node")
notifyBuildDetails = "\nFailed Step - Upload management template to IPFS node"
//TODO upload to CDN
sh """
set +e
cd /var/cache/subutai/
curl -sk -H "token: ${token}" -Ffile=@management-subutai-template_${artifactVersion}_amd64.tar.gz -Ftoken=${token} -X POST "https://${cdnHost}/rest/v1/cdn/uploadTemplate"
"""
if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME == 'dev' || env.BRANCH_NAME == 'sysnet') {
stage("Upload to REPO") {
notifyBuildDetails = "\nFailed Step - Upload to Repo"
deleteDir()
unstash 'deb'
//copy deb to repo
sh """
touch uploading_management
scp uploading_management ${debFileName} [email protected]:incoming/${env.BRANCH_NAME}/
ssh [email protected] sh /var/reprepro/scripts/scan-incoming.sh ${env.BRANCH_NAME} management
"""
}
}
}
} catch (e) {
currentBuild.result = "FAILED"
throw e
} finally {
// Success or failure, always send notifications
notifyBuild(currentBuild.result, notifyBuildDetails)
}
def getVersionFromPom(pom) {
def matcher = readFile(pom) =~ '<version>(.+)</version>'
matcher ? matcher[1][1] : null
}
def String getVersion(pom) {
def pomver = getVersionFromPom(pom)
def ver = sh(script: "/bin/echo ${pomver} | cut -d '-' -f 1", returnStdout: true)
return "${ver}".trim()
}
@NonCPS
def jsonParse(def json) {
new groovy.json.JsonSlurperClassic().parseText(json)
}
// https://jenkins.io/blog/2016/07/18/pipline-notifications/
def notifyBuild(String buildStatus = 'STARTED', String details = '') {
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESSFUL'
// Default values
def colorName = 'RED'
def colorCode = '#FF0000'
def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
def summary = "${subject} (${env.BUILD_URL})"
// Override default values based on build status
if (buildStatus == 'STARTED') {
color = 'YELLOW'
colorCode = '#FFFF00'
} else if (buildStatus == 'SUCCESSFUL') {
color = 'GREEN'
colorCode = '#00FF00'
} else {
color = 'RED'
colorCode = '#FF0000'
summary = "${subject} (${env.BUILD_URL})${details}"
}
// Get token
def slackToken = getSlackToken('ss-bots')
def mattermost_rest = "https://mm.optdyn.com/hooks/dhdtqg4eci8hxd1358icgujq9w"
// Send notifications
//slackSend(color: colorCode, message: summary, teamDomain: 'optdyn', token: "${slackToken}")
mattermostSend(color: colorCode, icon: "https://jenkins.io/images/logos/jenkins/jenkins.png", message: summary, channel: "#ss-bots", endpoint: "${mattermost_rest}" )
}
// get slack token from global jenkins credentials store
@NonCPS
def getSlackToken(String slackCredentialsId) {
// id is ID of creadentials
def jenkins_creds = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0]
String found_slack_token = jenkins_creds.getStore().getDomains().findResult { domain ->
jenkins_creds.getCredentials(domain).findResult { credential ->
if (slackCredentialsId.equals(credential.id)) {
credential.getSecret()
}
}
}
return found_slack_token
}