From 8ea827797dbf2b42e4175c8f7687aeab16572f8a Mon Sep 17 00:00:00 2001 From: hainenber Date: Sun, 5 Nov 2023 15:55:06 +0700 Subject: [PATCH 1/4] feat(techdocs-cli): check Docker status before running mkdocs server Signed-off-by: hainenber --- .../techdocs-cli/src/commands/serve/serve.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/techdocs-cli/src/commands/serve/serve.ts b/packages/techdocs-cli/src/commands/serve/serve.ts index 5fcf1787d3..328fcce607 100644 --- a/packages/techdocs-cli/src/commands/serve/serve.ts +++ b/packages/techdocs-cli/src/commands/serve/serve.ts @@ -24,6 +24,8 @@ import { LogFunc, waitForSignal } from '../../lib/run'; import { createLogger } from '../../lib/utility'; import { getMkdocsYml } from '@backstage/plugin-techdocs-node'; import fs from 'fs-extra'; +import { promisify } from 'util'; +import { execFile } from 'child_process'; function findPreviewBundlePath(): string { try { @@ -73,6 +75,23 @@ export default async function serve(opts: OptionValues) { mkdocsConfigFileName, }); + // Validate that Docker is up and running + if (opts.docker) { + logger.info('Checking Docker status...'); + try { + const runCheck = promisify(execFile); + await runCheck('docker', ['info'], { shell: true }); + logger.info( + 'Docker is up and running. Proceed to starting up mkdocs server', + ); + } catch { + logger.error( + 'Docker is not running. Exiting. Please check status of Docker daemon with `docker info` before re-running', + ); + return; + } + } + let mkdocsServerHasStarted = false; const mkdocsLogFunc: LogFunc = data => { // Sometimes the lines contain an unnecessary extra new line From 8600b868202c9926edf1eda8553e83ab831d2cde Mon Sep 17 00:00:00 2001 From: hainenber Date: Sun, 5 Nov 2023 15:58:43 +0700 Subject: [PATCH 2/4] chore(build): add changeset Signed-off-by: hainenber --- .changeset/selfish-games-deny.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/selfish-games-deny.md diff --git a/.changeset/selfish-games-deny.md b/.changeset/selfish-games-deny.md new file mode 100644 index 0000000000..2d283b10de --- /dev/null +++ b/.changeset/selfish-games-deny.md @@ -0,0 +1,5 @@ +--- +'@techdocs/cli': minor +--- + +validate Docker status before running mkdocs server From c9eba082f7de66cddee389e2a4a57833228f856b Mon Sep 17 00:00:00 2001 From: hainenber Date: Sat, 11 Nov 2023 21:13:45 +0700 Subject: [PATCH 3/4] fix(techdocs-cli): expand usage of Docker ops check Signed-off-by: hainenber --- .../techdocs-cli/src/commands/serve/mkdocs.ts | 8 ++++ .../techdocs-cli/src/commands/serve/serve.ts | 16 ++------ .../techdocs-cli/src/commands/serve/utils.ts | 38 +++++++++++++++++++ 3 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 packages/techdocs-cli/src/commands/serve/utils.ts diff --git a/packages/techdocs-cli/src/commands/serve/mkdocs.ts b/packages/techdocs-cli/src/commands/serve/mkdocs.ts index c061d4474f..00c5e5f0ed 100644 --- a/packages/techdocs-cli/src/commands/serve/mkdocs.ts +++ b/packages/techdocs-cli/src/commands/serve/mkdocs.ts @@ -21,6 +21,7 @@ import { runMkdocsServer } from '../../lib/mkdocsServer'; import { LogFunc, waitForSignal } from '../../lib/run'; import { getMkdocsYml } from '@backstage/plugin-techdocs-node'; import fs from 'fs-extra'; +import { checkIfDockerIsOperational } from './utils'; export default async function serveMkdocs(opts: OptionValues) { const logger = createLogger({ verbose: opts.verbose }); @@ -29,6 +30,13 @@ export default async function serveMkdocs(opts: OptionValues) { const localAddr = `http://127.0.0.1:${opts.port}`; const expectedDevAddr = opts.docker ? dockerAddr : localAddr; + if (opts.docker) { + const isDockerOperational = await checkIfDockerIsOperational(logger); + if (!isDockerOperational) { + return; + } + } + const { path: mkdocsYmlPath, configIsTemporary } = await getMkdocsYml( './', opts.siteName, diff --git a/packages/techdocs-cli/src/commands/serve/serve.ts b/packages/techdocs-cli/src/commands/serve/serve.ts index 328fcce607..98c9982b21 100644 --- a/packages/techdocs-cli/src/commands/serve/serve.ts +++ b/packages/techdocs-cli/src/commands/serve/serve.ts @@ -24,8 +24,7 @@ import { LogFunc, waitForSignal } from '../../lib/run'; import { createLogger } from '../../lib/utility'; import { getMkdocsYml } from '@backstage/plugin-techdocs-node'; import fs from 'fs-extra'; -import { promisify } from 'util'; -import { execFile } from 'child_process'; +import { checkIfDockerIsOperational } from './utils'; function findPreviewBundlePath(): string { try { @@ -77,17 +76,8 @@ export default async function serve(opts: OptionValues) { // Validate that Docker is up and running if (opts.docker) { - logger.info('Checking Docker status...'); - try { - const runCheck = promisify(execFile); - await runCheck('docker', ['info'], { shell: true }); - logger.info( - 'Docker is up and running. Proceed to starting up mkdocs server', - ); - } catch { - logger.error( - 'Docker is not running. Exiting. Please check status of Docker daemon with `docker info` before re-running', - ); + const isDockerOperational = await checkIfDockerIsOperational(logger); + if (!isDockerOperational) { return; } } diff --git a/packages/techdocs-cli/src/commands/serve/utils.ts b/packages/techdocs-cli/src/commands/serve/utils.ts new file mode 100644 index 0000000000..cf0a7ac296 --- /dev/null +++ b/packages/techdocs-cli/src/commands/serve/utils.ts @@ -0,0 +1,38 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { promisify } from 'util'; +import * as winston from 'winston'; +import { execFile } from 'child_process'; + +export async function checkIfDockerIsOperational( + logger: winston.Logger, +): Promise { + logger.info('Checking Docker status...'); + try { + const runCheck = promisify(execFile); + await runCheck('docker', ['info'], { shell: true }); + logger.info( + 'Docker is up and running. Proceed to starting up mkdocs server', + ); + return true; + } catch { + logger.error( + 'Docker is not running. Exiting. Please check status of Docker daemon with `docker info` before re-running', + ); + return false; + } +} From e0815ead2f6dc1da3fb0b291d192b88e4747e314 Mon Sep 17 00:00:00 2001 From: hainenber Date: Sat, 11 Nov 2023 21:20:06 +0700 Subject: [PATCH 4/4] fix(techdocs-cli): correct returned type Signed-off-by: hainenber --- packages/techdocs-cli/src/commands/serve/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/techdocs-cli/src/commands/serve/utils.ts b/packages/techdocs-cli/src/commands/serve/utils.ts index cf0a7ac296..2b19b0fbc0 100644 --- a/packages/techdocs-cli/src/commands/serve/utils.ts +++ b/packages/techdocs-cli/src/commands/serve/utils.ts @@ -20,7 +20,7 @@ import { execFile } from 'child_process'; export async function checkIfDockerIsOperational( logger: winston.Logger, -): Promise { +): Promise { logger.info('Checking Docker status...'); try { const runCheck = promisify(execFile);