|
| 1 | +import AWS from "aws-sdk"; |
| 2 | +import axios from "axios"; |
| 3 | +import through from "through2"; |
| 4 | +import Promise from "bluebird"; |
| 5 | +import crypto from "crypto"; |
| 6 | +import confirm from "gulp-confirm"; |
| 7 | +import gulp from "gulp"; |
| 8 | +import isString from "lodash/isString"; |
| 9 | +import fs from "fs"; |
| 10 | +import path from "path"; |
| 11 | +import yargs from "yargs"; |
| 12 | + |
| 13 | + |
| 14 | +export function readPackageJson() { |
| 15 | + return JSON.parse( fs.readFileSync( path.join( process.cwd(), "package.json" ) ) ); |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +export function getDeploySignature( body ) { |
| 20 | + const ssm = new AWS.SSM( { region: "us-east-1" } ); |
| 21 | + const getParameter = Promise.promisify( ssm.getParameter, { context: ssm } ); |
| 22 | + return getParameter( { Name: "/textpress-ci/signatureSecret", WithDecryption: true } ) |
| 23 | + .then( result => { |
| 24 | + const signatureSecret = result.Parameter.Value; |
| 25 | + if ( !signatureSecret ) |
| 26 | + throw new Error( "Could not obtain signature secret" ); |
| 27 | + |
| 28 | + const hmac = "sha1"; |
| 29 | + const hash = crypto.createHmac( hmac, signatureSecret ) |
| 30 | + .update( body ) |
| 31 | + .digest( "hex" ); |
| 32 | + return `${hmac}=${hash}`; |
| 33 | + } ); |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +export function makeDeployRequest( body ) { |
| 38 | + return getDeploySignature( body ) |
| 39 | + .then( singature => { |
| 40 | + const client = axios.create( { |
| 41 | + baseURL: "https://zp1v6aciyk.execute-api.us-east-1.amazonaws.com", |
| 42 | + headers: { "X-Hub-Signature": singature } |
| 43 | + } ); |
| 44 | + |
| 45 | + return client.post( "/production/deploy-from-s3", body ); |
| 46 | + } ); |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +export function deploy( argv ) { |
| 51 | + const version = argv.version; |
| 52 | + if ( !isString( version ) ) |
| 53 | + throw new Error( "Version is missing, usage: yarn deploy -v 0.0.1 -s staging" ); |
| 54 | + |
| 55 | + const stage = argv.stage === true ? "staging" : argv.stage; |
| 56 | + if ( [ "development", "staging", "production" ].indexOf( stage ) === -1 ) |
| 57 | + throw new Error( "Stage is missing, usage: yarn deploy -v 0.0.1 -s staging" ); |
| 58 | + |
| 59 | + const packageJson = readPackageJson(); |
| 60 | + return gulp.src( "" ) |
| 61 | + .pipe( confirm( { |
| 62 | + question: `\x1B[37mDeploy version \x1B[4m\x1B[36m${version}\x1B[24m\x1B[37m to \x1B[4m\x1B[36m${stage}\x1B[24m\x1B[37m?\x1B[22m`, |
| 63 | + input: "_key:y" |
| 64 | + } ) ) |
| 65 | + .pipe( through.obj( function ( chunk, enc, cb ) { |
| 66 | + const _this = this; |
| 67 | + |
| 68 | + const body = JSON.stringify( { |
| 69 | + repository: `${packageJson.author}/${packageJson.name}`, |
| 70 | + version, |
| 71 | + stage |
| 72 | + } ); |
| 73 | + |
| 74 | + makeDeployRequest( body ) |
| 75 | + .then( () => { cb(); } ) |
| 76 | + .catch( x => { |
| 77 | + _this.emit( "error", x ); |
| 78 | + cb(); |
| 79 | + } ); |
| 80 | + } ) ); |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +export function registerTask() { |
| 85 | + gulp.task( "deploy", () => deploy( yargs.alias( "v", "version" ).alias( "s", "stage" ).argv ) ); |
| 86 | +} |
| 87 | + |
| 88 | +export default deploy; |
0 commit comments