Last active
April 29, 2019 15:47
-
-
Save ahathaway/2afd0d810345fc6c85ad3c3e0f12d41c to your computer and use it in GitHub Desktop.
Deploy class for lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Deploy { | |
| constructor(S3, SSH, FS) { | |
| this.s3 = S3; | |
| this.SSH = SSH; | |
| this.fs = FS; | |
| this.msgString = null; | |
| this.zip_extract_path = '/home/golfmds/deploy/thriv-golfnow'; | |
| this.deploy_base_path = '/home/golfmds/thriv-golfnow'; | |
| this.deploy_base_path_suffix = '/view-applet_golfnow'; | |
| this.envs = { | |
| golfnow: { | |
| environment: 'golfnow', | |
| hosts: ['ip-10-42-52-50.ec2.internal'], | |
| symlink: '/home/golfmds/golfnow/golfmds/templates/view-applet_golfnow', | |
| user: 'ec2-user' | |
| }, | |
| dev: { | |
| environment: 'dev', | |
| hosts: ['ip-10-42-52-50.ec2.internal'], | |
| symlink: '/home/golfmds/ga/templates/view-applet_golfnow', | |
| user: 'ec2-user' | |
| }, | |
| qa: { | |
| environment: 'qa', | |
| hosts: ['ip-10-42-52-50.ec2.internal'], | |
| symlink: '/home/golfmds/qa/golfmds/templates/view-applet_golfnow', | |
| user: 'ec2-user' | |
| }, | |
| // stage: { | |
| // environment: 'stage', | |
| // hosts: ['ip-10-44-63-82.ec2.internal', 'ip-10-44-59-35.ec2.internal'], | |
| // symlink: '/home/golfmds/ga/templates/view-applet_golfnow' | |
| // } | |
| }; | |
| this.deploy_package = null; | |
| this.pkey = this.fs.readFileSync("keys/thriv-dev.pem"); | |
| this.snsMsgObject = null; | |
| this.srcBucket = null; | |
| this.srcKey = null; | |
| this.srcEnvPrefix = null; | |
| this.srcFileName = null; | |
| this.connectionConfig = { | |
| host: null, | |
| user: null, | |
| key: this.pkey | |
| }; | |
| this.ssh_connection = null; | |
| } | |
| startDeploymentProcess(event, callback) { | |
| console.log(event); | |
| this.msgString = JSON.stringify(event.Records[0].Sns.Message); | |
| this.callback = callback; | |
| this.processSNSEvent | |
| .then(this.getDeployPackage(result)) | |
| .then(this.setConnectionConfig(result)) | |
| .then(this.establishSshConnection(result)) | |
| .then(this.deployPackageToEc2(result)) | |
| ; | |
| } | |
| processSNSEvent = new Promise(function (resolve, reject) { | |
| this.snsMsgObject = this.extractSNSMessageObject(this.msgString); | |
| this.srcBucket = this.snsMsgObject.Records[0].s3.bucket.name; | |
| this.srcKey = this.snsMsgObject.Records[0].s3.object.key; | |
| this.srcEnvPrefix = this.srcKey.substring(0, this.srcKey.indexOf("/")); | |
| this.srcFileName = this.srcKey.substring(this.srcKey.indexOf('/') + 1, this.srcKey.length); | |
| console.log('SRC Bucket: ' + this.srcBucket); | |
| console.log('SRC Key: ' + this.srcKey); | |
| console.log('SRC Prefix: ' + this.srcEnvPrefix); | |
| resolve('SNS event processed'); | |
| }); | |
| extractSNSMessageObject(msgString) { | |
| var x = msgString.replace(/\\/g, ''); | |
| var y = x.substring(1, x.length - 1); | |
| var z = JSON.parse(y); | |
| return z; | |
| } | |
| getDeployPackage() { | |
| console.log("grabbing deployment package from s3"); | |
| let params = { | |
| Bucket: this.srcBucket, | |
| Key: this.srcKey | |
| }; | |
| this.s3.getObject(params, function (err, data) { | |
| if (err) { | |
| console.log(err, err.stack); | |
| let message = `Error getting object ${params.Key} from bucket ${params.Bucket}. Make sure they exist and your bucket is in the same region as this function.`; | |
| console.log(message); | |
| this.callback(message); | |
| } else { | |
| this.deploy_package = data; | |
| let message = 'got deployment package'; | |
| console.log(message); | |
| return (message); | |
| } | |
| }); | |
| } | |
| setConnectionConfig() { | |
| if (this.srcEnvPrefix && this.srcEnvPrefix !== null && this.srcEnvPrefix !== '') { | |
| this.connectionConfig.host = this.envs[this.srcEnvPrefix].hosts[0]; | |
| this.connectionConfig.user = this.envs[this.srcEnvPrefix].user; | |
| let message = 'Connection configuration set.'; | |
| console.log(message); | |
| return message; | |
| } else { | |
| let message = 'Error: missing environment prefix.'; | |
| console.log(message); | |
| return message; | |
| } | |
| } | |
| establishSshConnection() { | |
| this.ssh_connection = new this.SSH(this.connectionConfig); | |
| let m = 'ssh connection established'; | |
| console.log(m); | |
| return m; | |
| } | |
| deployPackageToEc2() { | |
| let fullzipfilepath = `${this.zip_extract_path}/${this.srcEnvPrefix}/${this.srcFileName}`; | |
| let fulldeploypath = `${this.zip_extract_path}/${this.srcEnvPrefix}/ ${this.deploy_base_path}/${this.srcEnvPrefix}/${this.deploy_base_path_suffix}`; | |
| /* -- execute SSH command -- */ | |
| this.ssh_connection.exec('sudo su - golfmds') | |
| .exec(`mkdir -p ${fulldeploypath}`, { | |
| out: function (stdout) { | |
| console.log('mkdir for deploy env'); | |
| console.log(stdout); | |
| } | |
| }) | |
| .exec(`mkdir -p ${this.zip_extract_path}/${this.srcEnvPrefix}`, { | |
| out: function (stdout) { | |
| console.log('mkdir for zip extract env'); | |
| console.log(stdout); | |
| } | |
| }) | |
| .exec(`cd ${this.zip_extract_path}/${this.srcEnvPrefix}`) | |
| .exec('ls -al', { | |
| out: function (stdout) { | |
| console.log('change directory to zip extract path'); | |
| console.log(stdout); | |
| } | |
| }) | |
| .exec(`cat > ${fullzipfilepath}`, { | |
| in: this.fs.readFileStream(this.deploy_package.Body) | |
| }) | |
| .exec('ls -al', { | |
| out: function (stdout) { | |
| console.log('push zip file to deploy zip extract location '); | |
| console.log(stdout); | |
| } | |
| }) | |
| .exec(`unzip -o ${fullzipfilepath}`) | |
| .exec('ls -al', { | |
| out: function (stdout) { | |
| console.log('extract zip, overwriting all files'); | |
| console.log(stdout); | |
| } | |
| }) | |
| .exec(`rsync -r --delete-before ${this.zip_extract_path}/${this.srcEnvPrefix}/ ${fulldeploypath}`) | |
| .exec('ls -al', { | |
| out: function (stdout) { | |
| console.log('rsync files over from extraction location to running location'); | |
| console.log(stdout); | |
| } | |
| }) | |
| .exec(`ln -sfn ${fulldeploypath} ${this.envs[this.srcEnvPrefix].symlink}`) | |
| .exec('ls -al', { | |
| out: function (stdout) { | |
| console.log('setup symlink'); | |
| console.log(stdout); | |
| }, | |
| exit: function (code, stdout, stderr) { | |
| console.log('operation exited with code: ' + code); | |
| console.log('STDOUT from EC2:\n' + stdout); | |
| console.log('STDERR from EC2:\n' + stderr); | |
| context.succeed('Success!'); | |
| } | |
| }).start(); | |
| } | |
| } | |
| module.exports = Deploy; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment