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

Add LogType 'text' in order to provide different way of logging #1282

Merged
merged 10 commits into from
Aug 31, 2023
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Options:
-c, --spawn-concurrency <concurrency> Number of concurrent spawning process to launch, default is 1
-p, --provider <provider> Override provider to use (choices: "podman", "kubernetes", "native")
default: kubernetes
-l, --logType <logType>" Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent")
-d, --dir <path> Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet)
-f, --force Force override all prompt commands
-m, --monitor Start as monitor, do not auto cleanup network
Expand Down
1 change: 1 addition & 0 deletions docs/src/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Options:
-c, --spawn-concurrency <concurrency> Number of concurrent spawning process to launch, default is 1
-p, --provider <provider> Override provider to use (choices: "podman","kubernetes", "native", default: kubernetes)
-d, --dir <path> Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet)
-l, --logType <logType>" Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent")
-f, --force Force override all prompt commands
-m, --monitor Start as monitor, do not auto cleanup network
-h, --help display help for command
Expand Down
1 change: 1 addition & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Options:
-p, --provider <provider> Override provider to use (choices: "podman", "kubernetes", "native")
default: kubernetes
-d, --dir <path> Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet)
-l, --logType <logType>" Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent")
-f, --force Force override all prompt commands
-m, --monitor Start as monitor, do not auto cleanup network
-h, --help display help for command
Expand Down
1 change: 1 addition & 0 deletions javascript/packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Options:
-p, --provider <provider> Override provider to use (choices: "podman", "kubernetes", "native")
default: kubernetes
-d, --dir <path> Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet)
-l, --logType <logType>" Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent")
-f, --force Force override all prompt commands
-m, --monitor Start as monitor, do not auto cleanup network
-h, --help display help for command
Expand Down
3 changes: 2 additions & 1 deletion javascript/packages/cli/src/actions/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
decorators,
getCredsFilePath,
readNetworkConfig,
getLogType,
} from "@zombienet/utils";
import fs from "fs";
import { resolve } from "path";
Expand Down Expand Up @@ -95,7 +96,7 @@ export async function spawn(
dir,
force,
inCI,
silent: false,
logType: getLogType(opts.logType),
setGlobalNetwork,
};
const network = await start(creds, config, options);
Expand Down
4 changes: 2 additions & 2 deletions javascript/packages/cli/src/actions/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parser from "@zombienet/dsl-parser-wrapper";
import type { TestDefinition } from "@zombienet/orchestrator";
import { run } from "@zombienet/orchestrator";
import { decorators, RelativeLoader } from "@zombienet/utils";
import { decorators, RelativeLoader, getLogType } from "@zombienet/utils";
import fs from "fs";
import { Environment } from "nunjucks";
import path from "path";
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function test(
providerToUse,
inCI,
opts.spawnConcurrency,
false,
getLogType(opts.logType),
runningNetworkSpec,
dir,
);
Expand Down
7 changes: 6 additions & 1 deletion javascript/packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ program
["podman", "kubernetes", "native"],
),
)

