Created
March 17, 2018 23:40
-
-
Save Jpuelpan/a1d63a42d24df83da42dd5396c234630 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env node | |
const fs = require('fs'); | |
const path = require('path'); | |
const exec = require('child_process').execSync; | |
const uuid = require('uuid'); | |
const S3_BUCKET = 'YOUR_BUCKET_NAME'; | |
const [ , , ...args] = process.argv; | |
const deployJob = (targetArns) => { | |
const jobId = uuid.v1(); | |
const zipFile = `${jobId}.zip` | |
const compressCommand = `find . -not -name '*.zip*' -not -path '*.git*' | xargs zip ${zipFile}`; | |
exec(compressCommand); | |
const uploadCommand = `aws s3api put-object --bucket ${S3_BUCKET} --key jobs/${zipFile} --body ${zipFile}` | |
exec(uploadCommand); | |
const presignCommand = `aws s3 presign s3://${S3_BUCKET}/jobs/${zipFile} --expires-in 3600`; | |
const presignUrl = exec(presignCommand).toString().trim() | |
// Creating Job command requirements | |
const targets = targetArns.map(item => `"${item}"`).join(" ") | |
const jobDocument = { | |
"type": "deploy", | |
"data": { | |
"application": "http-gateway", | |
"source": presignUrl | |
} | |
} | |
const deployCommand = `aws iot create-job --job-id ${jobId} --targets ${targets} --document '${JSON.stringify(jobDocument)}'` | |
const commandResult = JSON.parse(exec(deployCommand).toString().trim()); | |
console.log(commandResult); | |
} | |
if( args.length == 0 ){ | |
console.error('No targets given') | |
}else{ | |
deployJob(args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment