Issue
I'm trying to setup a Jenkins server with EC2 plugin such that all builds are executed by the EC2 instances, which act as agents for the master server.
While trying to save the cloud configuration for 'Amazon EC2' cloud in the Jenkins master, it fails with an exception. Checking in the Jenkins logs, I see this exception:
Caught unhandled exception with ID f6d45d51-fb00-4d1c-a474-0a55dd5ee710
org.kohsuke.stapler.WrongTypeException: Got type array but no lister class found for type class java.lang.String
at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:724)
at org.kohsuke.stapler.RequestImpl.bindJSON(RequestImpl.java:478)
at org.kohsuke.stapler.RequestImpl.instantiate(RequestImpl.java:787)
Caused: java.lang.IllegalArgumentException: Failed to convert the instanceCapStr parameter of
the constructor public hudson.plugins.ec2.AmazonEC2Cloud
(java.lang.String,boolean,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.util.List,java.lang.String,java.lang.String)
at org.kohsuke.stapler.RequestImpl.instantiate(RequestImpl.java:789)
at org.kohsuke.stapler.RequestImpl.access$200(RequestImpl.java:83)
at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:678)
Caused: java.lang.IllegalArgumentException: Failed to instantiate class hudson.plugins.ec2.AmazonEC2Cloud from
{
"cloudName":"ec2-cloud",
"includeUser":["false","false"],
"credentialsId":"",
"useInstanceProfileForCredentials":true,
"altEC2Endpoint":"",
"region":"eu-west-1",
"sshKeysCredentialsId":"jenkins-slave-ssh-key",
"instanceCapStr":["",""],
"noDelayProvisioning":false,
"roleArn":"",
"roleSessionName":"",
"templates":{
"description":"Amazon Linux 2 AMI",
"ami":"ami-0bb3fad3c0286ebd5",
"type":"T2Micro",
"ebsOptimized":true,
"monitoring":true,
"t2Unlimited":false,
"zone":"",
"securityGroups":"",
"remoteFS":"/var/lib/jenkins",
"remoteAdmin":"ec2-user",
"":"0",
"amiType":{
"rootCommandPrefix":"",
"slaveCommandPrefix":"",
"slaveCommandSuffix":"",
"sshPort":"22",
"stapler-class":"hudson.plugins.ec2.UnixData",
"$class":"hudson.plugins.ec2.UnixData"
},
"labelString":"ec2",
"mode":"EXCLUSIVE",
"idleTerminationMinutes":"30",
"initScript":"",
"tmpDir":"",
"userData":"",
"numExecutors":"",
"jvmopts":"",
"stopOnTerminate":false,
"subnetId":"",
"useDedicatedTenancy":false,
"name":"","value":""
},
"minimumNumberOfInstances":"0",
"minimumNumberOfSpareInstances":"0",
"iamInstanceProfile":"arn:aws:iam::xxxxxxxxxxxx:instance-profile/jenkins_server_role",
"deleteRootOnTermination":true,
"useEphemeralDevices":true,
"customDeviceMapping":"",
"launchTimeoutStr":"",
"associatePublicIp":false,
"connectionStrategy":"PRIVATE_IP",
"connectBySSHProcess":false,
"hostKeyVerificationStrategy":"CHECK_NEW_HARD",
"maxTotalUses":"-1",
"nodeProperties":{"stapler-class-bag":"true"}
},"stapler-class":"hudson.plugins.ec2.AmazonEC2Cloud","$class":"hudson.plugins.ec2.AmazonEC2Cloud"}
at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:681)
at org.kohsuke.stapler.RequestImpl.bindJSON(RequestImpl.java:478)
at org.kohsuke.stapler.RequestImpl.bindJSON(RequestImpl.java:474)
at hudson.model.Descriptor.newInstance(Descriptor.java:598)
I do see the property 'Instance cap' in two different locations in the Jenkins UI. My understanding is that one is for configuring a max limit on the total number of instances allowed in the entire cloud, and the other property describes a max limit on the number of instances for the particular AMI.
Is this a Jenkins issue? Or is it something wrong with the configuration I've provided?
NOTE: I'm okay with providing the configuration as code rather than via the Jenkins UI. If anyone is able to provide the same configuration through code, that is also fine.
Solution
I eventually ended up configuring this using a groovy script run in the Script Console in the Jenkins UI. The groovy script I used is:
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 = 'ec2-cloud'
def ec2_instance_cap = 5
def worker_description = 'jenkins-slave running in ec2 instance'
def worker_label_string = 'ec2-slave'
def ami_id = 'ami-xxxxxxxxxxxxxxxxx'
def security_groups = 'sg-xxxxxxxxxxxxxxxxxx'
def subnet_id = 'subnet-xxxxxxxx'
def instance_type = 't2.micro'
def instance_profile_arn = 'arn:aws:iam::xxxxxxxxxxxx:instance-profile/jenkins_server_role'
def number_of_executors = 2
def ec2_tags = [
new EC2Tag('Name', 'jenkins-slave-instance')
]
def priv_key_txt = '''
-----BEGIN RSA PRIVATE KEY-----
<My Private key>
-----END RSA PRIVATE KEY-----
'''
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, null, null),
// String jvmopts
'',
// boolean stopOnTerminate
false,
// String subnetId
subnet_id,
// List<EC2Tag> tags
ec2_tags,
// String idleTerminationMinutes
'30',
// int minimumNumberOfInstances
0,
// int minimumNumberOfSpareInstances
0,
// string instanceCapStr
'3',
// string iamInstanceProfile
'arn:aws:iam::xxxxxxxxxxxx:instance-profile/jenkins_server_role',
// boolean deleteRootOnTermination
true,
// boolean useEphemeralDevices
true,
// boolean useDedicatedTenancy
false,
// String launchTimeoutStr
'1800',
// boolean associatePublicIp
false,
// String customDeviceMapping
'',
// boolean connectBySSHProcess
false,
// boolean monitoring
false,
// boolean t2Unlimited
false,
// Enum connectionStrategy
ConnectionStrategy.PRIVATE_IP,
// int maxTotalUses
3,
// List<? extends NodeProperty<?>> nodeProperties
[],
// HostKeyVerificationStrategyEnum
HostKeyVerificationStrategyEnum.CHECK_NEW_HARD
)
def new_cloud = new AmazonEC2Cloud(
// String cloudName
ec2_cloud_name,
// boolean useInstanceProfileForCredentials
true,
// String credentialsId
'',
// String region
'eu-west-1',
// String privateKey
priv_key_txt,
// String sshKeysCredentialsId
'jenkins-slave-ssh-key',
// String instanceCapStr
"3",
// List<? extends SlaveTemplate> templates
[worker_ami],
// String roleArn
null,
// String roleSessionName
null
)
instance.clouds.add(new_cloud)
Strange thing is, after creating the config by running this script, now I am able to edit and save the created config via the Jenkins UI.
Answered By - Sidharth Ramesh
Answer Checked By - Marilyn (JavaFixing Volunteer)