forked from cloudbees/jenkins-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-controlled-agent.groovy
47 lines (45 loc) · 1.79 KB
/
create-controlled-agent.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
/*
* Basic script to create a controlled agent by setting properly the
* SecurityGrantsFolderProperty
*/
import hudson.model.*
import jenkins.model.*
import hudson.slaves.*
import com.cloudbees.hudson.plugins.folder.*
import com.cloudbees.jenkins.plugins.foldersplus.*
def parent=Jenkins.instance
Folder folder= Jenkins.instance.createProject(Folder.class, "Folder-A")
ComputerLauncher launcher = new hudson.plugins.sshslaves.SSHLauncher(
"host", // Host
22, // Port
"credentialsId", // Credentials
(String)null, // JVM Options
(String)null, // JavaPath
(String)null, // Prefix Start Slave Command
(String)null, // Suffix Start Slave Command
(Integer)null, // Connection Timeout in Seconds
(Integer)null, // Maximum Number of Retries
(Integer)null // The number of seconds to wait between retries
)
// Define a "Permanent Agent"
Slave node = new DumbSlave(
"agent-node-2",
"/home/jenkins",
launcher)
node.nodeDescription = "Agent node description"
node.numExecutors = 1
node.labelString = "agent-node-label"
node.mode = Node.Mode.NORMAL
node.retentionStrategy = new RetentionStrategy.Always()
// Create a "Permanent Agent"
Jenkins.instance.addNode(node)
node.getNodeProperties().add(new SecurityTokensNodeProperty(false));
SecurityToken token = SecurityToken.newInstance();
node.getNodeProperties().get(SecurityTokensNodeProperty.class).addSecurityToken(token);
node.save()
SecurityGrant request = SecurityGrant.newInstance();
SecurityGrant grant = token.issue(request);
folder.getProperties().replace(new SecurityGrantsFolderProperty(Collections.<SecurityGrant>emptyList()));
folder.getProperties().get(SecurityGrantsFolderProperty.class).addSecurityGrant(grant);
folder.save()
return "Node has been created successfully."