Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_js:
script:
- echo "Smoke testing d2 executable"
- ./packages/main/bin/d2 debug system
- yarn test
deploy:
- provider: script
skip_cleanup: true
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
"license": "BSD-3-Clause",
"devDependencies": {
"@dhis2/cli-style": "3.2.2",
"husky": "^2.0.0"
"husky": "^2.0.0",
"tape": "^4.10.1"
},
"husky": {
"hooks": {
"commit-msg": "d2-style commit check",
"pre-commit": "d2-style js apply"
}
},
"scripts": {
"test": "tape packages/**/tests/*.js"
}
}
6 changes: 1 addition & 5 deletions packages/cluster/src/commands/down.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const chalk = require('chalk')
const path = require('path')
const { exec, reporter } = require('@dhis2/cli-helpers-engine')
const {
initDockerComposeCache,
makeComposeProject,
makeDockerImage,
} = require('../common')
const { initDockerComposeCache, makeComposeProject } = require('../common')

const defaults = require('../defaults')

Expand Down
6 changes: 1 addition & 5 deletions packages/cluster/src/commands/logs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const chalk = require('chalk')
const path = require('path')
const { reporter, exec, tryCatchAsync } = require('@dhis2/cli-helpers-engine')
const {
initDockerComposeCache,
makeComposeProject,
makeDockerImage,
} = require('../common')
const { initDockerComposeCache, makeComposeProject } = require('../common')
const defaults = require('../defaults')

