Skip to content

Commit

Permalink
Adding a uuid for each bake request which is reused when running the …
Browse files Browse the repository at this point in the history
…packer job (#139)
  • Loading branch information
brujoand authored and Matt Duftler committed Oct 26, 2016
1 parent 7d6e0d1 commit c160921
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import io.swagger.annotations.ApiModelProperty
@CompileStatic
class BakeRequest {

// A generated uuid which will identify the request and be used as the jobId when running the bake
@ApiModelProperty(value = "A generated UUID which will be used to identify the effective packer bake", readOnly = true)
final String request_id = UUID.randomUUID().toString()
String user
@ApiModelProperty("The package(s) to install") @JsonProperty("package") @SerializedName("package")
String package_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import groovy.transform.Immutable
class JobRequest {
List<String> tokenizedCommand
List<String> maskedPackerParameters = []
String jobId

List<String> getMaskedTokenizedCommand() {
return tokenizedCommand.collect { String masked ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class JobExecutorLocal implements JobExecutor {
@Override
String startJob(JobRequest jobRequest) {
log.info("Starting job: $jobRequest.maskedTokenizedCommand...")

String jobId = UUID.randomUUID().toString()
String jobId = jobRequest.jobId

scheduler.createWorker().schedule(
new Action0() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class DockerBakeHandler extends CloudProviderBakeHandler {
return [
docker_source_image : dockerVirtualizationSettings.sourceImage,
docker_target_image : imageName,
docker_target_image_tag : appVersionStr,
docker_target_image_tag : bakeRequest.request_id,
docker_target_repository: dockerBakeryDefaults.targetRepository
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,14 @@ class DockerBakeHandlerSpec extends Specification implements TestDefaults {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGES_NAME,
base_os: "ubuntu",
request_id: SOME_UUID,
cloud_provider_type: BakeRequest.CloudProviderType.docker)
def targetImageName = "kato-x8664-timestamp-ubuntu"
def osPackages = parseDebOsPackageNames(bakeRequest.package_name)
def parameterMap = [
docker_source_image: SOURCE_UBUNTU_IMAGE_NAME,
docker_target_image: targetImageName,
docker_target_image_tag: SOME_MILLISECONDS,
docker_target_image_tag: SOME_UUID,
docker_target_repository: TARGET_REPOSITORY,
repository: DEBIAN_REPOSITORY,
package_type: DEB_PACKAGE_TYPE.packageType,
Expand Down Expand Up @@ -187,13 +188,14 @@ class DockerBakeHandlerSpec extends Specification implements TestDefaults {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGES_NAME,
base_os: "trusty",
request_id: SOME_UUID,
cloud_provider_type: BakeRequest.CloudProviderType.docker)
def targetImageName = "kato-x8664-timestamp-trusty"
def osPackages = parseDebOsPackageNames(bakeRequest.package_name)
def parameterMap = [
docker_source_image: SOURCE_TRUSTY_IMAGE_NAME,
docker_target_image: targetImageName,
docker_target_image_tag: SOME_MILLISECONDS,
docker_target_image_tag: SOME_UUID,
docker_target_repository: TARGET_REPOSITORY,
repository: DEBIAN_REPOSITORY,
package_type: DEB_PACKAGE_TYPE.packageType,
Expand Down Expand Up @@ -225,13 +227,14 @@ class DockerBakeHandlerSpec extends Specification implements TestDefaults {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGES_NAME,
base_os: "centos",
request_id: SOME_UUID,
cloud_provider_type: BakeRequest.CloudProviderType.docker)
def targetImageName = "kato-x8664-timestamp-centos"
def osPackages = parseRpmOsPackageNames(bakeRequest.package_name)
def parameterMap = [
docker_source_image: SOURCE_CENTOS_HVM_IMAGE_NAME,
docker_target_image: targetImageName,
docker_target_image_tag: SOME_MILLISECONDS,
docker_target_image_tag: SOME_UUID,
docker_target_repository: TARGET_REPOSITORY,
repository: YUM_REPOSITORY,
package_type: RPM_PACKAGE_TYPE.packageType,
Expand Down Expand Up @@ -266,13 +269,14 @@ class DockerBakeHandlerSpec extends Specification implements TestDefaults {
package_name: fullyQualifiedPackageName,
base_os: "trusty",
build_host: buildHost,
request_id: SOME_UUID,
cloud_provider_type: BakeRequest.CloudProviderType.gce)
def osPackage = PackageNameConverter.parseDebPackageName(bakeRequest.package_name)
def targetImageName = "kato-x8664-timestamp-trusty"
def parameterMap = [
docker_source_image: SOURCE_TRUSTY_IMAGE_NAME,
docker_target_image: targetImageName,
docker_target_image_tag: SOME_MILLISECONDS,
docker_target_image_tag: SOME_UUID,
docker_target_repository: TARGET_REPOSITORY,
repository: DEBIAN_REPOSITORY,
package_type: DEB_PACKAGE_TYPE.packageType,
Expand Down Expand Up @@ -309,6 +313,7 @@ class DockerBakeHandlerSpec extends Specification implements TestDefaults {
def targetQualifiedImageName = "${targetOrganization}/${targetImageName}"

def bakeRequest = new BakeRequest(
request_id: SOME_UUID,
package_name: "trojan-banker_0.1-3_all",
build_number: "12",
commit_hash: "170cdbd",
Expand All @@ -321,7 +326,7 @@ class DockerBakeHandlerSpec extends Specification implements TestDefaults {
def parameterMap = [
docker_source_image: SOURCE_UBUNTU_IMAGE_NAME,
docker_target_image: targetQualifiedImageName,
docker_target_image_tag: targetImageTag,
docker_target_image_tag: SOME_UUID,
docker_target_repository: TARGET_REPOSITORY,
repository: DEBIAN_REPOSITORY,
package_type: DEB_PACKAGE_TYPE.packageType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ trait TestDefaults {
static final BakeRequest.PackageType DEB_PACKAGE_TYPE = BakeRequest.PackageType.DEB
static final BakeRequest.PackageType RPM_PACKAGE_TYPE = BakeRequest.PackageType.RPM
static final String SOME_MILLISECONDS = "1470391070464"
static final String SOME_UUID = "55c25239-4de5-4f7a-b664-6070a1389680"

def parseDebOsPackageNames(String packages) {
PackageNameConverter.buildOsPackageNames(DEB_PACKAGE_TYPE, packages.tokenize(" "))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class BakeryController {
}

def packerCommand = cloudProviderBakeHandler.producePackerCommand(region, bakeRequest)
def jobRequest = new JobRequest(tokenizedCommand: packerCommand, maskedPackerParameters: cloudProviderBakeHandler.maskedPackerParameters)
def jobRequest = new JobRequest(tokenizedCommand: packerCommand, maskedPackerParameters: cloudProviderBakeHandler.maskedPackerParameters, jobId: bakeRequest.request_id)

if (bakeStore.acquireBakeLock(bakeKey)) {
return runBake(bakeKey, region, bakeRequest, jobRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class BakeryControllerSpec extends Specification {
private static final String BAKE_ID = "some-bake-id"
private static final String PACKER_COMMAND = "packer build ..."
private static final String LOGS_CONTENT = "Some logs content..."
private static final String SOME_UUID = "55c25239-4de5-4f7a-b664-6070a1389680"

void 'create bake launches job and returns new status'() {
setup:
Expand All @@ -52,7 +53,8 @@ class BakeryControllerSpec extends Specification {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGE_NAME,
base_os: "ubuntu",
cloud_provider_type: BakeRequest.CloudProviderType.gce)
cloud_provider_type: BakeRequest.CloudProviderType.gce,
request_id: SOME_UUID)
def runningBakeStatus = new BakeStatus(id: JOB_ID, resource_id: JOB_ID, state: BakeStatus.State.RUNNING)

@Subject
Expand All @@ -69,7 +71,7 @@ class BakeryControllerSpec extends Specification {
1 * bakeStoreMock.retrieveBakeStatusByKey(BAKE_KEY) >> null
1 * cloudProviderBakeHandlerMock.producePackerCommand(REGION, bakeRequest) >> [PACKER_COMMAND]
1 * bakeStoreMock.acquireBakeLock(BAKE_KEY) >> true
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND])) >> JOB_ID
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND], jobId: SOME_UUID)) >> JOB_ID
1 * jobExecutorMock.updateJob(JOB_ID) >> runningBakeStatus
1 * bakeStoreMock.storeNewBakeStatus(BAKE_KEY, REGION, bakeRequest, runningBakeStatus, PACKER_COMMAND) >> runningBakeStatus
returnedBakeStatus == runningBakeStatus
Expand All @@ -84,7 +86,9 @@ class BakeryControllerSpec extends Specification {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGE_NAME,
base_os: "ubuntu",
cloud_provider_type: BakeRequest.CloudProviderType.gce)
cloud_provider_type: BakeRequest.CloudProviderType.gce,
request_id: SOME_UUID)

def failedBakeStatus = new BakeStatus(id: JOB_ID,
resource_id: JOB_ID,
state: BakeStatus.State.CANCELED,
Expand All @@ -105,7 +109,7 @@ class BakeryControllerSpec extends Specification {
1 * bakeStoreMock.retrieveBakeStatusByKey(BAKE_KEY) >> null
1 * cloudProviderBakeHandlerMock.producePackerCommand(REGION, bakeRequest) >> [PACKER_COMMAND]
1 * bakeStoreMock.acquireBakeLock(BAKE_KEY) >> true
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND])) >> JOB_ID
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND], jobId: SOME_UUID)) >> JOB_ID
1 * jobExecutorMock.updateJob(JOB_ID) >> failedBakeStatus
IllegalArgumentException e = thrown()
e.message == "Some kind of failure..."
Expand Down Expand Up @@ -149,7 +153,9 @@ class BakeryControllerSpec extends Specification {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGE_NAME,
base_os: "ubuntu",
cloud_provider_type: BakeRequest.CloudProviderType.gce)
cloud_provider_type: BakeRequest.CloudProviderType.gce,
request_id: SOME_UUID)

def runningBakeStatus = new BakeStatus(id: JOB_ID, resource_id: JOB_ID, state: BakeStatus.State.RUNNING)

@Subject
Expand All @@ -168,7 +174,7 @@ class BakeryControllerSpec extends Specification {
1 * bakeStoreMock.acquireBakeLock(BAKE_KEY) >> false
(10.._) * bakeStoreMock.retrieveBakeStatusByKey(BAKE_KEY) >> null
1 * bakeStoreMock.acquireBakeLock(BAKE_KEY) >> true
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND])) >> JOB_ID
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND], jobId: SOME_UUID)) >> JOB_ID
1 * jobExecutorMock.updateJob(JOB_ID) >> runningBakeStatus
1 * bakeStoreMock.storeNewBakeStatus(BAKE_KEY, REGION, bakeRequest, runningBakeStatus, PACKER_COMMAND) >> runningBakeStatus
returnedBakeStatus == new BakeStatus(id: JOB_ID, resource_id: JOB_ID, state: BakeStatus.State.RUNNING)
Expand All @@ -182,9 +188,11 @@ class BakeryControllerSpec extends Specification {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGE_NAME,
base_os: "ubuntu",
cloud_provider_type: BakeRequest.CloudProviderType.gce)
cloud_provider_type: BakeRequest.CloudProviderType.gce,
request_id: SOME_UUID)

@Subject

@Subject
def bakeryController = new BakeryController(cloudProviderBakeHandlerRegistry: cloudProviderBakeHandlerRegistryMock,
bakeStore: bakeStoreMock)

Expand All @@ -209,7 +217,9 @@ class BakeryControllerSpec extends Specification {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGE_NAME,
base_os: "ubuntu",
cloud_provider_type: BakeRequest.CloudProviderType.gce)
cloud_provider_type: BakeRequest.CloudProviderType.gce,
request_id: SOME_UUID)


@Subject
def bakeryController = new BakeryController(cloudProviderBakeHandlerRegistry: cloudProviderBakeHandlerRegistryMock)
Expand All @@ -231,7 +241,9 @@ class BakeryControllerSpec extends Specification {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGE_NAME,
base_os: "ubuntu",
cloud_provider_type: BakeRequest.CloudProviderType.gce)
cloud_provider_type: BakeRequest.CloudProviderType.gce,
request_id: SOME_UUID)

def runningBakeStatus = new BakeStatus(id: EXISTING_JOB_ID,
resource_id: EXISTING_JOB_ID,
state: BakeStatus.State.RUNNING)
Expand Down Expand Up @@ -259,7 +271,9 @@ class BakeryControllerSpec extends Specification {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGE_NAME,
base_os: "ubuntu",
cloud_provider_type: BakeRequest.CloudProviderType.gce)
cloud_provider_type: BakeRequest.CloudProviderType.gce,
request_id: SOME_UUID)

def completedBakeStatus = new BakeStatus(id: EXISTING_JOB_ID,
resource_id: EXISTING_JOB_ID,
state: BakeStatus.State.COMPLETED,
Expand Down Expand Up @@ -289,7 +303,9 @@ class BakeryControllerSpec extends Specification {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGE_NAME,
base_os: "ubuntu",
cloud_provider_type: BakeRequest.CloudProviderType.gce)
cloud_provider_type: BakeRequest.CloudProviderType.gce,
request_id: SOME_UUID)

def failedBakeStatus = new BakeStatus(id: EXISTING_JOB_ID,
resource_id: EXISTING_JOB_ID,
state: BakeStatus.State.CANCELED,
Expand All @@ -310,7 +326,7 @@ class BakeryControllerSpec extends Specification {
1 * bakeStoreMock.retrieveBakeStatusByKey(BAKE_KEY) >> failedBakeStatus
1 * cloudProviderBakeHandlerMock.producePackerCommand(REGION, bakeRequest) >> [PACKER_COMMAND]
1 * bakeStoreMock.acquireBakeLock(BAKE_KEY) >> true
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND])) >> JOB_ID
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND], jobId: SOME_UUID)) >> JOB_ID
1 * jobExecutorMock.updateJob(JOB_ID) >> newBakeStatus
1 * bakeStoreMock.storeNewBakeStatus(BAKE_KEY, REGION, bakeRequest, newBakeStatus, PACKER_COMMAND) >> newBakeStatus
returnedBakeStatus == newBakeStatus
Expand All @@ -325,7 +341,9 @@ class BakeryControllerSpec extends Specification {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGE_NAME,
base_os: "ubuntu",
cloud_provider_type: BakeRequest.CloudProviderType.gce)
cloud_provider_type: BakeRequest.CloudProviderType.gce,
request_id: SOME_UUID)

