Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
setup container:run command
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Dec 8, 2017
1 parent 02cfeee commit 7d2d878
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ $ heroku plugins:install heroku-container-registry
In a directory with a Dockerfile:

```
$ heroku container:run web
$ heroku container:login
$ heroku create
$ heroku container:push
Expand Down
79 changes: 79 additions & 0 deletions commands/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const cli = require('heroku-cli-util')
const Sanbashi = require('../lib/sanbashi')

let usage = `
${ cli.color.bold.underline.magenta('Usage:')}
${ cli.color.cmd('heroku container:run web bash')} # Runs bash on the local web docker container
${ cli.color.cmd('heroku container:run worker')} # Runs the container CMD on the local worker container`

module.exports = function (topic) {
return {
topic: topic,
command: 'run',
description: 'builds, then runs the docker image locally',
variableArgs: true,
help: usage,
flags: [
{
name: 'verbose',
char: 'v',
hasValue: false
},
{
name: 'arg',
hasValue: true,
description: 'set build-time variables'
}
],
run: cli.command(run)
}
}

let run = async function (context, heroku) {
if (context.args.length === 0) {
cli.error(`Error: Requires one process type\n ${usage} `)
process.exit(1)
}

let processType = context.args.shift()
let command = context.args
let tag = `heroku-local-${processType}`
let dockerfiles = Sanbashi.getDockerfiles(process.cwd(), false)
let possibleJobs = Sanbashi.getJobs(tag, dockerfiles)

let jobs = []
if (possibleJobs.standard) {
possibleJobs.standard.forEach((pj) => { pj.resource = pj.resource.replace(/standard$/, processType)})
jobs = possibleJobs.standard || []
}
if (!jobs.length) {
cli.warn('No images to push')
process.exit(1)
}

let job = jobs[0]
let flagsArg = context.flags.arg
let buildArg = (flagsArg !== undefined) ? flagsArg.split(',') : []

try {
cli.styledHeader(`Building ${processType} (${job.dockerfile })`)
await Sanbashi.buildImage(job.dockerfile, job.resource, context.flags.verbose, buildArg)
} catch (err) {
cli.error(`Error: docker build exited with ${ err }`)
cli.hush(err.stack || err)
process.exit(1)
}

if (command == '') {
cli.styledHeader(`Running web`)
} else {
cli.styledHeader(`Running '${command}' on ${tag}`)
}
try {
await Sanbashi.runImage(job.resource, command, context.flags.verbose)
} catch(err) {
cli.error(`Error: docker run exited with ${ err }`)
cli.hush(err.stack || err)
process.exit(1)
}
}
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
require('./commands/index')(pkg),
require('./commands/login')(pkg.topic),
require('./commands/logout')(pkg.topic),
require('./commands/push')(pkg.topic)
require('./commands/push')(pkg.topic),
require('./commands/run')(pkg.topic)
]
}
11 changes: 11 additions & 0 deletions lib/sanbashi.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ Sanbashi.pushImage = function (resource, verbose) {
return Sanbashi.cmd('docker', args)
}

Sanbashi.runImage = function (resource, command, verbose) {
let args = ['run']
if (command == '') {
args.push(resource)
} else {
args.push('-it', resource, command)
}
log(verbose, args)
return Sanbashi.cmd('docker', args)
}

Sanbashi.cmd = function (cmd, args) {
return new Promise((resolve, reject) => {
Child.spawn(cmd, args, {stdio: 'inherit'})
Expand Down

0 comments on commit 7d2d878

Please sign in to comment.