Skip to content

Commit

Permalink
chore: include version in artifact names and tags
Browse files Browse the repository at this point in the history
Get rid of Terraform / Atlas Artifact Registry. Closes #78.

Also closes #49.

The following artifacts are named with version:
*	AMIs
*	VirtualBox VMs
*	VirtualBox images (files)
*	Vagrant box files
*	Packer output directories for VirtualBox builders

The following artifacts are named without version:
*	Vagrant boxes

The following artifacts are named with version and timestamp:
*	Packer manifests

Each obtainment of artifact is made with explicit version:
*	AMIs - with version tag
*	VagrantBox images - with version specified
	in names of directory and file
*	Vagrant boxes - with built-in version property

Use of `most_recent` filter is prohibited.

Labels in names are named according to SemVer with the following
interpretations and deviations:

1.	Environment name is considered as the part of the name. It is
separated from the base name with hyphen `-` and precedes a version.

2.	Architecture and builder (provider, format) are considered as build
metadata.

3.	If timestamp is present it is appended in the end with hyphen `-`
separator.

4.	In the names of the following artifacts plus `+` separator is
replaced with low line (ground) `_`:

	*	VirtualBox VMs
	*	VirtualBox images
	*	AMIs
	*	Vagrant box files
	*	Vagrant boxes

5.	The version can contain pre-release label as usual.

Additional changes:
*	Reorder variables in Packer templates more logically
*	Configure Chef environment for JenkinsMaster Packer build
	accordingly to build name

BREAKING CHANGE
  • Loading branch information
grv87 committed Mar 29, 2018
1 parent e03d9b6 commit 49fd273
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 265 deletions.
93 changes: 48 additions & 45 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ buildscript {
}
dependencies {
classpath 'gradle.plugin.fidata.org:gradle-packer-plugin:2.4.0'
classpath 'com.amazonaws:aws-java-sdk-ec2:1.11.289'
classpath 'org.apache.commons:commons-exec:1.3'
classpath 'com.jcraft:jsch:0.1.54'
}
Expand Down Expand Up @@ -65,6 +66,12 @@ import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import org.apache.commons.exec.*
import com.jcraft.jsch.*
import org.apache.commons.io.filefilter.DirectoryFileFilter
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder
import com.amazonaws.auth.AWSStaticCredentialsProvider
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.ec2.model.DescribeImagesRequest
import com.amazonaws.services.ec2.model.Image

task wrapper(type: Wrapper) {
gradleVersion = '4.6'
Expand Down Expand Up @@ -168,10 +175,7 @@ ext.with {
rubocopHTMLOutput = file("$htmlReportsDir/rubocop.html")
cookstyleHTMLOutput = file("$htmlReportsDir/cookstyle.html")

terraformDirs = ["$libDir/get_atlas_artifact_metadata_full"]
terraformDir.eachDir { d ->
terraformDirs << d
}
terraformDirs = Arrays.asList(terraformDir.listFiles((FileFilter)DirectoryFileFilter.DIRECTORY))
}

visteg {
Expand Down Expand Up @@ -405,7 +409,6 @@ ext.with {
'CHEF_VERSION': chefVersion,
'AWS_ACCESS_KEY_ID': awsAccessKey,
'AWS_SECRET_ACCESS_KEY': awsSecretKey,
'ATLAS_TOKEN': atlasToken,
'KITCHEN_SSH_KEY': kitchenSSHPrivateKey.absolutePath,
'ENCRYPTED_DATA_BAG_SECRET': encryptedDataBagSecret.absolutePath,
]
Expand All @@ -415,7 +418,6 @@ ext.with {
'keys_dir': keysDir.absolutePath,
'aws_access_key': awsAccessKey,
'aws_secret_key': awsSecretKey,
'atlas_token': atlasToken,
'cloudflare_email': cloudflareEmail,
'cloudflare_token': cloudflareToken,
]
Expand Down Expand Up @@ -455,28 +457,30 @@ ext.with {
'JenkinsEC2Cloud': [:],
'ReleaseCredentials': [:],
]
}

Map getAtlasArtifactMetadataFull(name, type, version) {
new ByteArrayOutputStream().withStream { os ->
exec {
environment([
'ATLAS_TOKEN': atlasToken
])
commandLine([
bundleExecutable, 'exec',
'ruby'
] +
rubyLogLevelArgs +
[
'--',
"$libDir/get_atlas_artifact_metadata_full/get_atlas_artifact_metadata_full.rb",
'get',
name, type, version
])
standardOutput = os
}
new JsonSlurper().parseText(os.toString())
ec2 = null
}

String findAMI(name, version) {
ec2 = ec2 ?: AmazonEC2ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, awsSecretKey)))
.withRegion('eu-west-1')
.build()
List<Image> result = ec2.describeImages(new DescribeImagesRequest()
.withFilters([
name: name,
'tag:version': version
])
.withOwners('880042334380')
).images
if (result.size() == 0) {
logger.error('findAMI: Can\'t find image')
}
else if (result.size() > 1) {
logger.error('findAMI: Too many images found')
}
else {
return result[0].imageId
}
}

