· 7 years ago · Apr 06, 2018, 01:30 PM
1import hudson.model.*;
2import jenkins.model.*;
3import hudson.plugins.ec2.*;
4import com.amazonaws.services.ec2.model.InstanceType
5import com.amazonaws.services.ec2.model.KeyPair
6
7if ( Jenkins.instance.pluginManager.activePlugins.find { it.shortName == "ec2" } != null ) {
8 println "--> setting ec2 plugin"
9
10EC2Cloud cloud = Jenkins.instance.clouds.find { it instanceof EC2Cloud }
11KeyPair key_pair= cloud.getKeyPair()
12private_key_text = key_pair.keyMaterial
13def secret_key = hudson.util.Secret.decrypt(cloud.getSecretKey()).toString()
14
15 ///////////////// GLOBAL SETTINGS ///////////////////////////////////////////
16 // should use the same tag for all slave templates
17 def ec2Tags = [
18 new EC2Tag('Name', 'jenkins-builder.elastic.us-west-2a'),
19 new EC2Tag('created_by', '<%= node['fqdn'] %>'), // master node
20 new EC2Tag('Service', 'jenkins'),
21 new EC2Tag('Team', 'releng'),
22 new EC2Tag('Stage', 'prod')
23 ] as List
24 UnixData unixData = new UnixData(null, '22') // linux box
25
26 ////////////////////// SLAVE INSTANCE TEMPLATES /////////////////////////////
27 SlaveTemplate awsTemplate = new SlaveTemplate(
28 'ami-37e7af07', // ami
29 'us-west-2a', // zone
30 null, // spotconfiguration
31 'corp, jenkins', // security groups
32 '/home/jenkins/slave-root', // remote fs
33 InstanceType.M3Large, // instance type
34 'aws', // jenkins label
35 hudson.model.Node.Mode.NORMAL, // hudson.model.Node.Mode
36 'aws builder us-west-2a', // description
37 """#!/bin/bash
38
39source /usr/local/lib/bob/rvm_s3.sh || true
40downloadRvmRubiesS3 || true""", // init script
41 '', // userdata
42 '1', // num executors
43 'jenkins', // remote admin user
44 unixData, // unix or windows (hudson.plugins.ec2.AMITypeData)
45 '', // slave jvmopts
46 true, // stop on terminate?
47 'subnet-cxxxxxxx', // subnet id
48 ec2Tags, // ec2 tags
49 '-5', // idle termination minutes
50 false, // use private dns name?
51 '200', // instance cap per ami
52 '', // IAM instance profile
53 false, // use ephemeral devices?
54 false, // use dedicated tenancy?
55 '1200', // launch timeout
56 false, // associate public ip?
57 '' // custom device mapping?
58 )
59 // a list of slave templates
60 def slaveTemplates = [awsTemplate]
61
62 ////////////////////////////// EC2 CLOUDs ///////////////////////////////////
63 def ec2Cloud = new AmazonEC2Cloud(
64 'SAMPLEID', // access id
65 secret_key, // secret key
66 'us-west-1', // region
67 private_key_text, // private key
68 '500', // instance cap
69 slaveTemplates // list of slave templates
70 )
71
72 //////////////////////////// ADDING EC2 CLOUDS //////////////////////////////
73 def cloudList = Jenkins.instance.clouds
74
75 // avoid duplicate cloud provider on the cloud list
76 if ( cloudList.getByName(ec2Cloud.name) ) {
77 cloudList.remove(cloudList.getByName(ec2Cloud.name))
78 }
79 cloudList.add(ec2Cloud)
80}