|
| 1 | +/*********************************** |
| 2 | + ciinabox VPC |
| 3 | +
|
| 4 | + Lookups the output Params from the ciinabox vpc and writes them to a |
| 5 | + json file in the workspace called base_params.json |
| 6 | +
|
| 7 | + example usage |
| 8 | + ciinaboxVPC( |
| 9 | + ciinabox: 'ciinabox', |
| 10 | + region: env.REGION |
| 11 | + ) |
| 12 | + ************************************/ |
| 13 | + @Grab(group='com.amazonaws', module='aws-java-sdk-cloudformation', version='1.11.359') |
| 14 | + |
| 15 | +import com.amazonaws.services.cloudformation.* |
| 16 | +import com.amazonaws.services.cloudformation.model.* |
| 17 | + |
| 18 | +def call(body) { |
| 19 | + def config = body |
| 20 | + |
| 21 | + def ciinabox = ciinaboxStack(config.get('ciinabox', 'ciinabox'), config.region) |
| 22 | + if(ciinabox) { |
| 23 | + def outputs = [:] |
| 24 | + ciinabox.outputs.each { output -> |
| 25 | + outputs[output.outputKey] = output.outputValue |
| 26 | + } |
| 27 | + println "ciinabox outputs:${outputs}" |
| 28 | + def paramsFile = config.get('outputFile','base_params.json') |
| 29 | + def exist = fileExists(paramsFile) |
| 30 | + if(exist) { |
| 31 | + new File(paramsFile).delete() |
| 32 | + } |
| 33 | + writeFile file: paramsFile, text: toJson(outputs) |
| 34 | + } else { |
| 35 | + throw new RuntimeException("no ciinabox stack ${ciinabox} found") |
| 36 | + } |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +@NonCPS |
| 41 | +def ciinaboxStack(stackName, region) { |
| 42 | + try { |
| 43 | + def cf = setupClient(region) |
| 44 | + DescribeStacksResult result = cf.describeStacks(new DescribeStacksRequest().withStackName(stackName)) |
| 45 | + return result.getStacks().get(0) |
| 46 | + } catch (AmazonCloudFormationException ex) { |
| 47 | + if(ex.message.contains("does not exist")) { |
| 48 | + return null |
| 49 | + } else { |
| 50 | + throw ex |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +@NonCPS |
| 56 | +def toJson(outputs) { |
| 57 | + def json_text = """{ |
| 58 | + "region": "${outputs['Region']}", |
| 59 | + "vpc_id": "${outputs['VPCId']}", |
| 60 | + "subnet_id": "${outputs['ECSPrivateSubnetA']}", |
| 61 | + "security_group": "${outputs['SecurityGroup']}", |
| 62 | + "packer_role": "${outputs['ECSRole']}", |
| 63 | + "packer_instance_profile": "${outputs['ECSInstanceProfile']}" |
| 64 | + }""" |
| 65 | + return json_text |
| 66 | +} |
| 67 | + |
| 68 | +@NonCPS |
| 69 | +def setupClient(region) { |
| 70 | + def cb = AmazonCloudFormationClientBuilder.standard().withRegion(region) |
| 71 | + return cb.build() |
| 72 | +} |
0 commit comments