使用Groovy

时间:2016-09-20 23:24:44

标签: jenkins groovy amazon-ec2

我正在尝试通过Groovy配置Jenkins EC2-Plugin。 插件代码:https://github.com/jenkinsci/ec2-plugin

我正在尝试使用名称来设置云端

import hudson.model.*
import jenkins.model.*
import hudson.plugins.ec2.*
import com.amazonaws.services.ec2.model.*

SlaveTemplate awsTemplate = new SlaveTemplate(
  'ami-1234567',
  '',
  '',
  'sg-1234567',
  't2.micro',
  true,
  'foo',
  '',
  'stuff',
  'run code',
  '/var/tmp',
  'more code',
  '4',
  '',
  '',
  '',
  false,
  'subnet-1234567',
  '',
  '60',
  false,
  '',
  'iam-profile',
  false,
  false,
  '',
  false,
  '',
  true,
  false
)

def slaveTemplates = [awsTemplate]

def ec2Cloud = new AmazonEC2Cloud(
  'foo',
  true,
  '',
  'us-west-2',
  '',
  '10',
  slaveTemplates
)

def cloudList = Jenkins.instance.clouds
cloudList.add(ec2Cloud)

有什么想法吗?

这些是错误消息

Error: Could not find matching constructor for: hudson.plugins.ec2.SlaveTemplate

Error: Could not find matching constructor for: hudson.plugins.ec2.AmazonEC2Cloud

4 个答案:

答案 0 :(得分:4)

您对SlaveTemplate的调用与任何构造函数都不匹配:https://github.com/jenkinsci/ec2-plugin/blob/master/src/main/java/hudson/plugins/ec2/SlaveTemplate.java

例如,第三个参数(spotConfig)正在寻找SpotConfiguration个对象。您当前在此处定义了一个空字符串。 (您可以将其替换为null。)

此外,instance-type参数应为InstanceType,而不是字符串。要更正这一点,您需要将import com.amazonaws.services.ec2.model.InstanceType添加到groovy脚本的顶部,并将't2.micro'替换为InstanceType.fromValue('t2.micro')

有关完整示例,请参阅https://gist.github.com/vrivellino/97954495938e38421ba4504049fd44ea

答案 1 :(得分:1)

这是我修改现有云配置并添加新SlaveTemplate的方法:

Jenkins.instance.clouds.each {
    if (it.displayName == "SOME_NAME") {
        SlaveTemplate slaveTemplate = new SlaveTemplate(...)
        Field field = hudson.plugins.ec2.EC2Cloud.class.getDeclaredField("templates")
        field.setAccessible(true);
        List templates = (List) field.get(it)
        templates.add(slaveTemplate)
    }
}

答案 2 :(得分:0)

对于初学者,此类唯一可用的构造函数具有以下签名:

public AmazonEC2Cloud(
  String cloudName, 
  boolean useInstanceProfileForCredentials, 
  String credentialsId, 
  String region, 
  String privateKey, 
  String instanceCapStr, 
  List<? extends SlaveTemplate> templates
)

答案 3 :(得分:0)

在您的问题中,您似乎想使用Groovy在Jenkins中以编程方式配置ec2实例。

  

例如:为了在我的移动应用上运行自动化测试,我想创建ec2实例来构建apk / ipa,并克隆项目   使用appium / selenium框架进行测试,并在我的请求上运行测试   请求。 ec2将使用特定的AMI(其中包含   环境和几个框架)。

解决方案:SlaveTemplate对象是代表ec2实例的“ pojo”对象。您可以使用代码文档,其中提供了可以使用的构造函数,属性和公共方法,链接为here

您可以编辑ec2实例配置(使用设置方法)或您可以创建新的ec2实例(使用构造函数),我将附上示例:

为了编辑您的实例:

