Skip to content

Commit

Permalink
Support the share_with and copy_to parameters as extended_attributes. (
Browse files Browse the repository at this point in the history
…#118)

The packer part is quite ugly since it's not possible to pass a list on the command line as `var`.
Accounts and regions must me passed one by one
  • Loading branch information
kalymero authored and Matt Duftler committed Jul 13, 2016
1 parent f165221 commit b414c73
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,31 @@ abstract class CloudProviderBakeHandler {
parameterMap.configDir = configDir

if (bakeRequest.extended_attributes) {
parameterMap << bakeRequest.extended_attributes
if (bakeRequest.extended_attributes.containsKey('share_with')) {
unrollParameters("share_with_", bakeRequest.extended_attributes.get('share_with'), parameterMap)
}

if (bakeRequest.extended_attributes.containsKey('copy_to')) {
unrollParameters("copy_to_", bakeRequest.extended_attributes.get('copy_to'), parameterMap)
}

List attributes = bakeRequest.extended_attributes.keySet().asList()
parameterMap << bakeRequest.extended_attributes.subMap(
attributes.findAll { !it.equals('share_with') && !it.equals('copy_to') })
}

def finalTemplateFileName = bakeRequest.template_file_name ?: templateFileName

return packerCommandFactory.buildPackerCommand(baseCommand, parameterMap, "$configDir/$finalTemplateFileName")
}

private void unrollParameters(String prefix, String rolledParameter, Map parameterMap) {
List<String> values = rolledParameter.tokenize(",")
values.eachWithIndex { value, index, counter = index + 1 ->
parameterMap.put(prefix + counter, value.trim())
}
}

BaseImage findBaseImage(BakeRequest bakeRequest) {
def osVirtualizationSettings = getBakeryDefaults().baseImages.find {
it.baseImage.id == bakeRequest.base_os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,4 +859,106 @@ class AWSBakeHandlerSpec extends Specification {
bakeKey1 == "bake:aws:centos:kato|nflx-djangobase-enhanced_0.1-h12.170cdbd_all|mongodb:us-east-1:hvm:enhancedNWDisabled"
bakeKey2 == bakeKey1
}
void 'produces packer command with all required parameters including shared_with multiple accounts as extended_attribute'() {
setup:
def imageNameFactoryMock = Mock(ImageNameFactory)
def packerCommandFactoryMock = Mock(PackerCommandFactory)
def fullyQualifiedPackageName = "nflx-djangobase-enhanced_0.1-h12.170cdbd_all"
def appVersionStr = "nflx-djangobase-enhanced-0.1-170cdbd.h12"
def buildHost = "http://some-build-server:8080"
def buildInfoUrl = "http://some-build-server:8080/repogroup/repo/builds/320282"
def share_account = "000001, 000002"
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: fullyQualifiedPackageName,
base_os: "trusty",
vm_type: BakeRequest.VmType.hvm,
build_host: buildHost,
cloud_provider_type: BakeRequest.CloudProviderType.aws,
extended_attributes: [share_with: share_account],
build_info_url: buildInfoUrl)
def targetImageName = "kato-x8664-timestamp-trusty"
def parameterMap = [
aws_region: REGION,
aws_ssh_username: "ubuntu",
aws_instance_type: "t2.micro",
aws_source_ami: SOURCE_TRUSTY_HVM_IMAGE_NAME,
aws_target_ami: targetImageName,
package_type: BakeRequest.PackageType.DEB.packageType,
repository: DEBIAN_REPOSITORY,
packages: fullyQualifiedPackageName,
share_with_1: "000001",
share_with_2: "000002",
configDir: configDir,
appversion: appVersionStr,
build_host: buildHost,
build_info_url: buildInfoUrl
]
@Subject
AWSBakeHandler awsBakeHandler = new AWSBakeHandler(configDir: configDir,
awsBakeryDefaults: awsBakeryDefaults,
imageNameFactory: imageNameFactoryMock,
packerCommandFactory: packerCommandFactoryMock,
debianRepository: DEBIAN_REPOSITORY,
yumRepository: YUM_REPOSITORY)
when:
awsBakeHandler.producePackerCommand(REGION, bakeRequest)
then:
1 * imageNameFactoryMock.deriveImageNameAndAppVersion(bakeRequest, _) >> [targetImageName, appVersionStr, fullyQualifiedPackageName]
1 * packerCommandFactoryMock.buildPackerCommand("", parameterMap, "$configDir/$awsBakeryDefaults.templateFile")
}
void 'produces packer command with all required parameters including copy_to multiple regions as extended_attribute'() {
setup:
def imageNameFactoryMock = Mock(ImageNameFactory)
def packerCommandFactoryMock = Mock(PackerCommandFactory)
def fullyQualifiedPackageName = "nflx-djangobase-enhanced_0.1-h12.170cdbd_all"
def appVersionStr = "nflx-djangobase-enhanced-0.1-170cdbd.h12"
def buildHost = "http://some-build-server:8080"
def buildInfoUrl = "http://some-build-server:8080/repogroup/repo/builds/320282"
def copy_regions = "us-west-1, us-west-2"
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: fullyQualifiedPackageName,
base_os: "trusty",
vm_type: BakeRequest.VmType.hvm,
build_host: buildHost,
cloud_provider_type: BakeRequest.CloudProviderType.aws,
extended_attributes: [copy_to: copy_regions],
build_info_url: buildInfoUrl)
def targetImageName = "kato-x8664-timestamp-trusty"
def parameterMap = [
aws_region: REGION,
aws_ssh_username: "ubuntu",
aws_instance_type: "t2.micro",
aws_source_ami: SOURCE_TRUSTY_HVM_IMAGE_NAME,
aws_target_ami: targetImageName,
package_type: BakeRequest.PackageType.DEB.packageType,
repository: DEBIAN_REPOSITORY,
packages: fullyQualifiedPackageName,
copy_to_1: "us-west-1",
copy_to_2: "us-west-2",
configDir: configDir,
appversion: appVersionStr,
build_host: buildHost,
build_info_url: buildInfoUrl
]
@Subject
AWSBakeHandler awsBakeHandler = new AWSBakeHandler(configDir: configDir,
awsBakeryDefaults: awsBakeryDefaults,
imageNameFactory: imageNameFactoryMock,
packerCommandFactory: packerCommandFactoryMock,
debianRepository: DEBIAN_REPOSITORY,
yumRepository: YUM_REPOSITORY)
when:
awsBakeHandler.producePackerCommand(REGION, bakeRequest)
then:
1 * imageNameFactoryMock.deriveImageNameAndAppVersion(bakeRequest, _) >> [targetImageName, appVersionStr, fullyQualifiedPackageName]
1 * packerCommandFactoryMock.buildPackerCommand("", parameterMap, "$configDir/$awsBakeryDefaults.templateFile")
}
}
36 changes: 35 additions & 1 deletion rosco-web/config/packer/aws-chroot.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@
"package_type": "",
"packages": "",
"upgrade": "",
"configDir": null
"configDir": null,
"share_with_1": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_2": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_3": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_4": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_5": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_6": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_7": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_8": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_9": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_10": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"copy_to_1": "{{env `SPINNAKER_AWS_DEFAULT_REGION`}}",
"copy_to_2": "{{env `SPINNAKER_AWS_DEFAULT_REGION`}}",
"copy_to_3": "{{env `SPINNAKER_AWS_DEFAULT_REGION`}}",
"copy_to_4": "{{env `SPINNAKER_AWS_DEFAULT_REGION`}}",
"copy_to_5": "{{env `SPINNAKER_AWS_DEFAULT_REGION`}}"
},
"builders": [{
"type": "amazon-chroot",
Expand All @@ -26,6 +41,25 @@
"secret_key": "{{user `aws_secret_key`}}",
"source_ami": "{{user `aws_source_ami`}}",
"ami_name": "{{user `aws_target_ami`}}",
"ami_users": [
"{{user `share_with_1`}}",
"{{user `share_with_2`}}",
"{{user `share_with_3`}}",
"{{user `share_with_4`}}",
"{{user `share_with_5`}}",
"{{user `share_with_6`}}",
"{{user `share_with_7`}}",
"{{user `share_with_8`}}",
"{{user `share_with_9`}}",
"{{user `share_with_10`}}"
],
"ami_regions": [
"{{user `copy_to_1`}}",
"{{user `copy_to_2`}}",
"{{user `copy_to_3`}}",
"{{user `copy_to_4`}}",
"{{user `copy_to_5`}}"
],
"tags": {
"appversion": "{{user `appversion`}}",
"build_host": "{{user `build_host`}}",
Expand Down
36 changes: 35 additions & 1 deletion rosco-web/config/packer/aws-ebs.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@
"package_type": "",
"packages": "",
"upgrade": "",
"configDir": null
"configDir": null,
"share_with_1": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_2": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_3": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_4": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_5": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_6": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_7": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_8": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_9": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"share_with_10": "{{env `SPINNAKER_AWS_DEFAULT_ACCOUNT`}}",
"copy_to_1": "{{env `SPINNAKER_AWS_DEFAULT_REGION`}}",
"copy_to_2": "{{env `SPINNAKER_AWS_DEFAULT_REGION`}}",
"copy_to_3": "{{env `SPINNAKER_AWS_DEFAULT_REGION`}}",
"copy_to_4": "{{env `SPINNAKER_AWS_DEFAULT_REGION`}}",
"copy_to_5": "{{env `SPINNAKER_AWS_DEFAULT_REGION`}}"
},
"builders": [{
"type": "amazon-ebs",
Expand All @@ -31,6 +46,25 @@
"instance_type": "{{user `aws_instance_type`}}",
"source_ami": "{{user `aws_source_ami`}}",
"ami_name": "{{user `aws_target_ami`}}",
"ami_users": [
"{{user `share_with_1`}}",
"{{user `share_with_2`}}",
"{{user `share_with_3`}}",
"{{user `share_with_4`}}",
"{{user `share_with_5`}}",
"{{user `share_with_6`}}",
"{{user `share_with_7`}}",
"{{user `share_with_8`}}",
"{{user `share_with_9`}}",
"{{user `share_with_10`}}"
],
"ami_regions": [
"{{user `copy_to_1`}}",
"{{user `copy_to_2`}}",
"{{user `copy_to_3`}}",
"{{user `copy_to_4`}}",
"{{user `copy_to_5`}}"
],
"associate_public_ip_address": "{{user `aws_associate_public_ip_address`}}",
"enhanced_networking": "{{user `aws_enhanced_networking`}}",
"tags": {
Expand Down

0 comments on commit b414c73

Please sign in to comment.