.addOption(
new Option(
"-l, --logType <logType>",
"Type of logging - defaults to 'table'",
),
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
)
.addOption(
new Option(
"-d, --dir <path>",
Expand Down
1 change: 1 addition & 0 deletions javascript/packages/orchestrator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Options:
-p, --provider <provider> Override provider to use (choices: "podman", "kubernetes", "native")
default: kubernetes
-d, --dir <path> Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet)
-l, --logType <logType>" Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent")
-f, --force Force override all prompt commands
-m, --monitor Start as monitor, do not auto cleanup network
-h, --help display help for command
Expand Down
16 changes: 11 additions & 5 deletions javascript/packages/orchestrator/src/orchestrator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LogType } from "@zombienet/utils";
import {
CreateLogTable,
PARACHAIN_NOT_FOUND,
Expand All @@ -11,7 +12,7 @@ import {
loadTypeDef,
makeDir,
series,
setSilent,
setLogType,
sleep,
} from "@zombienet/utils";
import fs from "fs";
Expand Down Expand Up @@ -64,7 +65,7 @@ export interface OrcOptionsInterface {
inCI?: boolean;
dir?: string;
force?: boolean;
silent?: boolean; // Mute logging output
logType?: LogType; // Mute logging output
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
setGlobalNetwork?: (network: Network) => void;
}

Expand All @@ -74,11 +75,16 @@ export async function start(
options?: OrcOptionsInterface,
) {
const opts = {
...{ monitor: false, spawnConcurrency: 1, inCI: false, silent: true },
...{
monitor: false,
spawnConcurrency: 1,
inCI: false,
logType: "silent" as LogType,
},
...options,
};

setSilent(opts.silent);
setLogType(opts.logType as LogType);
let network: Network | undefined;
let cronInterval = undefined;

Expand Down Expand Up @@ -362,7 +368,7 @@ export async function start(
}

const spawnOpts = {
silent: opts.silent,
logType: opts.logType as LogType,
inCI: opts.inCI,
monitorIsAvailable,
userDefinedTypes,
Expand Down
5 changes: 3 additions & 2 deletions javascript/packages/orchestrator/src/spawner.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LogType } from "@zombienet/utils";
import {
CreateLogTable,
decorators,
Expand Down Expand Up @@ -30,7 +31,7 @@ export const spawnNode = async (
bootnodes: string[],
filesToCopy: fileMap[],
opts: {
silent: boolean;
logType: LogType;
inCI: boolean;
monitorIsAvailable: boolean;
userDefinedTypes?: any;
Expand Down Expand Up @@ -231,7 +232,7 @@ export const spawnNode = async (
]);
logTable.print();

if (!opts.silent)
if (opts.logType !== "silent")
console.log(client.getLogsCommand(podDef.metadata.name) + "\n\n");
}

Expand Down
9 changes: 5 additions & 4 deletions javascript/packages/orchestrator/src/test-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {
decorators,
getLokiUrl,
readNetworkConfig,
setSilent,
setLogType,
sleep,
LogType,
} from "@zombienet/utils";
import fs from "fs";
import Mocha from "mocha";
Expand Down Expand Up @@ -34,11 +35,11 @@ export async function run(
provider: string,
inCI = false,
concurrency = 1,
silent = false,
logType: LogType = "table",
runningNetworkSpecPath: string | undefined,
dir: string | undefined,
) {
setSilent(silent);
logType && setLogType("silent");
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
let network: Network;
const backchannelMap: BackchannelMap = {};

Expand Down Expand Up @@ -92,7 +93,7 @@ export async function run(
network = await start(creds!, config, {
spawnConcurrency: concurrency,
inCI,
silent,
logType,
dir,
});
} else {
Expand Down
50 changes: 44 additions & 6 deletions javascript/packages/utils/src/tableCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Table, {
HorizontalTableRow,
VerticalTableRow,
} from "cli-table3";
import { decorators } from "./colors";

type CharsObj = {
[key in
Expand Down Expand Up @@ -54,15 +55,37 @@ interface TableCreationProps {
wordWrap?: boolean;
}

export type LogType = "json" | "text" | "table" | "silent";
wirednkod marked this conversation as resolved.
Show resolved Hide resolved

// Module level config.
let silent = true;
export function setSilent(value: boolean) {
silent = value;
}
let logType: LogType = "table";
const logTypeValues = ["json", "text", "table", "silent"];
wirednkod marked this conversation as resolved.
Show resolved Hide resolved

export const getLogType = (logType: LogType): LogType => {
if (logTypeValues.includes(logType)) {
return logType;
} else {
logType &&
console.error(
`${decorators.red(`
Argument 'logType' provided ('${logType}') is not one of the accepted params; Falling back to 'table'.
Possible values: ${logTypeValues.join(
", ",
)} - Defaults to 'table'.\n\n`)}`,
);
return "table";
}
};

export const setLogType = (value: LogType) => {
logType = value;
};

export class CreateLogTable {
table: CreatedTable | undefined;
colWidths: number[];
wordWrap: boolean;
text: string[];

constructor({ head, colWidths, doubleBorder, wordWrap }: TableCreationProps) {
this.wordWrap = wordWrap || false;
Expand All @@ -74,6 +97,7 @@ export class CreateLogTable {
if (doubleBorder) {
params.chars = chars;
}
this.text = [];
this.table = new Table(params);
}

Expand All @@ -96,12 +120,26 @@ export class CreateLogTable {
input[index] = inp;
}
});
this.table!.push(input);
if (logType === "text") {
if (input[0] === "\x1B[36mCommand\x1B[0m") {
input[1] = input[1].replace(/\n/g, " ");
}
// if input has a JSON - that means a merged cell
if (input[0]?.content) {
input[0] = input[0]?.content;
}
console.log(input.join(" : "));
} else if (logType === "silent") {
return;
} else if (logType === "table") {
this.table!.push(input);
}
});
};

print = () => {
if (!silent) console.log(this.table!.toString());
if (logType === "silent" || logType === "text") return;
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
if (logType === "table") console.log(this.table!.toString());
};

// This function makes the process of creating a table, pushing data and printing it faster
Expand Down
6 changes: 3 additions & 3 deletions scripts/ci/run-test-env-manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ function download_from_remote {
while read -r _path _type _download_url _url ; do
if [[ "${_type}" == "file" ]] ; then
echo curl "${_download_url}" --output "${OUTPUT_DIR}/${_path}"
curl "${_download_url}" --output "${OUTPUT_DIR}/${_path}" --silent
curl "${_download_url}" --output "${OUTPUT_DIR}/${_path}" --logtype silent
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
elif [[ "${_type}" == "symlink" ]] ; then
echo curl "${_download_url}" --output "${OUTPUT_DIR}/${_path}"
curl "${_download_url}" --output "${OUTPUT_DIR}/${_path}" --silent
curl "${_download_url}" --output "${OUTPUT_DIR}/${_path}" --logtype silent
wirednkod marked this conversation as resolved.
Show resolved Hide resolved

# replace _path with what you find in file download_url after removing
# the dots and slashes
Expand Down Expand Up @@ -165,7 +165,7 @@ function download_from_remote {
elif [[ "${_type}" == "dir" ]] ; then
gh_download_content "${_url}" "${_path}"
fi
done< <(jq '.[] | "\(.path) \(.type) \(.download_url) \(.url)"' --raw-output < <(curl --silent "${url}"))
done< <(jq '.[] | "\(.path) \(.type) \(.download_url) \(.url)"' --raw-output < <(curl --logtype silent "${url}"))
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
}

local url
Expand Down
Loading