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

Ability to modify SlaveTemplates programmatically

      What is the recommended way to modify list of templates in AmazonEC2Cloud instance?

      Because the `templates` field is private, there is no easy way to load and modify the could configuration using Jenkins Groovy scripts.

      One way is to use reflection to access the field and modify the list (from https://stackoverflow.com/a/49042851/1538920):

       

      import hudson.model.*
      import hudson.plugins.ec2.EC2Cloud
      import hudson.plugins.ec2.SlaveTemplate
      import jenkins.model.Jenkins
      import java.lang.reflect.Field
      
      def jenkins = Jenkins.get()
      def newTemplate = new SlaveTemplate(...)
      def cloud = jenkins.clouds.getByName('ec2-cloud')
      Field field = EC2Cloud.class.getDeclaredField('templates')
      field.setAccessible(true)
      def templates = (List) field.get(cloud)
      templates.add(newTemplate)
      jenkins.save() 

       

      The other would be to create a full copy of the AmazonEC2Cloud and remove the old one.

      Is there a better way to do that and if not, could it be implemented in the plugin?

          [JENKINS-71002] Ability to modify SlaveTemplates programmatically

          Antonio added a comment -

          Hey,

          Regarding addition of new SlaveTemplate objects to a cloud, I've implemented a new method inside EC2Cloud, which would allow this feature. I am having trouble opening a PR at the moment but if I manage to do so I will leave a comment here with a link to it.

          Cheers,
          Antonio

          Antonio added a comment - Hey, Regarding addition of new SlaveTemplate objects to a cloud, I've implemented a new method inside EC2Cloud, which would allow this feature. I am having trouble opening a PR at the moment but if I manage to do so I will leave a comment here with a link to it. Cheers, Antonio

            thoulen FABRIZIO MANFREDI
            piomar123 Piotr MarciƄczyk
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: