Configure amazon-ecs slave plugin using Groovy on Jenkins
amazon-ecs, groovy, jenkins
Solution
So I made some headway on this. It's not idempotent, but it works. The code was tailored for my use case, but shouldn't be too hard for you to tweak for your own.
import java.util.Arrays
import java.util.logging.Logger
Logger logger = Logger.getLogger("ecs-cluster")
logger.info("Loading Jenkins")
import jenkins.model.*
instance = Jenkins.getInstance()
import com.cloudbees.jenkins.plugins.amazonecs.ECSTaskTemplate.MountPointEntry
def mounts = Arrays.asList(
new MountPointEntry(
name="docker",
sourcePath="/var/run/docker.sock",
containerPath="/var/run/docker.sock",
readOnly=false),
new MountPointEntry(
name="jenkins",
sourcePath="/home/jenkins",
containerPath="/home/jenkins",
readOnly=false),
)
logger.info("Creating template")
import com.cloudbees.jenkins.plugins.amazonecs.ECSTaskTemplate
def ecsTemplate = new ECSTaskTemplate(
templateName="jnlp-slave-with-docker",
label="ecs-with-docker",
image="jnlp-slave-with-docker:latest",
remoteFSRoot=null,
memory=2048,
cpu=512,
privileged=false,
logDriverOptions=null,
environments=null,
extraHosts=null,
mountPoints=mounts
)
logger.info("Retrieving ecs cloud config by descriptor")
import com.cloudbees.jenkins.plugins.amazonecs.ECSCloud
ecsCloud = new ECSCloud(
name="name",
templates=Arrays.asList(ecsTemplate),
credentialsId=null,
cluster="arn:aws:ecs:us-east-1:123456789:cluster/ecs-jenkins-slave",
regionName="us-east-1",
jenkinsUrl="https://my-jenkins.com",
slaveTimoutInSeconds=60
)
logger.info("Gettings clouds")
def clouds = instance.clouds
clouds.add(ecsCloud)
logger.info("Saving jenkins")
instance.save()
Problem
I'm trying to configure amazon-ecs-plugin for Jenkins using `init.groovy` script, but couldn't find and docs on it. I'm new to groovy based configuration automation Tried to get all the properties using ``` import jenkins.model.* import com.cloudbees.jenkins.plugins.amazonecs.* ECSCloud.metaClass.properties.each {println it.name+":\t"+it.type } ``` The Output: ``` regionName: class java.lang.String searchName: class java.lang.String slaveTimoutInSeconds: int searchIndex: interface hudson.search.SearchIndex ACL: class hudson.security.ACL descriptor: class hudson.model.Descriptor credentialsId: class java.lang.String search: class hudson.search.Search ecsService: class com.cloudbees.jenkins.plugins.amazonecs.ECSService class: class java.lang.Class searchUrl: class java.lang.String tunnel: class java.lang.String templates: interface java.util.List cluster: class java.lang.String jenkinsUrl: class java.lang.String amazonECSClient: class com.amazonaws.services.ecs.AmazonECSClient displayName: class java.lang.String ``` But, not sure how to proceed with sub-classes like `ecsService: class com.cloudbees.jenkins.plugins.amazonecs.ECSService` not sure, how to define that property ``` def ecs-cloud = new ECSCloud( regionName="String" ecsService="<NOT SURE HOT TO CONFIGURE THIS>" ...... ) ``` The `.xml` file after a manual configuration look like ``` <clouds> <com.cloudbees.jenkins.plugins.amazonecs.ECSCloud plugin="scalable-amazon-ecs@1.0"> <name>ECS-CLUSTER-NAME</name> <templates> <com.cloudbees.jenkins.plugins.amazonecs.ECSTaskTemplate> <label>jnlp-slave</label> <image>jenkinsci/jnlp-slave</image> <remoteFSRoot>/home/jenkins</remoteFSRoot> <memory>800</memory> <cpu>800</cpu> <privileged>false</privileged> <taskDefinitionArn>TASK-DEF-ARN</taskDefinitionArn> </com.cloudbees.jenkins.plugins.amazonecs.ECSTaskTemplate> </templates> <credentialsId></credentialsId> <cluster>arn:aws:ecs:REGION:ACCOUNTID:cluster/ECS-CLUSTER-NAME</cluster> <regionName>REGION</regionName> <tunnel></tunnel> <jenkinsUrl>JENKINS-URL</jenkinsUrl> <slaveTimoutInSeconds>900</slaveTimoutInSeconds> <ecsService> <credentialsId></credentialsId> <regionName>REGION</regionName> </ecsService> </com.cloudbees.jenkins.plugins.amazonecs.ECSCloud> </clouds> ``` Thanks in advance. Update Configure Jenkins EC2-Plugin with Groovy Similar question for using EC2 Plugin.