From 9365df5f0592bb205c92910410e8e9710f7caf3d Mon Sep 17 00:00:00 2001 From: Aaron Walker Date: Fri, 17 Aug 2018 00:28:52 +0200 Subject: [PATCH 1/7] looks up the ciinabox cloudformation stack output params and write them to a file --- vars/bakeAMI.groovy | 5 +-- vars/ciinaboxVPC.groovy | 72 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 vars/ciinaboxVPC.groovy diff --git a/vars/bakeAMI.groovy b/vars/bakeAMI.groovy index 352665d3..00429e4e 100644 --- a/vars/bakeAMI.groovy +++ b/vars/bakeAMI.groovy @@ -58,13 +58,13 @@ def call(body) { node { println "bake config:${config}" deleteDir() - git(url: 'https://github.com/base2Services/ciinabox-bakery.git', branch: 'master') + git(url: 'https://github.com/base2Services/ciinabox-bakery.git', branch: config.get('ciinaboxBakeryBranch', 'master')) def sourceAMI = lookupAMI config def branchName = env.BRANCH_NAME.replaceAll("/", "-") bakeEnv << "SOURCE_AMI=${sourceAMI}" bakeEnv << "BRANCH=${branchName}" withEnv(bakeEnv) { - sh './configure $CIINABOX_NAME $REGION $AMI_USERS' + ciinaboxVPC config if(skipCookbookUpload) { sh 'mkdir -p cookbooks' @@ -82,6 +82,7 @@ def call(body) { ''' sh '''#!/bin/bash AMI_BUILD_ID=${BRANCH}-${AMI_BUILD_NUMBER} + export OPT_VARS="-var ami_users=${AMI_USERS}" echo "===================================================" echo "Baking AMI: ${ROLE}" echo "AMI Build NO: ${AMI_BUILD_ID}" diff --git a/vars/ciinaboxVPC.groovy b/vars/ciinaboxVPC.groovy new file mode 100644 index 00000000..b76fd1d2 --- /dev/null +++ b/vars/ciinaboxVPC.groovy @@ -0,0 +1,72 @@ +/*********************************** + ciinabox VPC + + Lookups the output Params from the ciinabox vpc and writes them to a + json file in the workspace called base_params.json + + example usage + ciinaboxVPC( + ciinabox: 'ciinabox', + region: env.REGION + ) + ************************************/ + @Grab(group='com.amazonaws', module='aws-java-sdk-cloudformation', version='1.11.359') + +import com.amazonaws.services.cloudformation.* +import com.amazonaws.services.cloudformation.model.* + +def call(body) { + def config = body + + def ciinabox = ciinaboxStack(config.get('ciinabox', 'ciinabox'), config.region) + if(ciinabox) { + def outputs = [:] + ciinabox.outputs.each { output -> + outputs[output.outputKey] = output.outputValue + } + println "ciinabox outputs:${outputs}" + def paramsFile = config.get('outputFile','base_params.json') + def exist = fileExists(paramsFile) + if(exist) { + new File(paramsFile).delete() + } + writeFile file: paramsFile, text: toJson(outputs) + } else { + throw new RuntimeException("no ciinabox stack ${ciinabox} found") + } + +} + +@NonCPS +def ciinaboxStack(stackName, region) { + try { + def cf = setupClient(region) + DescribeStacksResult result = cf.describeStacks(new DescribeStacksRequest().withStackName(stackName)) + return result.getStacks().get(0) + } catch (AmazonCloudFormationException ex) { + if(ex.message.contains("does not exist")) { + return null + } else { + throw ex + } + } +} + +@NonCPS +def toJson(outputs) { + def json_text = """{ + "region": "${outputs['Region']}", + "vpc_id": "${outputs['VPCId']}", + "subnet_id": "${outputs['ECSPrivateSubnetA']}", + "security_group": "${outputs['SecurityGroup']}", + "packer_role": "${outputs['ECSRole']}", + "packer_instance_profile": "${outputs['ECSInstanceProfile']}" + }""" + return json_text +} + +@NonCPS +def setupClient(region) { + def cb = AmazonCloudFormationClientBuilder.standard().withRegion(region) + return cb.build() +} From fdb2691a77ddaeb9a1f7994988dcde6346669212 Mon Sep 17 00:00:00 2001 From: Aaron Walker Date: Fri, 7 Sep 2018 13:33:17 +0200 Subject: [PATCH 2/7] adds support for baking AMIs in a different AZ --- vars/ciinaboxVPC.groovy | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/vars/ciinaboxVPC.groovy b/vars/ciinaboxVPC.groovy index b76fd1d2..ec6a30bd 100644 --- a/vars/ciinaboxVPC.groovy +++ b/vars/ciinaboxVPC.groovy @@ -7,8 +7,10 @@ example usage ciinaboxVPC( ciinabox: 'ciinabox', - region: env.REGION + region: env.REGION, + availabilityZone: 'a' ) + the optional az attribute allows you to override which availability zone is returned ************************************/ @Grab(group='com.amazonaws', module='aws-java-sdk-cloudformation', version='1.11.359') @@ -18,7 +20,9 @@ import com.amazonaws.services.cloudformation.model.* def call(body) { def config = body - def ciinabox = ciinaboxStack(config.get('ciinabox', 'ciinabox'), config.region) + az = config.get('availabilityZone', 'a').toUpperCase() + ciinaboxName = config.get('ciinabox', 'ciinabox') + def ciinabox = ciinaboxStack(ciinaboxName, config.region) if(ciinabox) { def outputs = [:] ciinabox.outputs.each { output -> @@ -30,7 +34,7 @@ def call(body) { if(exist) { new File(paramsFile).delete() } - writeFile file: paramsFile, text: toJson(outputs) + writeFile file: paramsFile, text: toJson(outputs, az) } else { throw new RuntimeException("no ciinabox stack ${ciinabox} found") } @@ -38,7 +42,7 @@ def call(body) { } @NonCPS -def ciinaboxStack(stackName, region) { +def ciinaboxStack(stackName, region, az) { try { def cf = setupClient(region) DescribeStacksResult result = cf.describeStacks(new DescribeStacksRequest().withStackName(stackName)) @@ -53,11 +57,12 @@ def ciinaboxStack(stackName, region) { } @NonCPS -def toJson(outputs) { +def toJson(outputs, az) { + subnet = "ECSPrivateSubnet${az}" def json_text = """{ "region": "${outputs['Region']}", "vpc_id": "${outputs['VPCId']}", - "subnet_id": "${outputs['ECSPrivateSubnetA']}", + "subnet_id": "${outputs[subnet]}", "security_group": "${outputs['SecurityGroup']}", "packer_role": "${outputs['ECSRole']}", "packer_instance_profile": "${outputs['ECSInstanceProfile']}" From 315b6862e423b6a4bde820b56250e4ced0b2b28c Mon Sep 17 00:00:00 2001 From: Aaron Walker Date: Fri, 7 Sep 2018 13:39:02 +0200 Subject: [PATCH 3/7] fixes --- vars/ciinaboxVPC.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vars/ciinaboxVPC.groovy b/vars/ciinaboxVPC.groovy index ec6a30bd..47f8781a 100644 --- a/vars/ciinaboxVPC.groovy +++ b/vars/ciinaboxVPC.groovy @@ -42,7 +42,7 @@ def call(body) { } @NonCPS -def ciinaboxStack(stackName, region, az) { +def ciinaboxStack(stackName, region) { try { def cf = setupClient(region) DescribeStacksResult result = cf.describeStacks(new DescribeStacksRequest().withStackName(stackName)) From ac91d4e6e05f10e29efa475bca227f8d4c33aa69 Mon Sep 17 00:00:00 2001 From: Aaron Walker Date: Fri, 7 Sep 2018 17:24:42 +0200 Subject: [PATCH 4/7] fixes --- vars/ciinaboxVPC.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vars/ciinaboxVPC.groovy b/vars/ciinaboxVPC.groovy index 47f8781a..eefe02f0 100644 --- a/vars/ciinaboxVPC.groovy +++ b/vars/ciinaboxVPC.groovy @@ -20,8 +20,8 @@ import com.amazonaws.services.cloudformation.model.* def call(body) { def config = body - az = config.get('availabilityZone', 'a').toUpperCase() - ciinaboxName = config.get('ciinabox', 'ciinabox') + def az = config.get('availabilityZone', 'a').toUpperCase() + def ciinaboxName = config.get('ciinabox', 'ciinabox') def ciinabox = ciinaboxStack(ciinaboxName, config.region) if(ciinabox) { def outputs = [:] From 352b31c4c47b6dab448709dbbdc34fe4bad88a33 Mon Sep 17 00:00:00 2001 From: Aaron Walker Date: Wed, 7 Nov 2018 20:13:36 +0100 Subject: [PATCH 5/7] allows overridimg an instance_type to test kitchen --- vars/verifyAMI.groovy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vars/verifyAMI.groovy b/vars/verifyAMI.groovy index a6d018e7..9377a6f4 100644 --- a/vars/verifyAMI.groovy +++ b/vars/verifyAMI.groovy @@ -12,7 +12,7 @@ def call(body) { node { deleteDir() unstash 'cookbook' - withEnv(["REGION=${config.get('region')}", "VERIFY_AMI=${config.get('ami')}", "ROLE=${config.get('role')}", "COOKBOOK=${config.get('cookbook')}"]) { + withEnv(["REGION=${config.get('region')}", "VERIFY_AMI=${config.get('ami')}", "ROLE=${config.get('role')}", "COOKBOOK=${config.get('cookbook')}", "INSTANCE_TYPE=${config.get('instance_type')}"]) { withAWSKeyPair(config.get('region')) { sh '''#!/bin/bash eval "$(/opt/chefdk/bin/chef shell-init bash)" @@ -27,6 +27,7 @@ cat < .kitchen.local.yml driver: aws_ssh_key_id: ${KEYNAME} user_data: userdata.sh + instance_type: ${INSTANCE_TYPE} verifier: name: inspec From f55b42e5a5de6802d0227477ef76a83c3b3a57b3 Mon Sep 17 00:00:00 2001 From: Guslington Date: Wed, 14 Nov 2018 15:03:15 +1100 Subject: [PATCH 6/7] use chef bundle --- vars/bakeAMI.groovy | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vars/bakeAMI.groovy b/vars/bakeAMI.groovy index 00429e4e..4e87b251 100644 --- a/vars/bakeAMI.groovy +++ b/vars/bakeAMI.groovy @@ -52,6 +52,7 @@ def call(body) { bakeEnv << "CB_BUILD_NO=${config.cookbookVersion}" bakeEnv << "BUCKET_REGION=${config.bucketRegion}" def skipCookbookUpload = config.get('skipCookbookUpload',false) + def cookbookBundle = config.get('cookbookBundle',false) def role = config.get('role').toUpperCase() @@ -65,9 +66,12 @@ def call(body) { bakeEnv << "BRANCH=${branchName}" withEnv(bakeEnv) { ciinaboxVPC config - + if(skipCookbookUpload) { sh 'mkdir -p cookbooks' + } else if(cookbookBundle) { + unstash 'chefbundle' + sh 'tar xvfz chef-bundle.tar.gz' } else { unstash 'cookbook' sh 'tar xvfz cookbooks.tar.gz' From 741f3d5f0ba940e5e7fb24c1a2768bdc6d09a698 Mon Sep 17 00:00:00 2001 From: Guslington Date: Wed, 11 Mar 2020 10:45:03 +1100 Subject: [PATCH 7/7] new chefdk doesnt include rspec_junit_formatter --- vars/chefspec.groovy | 1 + 1 file changed, 1 insertion(+) diff --git a/vars/chefspec.groovy b/vars/chefspec.groovy index e6271385..07feb2c7 100644 --- a/vars/chefspec.groovy +++ b/vars/chefspec.groovy @@ -17,6 +17,7 @@ def call(body) { echo "==========================================" cd $WORKSPACE/$COOKBOOK gem install version + gem install rspec_junit_formatter berks install if [ $? -ne 0 ]; then echo "Berkshelf install Failed!"