Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-51758

Register new vsphere template under vsphere plugin via groovy/jenkinsfile

    Details

    • Similar Issues:

      Description

      I have a process that creates new vsphere vm template in vsphere. These vm template represent a "newer" version of a jenkins builder profile. I use the vsphere plugin to dynamically create new builder. Is there a way to register a new template profile under vsphere plugin via groovy/jenkinsfile ? 

        Attachments

          Activity

          Hide
          aszostak Artur Szostak added a comment - - edited

          You can do this in a groovy script run within the Jenkins master. You will need to write the code to create new vSphereCloudSlaveTemplate objects and add them to the correct cloud configuration object in the Jenkins instance, e.g. the following pseudo code:

          def tmpl = new vSphereCloudSlaveTemplate( ... /* Many parameters to fill, see the source code docs. */ ... )
          
          def clouds = Jenkins.getInstance().clouds
          def cloud = findVsphereCloudObject(clouds)
          if (cloud.getTemplates().size() == 0) {
              // If the cloud had no templates set, then we have to recreate the cloud
              // with the new template. This has to be done this way because we
              // otherwise get a Java exception if adding to an empty list returned by
              // getTemplates().
              def cloud_name = 'some_cloud_name'
              def max_build_nodes = cloud.getMaxOnlineSlaves()
              def max_instances = cloud.getInstanceCap()
              def con = cloud.getVsConnectionConfig()
              clouds.remove(cloud)
              cloud = new vSphereCloud(con, cloud_name, max_build_nodes, max_instances, [tmpl])
              clouds.add(cloud)
          } else {
              cloud.getTemplates().add(tmpl)
          }
          
          Jenkins.getInstance().save()
          
          Show
          aszostak Artur Szostak added a comment - - edited You can do this in a groovy script run within the Jenkins master. You will need to write the code to create new vSphereCloudSlaveTemplate objects and add them to the correct cloud configuration object in the Jenkins instance, e.g. the following pseudo code: def tmpl = new vSphereCloudSlaveTemplate( ... /* Many parameters to fill, see the source code docs. */ ... ) def clouds = Jenkins.getInstance().clouds def cloud = findVsphereCloudObject(clouds) if (cloud.getTemplates().size() == 0) { // If the cloud had no templates set, then we have to recreate the cloud // with the new template. This has to be done this way because we // otherwise get a Java exception if adding to an empty list returned by // getTemplates(). def cloud_name = 'some_cloud_name' def max_build_nodes = cloud.getMaxOnlineSlaves() def max_instances = cloud.getInstanceCap() def con = cloud.getVsConnectionConfig() clouds.remove(cloud) cloud = new vSphereCloud(con, cloud_name, max_build_nodes, max_instances, [tmpl]) clouds.add(cloud) } else { cloud.getTemplates().add(tmpl) } Jenkins.getInstance().save()
          Hide
          gmostack Guillermo Menjivar added a comment -

          Wow thanks alot I will try this!

          Show
          gmostack Guillermo Menjivar added a comment - Wow thanks alot I will try this!

            People

            • Assignee:
              Unassigned
              Reporter:
              gmostack Guillermo Menjivar
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: