Skip to content

Commit

Permalink
fix: Add healthcheck to nox, persistence to chain-rpc container and s…
Browse files Browse the repository at this point in the history
…et block mining time to 1 (#816)

* Add healthcheck to nox

* Check http code in nox healthcheck

* support volumes flag. add ability to pass arbitrary flags. remove local down when doing local up

* Apply automatic changes

* add v flag for local tests

* Add persisten storage to anvil

* Apply automatic changes

---------

Co-authored-by: Artsiom Shamsutdzinau <shamsartem@gmail.com>
Co-authored-by: shamsartem <shamsartem@users.noreply.github.com>
Co-authored-by: nahsi <nahsi@users.noreply.github.com>
  • Loading branch information
4 people authored Feb 27, 2024
1 parent 1e6be90 commit 9e41ec4
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 62 deletions.
37 changes: 24 additions & 13 deletions docs/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -979,10 +979,13 @@ Stop currently running docker-compose.yaml using docker compose

```
USAGE
$ fluence local down [--no-input]
$ fluence local down [--no-input] [-v] [--flags <value>]
FLAGS
--no-input Don't interactively ask for any input from the user
-v, --volumes Remove named volumes declared in the "volumes" section of the Compose file and anonymous
volumes attached to containers
--flags=<--flag arg> Space separated flags to pass to `docker compose`
--no-input Don't interactively ask for any input from the user
DESCRIPTION
Stop currently running docker-compose.yaml using docker compose
Expand Down Expand Up @@ -1024,10 +1027,11 @@ Display docker-compose.yaml logs

```
USAGE
$ fluence local logs [--no-input]
$ fluence local logs [--no-input] [--flags <value>]
FLAGS
--no-input Don't interactively ask for any input from the user
--flags=<--flag arg> Space separated flags to pass to `docker compose`
--no-input Don't interactively ask for any input from the user
DESCRIPTION
Display docker-compose.yaml logs
Expand All @@ -1044,10 +1048,11 @@ List containers using docker compose

```
USAGE
$ fluence local ps [--no-input]
$ fluence local ps [--no-input] [--flags <value>]
FLAGS
--no-input Don't interactively ask for any input from the user
--flags=<--flag arg> Space separated flags to pass to `docker compose`
--no-input Don't interactively ask for any input from the user
DESCRIPTION
List containers using docker compose
Expand All @@ -1064,15 +1069,21 @@ Run docker-compose.yaml using docker compose and set up provider using the first

```
USAGE
$ fluence local up [--no-input] [--noxes <value>] [--timeout <value>] [--priv-key <value>]
$ fluence local up [--no-input] [--noxes <value>] [--timeout <value>] [--priv-key <value>] [--quiet-pull]
[-d] [--build] [--flags <value>]
FLAGS
--no-input Don't interactively ask for any input from the user
--noxes=<value> Number of Compute Peers to generate when a new provider.yaml is created
--priv-key=<private-key> !WARNING! for debug purposes only. Passing private keys through flags is unsecure. On local
network 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 key will be used
by default
--timeout=<value> [default: 120] Timeout in seconds for attempting to register local network on local peers
-d, --detach Detached mode: Run containers in the background
--build Build images before starting containers
--flags=<--flag arg> Space separated flags to pass to `docker compose`
--no-input Don't interactively ask for any input from the user
--noxes=<value> Number of Compute Peers to generate when a new provider.yaml is created
--priv-key=<private-key> !WARNING! for debug purposes only. Passing private keys through flags is unsecure. On
local network 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 key
will be used by default
--quiet-pull Pull without printing progress information
--timeout=<value> [default: 120] Timeout in seconds for attempting to register local network on local
peers
DESCRIPTION
Run docker-compose.yaml using docker compose and set up provider using the first offer from the 'offers' section in
Expand Down
23 changes: 19 additions & 4 deletions src/commands/local/down.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
* limitations under the License.
*/

import { Flags } from "@oclif/core";

import { BaseCommand, baseFlags } from "../../baseCommand.js";
import { commandObj } from "../../lib/commandObj.js";
import { initReadonlyDockerComposeConfig } from "../../lib/configs/project/dockerCompose.js";
import { DOCKER_COMPOSE_FULL_FILE_NAME } from "../../lib/const.js";
import {
DOCKER_COMPOSE_FULL_FILE_NAME,
DOCKER_COMPOSE_FLAGS,
} from "../../lib/const.js";
import { dockerCompose } from "../../lib/dockerCompose.js";
import { initCli } from "../../lib/lifeCycle.js";

Expand All @@ -26,9 +31,16 @@ export default class Down extends BaseCommand<typeof Down> {
static override examples = ["<%= config.bin %> <%= command.id %>"];
static override flags = {
...baseFlags,
volumes: Flags.boolean({
char: "v",
description:
'Remove named volumes declared in the "volumes" section of the Compose file and anonymous volumes attached to containers',
default: false,
}),
...DOCKER_COMPOSE_FLAGS,
};
async run(): Promise<void> {
await initCli(this, await this.parse(Down));
const { flags } = await initCli(this, await this.parse(Down));
const dockerComposeConfig = await initReadonlyDockerComposeConfig();

if (dockerComposeConfig === null) {
Expand All @@ -38,9 +50,12 @@ export default class Down extends BaseCommand<typeof Down> {
}

await dockerCompose({
args: ["down"],
args: [
"down",
...(flags.flags === undefined ? [] : flags.flags.split(" ")),
],
flags: {
v: true,
v: flags.volumes,
},
printOutput: true,
options: {
Expand Down
13 changes: 10 additions & 3 deletions src/commands/local/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import { BaseCommand, baseFlags } from "../../baseCommand.js";
import { commandObj } from "../../lib/commandObj.js";
import { initReadonlyDockerComposeConfig } from "../../lib/configs/project/dockerCompose.js";
import { DOCKER_COMPOSE_FULL_FILE_NAME } from "../../lib/const.js";
import {
DOCKER_COMPOSE_FULL_FILE_NAME,
DOCKER_COMPOSE_FLAGS,
} from "../../lib/const.js";
import { dockerCompose } from "../../lib/dockerCompose.js";
import { initCli } from "../../lib/lifeCycle.js";

Expand All @@ -26,9 +29,10 @@ export default class Logs extends BaseCommand<typeof Logs> {
static override examples = ["<%= config.bin %> <%= command.id %>"];
static override flags = {
...baseFlags,
...DOCKER_COMPOSE_FLAGS,
};
async run(): Promise<void> {
await initCli(this, await this.parse(Logs));
const { flags } = await initCli(this, await this.parse(Logs));
const dockerComposeConfig = await initReadonlyDockerComposeConfig();

if (dockerComposeConfig === null) {
Expand All @@ -38,7 +42,10 @@ export default class Logs extends BaseCommand<typeof Logs> {
}

const logs = await dockerCompose({
args: ["logs"],
args: [
"logs",
...(flags.flags === undefined ? [] : flags.flags.split(" ")),
],
options: {
cwd: dockerComposeConfig.$getDirPath(),
},
Expand Down
13 changes: 10 additions & 3 deletions src/commands/local/ps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import { BaseCommand, baseFlags } from "../../baseCommand.js";
import { commandObj } from "../../lib/commandObj.js";
import { initReadonlyDockerComposeConfig } from "../../lib/configs/project/dockerCompose.js";
import { DOCKER_COMPOSE_FULL_FILE_NAME } from "../../lib/const.js";
import {
DOCKER_COMPOSE_FULL_FILE_NAME,
DOCKER_COMPOSE_FLAGS,
} from "../../lib/const.js";
import { dockerCompose } from "../../lib/dockerCompose.js";
import { initCli } from "../../lib/lifeCycle.js";

Expand All @@ -26,9 +29,10 @@ export default class PS extends BaseCommand<typeof PS> {
static override examples = ["<%= config.bin %> <%= command.id %>"];
static override flags = {
...baseFlags,
...DOCKER_COMPOSE_FLAGS,
};
async run(): Promise<void> {
await initCli(this, await this.parse(PS));
const { flags } = await initCli(this, await this.parse(PS));
const dockerComposeConfig = await initReadonlyDockerComposeConfig();

if (dockerComposeConfig === null) {
Expand All @@ -38,7 +42,10 @@ export default class PS extends BaseCommand<typeof PS> {
}

const psResult = await dockerCompose({
args: ["ps"],
args: [
"ps",
...(flags.flags === undefined ? [] : flags.flags.split(" ")),
],
options: {
cwd: dockerComposeConfig.$getDirPath(),
},
Expand Down
59 changes: 30 additions & 29 deletions src/commands/local/up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { setEnvConfig } from "../../lib/configs/globalConfigs.js";
import { initNewReadonlyDockerComposeConfig } from "../../lib/configs/project/dockerCompose.js";
import { initNewEnvConfig } from "../../lib/configs/project/env.js";
import { initNewReadonlyProviderConfig } from "../../lib/configs/project/provider.js";
import { DOCKER_COMPOSE_FLAGS } from "../../lib/const.js";
import {
ALL_FLAG_VALUE,
DOCKER_COMPOSE_FULL_FILE_NAME,
Expand All @@ -50,6 +51,20 @@ export default class Up extends BaseCommand<typeof Up> {
default: 120,
}),
...PRIV_KEY_FLAG,
"quiet-pull": Flags.boolean({
description: "Pull without printing progress information",
default: true,
}),
detach: Flags.boolean({
char: "d",
description: "Detached mode: Run containers in the background",
default: true,
}),
build: Flags.boolean({
description: "Build images before starting containers",
default: true,
}),
...DOCKER_COMPOSE_FLAGS,
};

async run(): Promise<void> {
Expand All @@ -63,35 +78,21 @@ export default class Up extends BaseCommand<typeof Up> {
const providerConfig = await initNewReadonlyProviderConfig({});
const dockerComposeConfig = await initNewReadonlyDockerComposeConfig();

try {
const res = await dockerCompose({
args: ["down"],
flags: {
v: true,
},
printOutput: true,
options: {
cwd: dockerComposeConfig.$getDirPath(),
},
});

if (res.trim() === "") {
throw new Error("docker-compose down failed");
}
} catch {
await dockerCompose({
args: ["up"],
flags: {
"quiet-pull": true,
d: true,
build: true,
},
printOutput: true,
options: {
cwd: dockerComposeConfig.$getDirPath(),
},
});
}
await dockerCompose({
args: [
"up",
...(flags.flags === undefined ? [] : flags.flags.split(" ")),
],
flags: {
"quiet-pull": flags["quiet-pull"],
d: flags.detach,
build: flags.build,
},
printOutput: true,
options: {
cwd: dockerComposeConfig.$getDirPath(),
},
});

const allNoxNames = {
[NOX_NAMES_FLAG_NAME]: ALL_FLAG_VALUE,
Expand Down
31 changes: 21 additions & 10 deletions src/lib/configs/project/dockerCompose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ import { yamlDiffPatch } from "yaml-diff-patch";
import { versions } from "../../../versions.js";
import {
CHAIN_DEPLOY_SCRIPT_NAME,
GRAPH_NODE_PORT,
POSTGRES_CONTAINER_NAME,
DOCKER_COMPOSE_FILE_NAME,
IPFS_PORT,
IPFS_CONTAINER_NAME,
CHAIN_RPC_PORT,
CHAIN_RPC_CONTAINER_NAME,
TCP_PORT_START,
WEB_SOCKET_PORT_START,
PROVIDER_CONFIG_FULL_FILE_NAME,
CHAIN_RPC_PORT,
CONFIGS_DIR_NAME,
DOCKER_COMPOSE_FILE_NAME,
GRAPH_NODE_CONTAINER_NAME,
GRAPH_NODE_PORT,
IPFS_CONTAINER_NAME,
IPFS_PORT,
POSTGRES_CONTAINER_NAME,
PROVIDER_CONFIG_FULL_FILE_NAME,
SUBGRAPH_DEPLOY_SCRIPT_NAME,
TCP_PORT_START,
WEB_SOCKET_PORT_START,
} from "../../const.js";
import { genSecretKeyOrReturnExisting } from "../../keyPairs.js";
import { ensureFluenceConfigsDir, getFluenceDir } from "../../paths.js";
import {
getConfigInitFunction,
getReadonlyConfigInitFunction,
type GetDefaultConfig,
getReadonlyConfigInitFunction,
type InitializedConfig,
type InitializedReadonlyConfig,
type Migrations,
Expand Down Expand Up @@ -125,6 +125,12 @@ function genNox({
`${name}:/.fluence`,
],
secrets: [name],
healthcheck: {
test: "curl -o /dev/null --silent -Iw '%{http_code}' | grep 200",
interval: "5s",
timeout: "2s",
retries: 10,
},
},
];
}
Expand Down Expand Up @@ -164,6 +170,7 @@ async function genDockerCompose(): Promise<LatestConfig> {
return {
version: "3",
volumes: {
"chain-rpc": null,
[IPFS_CONTAINER_NAME]: null,
[POSTGRES_CONTAINER_NAME]: null,
...Object.fromEntries(
Expand Down Expand Up @@ -208,6 +215,10 @@ async function genDockerCompose(): Promise<LatestConfig> {
[CHAIN_RPC_CONTAINER_NAME]: {
image: versions[CHAIN_RPC_CONTAINER_NAME],
ports: [`${CHAIN_RPC_PORT}:${CHAIN_RPC_PORT}`],
volumes: [`chain-rpc:/data`],
environment: {
LOCAL_CHAIN_BLOCK_MINING_INTERVAL: 1,
},
healthcheck: {
test: `curl -s -X POST 'http://localhost:${CHAIN_RPC_PORT}' -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0", "method":"eth_chainId", "params":[], "id":1}' | jq -e '.result != null'`,
interval: "8s",
Expand Down
7 changes: 7 additions & 0 deletions src/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,13 @@ export const MARINE_BUILD_ARGS_FLAG = {
}),
};

export const DOCKER_COMPOSE_FLAGS = {
flags: Flags.string({
description: "Space separated flags to pass to `docker compose`",
helpValue: "<--flag arg>",
}),
};

export const TTL_FLAG_NAME = "ttl";
export const DIAL_TIMEOUT_FLAG_NAME = "dial-timeout";

Expand Down
1 change: 1 addition & 0 deletions test/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ async function setupLocalEnvironment() {
await fluence({
cwd: pathToTheTemplateWhereLocalEnvironmentIsSpunUp,
args: ["local", "down"],
flags: { v: true },
});
}
} catch {}
Expand Down

0 comments on commit 9e41ec4

Please sign in to comment.