def canceledBakeStatus = new BakeStatus(id: EXISTING_JOB_ID,
resource_id: EXISTING_JOB_ID,
state: BakeStatus.State.CANCELED)
Expand All @@ -345,7 +363,7 @@ class BakeryControllerSpec extends Specification {
1 * bakeStoreMock.retrieveBakeStatusByKey(BAKE_KEY) >> canceledBakeStatus
1 * cloudProviderBakeHandlerMock.producePackerCommand(REGION, bakeRequest) >> [PACKER_COMMAND]
1 * bakeStoreMock.acquireBakeLock(BAKE_KEY) >> true
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND])) >> JOB_ID
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND], jobId: SOME_UUID)) >> JOB_ID
1 * jobExecutorMock.updateJob(JOB_ID) >> newBakeStatus
1 * bakeStoreMock.storeNewBakeStatus(BAKE_KEY, REGION, bakeRequest, newBakeStatus, PACKER_COMMAND) >> newBakeStatus
returnedBakeStatus == newBakeStatus
Expand All @@ -360,7 +378,9 @@ class BakeryControllerSpec extends Specification {
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: PACKAGE_NAME,
base_os: "ubuntu",
cloud_provider_type: BakeRequest.CloudProviderType.gce)
cloud_provider_type: BakeRequest.CloudProviderType.gce,
request_id: SOME_UUID)

def newBakeStatus = new BakeStatus(id: JOB_ID, resource_id: JOB_ID, state: BakeStatus.State.RUNNING)

@Subject
Expand All @@ -378,7 +398,7 @@ class BakeryControllerSpec extends Specification {
1 * bakeStoreMock.deleteBakeByKeyPreserveDetails(BAKE_KEY) >> BAKE_ID
1 * cloudProviderBakeHandlerMock.producePackerCommand(REGION, bakeRequest) >> [PACKER_COMMAND]
1 * bakeStoreMock.acquireBakeLock(BAKE_KEY) >> true
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND])) >> JOB_ID
1 * jobExecutorMock.startJob(new JobRequest(tokenizedCommand: [PACKER_COMMAND], jobId: SOME_UUID)) >> JOB_ID
1 * jobExecutorMock.updateJob(JOB_ID) >> newBakeStatus
1 * bakeStoreMock.storeNewBakeStatus(BAKE_KEY, REGION, bakeRequest, newBakeStatus, PACKER_COMMAND) >> newBakeStatus
returnedBakeStatus == newBakeStatus
Expand Down

0 comments on commit c160921

Please sign in to comment.