-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cluster db commands, update images on up, add compose cmd (#95
) * feat: add cluster db commands, update images on up, add compose cmd * chore: make sure we are catching errors at the CLI entrypoints * fix: use spawn with inherited stdio for local tty * fix: update help text and fix duplicated aliases
- Loading branch information
Showing
11 changed files
with
314 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const path = require('path') | ||
const { spawn } = require('child_process') | ||
const { reporter, tryCatchAsync } = require('@dhis2/cli-helpers-engine') | ||
const { | ||
initDockerComposeCache, | ||
makeComposeProject, | ||
makeEnvironment, | ||
resolveConfiguration, | ||
} = require('../common') | ||
|
||
const run = async function(argv) { | ||
const { name, _ } = argv | ||
const cfg = await resolveConfiguration(argv) | ||
|
||
const args = _.slice(_.findIndex(x => x === 'compose') + 1) | ||
reporter.debug('Passing arguments to docker-compose', args) | ||
|
||
const cacheLocation = await initDockerComposeCache({ | ||
composeProjectName: name, | ||
cache: argv.getCache(), | ||
dockerComposeRepository: cfg.dockerComposeRepository, | ||
dockerComposeDirectory: cfg.dockerComposeDirectory, | ||
force: false, | ||
}) | ||
if (!cacheLocation) { | ||
reporter.error('Failed to initialize cache...') | ||
process.exit(1) | ||
} | ||
const res = await tryCatchAsync( | ||
'exec(docker-compose)', | ||
spawn( | ||
'docker-compose', | ||
[ | ||
'-p', | ||
makeComposeProject(name), | ||
'-f', | ||
path.join(cacheLocation, 'docker-compose.yml'), | ||
...args, | ||
], | ||
{ | ||
env: { | ||
...process.env, | ||
...makeEnvironment(cfg), | ||
}, | ||
stdio: 'inherit', | ||
} | ||
) | ||
) | ||
if (res.err) { | ||
reporter.error('Failed to run docker-compose command') | ||
process.exit(1) | ||
} | ||
} | ||
|
||
module.exports = { | ||
command: 'compose <name>', | ||
desc: | ||
'Run arbitrary docker-compose commands against a DHIS2 cluster.\nNOTE: pass -- after <name>', | ||
aliases: 'c', | ||
handler: run, | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { namespace } = require('@dhis2/cli-helpers-engine') | ||
|
||
const command = namespace('db', { | ||
desc: 'Manage the database in a DHIS2 Docker cluster', | ||
builder: yargs => | ||
yargs.commandDir('db_cmds').parserConfiguration({ | ||
'parse-numbers': false, | ||
}), | ||
}) | ||
|
||
module.exports = command |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const { reporter, tryCatchAsync } = require('@dhis2/cli-helpers-engine') | ||
const { initDockerComposeCache, resolveConfiguration } = require('../../common') | ||
const { backup } = require('../../helpers/db') | ||
|
||
const run = async function(argv) { | ||
const { name, getCache, path, fat } = argv | ||
|
||
const cfg = await resolveConfiguration(argv) | ||
|
||
const cacheLocation = await initDockerComposeCache({ | ||
composeProjectName: name, | ||
cache: getCache(), | ||
dockerComposeRepository: cfg.dockerComposeRepository, | ||
dockerComposeDirectory: cfg.dockerComposeDirectory, | ||
force: false, | ||
}) | ||
|
||
if (!cacheLocation) { | ||
reporter.error('Failed to initialize cache...') | ||
process.exit(1) | ||
} | ||
|
||
const res = await tryCatchAsync( | ||
'db::backup', | ||
backup({ | ||
name, | ||
cacheLocation, | ||
path, | ||
fat, | ||
}) | ||
) | ||
|
||
if (res.err) { | ||
reporter.error('Failed to backup database') | ||
reporter.debugErr(res.err) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
module.exports = { | ||
command: 'backup <name> <path>', | ||
desc: 'Backup the database to a file', | ||
builder: { | ||
fat: { | ||
desc: 'Force re-download of cached files', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
}, | ||
handler: run, | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const { reporter, tryCatchAsync } = require('@dhis2/cli-helpers-engine') | ||
const { initDockerComposeCache, resolveConfiguration } = require('../../common') | ||
const { restore } = require('../../helpers/db') | ||
|
||
const run = async function(argv) { | ||
const { name, getCache } = argv | ||
|
||
const cfg = await resolveConfiguration(argv) | ||
|
||
const cacheLocation = await initDockerComposeCache({ | ||
composeProjectName: name, | ||
cache: getCache(), | ||
dockerComposeRepository: cfg.dockerComposeRepository, | ||
dockerComposeDirectory: cfg.dockerComposeDirectory, | ||
force: false, | ||
}) | ||
|
||
if (!cacheLocation) { | ||
reporter.error('Failed to initialize cache...') | ||
process.exit(1) | ||
} | ||
|
||
const rest = await tryCatchAsync( | ||
'db::restore', | ||
restore({ | ||
cacheLocation, | ||
dbVersion: cfg.dbVersion, | ||
url: cfg.demoDatabaseURL, | ||
...argv, | ||
}) | ||
) | ||
|
||
if (res.err) { | ||
reporter.error('Failed to restore database') | ||
reporter.debugErr(res.err) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
module.exports = { | ||
command: 'restore <name> [path]', | ||
desc: 'Restore the database from a backup', | ||
builder: { | ||
update: { | ||
alias: 'u', | ||
desc: 'Force re-download of cached files', | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
dbVersion: { | ||
desc: 'Version of the Database dump to use', | ||
type: 'string', | ||
}, | ||
}, | ||
handler: run, | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const { makeComposeProject } = require('../../common') | ||
const chalk = require('chalk') | ||
const path = require('path') | ||
const { reporter, exec } = require('@dhis2/cli-helpers-engine') | ||
|
||
module.exports = async ({ cacheLocation, name, path: dbPath, fat }) => { | ||
const destinationFile = path.resolve(dbPath) | ||
const composeProject = makeComposeProject(name) | ||
|
||
reporter.info(`Backing up database (this may take some time)...`) | ||
reporter.debug(`Dumping database to ${chalk.bold(destinationFile)}`) | ||
|
||
if (fat) { | ||
reporter.info( | ||
`Performing fat backup, ${chalk.bold( | ||
'including' | ||
)} Analytics and Resource tables` | ||
) | ||
} else { | ||
reporter.info( | ||
`Performing lean backup, ${chalk.bold( | ||
'excluding' | ||
)} Analytics and Resource tables` | ||
) | ||
} | ||
|
||
await exec({ | ||
cmd: './scripts/backup.sh', | ||
cwd: cacheLocation, | ||
args: [destinationFile, fat ? 'full' : ''], | ||
pipe: false, | ||
env: { | ||
DOCKER_COMPOSE: `docker-compose -p ${composeProject}`, | ||
}, | ||
}) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports.backup = require('./backup') | ||
module.exports.restore = require('./restore') |
Oops, something went wrong.