Skip to content

Commit

Permalink
move to typescript and new cdk provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Walker committed Nov 4, 2020
1 parent a534c76 commit d3b8ba2
Show file tree
Hide file tree
Showing 19 changed files with 2,172 additions and 221 deletions.
9 changes: 4 additions & 5 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env node
"use strict";
const deployer = require('../lib/deployer');
const minimist = require('minimist');

const run = async () => {
const args = minimist(process.argv);
process.argv.forEach(arg => process.argv.pop(arg));
process.argv.forEach(arg => delete process.argv[process.argv.indexOf(arg)]);
const file = args._[3];
const command = args._[2];
const deploy = new deployer(command, file);
await deploy.run();
}

run();
};
run();
14 changes: 14 additions & 0 deletions bin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node
const deployer = require('../lib/deployer');
const minimist = require('minimist');

const run = async () => {
const args = minimist(process.argv);
process.argv.forEach(arg => delete process.argv[process.argv.indexOf(arg)]);
const file = args._[3];
const command = args._[2];
const deploy = new deployer(command, file);
await deploy.run();
}

run();
48 changes: 27 additions & 21 deletions lib/deployer.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,66 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const parseYaml = require('./utils/parseYaml');
const ServerlessV1 = require('./providers/serverless');
const cdk_1 = require("./providers/cdk");
const serverless_1 = require("./providers/serverless");
const HardCoded = require('./providers/hardcoded');
const DsicollectionDynamicEnvironment = require('./providers/dsicollectionDynamicEnvironment');
const environmentService = require('./service/environmentService');

