Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Commit

Permalink
Process PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lievendoclo committed Sep 11, 2018
1 parent 59953b1 commit 7fe55fb
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 154 deletions.
113 changes: 113 additions & 0 deletions lib/docker/DockerDeploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright © 2018 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
DefaultGoalNameGenerator,
ExecuteGoal,
ExecuteGoalResult,
FulfillableGoalDetails,
FulfillableGoalWithRegistrations,
getGoalDefintionFrom,
Goal,
GoalInvocation,
ImplementationRegistration,
IndependentOfEnvironment,
} from "@atomist/sdm";
import {
DockerPerBranchDeployer,
DockerPerBranchDeployerOptions,
} from "./DockerPerBranchDeployer";

/**
* Options for Docker deployment goal
*/
export interface DockerDeployRegistration extends Partial<ImplementationRegistration> {
/**
* Starting port to be scanned for free ports. Defaults to 9090
*/
lowerPort?: number;
/**
* Pattern that indicates that the container has started up correctly
*/
successPatterns: RegExp[];
/**
* Base URL for the docker container. Probably localhost or your Docker machine IP. Defaults to localhost
*/
baseUrl?: string;
/**
* The exposed port in the Dockerfile to be mapped externally
*/
sourcePort: number;
}

/**
* Goal definition for deployment using Docker
*/
export const DockerDeployGoal = new Goal({
uniqueName: "docker-deploy",
displayName: "docker deploy",
environment: IndependentOfEnvironment,
workingDescription: "Deploying using Docker",
completedDescription: "Deployed with Docker",
failedDescription: "Docker deployment failed",
});

/**
* This goal will deploy the Docker image built by the `DockerBuild` goal. Deployments mapped
* to free ports and a deployment will be done per branch.
*/
export class DockerDeploy extends FulfillableGoalWithRegistrations<DockerDeployRegistration> {
constructor(private readonly goalDetailsOrUniqueName: FulfillableGoalDetails | string = DefaultGoalNameGenerator.generateName("docker-deploy"),
...dependsOn: Goal[]) {
super({
...DockerDeployGoal.definition,
...getGoalDefintionFrom(goalDetailsOrUniqueName, DefaultGoalNameGenerator.generateName("docker-deploy")),
displayName: "version",
}, ...dependsOn);
}

public with(registration: DockerDeployRegistration): this {
this.addFulfillment({
goalExecutor: executeDockerRun( {
successPatterns: registration.successPatterns,
lowerPort: registration.lowerPort ? registration.lowerPort : 9090,
baseUrl: registration.baseUrl ? registration.baseUrl : "http://localhost",
sourcePort: registration.sourcePort,
}),
name: DefaultGoalNameGenerator.generateName("docker-runner"),
...registration as ImplementationRegistration,
});
return this;
}
}

function executeDockerRun(options: DockerPerBranchDeployerOptions): ExecuteGoal {
const deployer = new DockerPerBranchDeployer(options);
return async (goalInvocation: GoalInvocation): Promise<ExecuteGoalResult> => {
return await deployer.deployProject(goalInvocation).then(deployment => {
return {
code: 0,
targetUrl: deployment.endpoint,
};
},
).catch(reason => {
return {
code: 1,
message: reason,
};
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,40 @@ import { DelimitedWriteProgressLogDecorator } from "@atomist/sdm/api-helper/log/
import { spawn } from "child_process";
import * as portfinder from "portfinder";

export interface DockerRunnerOptions {
/**
* Options for the DockerPerBranchDeployer
*/
export interface DockerPerBranchDeployerOptions {
/**
* Starting port to be scanned for free ports
*/
lowerPort: number;
/**
* Pattern that indicates that the container has started up correctly
*/
successPatterns: RegExp[];
/**
* Base URL for the docker container. Probably localhost or your Docker machine IP
*/
baseUrl: string;
/**
* The exposed port in the Dockerfile to be mapped externally
*/
sourcePort: number;
}

export class DockerRunner {
/**
* Deployer that uses `docker run` in order to deploy images produces by the `DockerBuild` goal.
*/
export class DockerPerBranchDeployer {

// Already allocated ports
public readonly repoBranchToPort: { [repoAndBranch: string]: number } = {};

// Keys are ports: values are containerIds
private readonly portToContainer: { [port: number]: string } = {};

constructor(private readonly options: DockerRunnerOptions) {}
constructor(private readonly options: DockerPerBranchDeployerOptions) {}

public async deployProject(goalInvocation: GoalInvocation): Promise<SpawnedDeployment> {
const branch = goalInvocation.sdmGoal.branch;
Expand Down Expand Up @@ -104,9 +122,9 @@ export class DockerRunner {
if (this.options.successPatterns.some(successPattern => successPattern.test(stdout))) {
resolve(deployment);
} else {
logger.error("Maven deployment failure vvvvvvvvvvvvvvvvvvvvvv");
logger.error("Docker deployment failure vvvvvvvvvvvvvvvvvvvvvv");
logger.error("stdout:\n%s\nstderr:\n%s\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", stdout, stderr);
reject(new Error("Maven deployment failure"));
reject(new Error("Docker deployment failure"));
}
});
childProcess.addListener("error", reject);
Expand Down
81 changes: 0 additions & 81 deletions lib/docker/DockerRun.ts

This file was deleted.

6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export {
DockerBuildRegistration,
} from "./docker/DockerBuild";
export {
DockerRun,
DockerRunRegistration,
} from "./docker/DockerRun";
DockerDeploy,
DockerDeployRegistration,
} from "./docker/DockerDeploy";
export {
DockerProgressReporter,
DockerProgressTests,
Expand Down
Loading

0 comments on commit 7fe55fb

Please sign in to comment.