-
Notifications
You must be signed in to change notification settings - Fork 11
/
MacSlave.groovy
153 lines (134 loc) · 4.23 KB
/
MacSlave.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
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
package fr.edf.jenkins.plugins.mac.slave;
import java.util.concurrent.atomic.AtomicBoolean
import java.util.logging.Level
import java.util.logging.Logger
import org.jenkinsci.plugins.durabletask.executors.OnceRetentionStrategy
import org.kohsuke.accmod.Restricted
import org.kohsuke.accmod.restrictions.NoExternalUse
import fr.edf.jenkins.plugins.mac.MacCloud
import fr.edf.jenkins.plugins.mac.MacHost
import fr.edf.jenkins.plugins.mac.MacUser
import fr.edf.jenkins.plugins.mac.cause.MacOfflineCause
import fr.edf.jenkins.plugins.mac.ssh.SSHCommand
import hudson.Extension
import hudson.model.Computer
import hudson.model.Slave
import hudson.model.TaskListener
import hudson.model.Node.Mode
import hudson.model.Slave.SlaveDescriptor
import hudson.slaves.AbstractCloudSlave
import hudson.slaves.Cloud
import hudson.slaves.ComputerLauncher
import hudson.slaves.RetentionStrategy
import jenkins.model.Jenkins
class MacSlave extends AbstractCloudSlave {
private static final Logger LOGGER = Logger.getLogger(MacSlave.name)
final String cloudId
final MacHost macHost
AtomicBoolean acceptingTasks = new AtomicBoolean(true)
MacSlave(String cloudId, String labels, MacUser user, MacHost macHost, ComputerLauncher launcher, Integer idleMinutes) {
super(
user.username,
"Agent Mac for the user " + user.username,
user.workdir,
1,
Mode.EXCLUSIVE,
labels,
launcher,
buildRetentionStrategy(idleMinutes),
Collections.EMPTY_LIST
)
this.cloudId = cloudId
this.macHost = macHost
setUserId(user.username)
}
/**
* Return the retention strategy used for this MacSlave
* @param idleMinutes
* @return OnceRetentionStrategy
*/
private static RetentionStrategy buildRetentionStrategy(Integer idleMinutes) {
return new OnceRetentionStrategy(idleMinutes.intValue())
}
/**
* {@inheritDoc}
*/
@Override
boolean isAcceptingTasks() {
return acceptingTasks == null || acceptingTasks.get()
}
/**
* {@inheritDoc}
*/
@Override
String getDisplayName() {
if (cloudId != null) {
return getNodeName() + " on " + cloudId;
}
return getNodeName()
}
/**
* {@inheritDoc}
*/
@Override
MacComputer createComputer() {
return MacComputerFactory.createInstance(this)
}
/**
* {@inheritDoc}
*/
@Override
@Restricted(NoExternalUse)
void _terminate(final TaskListener listener) {
try {
final Computer computer = toComputer()
if (computer != null) {
computer.disconnect(new MacOfflineCause())
LOGGER.log(Level.FINE, "Disconnected computer for node {0}.", name)
}
} catch (Exception e) {
String message = String.format("Can't disconnect computer for node %s", name)
LOGGER.log(Level.SEVERE, message, e)
listener.error(message)
}
try {
SSHCommand.deleteUserOnMac(this.name, this.macHost)
} catch (Exception e) {
String message = String.format("Failed to remove user %s on mac %s due to exception : %s", this.name, this.macHost.host, e.message)
LOGGER.log(Level.SEVERE, message, e)
listener.fatalError(message)
}
}
/**
* Retrieve the cloud attached to this MacSlave
* @return MacCloud
*/
public MacCloud getCloud() {
if (cloudId == null) return null;
final Cloud cloud = Jenkins.get().getCloud(cloudId)
if (cloud == null) {
throw new RuntimeException("Failed to retrieve Cloud " + cloudId)
}
if (!(cloud instanceof MacCloud)) {
throw new RuntimeException(cloudId + " is not a MacCloud, it's a " + cloud.getClass().toString())
}
return (MacCloud) cloud
}
@Extension
static final class MacSlaveDescriptor extends SlaveDescriptor {
/**
* {@inheritDoc}
*/
@Override
String getDisplayName() {
return "Mac Agent"
}
/**
* {@inheritDoc}
*/
@Override
boolean isInstantiable() {
return false
}
}
}