@contiamo/dev
Version:
Dev enviromnent for contiamo
128 lines (112 loc) • 3.65 kB
JavaScript
const program = require("commander");
const path = require("path");
const fs = require("fs");
const slash = require("slash");
const { spawn, execSync } = require("child_process");
const YAML = require("yaml");
/**
* Execute a command with nice console output.
*
* @param {string} command to execute
*/
const exec = command =>
new Promise(resolve => {
const [c, ...args] = command.split(" ");
const task = spawn(c, args, { stdio: "inherit" });
task.on("close", resolve);
});
const dockerComposeFile = slash(
path.relative(process.cwd(), path.join(__dirname, "/docker-compose.yml"))
);
const dockerCompose = YAML.parse(fs.readFileSync(dockerComposeFile, "utf-8"));
const services = Object.keys(dockerCompose.services);
// Program
const { version } = fs.readFileSync(
path.join(__dirname, "./package.json"),
"utf-8"
);
program.version(version);
program
.command("clean")
.description("Stop the environment and remove volumes")
.action(() => exec(`docker-compose -f ${dockerComposeFile} down -v`));
program
.command("docker-auth")
.description("Setup docker authentification with gcloud")
.action(() => {
// Doesn't work with `exec` ¯\_(ツ)_/¯
console.log("Please execute the following commands:");
console.log("- gcloud auth login");
console.log("- gcloud auth configure-docker");
});
program
.command("pull")
.description("Pull the latest docker images")
.action(() => {
exec(
`docker-compose -f ${dockerComposeFile} pull datastore ui auth pantheon profiler hub`
);
});
program
.command("start")
.description("Run the local dev enviromnent on http://localhost:9898")
.action(() => {
exec(
`docker-compose -f ${dockerComposeFile} up -d --force-recreate --remove-orphans`
).then(() => {
console.log("-----");
console.log("Tracing ui: http://localhost:16686/search");
console.log("Dev ui: http://localhost:9898");
console.log("Email: lemon@example.com");
console.log("Password: localdev");
});
});
program
.command("logs [SERVICE...]")
.option(
"--tail [n]",
"Number of lines to show from the end of the logs for each container."
)
.option("-f, --follow", "Follow log output.")
.description("Get logs of a service.")
.action((_, { parent: { rawArgs }, tail, follow }) => {
let args = [];
if (typeof tail === "string") args.push(`--tail ${tail}`);
if (follow) args.push(`-f`);
args.push(...rawArgs.filter(i => services.includes(i)));
exec(`docker-compose -f ${dockerComposeFile} logs ${args.join(" ")}`);
});
program
.command("restart [SERVICE...]")
.description("Restart running containers.")
.action((_, { parent: { rawArgs } }) => {
let args = rawArgs.filter(i => services.includes(i));
exec(`docker-compose -f ${dockerComposeFile} restart ${args.join(" ")}`);
});
program
.command("stop")
.description("Stop the running docker container")
.action(() => {
exec(`docker-compose -f ${dockerComposeFile} down`);
});
program
.command("repro")
.description(
"Compute the exact image digests that are used in the current setup"
)
.action(() => {
const configYaml = execSync(
`docker-compose -f ${dockerComposeFile} config --resolve-image-digests`
);
const config = YAML.parse(configYaml.toString());
const output = {
version: config.version,
services: Object.entries(config.services).reduce(
(mem, [key, value]) => ({ ...mem, [key]: { image: value.image } }),
{}
)
};
console.log(YAML.stringify(output));
});
program.parse(process.argv);