const run = async function({ service, name, ...argv }) {
Expand Down
11 changes: 4 additions & 7 deletions packages/cluster/src/commands/restart.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const chalk = require('chalk')
const path = require('path')
const { reporter, exec, tryCatchAsync } = require('@dhis2/cli-helpers-engine')
const {
initDockerComposeCache,
makeComposeProject,
makeDockerImage,
} = require('../common')
const { initDockerComposeCache, makeComposeProject } = require('../common')

const defaults = require('../defaults')

Expand Down Expand Up @@ -53,9 +49,10 @@ module.exports = {
builder: {
port: {
alias: 'p',
desc: 'Specify the port on which to expose the DHIS2 instance',
desc: `Specify the port on which to expose the DHIS2 instance (default: ${
defaults.port
})`,
type: 'integer',
default: defaults.port,
},
},
handler: run,
Expand Down
1 change: 0 additions & 1 deletion packages/cluster/src/commands/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ module.exports = {
dhis2Version: {
desc: 'DHIS2 version to use',
type: 'string',
default: '',
},
},
handler: run,
Expand Down
65 changes: 24 additions & 41 deletions packages/cluster/src/commands/up.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,21 @@ const { reporter, exec, tryCatchAsync } = require('@dhis2/cli-helpers-engine')
const {
initDockerComposeCache,
makeComposeProject,
makeDockerImage,
substituteVersion,
makeEnvironment,
} = require('../common')

const defaults = require('../defaults')
const { seed: doSeed } = require('../db')

const run = async function({
name,
port,
seed,
seedFile,
update,
image,
dhis2Version,
customContext,
...argv
}) {
const { cluster } = argv
const run = async function(argv) {
const { cluster, name, seed, seedFile, update } = argv

const resolvedVersion = dhis2Version || name
const resolvedImage = substituteVersion(
image || cluster.image || defaults.image,
resolvedVersion
)

const resolvedPort = port || cluster.port || defaults.port
const resolvedContext =
customContext || cluster.customContext || defaults.customContext
const resolvedContextPath = resolvedContext ? `/${name}` : ''
const runtime = makeEnvironment(argv, {}, cluster)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second empty object is going to be replaced with the per-cluster cached configuration. The cluster object is the default configuration from ~/.config/d2/config.js.


const cacheLocation = await initDockerComposeCache({
cache: argv.getCache(),
dockerComposeRepository:
argv.cluster.dockerComposeRepository ||
defaults.dockerComposeRepository,
cluster.dockerComposeRepository || defaults.dockerComposeRepository,
force: update,
})

Expand All @@ -51,7 +30,7 @@ const run = async function({
if (seed || seedFile) {
await doSeed({
cacheLocation,
dbVersion: resolvedVersion,
dbVersion: runtime.DHIS2_CORE_DB_VERSION,
name,
path: seedFile,
update,
Expand All @@ -64,6 +43,7 @@ const run = async function({
const res = await tryCatchAsync(
'exec(docker-compose)',
exec({
env: runtime,
cmd: 'docker-compose',
args: [
'-p',
Expand All @@ -73,13 +53,6 @@ const run = async function({
'up',
'-d',
],
env: {
DHIS2_CORE_NAME: name,
DHIS2_CORE_CONTEXT_PATH: resolvedContextPath,
DHIS2_CORE_IMAGE: resolvedImage,
DHIS2_CORE_VERSION: resolvedVersion,
DHIS2_CORE_PORT: resolvedPort,
},
pipe: true,
})
)
Expand All @@ -96,15 +69,15 @@ module.exports = {
builder: {
port: {
alias: 'p',
desc: 'Specify the port on which to expose the DHIS2 instance',
desc: `Specify the port on which to expose the DHIS2 instance (default: ${
defaults.port
})`,
type: 'integer',
default: defaults.port,
Comment thread
varl marked this conversation as resolved.
},
seed: {
alias: 's',
desc: 'Seed the detabase from a sql dump',
type: 'boolean',
default: defaults.seed,
},
seedFile: {
desc:
Expand All @@ -115,24 +88,34 @@ module.exports = {
alias: 'u',
desc: 'Indicate that d2 cluster should re-download cached files',
type: 'boolean',
default: defaults.update,
},
image: {
alias: 'i',
desc: 'Specify the Docker image to use',
type: 'string',
default: defaults.image,
},
dhis2Version: {
desc: 'Set the DHIS2 version',
type: 'string',
default: defaults.dhis2Version,
},
dbVersion: {
desc: 'Set the database version',
type: 'string',
},
channel: {
desc: `Set the release channel to use (default: ${
defaults.channel
})`,
type: 'string',
},
customContext: {
alias: 'c',
desc: 'Serve on a custom context path',
type: 'boolean',
default: defaults.customContext,
},
variant: {
desc: 'Append variant options to the image',
type: 'string',
},
},
handler: run,
Expand Down
78 changes: 76 additions & 2 deletions packages/cluster/src/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { reporter } = require('@dhis2/cli-helpers-engine')

const defaults = require('./defaults')

const dockerComposeCacheName = 'd2-cluster-docker-compose-v2'

module.exports.initDockerComposeCache = async ({
Expand Down Expand Up @@ -33,8 +35,80 @@ module.exports.initDockerComposeCache = async ({
}

module.exports.makeComposeProject = version => `d2-cluster-${version}`
module.exports.makeDockerImage = version => `${version}-alpine`

module.exports.substituteVersion = (string, version) =>
string.replace(/{version}/g, version)
replacer(string, 'version', version)

function makeDockerImage(string = '', substitutes = {}, variant = '') {
let res = string

// the stable channel is just dhis2/core, so if the channel is
// unspecified or 'stable', we should just strip it out from the
// image tag
if (!substitutes.channel || substitutes.channel === 'stable') {
res = replacer(res, 'channel', '')
}

for (const token in substitutes) {
const value =
token === 'channel'
? `-${substitutes[token]}` // add a leading '-' to channel
: substitutes[token]

res = replacer(res, token, value)
}

if (variant) {
res = `${res}-${variant}`
}
return res
}

function replacer(string, token, value) {
const regexp = new RegExp(`{${token}}`, 'g')
return string.replace(regexp, value)
}

function resolveConfiguration(argv = {}, cache = {}, config = {}) {
const resolved = Object.assign({}, defaults, config, cache, argv)

// resolve specials...
resolved.dhis2Version = resolved.dhis2Version || resolved.name
resolved.dbVersion = resolved.dbVersion || resolved.dhis2Version
resolved.contextPath = resolved.customContext ? `/${resolved.name}` : ''

resolved.dockerImage = makeDockerImage(
resolved.image,
{
channel: resolved.channel,
version: resolved.dhis2Version,
},
resolved.variant
)

reporter.debug('Resolved configuration\n', resolved)

return resolved
}

module.exports.makeEnvironment = (argv = {}, cache = {}, config = {}) => {
const cfg = resolveConfiguration(argv, cache, config)

const env = {
DHIS2_CORE_NAME: cfg.name,
DHIS2_CORE_IMAGE: cfg.dockerImage,
DHIS2_CORE_CONTEXT_PATH: cfg.contextPath,
DHIS2_CORE_VERSION: cfg.dhis2Version,
DHIS2_CORE_DB_VERSION: cfg.dbVersion,
DHIS2_CORE_PORT: cfg.port,
}

reporter.debug('Runtime environment\n', env)

return env
}

module.exports.getLocalClusters = async () => {}

module.exports.makeDockerImage = makeDockerImage
module.exports.resolveConfiguration = resolveConfiguration
4 changes: 3 additions & 1 deletion packages/cluster/src/defaults.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = {
dhis2Version: '',
image: 'amcgee/dhis2-core:{version}-alpine',
dbVersion: '',
channel: 'stable',
image: 'dhis2/core{channel}:{version}',
port: 8080,
customContext: false,
update: false,
Expand Down
7 changes: 4 additions & 3 deletions packages/cluster/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ const { namespace } = require('@dhis2/cli-helpers-engine')
const command = namespace('cluster', {
desc: 'Manage DHIS2 Docker clusters',
aliases: 'c',
builder: yargs => {
yargs.commandDir('commands')
},
builder: yargs =>
yargs.commandDir('commands').parserConfiguration({
'parse-numbers': false,
}),
})

module.exports = command
Loading