Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query AWS CLI to populate Jenkins "Active Choices Reactive Parameter" (Linux)

I have a Jenkins 2.0 job where I require the user to select the list of servers to execute the job against via a Jenkins "Active Choices Reactive Parameter". These servers which the job will execute against are AWS EC2 instances. Instead of hard-coding the available servers in the "Active Choices Reactive Parameter", I'd like to query the AWS CLI to get a list of servers.

A few notes:

  • I've assigned the Jenkins 2.0 EC2 an IAM role which has sufficient privileges to query AWS via the CLI.
  • The AWS CLI is installed on the Jenkins EC2.
  • The "Active Choices Reactive Parameter" will return a list of checkboxes if I hardcode values in a Groovy script, such as: return ["10.1.1.1", "10.1.1.2", 10.1.1.3"]
  • I know my awk commands can be improved, I'm not yet sure how, but my primary goal is to get the list of servers dynamically loaded in Jenkins.

I can run the following command directly on the EC2 instance which is hosting Jenkins:

aws ec2 describe-instances --region us-east-2 --filters
   "Name=tag:Env,Values=qa" --query
   "Reservations[*].Instances[*].PrivateIpAddress" | grep -o
   '\"[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\"' | awk
   {'printf $0 ", "'} | awk {'print "[" $0'} | awk {'gsub(/^[ \t]+|[
   \t]+$/, ""); print'} | awk {'print substr ($0, 1, length($0)-1)'} |
   awk {'print $0 "]"'}

This will return the following, which is in the format expected by the "Active Choices Reactive Parameter":

["10.1.1.1", "10.1.1.2", 10.1.1.3"]

So, in the "Script" textarea of the "Active Choices Reactive Parameter", I have the following script. The problem is that my server list is never populated. I've tried numerous variations of this script without luck. Can someone please tell me where I've went wrong and what I can do to correct this script so that my list of server IP addresses is dynamically loaded into a Jenkins job?

def standardOut = new StringBuffer(), standardErr = new StringBuffer()

def command = $/
    aws ec2 describe-instances --region us-east-2 --filters "Name=tag:Env,Values=qaint" --query "Reservations[*].Instances[*].PrivateIpAddress" | 
    grep -o '\"[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\"' | 
    awk {'printf $0 ", "'} | 
    awk {'print "[" $0'} | 
    awk {'gsub(/^[ \t]+|[ \t]+$/, ""); print'} | 
    awk {'print substr ($0, 1, length($0)-1)'} | 
    awk {'print $0 "]"'}
    /$

def proc = command.execute()
proc.consumeProcessOutput(standardOut, standardErr)
proc.waitForOrKill(1000)

return standardOut
like image 529
Wylice Avatar asked Nov 07 '22 10:11

Wylice


1 Answers

I tried to execute your script and the standardErr had some errors, Looks like groovy didn't like the double quotes in the AWS CLI. Here is a cleaner way to do without using awk

def command = 'aws ec2 describe-instances \
               --filters Name=tag:Name,Values=Test \
               --query Reservations[*].Instances[*].PrivateIpAddress \
               --output text'
def proc = command.execute()
proc.waitFor()              

def output = proc.in.text
def exitcode= proc.exitValue()
def error = proc.err.text

if (error) {
    println "Std Err: ${error}"
    println "Process exit code: ${exitcode}"
    return exitcode
}

//println output.split()
return output.split()
like image 82
Sudharsan Sivasankaran Avatar answered Nov 15 '22 07:11

Sudharsan Sivasankaran