Expand Down Expand Up @@ -672,30 +676,29 @@ packer {
'manifest_output_dir': manifestOutputDir,
'aws_access_key': awsAccessKey,
'aws_secret_key': awsSecretKey,
'atlas_token': atlasToken,
'vagrant_cloud_token': vagrantCloudToken,
'encrypted_data_bag_secret': encryptedDataBagSecret,
]
// Kernel images
template 'src/packer/kernel_images/kernel-UbuntuServer-16.04.4_amd64.json'
template 'src/packer/kernel_images/kernel-UbuntuServer-16.04.4+amd64.json'

// Base images
template 'src/packer/base_images/UbuntuServer-16.04.4_amd64.json'
template 'src/packer/base_images/UbuntuServer-16.04.4+amd64.json'

// Instances
template 'src/packer/instances/JenkinsMaster.json'
template 'src/packer/instances/JenkinsSlaves.json'
}

tasks['build-kernel-UbuntuServer-16.04.4_amd64-vbox'].dependsOn 'createVideocaptureDir', 'createManifestOutputDir'
tasks['build-UbuntuServer-16.04.4_amd64-vbox'].dependsOn 'createManifestOutputDir'
tasks['build-UbuntuServer-16.04.4_amd64-vbox'].mustRunAfter 'build-kernel-UbuntuServer-16.04.4_amd64-vbox'
tasks['build-UbuntuServer-16.04.4_amd64-amazon'].dependsOn 'createManifestOutputDir'
tasks['build-UbuntuServer-16.04.4_amd64'].dependsOn 'createManifestOutputDir'
tasks['build-kernel-UbuntuServer-16.04.4+amd64-vbox'].dependsOn 'createVideocaptureDir', 'createManifestOutputDir'
tasks['build-UbuntuServer-16.04.4+amd64-vbox'].dependsOn 'createManifestOutputDir'
tasks['build-UbuntuServer-16.04.4+amd64-vbox'].mustRunAfter 'build-kernel-UbuntuServer-16.04.4+amd64-vbox'
tasks['build-UbuntuServer-16.04.4+amd64-amazon'].dependsOn 'createManifestOutputDir'
tasks['build-UbuntuServer-16.04.4+amd64'].dependsOn 'createManifestOutputDir'

task('clean-base') {
group 'Build'
dependsOn 'clean-UbuntuServer-16.04.4_amd64'
dependsOn 'clean-UbuntuServer-16.04.4+amd64'
}