class Deployer {
constructor(command, file) {
this.file = file;
this.command = command;
this.component = this.parseComponentTemplate(this.file);
console.log(this.component);
}

async run() {
if(this.command === 'deploy') {
if (this.command === 'deploy') {
await this.deploy();
} else if(this.command === 'destroy') {
}
else if (this.command === 'destroy') {
await this.destroy();
} else {
}
else {
console.error(`The command ${this.command} is not implemented`);
}
}

async deploy() {
let provider
switch(this.component.provider) {
let provider;
let providerType;
if (this.component.provider instanceof String) {
providerType = this.component.provider;
}
else {
providerType = this.component.provider.name;
}
switch (providerType) {
case 'serverless-framework':
provider = new ServerlessV1(this.component);
provider = new serverless_1.ServerlessV1(this.component);
break;
case 'hardcoded':
provider = new HardCoded(this.component);
break;
case 'dsicollection-dynamic-environment':
provider = new DsicollectionDynamicEnvironment(this.component);
break;
case 'cdk':
provider = new cdk_1.CDK(this.component.name, this.component.env, this.component.provider.config, this.component.input);
break;
default:
throw(`The provider ${this.component.provider} is not implemented!`);
throw (`The provider ${providerType} is not implemented!`);
}

const deployResp = await provider.deploy();

// Store the component in the environment service with it's outputs
if(deployResp.outputs) {
await deployResp.outputs.forEach(output => {
environmentService.putComponentOutput(this.component.env, this.component.name, output.key, output.value)
if (deployResp.outputs) {
await deployResp.outputs.forEach((output) => {
environmentService.putComponentOutput(this.component.env, this.component.name, output.key, output.value);
});
}
}

async destroy() {

}

parseComponentTemplate(file) {
return parseYaml(file);
}
}

module.exports = Deployer;
module.exports = Deployer;
74 changes: 74 additions & 0 deletions lib/deployer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const parseYaml = require('./utils/parseYaml');
import { CDK } from './providers/cdk';
import { ServerlessV1 } from './providers/serverless';
const HardCoded = require('./providers/hardcoded');
const DsicollectionDynamicEnvironment = require('./providers/dsicollectionDynamicEnvironment');
const environmentService = require('./service/environmentService');

class Deployer {
public file: any;
public command: any;
public component: any;

constructor(command: any, file: string) {
this.file = file;
this.command = command;
this.component = this.parseComponentTemplate(this.file);
console.log(this.component);
}

async run() {
if(this.command === 'deploy') {
await this.deploy();
} else if(this.command === 'destroy') {
await this.destroy();
} else {
console.error(`The command ${this.command} is not implemented`);
}
}

async deploy() {
let provider
let providerType;
if(this.component.provider instanceof String) {
providerType = this.component.provider;
} else {
providerType = this.component.provider.name;
}
switch(providerType) {
case 'serverless-framework':
provider = new ServerlessV1(this.component);
break;
case 'hardcoded':
provider = new HardCoded(this.component);
break;
case 'dsicollection-dynamic-environment':
provider = new DsicollectionDynamicEnvironment(this.component);
break;
case 'cdk':
provider = new CDK(this.component.name, this.component.env, this.component.provider.config, this.component.input);
break;
default:
throw(`The provider ${providerType} is not implemented!`);
}

const deployResp = await provider.deploy();

// Store the component in the environment service with it's outputs
if(deployResp.outputs) {
await deployResp.outputs.forEach((output: any) => {
environmentService.putComponentOutput(this.component.env, this.component.name, output.key, output.value)
});
}
}

async destroy() {

}

parseComponentTemplate(file: string) {
return parseYaml(file);
}
}

module.exports = Deployer;
73 changes: 73 additions & 0 deletions lib/providers/cdk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CDK = void 0;
const cdk = require("@aws-cdk/core");
const aws_auth_1 = require("aws-cdk/lib/api/aws-auth");
const cloudformation_deployments_1 = require("aws-cdk/lib/api/cloudformation-deployments");
class CDK {
constructor(name, env, config, props) {
this.name = name;
this.env = env;
this.config = config;
this.props = props;
}
async deploy() {
var _a;
const construct = await Promise.resolve().then(() => __importStar(require(process.cwd() + '/' + this.config.constructPath)));
const constructName = this.config.constructName ? this.config.constructName : Object.keys(construct)[0];
const componentName = this.name;
const env = this.env;
const componentProps = this.props;
const config = this.config;
class CdkStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
new construct[constructName](this, constructName, componentProps);
}
}
const app = new cdk.App();
new CdkStack(app, `${env}-${componentName}`, {
env: config.env
});
const assembly = app.synth();
const sdkProvider = await aws_auth_1.SdkProvider.withAwsCliCompatibleDefaults({});
const cloudformation = new cloudformation_deployments_1.CloudFormationDeployments({ sdkProvider: sdkProvider });
const stack = assembly.stacks[0];
let result;
try {
result = await cloudformation.deployStack({
stack,
deployName: stack.stackName
});
}
catch (err) {
throw err;
}
if (result.noOp) {
console.log(`Successfully deployed ${result.stackArtifact.stackName}!`);
}
return {
outputs: (_a = Object.entries(result.outputs)) === null || _a === void 0 ? void 0 : _a.map(output => { return { key: output[0], value: output[1] }; })
};
}
}
exports.CDK = CDK;
69 changes: 69 additions & 0 deletions lib/providers/cdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict'
import cdk = require('@aws-cdk/core');
import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
import { DeployStackResult } from 'aws-cdk/lib/api/deploy-stack';

export interface CDKProviderProps {
env: cdk.Environment;
constructPath: string;
constructName: string;
}

export class CDK {
readonly name: string;
readonly env: string;
readonly config: CDKProviderProps;
readonly props: any;

constructor(name: string, env: string, config: CDKProviderProps, props: any) {
this.name = name;
this.env = env;
this.config = config;
this.props = props;
}

async deploy() {
const construct = await import(process.cwd() + '/' + this.config.constructPath);
const constructName = this.config.constructName ? this.config.constructName : Object.keys(construct)[0];
const componentName = this.name;
const env = this.env;
const componentProps = this.props;
const config = this.config;
class CdkStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: any) {
super(scope, id, props);

new construct[constructName](this, constructName, componentProps);
}
}

const app = new cdk.App();
new CdkStack(app, `${env}-${componentName}`, {
env: config.env
});
const assembly = app.synth();

const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({});
const cloudformation = new CloudFormationDeployments({sdkProvider: sdkProvider});

const stack = assembly.stacks[0];
let result: DeployStackResult;
try {
result = await cloudformation.deployStack({
stack,
deployName: stack.stackName
});
} catch(err) {
throw err;
}

if(result.noOp) {
console.log(`Successfully deployed ${result.stackArtifact.stackName}!`)
}

return {
outputs: Object.entries(result.outputs)?.map(output => {return {key: output[0], value: output[1]}})
}
}
}
Loading

0 comments on commit d3b8ba2

Please sign in to comment.