Skip to content

Commit f443f42

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

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
@@ -52,13 +52,13 @@ def call(body) {
5252
node {
5353
println "bake config:${config}"
5454
deleteDir()
55-
git(url: 'https://github.com/base2Services/ciinabox-bakery.git', branch: 'master')
55+
git(url: 'https://github.com/base2Services/ciinabox-bakery.git', branch: config.get('ciinaboxBakeryBranch', 'master'))
5656
def sourceAMI = lookupAMI config
5757
def branchName = env.BRANCH_NAME.replaceAll("/", "-")
5858
bakeEnv << "SOURCE_AMI=${sourceAMI}"
5959
bakeEnv << "BRANCH=${branchName}"
6060
withEnv(bakeEnv) {
61-
sh './configure $CIINABOX_NAME $REGION $AMI_USERS'
61+
ciinaboxVPC config
6262

6363
if(skipCookbookUpload) {
6464
sh 'mkdir -p cookbooks'
@@ -76,6 +76,7 @@ def call(body) {
7676
'''
7777
sh '''#!/bin/bash
7878
AMI_BUILD_ID=${BRANCH}-${AMI_BUILD_NUMBER}
79+
export OPT_VARS="-var ami_users=${AMI_USERS}"
7980
echo "==================================================="
8081
echo "Baking AMI: ${ROLE}"
8182
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)