-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathssh_slave.groovy
108 lines (102 loc) · 2.81 KB
/
ssh_slave.groovy
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
/*
Anything that requires a connection to the jenkins api must run on an internal
slave. These are all currently CentOS. We can't add a sensible label like
internal as puppet removes them.
To ensure that Jenkins API operations are always executed on an internal slave
these functions allocate their own node block and run venv creation if
necessary.
*/
/* Connect a slave to the jenkins master
* Params: None
* Environment:
* - WORKSPACE
* Files:
* - playbooks/inventory/hosts
*/
def _connect(Map args){
common.conditionalStep(
step_name: "Connect Slave",
step: {
if ("ip" in args){
writeFile(
file: "ssh_slave_inventory",
text: """
[job_nodes]
${ip}
"""
)
args.inventory="ssh_slave_inventory"
}
if (!("inventory" in args)){
args.inventory = "inventory"
}
if (!("port" in args)){
args.port = 22
}
common.withRequestedCredentials("jenkins_ssh_privkey, jenkins_api_creds"){
dir("rpc-gating/playbooks"){
unstash args.inventory
common.venvPlaybook(
playbooks: ["setup_jenkins_slave.yml"],
args: [
"-i ${args.inventory}",
"--limit job_nodes",
"--extra-vars='ansible_port=${args.port}'",
"--private-key=\"${env.JENKINS_SSH_PRIVKEY}\""
]
) //venvPlaybook
} // dir
} // withRequestedCredentials
})
}
def connect(Map args){
if (env.STAGES == null){
env.STAGES="Connect Slave"
}
// scl is only available on centos slaves
// all centos slaves are internal
// :. scl avilable -> slave is internal
scl = sh(returnStatus: true,
script:"which scl")
if (scl == 0){
echo "Connect SSH Slave, already running on internal node, not allocating another."
// on internal / centos slave already
_connect(args)
} else {
// not on internal slave, so allocate node
echo "Connect SSH Slave, not running on internal node, allocating one before proceeding."
common.internal_slave(){
_connect(args)
}
}
}
/* Disconnect slave
* Reads global var: instance_name
*/
def destroy(slave_name){
common.conditionalStep(
step_name: 'Destroy Slave',
step: {
common.internal_slave(){
withCredentials([
usernamePassword(
credentialsId: "service_account_jenkins_api_creds",
usernameVariable: "JENKINS_USERNAME",
passwordVariable: "JENKINS_API_KEY"
)
]){
dir("rpc-gating/scripts"){
retry(5) {
sh """
set +x; . ${env.WORKSPACE}/.venv/bin/activate; set +x
python jenkins_node.py \
delete --name "${slave_name}"
"""
}
}
}
}
}
)
}
return this