Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to compatible docker compose usage #195

Merged
merged 14 commits into from
Nov 15, 2023
Merged
2 changes: 1 addition & 1 deletion features/run-kuzzle-stack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mkdir -p /tmp/snapshots
chmod 777 -R /tmp/snapshots

# Launch the kuzzle stack
docker-compose -f features/docker/docker-compose.yml up -d
docker compose -f features/docker/docker-compose.yml up -d

echo "[$(date --rfc-3339 seconds)] - Starting Kuzzle..."
while ! curl -f -s -o /dev/null http://localhost:7512
Expand Down
23 changes: 8 additions & 15 deletions src/commands/app/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { kuzzleFlags } from "../../support/kuzzle";
import { Client } from "@elastic/elasticsearch";
import { execute } from "../../support/execute";
import _ from "lodash";
import { checkPrerequisites } from "../../support/docker/checkPrerequisites";

export default class AppDoctor extends Kommand {
static description = "Analyze a Kuzzle application";
Expand Down Expand Up @@ -50,7 +51,7 @@ export default class AppDoctor extends Kommand {
const suggestions = [];

const [nodeVersion, adminExists, anonymous] = await Promise.all([
this.sdk.query({ controller: "debug", action: "nodeVersion", }),
this.sdk.query({ controller: "debug", action: "nodeVersion", }),
this.sdk.server.adminExists({}),
this.sdk.security.getRole("anonymous")
]);
Expand All @@ -67,8 +68,7 @@ export default class AppDoctor extends Kommand {
} else {
this.logKo("No admin user exists");
suggestions.push(
`Create an admin user ${
anonymousNotRestricted ? "and restrict anonymous role " : ""
`Create an admin user ${anonymousNotRestricted ? "and restrict anonymous role " : ""
}with
kourou security:createFirstAdmin '{
credentials: {
Expand Down Expand Up @@ -216,20 +216,13 @@ export default class AppDoctor extends Kommand {
}

this.log("Docker checks");
try {
const docov = await execute("docker-compose", "-v");
const matches = docov.stdout.match(/[^0-9.]*([0-9.]*).*/);
if (matches === null || matches.length === 0) {
this.logKo("Docker Version cannot be found");
} else {
this.logOk(`Docker Compose Version: ${matches[1]}`);
}
} catch (error: any) {
this.logKo("Docker Compose cannot be found");
suggestions.push("Install Docker Compose with 'npm run install:docker'");
const result = await checkPrerequisites(this);

if (!result) {
suggestions.push("Install Docker and the Compose plugin");
}

this.log(`----------------- DoKtor finish his job ! -----------------`);
this.log(`----------------- DoKtor finished its job ! -----------------`);
this.log(`He suggest you to check the following points:`);
for (const suggestion of suggestions) {
this.logInfo(" => " + suggestion);
Expand Down
68 changes: 8 additions & 60 deletions src/commands/app/start-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import path from "path";

import { flags } from "@oclif/command";
import chalk from "chalk";
import Listr from "listr";

import emoji from "node-emoji";

import { Kommand } from "../../common";
import { execute } from "../../support/execute";

const MIN_DOCO_VERSION = "1.12.0";
import { checkPrerequisites } from "../../support/docker/checkPrerequisites";

const kuzzleServicesFile = `
version: '3'
Expand Down Expand Up @@ -48,7 +47,7 @@ export default class AppStartServices extends Kommand {
const docoFilename = path.join(this.kourouDir, "kuzzle-services.yml");

const successfullCheck = this.flags.check
? await this.checkPrerequisites()
? await checkPrerequisites(this)
: true;

if (this.flags.check && successfullCheck) {
Expand All @@ -64,22 +63,22 @@ export default class AppStartServices extends Kommand {
}

this.log(
chalk.grey(`\nWriting docker-compose file to ${docoFilename}...\n`)
chalk.grey(`\nWriting the Docker Compose file to ${docoFilename}...\n`)
);

writeFileSync(docoFilename, kuzzleServicesFile);

// clean up
await execute("docker-compose", "-f", docoFilename, "down");
await execute("docker", "compose", "-f", docoFilename, "down");

try {
await execute("docker-compose", "-f", docoFilename, "up", "-d");
await execute("docker", "compose", "-f", docoFilename, "up", "-d");

this.logOk(
"Elasticsearch and Redis are booting in the background right now."
);
this.log(chalk.grey("\nTo watch the logs, run"));
this.log(chalk.blue.bold(` docker-compose -f ${docoFilename} logs -f\n`));
this.log(chalk.blue.bold(` docker compose -f ${docoFilename} logs -f\n`));
this.log(` Elasticsearch port: ${chalk.bold("9200")}`);
this.log(` Redis port: ${chalk.bold("6379")}`);
} catch (error: any) {
Expand All @@ -88,58 +87,7 @@ export default class AppStartServices extends Kommand {
chalk.grey("If you want to investigate the problem, try running")
);

this.log(chalk.grey(` docker-compose -f ${docoFilename} up\n`));
}
}

public async checkPrerequisites(): Promise<boolean> {
this.log(chalk.grey("Checking prerequisites..."));

const checks: Listr = new Listr([
{
title: `docker-compose exists and the version is at least ${MIN_DOCO_VERSION}`,
task: async () => {
try {
const docov = await execute("docker-compose", "-v");
const matches = docov.stdout.match(/[^0-9.]*([0-9.]*).*/);
if (matches === null) {
throw new Error(
"Unable to read docker-compose verson. This is weird."
);
}
const docoVersion = matches.length > 0 ? matches[1] : null;

if (docoVersion === null) {
throw new Error(
"Unable to read docker-compose version. This is weird."
);
}
try {
if (docoVersion < MIN_DOCO_VERSION) {
throw new Error(
`The detected version of docker-compose (${docoVersion}) is not recent enough (${MIN_DOCO_VERSION})`
);
}
} catch (error: any) {
throw new Error(error);
}
} catch (error: any) {
throw new Error(
"No docker-compose found. Are you sure docker-compose is installed?"
);
}
},
},
]);

try {
await checks.run();

return true;
} catch (error: any) {
this.logKo(error.message);

return false;
this.log(chalk.grey(` docker compose -f ${docoFilename} up\n`));
}
}
}
3 changes: 2 additions & 1 deletion src/commands/instance/kill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export class InstanceLogs extends Kommand {
stdout: true,
}
);
const instanceKill: ChildProcess = spawn("docker-compose", [
const instanceKill: ChildProcess = spawn("docker", [
"compose",
"-f",
docoFilename,
"-p",
Expand Down
99 changes: 13 additions & 86 deletions src/commands/instance/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import chalk from "chalk";
import { ChildProcess, spawn } from "child_process";
import cli from "cli-ux";
import Listr from "listr";

Check warning on line 9 in src/commands/instance/spawn.ts

View workflow job for this annotation

GitHub Actions / Lint

'Listr' is defined but never used
import emoji from "node-emoji";

import { Kommand } from "../../common";
import { execute } from "../../support/execute";
import { checkPrerequisites } from "../../support/docker/checkPrerequisites";

const MIN_MAX_MAP_COUNT = 262144;

Check warning on line 16 in src/commands/instance/spawn.ts

View workflow job for this annotation

GitHub Actions / Lint

'MIN_MAX_MAP_COUNT' is assigned a value but never used
const MIN_DOCO_VERSION = "1.12.0";
const MIN_DOCO_VERSION = "2.0.0";

Check warning on line 17 in src/commands/instance/spawn.ts

View workflow job for this annotation

GitHub Actions / Lint

'MIN_DOCO_VERSION' is assigned a value but never used

const kuzzleStackV1 = (increment: number): string => `
version: '3'
Expand Down Expand Up @@ -112,7 +113,7 @@
);

const successfullCheck = this.flags.check
? await this.checkPrerequisites()
? await checkPrerequisites(this)
: true;

if (this.flags.check && successfullCheck) {
Expand All @@ -127,23 +128,25 @@
);
}

this.log(chalk.grey(`\nWriting docker-compose file to ${docoFilename}...`));
this.log(chalk.grey(`\nWriting docker compose file to ${docoFilename}...`));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
this.log(chalk.grey(`\nWriting docker compose file to ${docoFilename}...`));
this.log(chalk.grey(`\nWriting the Docker Compose file to ${docoFilename}...`));

writeFileSync(
docoFilename,
this.generateDocoFile(this.flags.version, portIndex)
);

// clean up
await execute(
"docker-compose",
"docker",
"compose",
"-f",
docoFilename,
"-p",
`stack-${portIndex}`,
"down"
);

const doco: ChildProcess = spawn("docker-compose", [
const doco: ChildProcess = spawn("docker", [
"compose",
"-f",
docoFilename,
"-p",
Expand All @@ -153,8 +156,7 @@
]);

cli.action.start(
` ${emoji.get("rocket")} Kuzzle version ${
this.flags.version
` ${emoji.get("rocket")} Kuzzle version ${this.flags.version
} is launching`,
undefined,
{
Expand All @@ -173,7 +175,7 @@
this.log(chalk.grey("To watch the logs, run"));
this.log(
chalk.grey(
` docker-compose -f ${docoFilename} -p stack-${portIndex} logs -f\n`
` docker compose -f ${docoFilename} -p stack-${portIndex} logs -f\n`
)
);
this.log(` Kuzzle port: ${7512 + portIndex}`);
Expand All @@ -183,97 +185,22 @@
} else {
cli.action.stop(
chalk.red(
` Something went wrong: docker-compose exited with ${docoCode}`
` Something went wrong: docker compose exited with ${docoCode}`
)
);
this.log(
chalk.grey("If you want to investigate the problem, try running")
);
this.log(
chalk.grey(
` docker-compose -f ${docoFilename} -p stack-${portIndex} up\n`
` docker compose -f ${docoFilename} -p stack-${portIndex} up\n`
)
);
throw new Error("docker-compose exited witn non-zero status");
throw new Error("docker compose exited with a non-zero status");
}
});
}

public async checkPrerequisites(): Promise<boolean> {
this.log(chalk.grey("Checking prerequisites..."));
const checks: Listr = new Listr([
{
title: `docker-compose exists and the version is at least ${MIN_DOCO_VERSION}`,
task: async () => {
try {
const docov = await execute("docker-compose", "-v");
const matches = docov.stdout.match(/[^0-9.]*([0-9.]*).*/);
if (matches === null) {
throw new Error(
"Unable to read docker-compose verson. This is weird."
);
}
const docoVersion = matches.length > 0 ? matches[1] : null;

if (docoVersion === null) {
throw new Error(
"Unable to read docker-compose version. This is weird."
);
}
try {
if (docoVersion < MIN_DOCO_VERSION) {
throw new Error(
`The detected version of docker-compose (${docoVersion}) is not recent enough (${MIN_DOCO_VERSION})`
);
}
} catch (error: any) {
throw new Error(error);
}
} catch (error: any) {
throw new Error(
"No docker-compose found. Are you sure docker-compose is installed?"
);
}
},
},
{
title: `vm.max_map_count is greater than ${MIN_MAX_MAP_COUNT}`,
task: async () => {
try {
const sysctl = await execute(
"/sbin/sysctl",
"-n",
"vm.max_map_count"
);

if (sysctl.exitCode !== 0) {
throw new Error("Something went wrong checking vm.max_map_count");
}

const value: number = parseInt(sysctl.stdout, 10);
if (value < MIN_MAX_MAP_COUNT) {
throw new Error(
`vm.max_map_count must be at least ${MIN_MAX_MAP_COUNT} (found ${value})`
);
}
} catch (error: any) {
throw new Error(
`Something went wrong checking vm.max_map_count: ${error.message}`
);
}
},
},
]);

try {
await checks.run();
return true;
} catch (error: any) {
this.logKo(error.message);
return false;
}
}

private generateDocoFile(kuzzleMajor: string, portIndex: number): string {
if (kuzzleMajor === "1") {
return kuzzleStackV1(portIndex);
Expand Down
44 changes: 44 additions & 0 deletions src/support/docker/checkPrerequisites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Listr from "listr";

import { execute } from "../execute";
import { Kommand } from "../../common";


const MIN_DOCO_VERSION = "2.0.0";

export async function checkPrerequisites(ctx: Kommand): Promise<boolean> {
const checks: Listr = new Listr([
{
title: `docker compose exists and the version is at least ${MIN_DOCO_VERSION}`,
task: async () => {
const docov = await execute("docker", "compose", "version");
const matches = docov.stdout.match(/[^0-9.]*([0-9.]*).*/);
const docoVersion = matches ? matches[1] : null;
ctx.logInfo(`Found Docker Compose version: ${docoVersion}`);

if (!docoVersion) {
throw new Error(
"Unable to read the version of Docker Compose. Are you sure Docker and the Compose plugin are installed?"
);
}


if (docoVersion < MIN_DOCO_VERSION) {
throw new Error(
`Your version of Docker Compose (${docoVersion}) is below the required version (${MIN_DOCO_VERSION}).`
);
}
},
},
]);

try {
await checks.run();

return true;
} catch (error: any) {
ctx.logKo(error.message);

return false;
}
}
Loading