This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Jenkinsfile
70 lines (64 loc) · 2.44 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
currentBuild.result = "SUCCESS"
ansiColor('xterm') {
stage("Puppet Apply On All Nodes") {
node('master') {
try {
slackSend "Started ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
/**
* Retrieve Nodes from Consul HTTP API.
* https://www.consul.io/docs/agent/http.html
*/
def consul_base_url = "http://10.0.50.10:8500"
def response = httpRequest "${consul_base_url}/v1/catalog/nodes"
def nodesJson = parseJsonText response.content
def nodes = [:]
for (int i = 0; i < nodesJson.size(); i++) {
def node = nodesJson[i];
nodes[node.Node] = {
// retrieve status of serfHealth to check if node is online.
response = httpRequest "${consul_base_url}/v1/health/node/${node.Node}"
status = parseHealthCheck response
if (!node.Node.contains('p-ci-01') && status == "passing") {
puppetApply node.Address
}
}
}
parallel nodes
slackSend "${env.JOB_NAME} - ${env.BUILD_NUMBER} ${nodes.size()} nodes was updated. (<${env.BUILD_URL}|Open>)"
} catch (err) {
currentBuild.result = "FAILURE"
slackSend "${env.JOB_NAME} - ${env.BUILD_NUMBER} FAILED."
throw err
} finally {
deleteDir()
}
}
}
}
@NonCPS
def parseHealthCheck(response) {
def health = parseJsonText response.content
for (int i = 0; i < health.size; i++) {
if (health[i].CheckID == 'serfHealth') return health[i].Status;
}
}
/**
* Use serializable version of JsonSlurper that
* use response objects that are serializable.
* @param json text
* @return json objects
*/
@NonCPS
def parseJsonText(String json) {
new groovy.json.JsonSlurperClassic().parseText(json)
}
/**
* Checkout latest version of puppet scripts from GITHUB and run apply.sh
* @param server
* @return
*/
def puppetApply(server) {
print "Update ${server}"
sh "ssh -o StrictHostKeyChecking=no jenkins@${server} 'git --work-tree=/opt/puppet/pve --git-dir=/opt/puppet/pve/.git pull'"
sh "ssh -o StrictHostKeyChecking=no jenkins@${server} 'sudo /opt/puppet/pve/apply.sh'"
}