Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaces the configure shell script with AWS SDK #50

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions vars/bakeAMI.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,26 @@ 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()

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'
} else if(cookbookBundle) {
unstash 'chefbundle'
sh 'tar xvfz chef-bundle.tar.gz'
} else {
unstash 'cookbook'
sh 'tar xvfz cookbooks.tar.gz'
Expand All @@ -82,6 +86,7 @@ def call(body) {
'''
sh '''#!/bin/bash
AMI_BUILD_ID=${BRANCH}-${AMI_BUILD_NUMBER}
export OPT_VARS="-var ami_users=${AMI_USERS}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if this this is a null value? Would this be better off being added to the bakery script with the rest of the options?

echo "==================================================="
echo "Baking AMI: ${ROLE}"
echo "AMI Build NO: ${AMI_BUILD_ID}"
Expand Down
1 change: 1 addition & 0 deletions vars/chefspec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down
77 changes: 77 additions & 0 deletions vars/ciinaboxVPC.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/***********************************
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,
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')

import com.amazonaws.services.cloudformation.*
import com.amazonaws.services.cloudformation.model.*

def call(body) {
def config = body

def az = config.get('availabilityZone', 'a').toUpperCase()
def ciinaboxName = config.get('ciinabox', 'ciinabox')
def ciinabox = ciinaboxStack(ciinaboxName, 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, az)
} 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, az) {
subnet = "ECSPrivateSubnet${az}"
def json_text = """{
"region": "${outputs['Region']}",
"vpc_id": "${outputs['VPCId']}",
"subnet_id": "${outputs[subnet]}",
"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()
}
3 changes: 2 additions & 1 deletion vars/verifyAMI.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -27,6 +27,7 @@ cat <<EOT > .kitchen.local.yml
driver:
aws_ssh_key_id: ${KEYNAME}
user_data: userdata.sh
instance_type: ${INSTANCE_TYPE}

verifier:
name: inspec
Expand Down