def instance = Jenkins.getInstance()
def ec2_instances = instance.clouds[0].getTemplates().each{
  if(it.labels == 'myInstanceLabelNameThatShouldBeChanged'){
    it.setAmi('ami-xxx')
    it.maxTotalUses=3
    it.launchTimeout=60000
  }
}

为了从头开始创建实例

使用SlaveTemplate构造函数并将其添加到您的clouds数组:

    public SlaveTemplate(String ami, String zone, SpotConfiguration spotConfig, String securityGroups, String remoteFS,
        InstanceType type, boolean ebsOptimized, String labelString, Node.Mode mode, String description, String initScript,
        String tmpDir, String userData, String numExecutors, String remoteAdmin, AMITypeData amiType, String jvmopts,
        boolean stopOnTerminate, String subnetId, List<EC2Tag> tags, String idleTerminationMinutes, int minimumNumberOfInstances,
        String instanceCapStr, String iamInstanceProfile, boolean deleteRootOnTermination,
        boolean useEphemeralDevices, boolean useDedicatedTenancy, String launchTimeoutStr, boolean associatePublicIp,
        String customDeviceMapping, boolean connectBySSHProcess, boolean monitoring,
        boolean t2Unlimited, ConnectionStrategy connectionStrategy, int maxTotalUses) {

示例,使用这个精彩的reference

import hudson.model.*
import jenkins.model.*
import hudson.plugins.ec2.*
import com.amazonaws.services.ec2.model.InstanceType

def instance = Jenkins.getInstance()

def ec2_cloud_name = 'exampleCloud'
def ec2_instance_cap = 5
def worker_description = 'jenkins-worker'
def worker_label_string = 'worker'
def ami_id = 'ami-xxx'
def security_groups = 'sg-123,sg-456'
def subnet_id = 'subnet-someSUbnet'
def instance_type = 'm2.xlarge'
def instance_profile_arn = 'arn:aws:iam::123:instance-profile/JenkinsInstanceProfile'
def number_of_executors = 5

def ec2_tags = [
  new EC2Tag('Name', 'jenkins-worker')
]

def priv_key_txt = ''''''

def worker_ami = new SlaveTemplate(
  // String ami
  ami_id,
  // String zone
  '',
  // SpotConfiguration spotConfig
  null,
  // String securityGroups
  security_groups,
  // String remoteFS
  '',
  // InstanceType type
  InstanceType.fromValue(instance_type),
  // boolean ebsOptimized
  false,
  // String labelString
  worker_label_string,
  // Node.Mode mode
  Node.Mode.NORMAL,
  // String description
  worker_description,
  // String initScript
  '',
  // String tmpDir
  '',
  // String userData
  '',
  // String numExecutors
  "${number_of_executors}",
  // String remoteAdmin
  '',
  // AMITypeData amiType
  new UnixData(null, null),
  // String jvmopts
  '',
  // boolean stopOnTerminate
  false,
  // String subnetId
  subnet_id,
  // List<EC2Tag> tags
  ec2_tags,
  // String idleTerminationMinutes
  '30',
  // boolean usePrivateDnsName
  true,
  // String instanceCapStr
  '50',
  // String iamInstanceProfile
  instance_profile_arn,
  // boolean useEphemeralDevices
  true,
  // boolean useDedicatedTenancy
  false,
  // String launchTimeoutStr
  '1800',
  // boolean associatePublicIp
  false,
  // String customDeviceMapping
  '',
  // boolean connectBySSHProcess
  false,
  // boolean connectUsingPublicIp
  false
)

def new_cloud = new AmazonEC2Cloud(
  // String cloudName
  ec2_cloud_name,
  // boolean useInstanceProfileForCredentials
  true,
  // String credentialsId
  '',
  // String region
  'us-east-1',
  // String privateKey
  priv_key_txt,
  // String instanceCapStr
  "${ec2_instance_cap}",
  // List<? extends SlaveTemplate> templates
  [worker_ami]
)

instance.clouds.add(new_cloud)