task('clean') {
Expand All @@ -704,17 +707,17 @@ task('clean') {

task('build-base-vbox') {
group 'Build'
dependsOn 'build-UbuntuServer-16.04.4_amd64-vbox'
dependsOn 'build-UbuntuServer-16.04.4+amd64-vbox'
}

task('build-base-amazon') {
group 'Build'
dependsOn 'build-UbuntuServer-16.04.4_amd64-amazon'
dependsOn 'build-UbuntuServer-16.04.4+amd64-amazon'
}

task('build-base') {
group 'Build'
dependsOn 'build-UbuntuServer-16.04.4_amd64'
dependsOn 'build-UbuntuServer-16.04.4+amd64'
}

task('berksUpdate') {
Expand Down Expand Up @@ -940,7 +943,7 @@ task('generateRuntimeDataBag') {
}
property 'JenkinsEC2CloudDataBag', {
for (s in jenkinsSlaves) {
s.value['ami'] = getAtlasArtifactMetadataFull("fidata/JenkinsSlave-${s.key.replace('+', '_')}", 'amazon.image', s.value['version'])['ami_id']
s.value['ami'] = findAMI("JenkinsSlave-${s.key.replace('+', '_')}-*", s.value['version'])
}
dataBags['JenkinsEC2Cloud'] << [
'region': 'eu-west-1',
Expand Down Expand Up @@ -1004,7 +1007,7 @@ task('generateRuntimeDataBag') {
task('kitchenTest-JenkinsMaster-vbox', type: CrossPlatformExec) {
group 'Test'
dependsOn 'terraformOutput-common', 'generateRuntimeDataBag', 'berksUpdate'
mustRunAfter 'build-UbuntuServer-16.04.4_amd64-vbox'
mustRunAfter 'build-UbuntuServer-16.04.4+amd64-vbox'
workingDir chefDir
doFirst {
environment << kitchenEnvironment
Expand All @@ -1026,7 +1029,7 @@ task('kitchenTest-JenkinsMaster-vbox', type: CrossPlatformExec) {
task('kitchenTest-JenkinsMaster-amazon', type: CrossPlatformExec) {
group 'Test'
dependsOn 'terraformApply-common', 'terraformOutput-common', 'generateRuntimeDataBag', 'berksUpdate'
mustRunAfter 'build-UbuntuServer-16.04.4_amd64-amazon', 'build-UbuntuServer-16.04.4_amd64'
mustRunAfter 'build-UbuntuServer-16.04.4+amd64-amazon', 'build-UbuntuServer-16.04.4+amd64'
workingDir chefDir
doFirst {
environment << kitchenEnvironment
Expand All @@ -1048,7 +1051,7 @@ task('kitchenTest-JenkinsMaster-amazon', type: CrossPlatformExec) {
task('kitchenConverge-JenkinsMaster-vbox', type: CrossPlatformExec) {
group 'Test'
dependsOn 'terraformOutput-common', 'generateRuntimeDataBag'
mustRunAfter 'berksUpdate', 'build-UbuntuServer-16.04.4_amd64-vbox'
mustRunAfter 'berksUpdate', 'build-UbuntuServer-16.04.4+amd64-vbox'
workingDir chefDir
doFirst {
environment << kitchenEnvironment
Expand All @@ -1070,7 +1073,7 @@ task('kitchenConverge-JenkinsMaster-vbox', type: CrossPlatformExec) {
task('kitchenConverge-JenkinsMaster-amazon', type: CrossPlatformExec) {
group 'Test'
dependsOn 'terraformApply-common', 'terraformOutput-common', 'generateRuntimeDataBag'
mustRunAfter 'berksUpdate', 'build-UbuntuServer-16.04.4_amd64-amazon', 'build-UbuntuServer-16.04.4_amd64'
mustRunAfter 'berksUpdate', 'build-UbuntuServer-16.04.4+amd64-amazon', 'build-UbuntuServer-16.04.4+amd64'
workingDir chefDir
doFirst {
environment << kitchenEnvironment
Expand Down Expand Up @@ -1183,7 +1186,7 @@ task('kitchenDestroy-JenkinsMaster-amazon', type: CrossPlatformExec) {

tasks['build-JenkinsMaster'].with {
dependsOn 'createManifestOutputDir', 'terraformApply-common', 'terraformOutput-common', 'generateRuntimeDataBag', 'berksVendor'
mustRunAfter 'build-UbuntuServer-16.04.4_amd64-amazon', 'build-UbuntuServer-16.04.4_amd64', 'kitchenTest-JenkinsMaster-vbox', 'kitchenConverge-JenkinsMaster-vbox', 'kitchenTest-JenkinsMaster-amazon', 'kitchenConverge-JenkinsMaster-amazon'
mustRunAfter 'build-UbuntuServer-16.04.4+amd64-amazon', 'build-UbuntuServer-16.04.4+amd64', 'kitchenTest-JenkinsMaster-vbox', 'kitchenConverge-JenkinsMaster-vbox', 'kitchenTest-JenkinsMaster-amazon', 'kitchenConverge-JenkinsMaster-amazon'
}

task('terraformValidate-instances', type: Exec) {
Expand Down
8 changes: 5 additions & 3 deletions src/chef/.kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# <% require_relative '../lib/get_atlas_artifact_metadata_full/get_atlas_artifact_metadata_full' %>
platforms:
- name: UbuntuServer-16.04.4+amd64-amazon
driver:
name: ec2
image_id: <%= get_atlas_artifact_metadata_full('fidata/UbuntuServer-16.04.4_amd64', 'amazon.image', '0.1.0')['ami_id'] %>
image_search:
name: UbuntuServer-16.04.4-*
"tag:version": "0.1.0"
"tag:architecture": amd64
owner-id: "880042334380"
region: eu-west-1
subnet_id: <%= ENV['AWS_SUBNET_ID'] %>
aws_ssh_key_id: kitchen
Expand Down
3 changes: 0 additions & 3 deletions src/lib/get_atlas_artifact_metadata_full/.gitignore

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 49fd273

Please sign in to comment.