Skip to content

Commit

Permalink
0.3.0
Browse files Browse the repository at this point in the history
 - use --dockerfile on deploy to parse custom dockerfile
 - pack all build files for docker build
 - env.LOG_LEVEL for custom log level
  • Loading branch information
dominicporteous committed Jun 12, 2020
1 parent 289ca02 commit b9bcc55
Show file tree
Hide file tree
Showing 5 changed files with 711 additions and 541 deletions.
9 changes: 8 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require('dotenv').config()

const fs = require('fs')
const path = require('path')
const yargs = require('yargs')
const logger = require('../lib/logger')

Expand Down Expand Up @@ -151,6 +153,7 @@ const deploy = async (argv) => {
const registry = argv.registry
const force = argv.force
const failFast = argv.failFast
const dockerfile = argv.dockerfile ? fs.readFileSync(path.resolve(process.cwd(), argv.dockerfile)) : ''

if (argv.insecureRegistry) {

Expand Down Expand Up @@ -284,7 +287,7 @@ const deploy = async (argv) => {

logger.info(`Starting deployment`)

const res = await sputnik.deploy()
const res = await sputnik.deploy({ dockerfile })

if (res) {

Expand Down Expand Up @@ -347,6 +350,10 @@ yargs
type: 'array',
description: 'Provide docker credentials for private registies (e.g --docker-auth username:password --docker-auth my.registry.local:5000:username:password)'
})
.option('dockerfile', {
type: 'string',
description: 'Path to custom Dockerfile for build. Defaults to a simple alpine node12 image'
})
.option('cwd', {
type: 'string',
description: 'Path to the working directory. Defaults to the cwd'
Expand Down
95 changes: 56 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const tar = require('tar-stream')
const tmp = require('tmp-promise')
const shell = require('shelljs')
const os = require('os')
const tplv = require('tplv')
const recursive = require('recursive-readdir')

class Sputnik extends EventEmitter {

Expand Down Expand Up @@ -252,7 +254,7 @@ class Sputnik extends EventEmitter {

}

async deploy () {
async deploy (argv) {

const self = this

Expand Down Expand Up @@ -305,6 +307,7 @@ class Sputnik extends EventEmitter {
const deploymentVersion = manifest.version

const file = path.resolve(deploymentDir, deployment).replace(`${this.config.cwd}/`, '')
const deploymentFiles = (await recursive(deploymentDir)).map((f) => f.replace(`${deploymentDir}/`, ''))

const [ index, ...parts ] = registry.split('/')

Expand Down Expand Up @@ -349,8 +352,9 @@ class Sputnik extends EventEmitter {

needsdeploy = true

const dockerfile = `
const dockerfile = argv.dockerfile
? tplv.render(argv.dockerfile, { deploymentName, deploymentVersion })
: `
FROM node:12-alpine
ENV APP_NAME="${deploymentName}"
Expand All @@ -367,80 +371,93 @@ CMD ["node", "/usr/src/app/main.js"]
const pack = tar.pack()

await pack.entry({ name: 'Dockerfile' }, dockerfile)
await pack.entry({ name: 'main.js' }, fs.readFileSync(deployment))

for (const file of deploymentFiles) {

await pack.entry({ name: file, type: 'file' }, fs.readFileSync(path.resolve(deploymentDir, file)))

}

await pack.finalize()

try {
pack.pipe(fs.createWriteStream(path.resolve(process.cwd(), './tarpack.tar')))

const buildStream = await docker.buildImage(pack, { t: tag })

await (new Promise((resolve, reject) => {

const stream = await docker.buildImage(pack, { t: tag })
const progress = (e) => {

await (new Promise((resolve, reject) => {
const { error, stream, aux } = e

const progress = ({ stream, aux }) => {
if (error) {

if (stream) {
return reject(new Error(error))

this.emit('deployment.image.build', { deployment, file, stdout: stream })
}

if (stream) {

}
this.emit('deployment.image.build', { deployment, file, stdout: stream })

if (aux) {
}

const hash = aux.ID
if (aux) {

this.emit('deployment.image.build.complete', { deployment, file, hash })
const hash = aux.ID

}
this.emit('deployment.image.build.complete', { deployment, file, hash })

}

docker.modem.followProgress(stream, resolve, progress)
}

stream.on('error', reject)
docker.modem.followProgress(buildStream, resolve, progress)

}))
buildStream.on('error', reject)

} finally {}
}))

try {
const image = await docker.getImage(tag)
const pushStream = await image.push({ registry, authconfig })

const image = await docker.getImage(tag)
const stream = await image.push({ registry, authconfig })
await (new Promise((resolve, reject) => {

await (new Promise((resolve, reject) => {
const progress = (e) => {

const progress = (e) => {
const { id, error, status, progressDetail } = e

const { id, status, progressDetail } = e
if (error) {

if (status && (status.toLowerCase() === 'pushed' || status.toLowerCase() === 'layer already exists')) {
return reject(new Error(error))

const layer = id
}

if (status && (status.toLowerCase() === 'pushed' || status.toLowerCase() === 'layer already exists')) {

this.emit('deployment.image.pushed', { deployment, file, tag, layer })
const layer = id

} else if (status && status.toLowerCase() === 'pushing') {
this.emit('deployment.image.pushed', { deployment, file, tag, layer })

const layer = id
} else if (status && status.toLowerCase() === 'pushing') {

const progress = progressDetail.current
? Math.floor((progressDetail.current / progressDetail.current) * 100)
: 0
const layer = id

this.emit('deployment.image.push', { deployment, file, tag, layer, progress })
const progress = progressDetail.current
? Math.floor((progressDetail.current / progressDetail.current) * 100)
: 0

}
this.emit('deployment.image.push', { deployment, file, tag, layer, progress })

}

docker.modem.followProgress(stream, resolve, progress)
}

stream.on('error', reject)
docker.modem.followProgress(pushStream, resolve, progress)

}))
pushStream.on('error', reject)

} finally {}
}))

} else {

Expand Down
2 changes: 1 addition & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const winston = require('winston')
const format = winston.format.combine(winston.format.colorize(), winston.format.simple())

const logger = winston.createLogger({
level: 'info',
level: process.env.LOG_LEVEL || 'info',
transports: [
new winston.transports.Console({ format, stderrLevels: [ 'info', 'warn', 'debug', 'trace', 'error' ] })
]
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sputnik",
"version": "0.2.2",
"version": "0.3.0",
"description": "An opinionated command line tool for building and deploying Node apps to a Kubernetes cluster",
"main": "index.js",
"bin": {
Expand All @@ -20,11 +20,13 @@
"hash.js": "^1.1.7",
"madge": "^3.6.0",
"merge-deep": "^3.0.2",
"recursive-readdir": "^2.2.2",
"shelljs": "^0.8.3",
"shortid32": "^0.1.1",
"simple-git": "^1.129.0",
"tar-stream": "^2.1.0",
"tmp-promise": "^2.0.2",
"tplv": "^1.0.0",
"webpack": "^4.41.4",
"winston": "^3.2.1",
"yargs": "^15.0.2"
Expand Down
Loading

0 comments on commit b9bcc55

Please sign in to comment.