Issue
- I want to programmatically instantiate and connect a CloudBees Jenkins Enterprise Client Master to a CloudBees Jenkins Operations Center (CJOC) server. This is typically needed when creating Client Masters using Chef, Puppet, Ansible, AWS Cloud Formation or Azure ARM Templates.
WARNING: API subject to change or replacement in the future.
Environment
- CloudBees CI (CloudBees Core)
- CloudBees CI (CloudBees Core) on modern cloud platforms - Operations Center
- CloudBees CI (CloudBees Core) on traditional platforms - Client Master
- CloudBees CI (CloudBees Core) on traditional platforms - Operations Center
- CloudBees Jenkins Enterprise - Operations Center
- CloudBees Jenkins Platform - Client Master
- CloudBees Jenkins Platform - Operations Center
Resolution
We can programmatically connect a Client Master to CJOC. To do so, we have to do the following - preferably in that order:
- Execute a Groovy script on CJOC to declare the Client Master
- Start/Restart the Client Master with specific Java system properties in the startup parameters
On the sample below, we will use the following values:
- CJOC Server
- URL:
https://operations-center.jenkins.example.com
- URL:
- Client Master to create
- Name:
my-client-master
- This name is visible in the user interface of CloudBees Jenkins Platform
- Id:
5
- Arbitrary integer
- This internal identifier is not visible
- The identifier
clientMaster.name + '-' + clientMaster.id
- also known asidName
- must be unique.
- Grant Id:
fjs2ktwfgd
- Arbitrary secret shared between Operations Center and the Client Master to establish the connection. This secret is no longer used once the connection is established.
- Using a random value is recommended
- Name:
1. Declare the Client Master on CJOC
Run a Groovy script on CJOC to create a Client Master.
Create via Groovy
We can create a Client Master by only providing its name and print the values we need:
import com.cloudbees.opscenter.server.model.ClientMaster
import com.cloudbees.opscenter.server.model.OperationsCenter
import com.cloudbees.opscenter.server.properties.ConnectedMasterLicenseServerProperty
def clientMasterName = "my-client-master"
// Create Client Master Declaration
ClientMaster clientMaster = OperationsCenter.instance.createClientMaster(clientMasterName)
// Set the licensing strategy to its default by passing 'null'
clientMaster.properties.replace(new ConnectedMasterLicenseServerProperty(null))
clientMaster.save()
// Another way to create a ClientMaster is as follow
//ClientMaster cm = Jenkins.instance.createProject(ClientMaster.class, clientMasterName)
if (OperationsCenter.instance.getConnectedMasterByName(clientMaster.idName)!=null){
println "Created ClientMaster '${clientMaster.name}' known as '${clientMaster.idName}'"
println "-DMASTER_INDEX=${clientMaster.id}'"
println "-DMASTER_NAME=${clientMaster.name}'"
println "-DMASTER_GRANT_ID=${clientMaster.grantId}'"
} else {
println "[ERROR:]" + clientMasterName + "has not been created in CJOC"
}
return
Or we can declare a Client Master and set properties explicitly:
// Imports
import com.cloudbees.opscenter.server.model.ClientMaster
import com.cloudbees.opscenter.server.model.ConnectedMaster
import com.cloudbees.opscenter.server.model.OperationsCenter
import com.cloudbees.opscenter.server.properties.ConnectedMasterLicenseServerProperty
import jenkins.model.Jenkins
// Input parameters
def clientMasterName = "my-client-master"
def clientMasterId = 5
def clientMasterGrantId = "fjs2ktwfgd"
// Create Client Master Declaration
ClientMaster cm = OperationsCenter.instance.createClientMaster(clientMasterName)
// Another way to create a ClientMaster is as follow
//ClientMaster cm = Jenkins.instance.createProject(ClientMaster.class, clientMasterName)
//Set Client Master properties
cm.setId(clientMasterId)
cm.setIdName(ConnectedMaster.createIdName(clientMasterId, clientMasterName))
cm.setGrantId(clientMasterGrantId)
// Set the licensing strategy to its default by passing 'null'
cm.properties.replace(new ConnectedMasterLicenseServerProperty(null))
cm.save()
if (OperationsCenter.instance.getConnectedMasterByName(cm.idName)!=null){
println "Created ClientMaster '${cm.name}' known as '${cm.idName}'"
println "-DMASTER_INDEX=${cm.id}'"
println "-DMASTER_NAME=${cm.name}'"
println "-DMASTER_GRANT_ID=${cm.grantId}'"
} else {
println "[ERROR:]" + clientMasterName + "has not been created in CJOC"
}
return
2. Add CJOC connection details in the startup parameters of the Client Master
Add CJOC connection details in the JVM startup parameters of the Client Master:
-DMASTER_ENDPOINT="https://my-client-master.jenkins.example.com"
-DMASTER_OPERATIONSCENTER_ENDPOINT="https://operations-center.jenkins.example.com"
-DMASTER_INDEX="5"
-DMASTER_NAME="my-client-master"
-DMASTER_GRANT_ID="fjs2ktwfgd"
We can use the optional startup parameter -DMASTER_OPERATIONSCENTER_CERTS
to define the SSL Certificate used by the HTTP endpoint of CJOC if the endpoint uses HTTPS and if the SSL certificate is not trusted by the JVM SSL trust store.
Troubleshooting
The following groovy provides information about the client masters (id
, idName
, grantId
) defined in the CJOC. It must therefore be run on the CJOC:
import com.cloudbees.opscenter.server.model.OperationsCenter
OperationsCenter.instance.getConnectedMasters().each {
println "${it.name}"
println " id: ${it.id}"
println " idName: ${it.idName}"
println " grantId: ${it.grantId}"
}
return
4 Comments