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 Jan 2, 2018
1 parent 02cfeee commit d71fea3
Show file tree
Hide file tree
Showing 4 changed files with 1,015 additions and 1 deletion.
72 changes: 72 additions & 0 deletions commands/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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',
needsApp: true,
needsAuth: true,
variableArgs: true,
help: usage,
flags: [
{
name: 'port',
char: 'p',
hasValue: true,
description: 'port the app will run on'
},
{
name: 'verbose',
char: 'v',
hasValue: false
},
],
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 herokuHost = process.env.HEROKU_HOST || 'heroku.com'
let registry = `registry.${ herokuHost }`
let dockerfiles = Sanbashi.getDockerfiles(process.cwd(), false)
let possibleJobs = Sanbashi.getJobs(`${ registry }/${ context.app }`, 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 run')
process.exit(1)
}
let job = jobs[0]

if (command == '') {
cli.styledHeader(`Running ${job.resource}`)
} else {
cli.styledHeader(`Running '${command}' on ${job.resource}`)
}
try {
await Sanbashi.runImage(job.resource, command, context.flags.port || 5000, 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)
]
}
12 changes: 12 additions & 0 deletions lib/sanbashi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
let Glob = require('glob')
let Path = require('path')
let Inquirer = require('inquirer')
let os = require("os")
const Child = require('child_process')
const log = require('./log')

Expand Down Expand Up @@ -101,6 +102,17 @@ Sanbashi.pushImage = function (resource, verbose) {
return Sanbashi.cmd('docker', args)
}

Sanbashi.runImage = function (resource, command, port, verbose) {
let args = ['run', '--user', os.userInfo().uid, '-e', `PORT=${port}`]
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
Loading

0 comments on commit d71fea3

Please sign in to comment.