Skip to content

Commit 9365df5

Browse files
committed
looks up the ciinabox cloudformation stack output params and write them to a file
1 parent b737326 commit 9365df5

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

vars/bakeAMI.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ def call(body) {
5858
node {
5959
println "bake config:${config}"
6060
deleteDir()
61-
git(url: 'https://github.com/base2Services/ciinabox-bakery.git', branch: 'master')
61+
git(url: 'https://github.com/base2Services/ciinabox-bakery.git', branch: config.get('ciinaboxBakeryBranch', 'master'))
6262
def sourceAMI = lookupAMI config
6363
def branchName = env.BRANCH_NAME.replaceAll("/", "-")
6464
bakeEnv << "SOURCE_AMI=${sourceAMI}"
6565
bakeEnv << "BRANCH=${branchName}"
6666
withEnv(bakeEnv) {
67-
sh './configure $CIINABOX_NAME $REGION $AMI_USERS'
67+
ciinaboxVPC config
6868

6969
if(skipCookbookUpload) {
7070
sh 'mkdir -p cookbooks'
@@ -82,6 +82,7 @@ def call(body) {
8282
'''
8383
sh '''#!/bin/bash
8484
AMI_BUILD_ID=${BRANCH}-${AMI_BUILD_NUMBER}
85+
export OPT_VARS="-var ami_users=${AMI_USERS}"
8586
echo "==================================================="
8687
echo "Baking AMI: ${ROLE}"
8788
echo "AMI Build NO: ${AMI_BUILD_ID}"

vars/ciinaboxVPC.groovy

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)