Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to programatically update the AMI ID using Groovy #154

Merged
merged 1 commit into from
Jul 5, 2015

Conversation

marvinpinto
Copy link
Contributor

This is a proposed fix for JENKINS-28268

Our Use Case

At FreshBooks, we have multiple Packer jobs that build new Jenkins build workers as and when the underlying repos are updated. The problem with this approach (currently) is that after this is done, someone needs to go in and manually update the Jenkins configuration to point to the new AMI ID.

Proposed Fix

With this approach, we could trigger a post-build Groovy script to automatically go in and update the relevant AMI ID without us having to do this manually.

Groovy Example

Here is an example Groovy script that demonstrates getting and setting the AMI ID programatically:

import jenkins.model.*;
import hudson.plugins.ec2.*;

Jenkins.instance.clouds.each {
  if (it.displayName == 'test-cloud') {
    it.getTemplates().each {
      println(it.displayName)
      println("Print current AMI id : " + it.getAmi())
      it.setAmi("ami-4567")
      println("Print current AMI id : " + it.getAmi())
    }
  }
}

Jenkins.instance.save()

Reference

https://issues.jenkins-ci.org/browse/JENKINS-28268

Local Test Results

Results :

Tests run: 54, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:19 min
[INFO] Finished at: 2015-07-05T11:57:36-04:00
[INFO] Final Memory: 39M/588M
[INFO] ------------------------------------------------------------------------

This is a proposed fix for JENKINS-28268

Our Use Case:

At FreshBooks, we have multiple Packer jobs that build new Jenkins build
workers as and when the underlying repos are updated. The problem with this
approach (currently) is that after this is done, someone needs to go in and
manually update the Jenkins configuration to point to the new AMI ID.

Proposed Fix:

With this approach, we could trigger a post-build Groovy script to
automatically go in and update the relevant AMI ID without us having to do this
manually.

Groovy Example:

Here is an example Groovy script that demonstrates getting and setting the AMI
ID programatically:

```
import jenkins.model.*;
import hudson.plugins.ec2.*;

Jenkins.instance.clouds.each {
  if (it.displayName == 'test-cloud') {
    it.getTemplates().each {
      println(it.displayName)
      println("Print current AMI id : " + it.getAmi())
      it.setAmi("ami-4567")
      println("Print current AMI id : " + it.getAmi())
    }
  }
}

Jenkins.instance.save()
```

Reference:

https://issues.jenkins-ci.org/browse/JENKINS-28268

[FIXED JENKINS-28268]
@buildhive
Copy link

Jenkins » ec2-plugin #123 SUCCESS
This pull request looks good
(what's this?)

@buildhive
Copy link

Jenkins » ec2-plugin #124 FAILURE
Looks like there's a problem with this pull request
(what's this?)

@jenkinsadmin
Copy link
Member

Thank you for a pull request! Please check this document for how the Jenkins project handles pull requests

@emoshaya
Copy link

emoshaya commented Jul 5, 2015

This solution fixes the exact same issue we're having at NewVoiceMedia where we have a number of CI Packer Jobs creating base AMI's for existing AMI templates in Jenkins. We're also having to manually update the configuration with the new AMI IDs created from the packer jobs. This will be a great addition hopefully to get merged soon.

Thanks

@fonsecas72
Copy link

+1 this would be great! thanks!

francisu added a commit that referenced this pull request Jul 5, 2015
Add the ability to programatically update the AMI ID using Groovy
@francisu francisu merged commit 236f876 into jenkinsci:master Jul 5, 2015
@marvinpinto marvinpinto deleted the update-ami-id branch July 5, 2015 21:09
@marvinpinto
Copy link
Contributor Author

Thank you for merging this so quickly! 😄

@francisu
Copy link
Member

francisu commented Jul 5, 2015

Thanks for your contribution.

Sent from my iPhone

On Jul 5, 2015, at 17:22, Marvin Pinto notifications@github.com wrote:

Thank you for merging this so quickly!


Reply to this email directly or view it on GitHub.

@lielran
Copy link

lielran commented Jul 6, 2015

+1

At Kenshoo we update the ami manually(After a packer job that built the slaves) till now.

Thanks for this PR!

@marvinpinto
Copy link
Contributor Author

If this is helpful to anyone else, this is the python script we use to cleanup and update the AMI IDs.

https://gist.github.com/marvinpinto/a6c9b5119d418a65d489

@fonsecas72
Copy link

Thanks again @marvinpinto 👍

@ghost
Copy link

ghost commented Aug 2, 2015

@francisu is there a release planned soon ?
this feature would help us a lot

@francisu
Copy link
Member

francisu commented Aug 3, 2015

Just did the release, should be available in a day or so.

@ghost
Copy link

ghost commented Aug 3, 2015

Got it, thank you!

@burukuru
Copy link

The script is a bit out of date and didn't work for me so I updated it and wrote up some instructions here https://github.com/burukuru/jenkins-update-ec2-ami

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants