|
| 1 | +/* |
| 2 | +Author: kuisathaverat |
| 3 | +configure Mesos Cloud from a groovy script |
| 4 | +*/ |
| 5 | +import org.jenkinsci.plugins.mesos.MesosCloud; |
| 6 | +import org.jenkinsci.plugins.mesos.MesosSlaveInfo; |
| 7 | +import org.apache.mesos.Protos; |
| 8 | +import hudson.slaves.NodeProperty; |
| 9 | + |
| 10 | +def createMesosCloud( |
| 11 | + nativeLibraryPath, |
| 12 | + master, |
| 13 | + description, |
| 14 | + frameworkName, |
| 15 | + role, |
| 16 | + slavesUser, |
| 17 | + credentialsId, |
| 18 | + slaveInfos, |
| 19 | + checkpoint, |
| 20 | + onDemandRegistration, |
| 21 | + jenkinsURL, |
| 22 | + declineOfferDuration, |
| 23 | + cloudID) { |
| 24 | + return new MesosCloud( |
| 25 | + nativeLibraryPath, |
| 26 | + master, |
| 27 | + description, |
| 28 | + frameworkName, |
| 29 | + role, |
| 30 | + slavesUser, |
| 31 | + credentialsId, |
| 32 | + null, |
| 33 | + null, |
| 34 | + slaveInfos, |
| 35 | + checkpoint, |
| 36 | + onDemandRegistration, |
| 37 | + jenkinsURL, |
| 38 | + declineOfferDuration, |
| 39 | + cloudID) |
| 40 | +} |
| 41 | + |
| 42 | +def createMesosSlaveInfo( |
| 43 | + labelString, mode, slaveCpus, slaveMem, |
| 44 | + minExecutors, maxExecutors, executorCpus, executorMem, |
| 45 | + remoteFSRoot, idleTerminationMinutes, slaveAttributes, |
| 46 | + jvmArgs, jnlpArgs, defaultSlave, |
| 47 | + containerInfo, additionalURIs, nodeProperties){ |
| 48 | + return new MesosSlaveInfo(labelString, mode, slaveCpus, slaveMem, minExecutors, |
| 49 | + maxExecutors, executorCpus, executorMem, remoteFSRoot, idleTerminationMinutes, |
| 50 | + slaveAttributes, jvmArgs, jnlpArgs, defaultSlave, containerInfo, additionalURIs, |
| 51 | + nodeProperties) |
| 52 | +} |
| 53 | + |
| 54 | +def createContainerInfo( |
| 55 | + type, dockerImage, dockerPrivilegedMode, dockerForcePullImage, |
| 56 | + dockerImageCustomizable, useCustomDockerCommandShell, customDockerCommandShell, |
| 57 | + volumes, parameters, networking, portMappings, networkInfos){ |
| 58 | + return new MesosSlaveInfo.ContainerInfo(type, dockerImage,dockerPrivilegedMode, |
| 59 | + dockerForcePullImage,dockerImageCustomizable, useCustomDockerCommandShell, |
| 60 | + customDockerCommandShell,volumes,parameters, networking,portMappings,networkInfos) |
| 61 | +} |
| 62 | + |
| 63 | +def createPortMapping(containerPort, hostPort, protocol){ |
| 64 | + return new MesosSlaveInfo.PortMapping(containerPort,hostPort,protocol) |
| 65 | +} |
| 66 | + |
| 67 | +def createNetworkInfo(networkName){ |
| 68 | + return new MesosSlaveInfo.NetworkInfo(networkName) |
| 69 | +} |
| 70 | + |
| 71 | +def createVolume(containerPath, hostPath, readOnly){ |
| 72 | + return new MesosSlaveInfo.Volume(containerPath,hostPath,readOnly) |
| 73 | +} |
| 74 | + |
| 75 | +def createParameter(key, value){ |
| 76 | + return new MesosSlaveInfo.Parameter(key, value) |
| 77 | +} |
| 78 | + |
| 79 | +//configured |
| 80 | +MesosCloud cloud = MesosCloud.get(); |
| 81 | + |
| 82 | +//new cloud from scratch |
| 83 | +/* |
| 84 | +def cloud = createMesosCloud('/native/Library/Path', |
| 85 | + '127.0.0.1:8080', |
| 86 | + 'description', |
| 87 | + 'Jenkins Scheduler', |
| 88 | + '*', |
| 89 | + 'slavesUser', |
| 90 | + 'credentialsId', |
| 91 | + null, |
| 92 | + true, |
| 93 | + false, |
| 94 | + Jenkins.getInstance().getRootUrl(), |
| 95 | + '600000', |
| 96 | + null) |
| 97 | +
|
| 98 | +Jenkins.instance.clouds.add(cloud) |
| 99 | +*/ |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +if (cloud != null) { |
| 104 | + def volumes = new LinkedList<MesosSlaveInfo.Volume>() |
| 105 | + def parameters = new LinkedList<MesosSlaveInfo.Parameter>() |
| 106 | + def portMappings = new LinkedList<MesosSlaveInfo.PortMapping>() |
| 107 | + def networkInfos = new LinkedList<MesosSlaveInfo.NetworkInfo>() |
| 108 | + |
| 109 | + def nodeProperties = new LinkedList<? extends NodeProperty<?>>() |
| 110 | + def additionalURIs = new LinkedList<URI>() |
| 111 | + |
| 112 | + volumes.add(createVolume('/mnt/folder','/mnt',false)) |
| 113 | + parameters.add(createParameter('key','value')) |
| 114 | + portMappings.add(createPortMapping(8080,80,'http')) |
| 115 | + networkInfos.add(createNetworkInfo('sampleNet')) |
| 116 | + |
| 117 | + def containerInfo = createContainerInfo("", |
| 118 | + "organization/image:version", |
| 119 | + false, |
| 120 | + false, |
| 121 | + false, |
| 122 | + false, |
| 123 | + "", |
| 124 | + volumes, |
| 125 | + parameters, |
| 126 | + Protos.ContainerInfo.DockerInfo.Network.BRIDGE.name(), |
| 127 | + portMappings, |
| 128 | + networkInfos) |
| 129 | + |
| 130 | + def mesosSlaveInfo = createMesosSlaveInfo('labelString', |
| 131 | + Node.Mode.NORMAL, |
| 132 | + '0.1', |
| 133 | + '512', |
| 134 | + '1', |
| 135 | + '2', |
| 136 | + '0.1', |
| 137 | + '128', |
| 138 | + '/jenkins', |
| 139 | + '3', |
| 140 | + '', |
| 141 | + '-Xms16m -XX:+UseConcMarkSweepGC -Djava.net.preferIPv4Stack=true', |
| 142 | + '-noReconnect', |
| 143 | + 'false', |
| 144 | + containerInfo, |
| 145 | + additionalURIs, |
| 146 | + nodeProperties) |
| 147 | + |
| 148 | + cloud.getSlaveInfos().add(mesosSlaveInfo) |
| 149 | + cloud.getSlaveInfos().each { |
| 150 | + t -> |
| 151 | + println t |
| 152 | + } |
| 153 | +} |
0 commit comments