How to create a Mesos Managed controller programmatically

Article ID:360035365271
2 minute readKnowledge base

Issue

  • How to create a Mesos Managed controller programmatically.

Resolution

Create Using Groovy

The easiest approach would be by using a Groovy script.

See createManagedcontrollerMesos.groovy. This script can be modified as needed.

If you need access to the source code then please see Custom Plugins: APIs and Javadocs of CloudBees Jenkins Enterprise plugins

The classes used in the provided script are coming from the operations-center-server plugin.

You can find your version of the plugin by looking at the Plugin Manager on the CJOC instance and searching for operations-center-server under the Installed tab.

Create via the Script Console

You can execute the groovy script using the Script Console under Managed Jenkins  Script Console.

Create via the Jenkins CLI

You can execute a groovy script via the groovy command and passing the script to the standard input:

java -jar jenkins-cli.jar \
    -auth $USER:$TOKEN \
    -s $CJOC_URL/ \
    groovy = < createManagedcontrollerMesos.groovy
Create via the REST API

You can execute a groovy script via the /scriptText endpoint and pass the script via a bash command:

curl -v -XPOST \
    -u $USER:$TOKEN \
    --data-urlencode "script=$(<./createManagedcontrollerMesos.groovy)" \
    "$CJOC_URL/scriptText"

See Script Console - Remote Access for more details.

Create remotely using config files

You can also create a Managed controller remotely, by either using the REST API, or by using the Jenkins CLI to create the job.

This approach is a bit cumbersome, as some fields are auto-generated (id, idName, grantId, identity, encodedName) and must be unique. This means that several calls must be made to adjust a controller configuration.

You would need to do the following:

  • Create a job

  • Get the job config file

  • Update the config file (preserve auto-generated fields)

  • Post the updated config file

Create via the Jenkins CLI

You need to create a config.xml to pass to the create-job command. The following is an example:

java -jar jenkins-cli.jar \
    -auth $USER:$TOKEN \
    -s $CJOC_URL/ \
    create-job $MASTER_NAME < my-mesos-managed-controller.xml

java -jar jenkins-cli.jar \
    -auth $USER:$TOKEN \
    -s $CJOC_URL/ \
    mesos-managed-controller-provision-and-start $MASTER_NAME
Create via the REST API

You need to pass a config.xml to the /createItem endpoint. The following is an example:

curl -v -XPOST \
    -u $USER:$TOKEN \
    -H "Content-Type:text/xml" \
    --data-binary @my-mesos-managed-controller.xml \
    "$CJOC_URL/createItem?name=$MASTER_NAME"

curl -v -XPOST \
    -u $USER:$TOKEN \
    "$CJOC_URL/job/$MASTER_NAME/provisionAndStartAction"