Ability to modify SlaveTemplates programmatically

This issue is archived. You can view it, but you can't modify it. Learn more

XMLWordPrintable

      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?

            Assignee:
            FABRIZIO MANFREDI
            Reporter:
            Piotr Marcińczyk
            Archiver:
            Jenkins Service Account

              Created:
              Updated:
              Archived: