Skip to content

Commit

Permalink
feat!: improve fluence env switching [fixes DXJ-473] (#502)
Browse files Browse the repository at this point in the history
* feat: improve fluence env switching [fixes DXJ-473]

* improve logs

* fix

* Apply automatic changes

* fix description

* Apply automatic changes

* add env.yaml to gitignore

* review fixes

* fix

* Apply automatic changes

* fix

* check registry cli-up branch

* up

* use main

---------

Co-authored-by: shamsartem <[email protected]>
  • Loading branch information
shamsartem and shamsartem authored Oct 2, 2023
1 parent efb88fb commit a09f39b
Show file tree
Hide file tree
Showing 77 changed files with 1,384 additions and 1,007 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ When you contribute, you have to be aware that your contribution is covered by *

## Guidelines for contributors

- CLI uses `yarn` as a package manager. To install yarn you need Node.js 18 installed and then run `corepack enable`. Then you do `yarn install` to install all the dependencies and you need to also do `yarn before-build` before moving any further
- CLI uses `yarn` as a package manager. To install yarn you need Node.js 18 installed and then run `corepack enable`. Then you run `yarn` to install all the dependencies and you need to also run `yarn before-build` before moving any further
- **First** commit in your PR or PR title must use [Conventional Commits](https://www.conventionalcommits.org/) (optionally end your commit message with: `[fixes DXJ-000 DXJ-001]`. Where `DXJ-000` and `DXJ-001` are ids of the Linear issues that you were working on)
- To use Fluence CLI in the development mode, run: `./bin/dev.js` (types are not checked in development mode because it's faster and more convenient to work with. Use typechecking provided by your IDE during development)
- To use Fluence CLI in the production mode, run `yarn build` first, then run: `./bin/run.js`. If you want to make sure you are running the actual package the users will use, do `yarn pack-*` command for your platform and architecture (this is used for tests as well)
Expand All @@ -26,5 +26,5 @@ When you contribute, you have to be aware that your contribution is covered by *
- To run tests locally you need to do the following:
1. [Docker](https://docs.docker.com/get-docker/)
1. run `docker compose up -d`
1. run `FLUENCE_ENV=local yarn test-linux-x64` or `FLUENCE_ENV=local yarn test-darwin-arm64` which will lint and check the code, build it, package it, prepare the tests and run them
1. run e.g. for linux: `DEBUG=fluence:* yarn test-linux-x64` which will lint and check the code, build it, package it, prepare the tests and run them
- for your convenience a dir `.f` is added to gitignore so you can generate projects in this dir for testing and development purposes
321 changes: 186 additions & 135 deletions docs/commands/README.md

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion docs/configs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ Defines project's secret keys that are used only in the scope of this particular
Defines user's secret keys that can be used across different Fluence projects. You can manage user's keys using commands from `fluence key` group of commands with `--user` flag
## [config.yaml](./config.md)

Defines global config for Fluence CLI
Defines global config for Fluence CLI
## [env.yaml](./env.md)

Defines user project preferences
11 changes: 11 additions & 0 deletions docs/configs/env.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# env.yaml

Defines user project preferences

## Properties

| Property | Type | Required | Description |
|--------------|--------|----------|-------------------------------------------------------------------------------------------------------|
| `version` | number | **Yes** | |
| `fluenceEnv` | string | No | Fluence environment to connect to Possible values are: `kras`, `stage`, `testnet`, `local`, `custom`. |

46 changes: 28 additions & 18 deletions docs/configs/fluence.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# Remove colors from CLI output
# FORCE_COLOR=0

# Fluence network to use. Can be 'stage', 'testnet', 'kras' or 'local'
# 'local' multiaddresses are hardcoded at `src/lib/localNodes.ts`
# Fluence network to use for tests. Can be 'stage', 'testnet', 'kras' or 'local'
# 'local' multiaddresses are currently hardcoded at `src/lib/multiaddres.ts`
# FLUENCE_ENV="local"

# To set path for the user's .fluence dir. Default: ~/.fluence (for Windows: %LOCALAPPDATA%\fluence)
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@
"extends": [
"eslint:recommended",
"plugin:node/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:@typescript-eslint/strict-type-checked",
"plugin:import/recommended",
"plugin:import/typescript",
"prettier"
Expand Down
6 changes: 3 additions & 3 deletions src/commands/aqua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
FLUENCE_CONFIG_FULL_FILE_NAME,
} from "../lib/const.js";
import { ensureAquaImports } from "../lib/helpers/aquaImports.js";
import { stringifyUnknown } from "../lib/helpers/jsonStringify.js";
import { stringifyUnknown } from "../lib/helpers/utils.js";
import { initCli, exitCli } from "../lib/lifeCycle.js";
import { projectRootDir, validatePath } from "../lib/paths.js";
import { input, type InputArg } from "../lib/prompt.js";
Expand Down Expand Up @@ -175,7 +175,7 @@ export default class Aqua extends Command {
}

const watchingNotification = (): void => {
return commandObj.logToStderr(
commandObj.logToStderr(
`Watching for changes at ${color.yellow(inputFlag)}...`,
);
};
Expand All @@ -198,7 +198,7 @@ export default class Aqua extends Command {
})
.catch((error): void => {
commandObj.logToStderr(stringifyUnknown(error));
return watchingNotification();
watchingNotification();
});
});
}
Expand Down
9 changes: 3 additions & 6 deletions src/commands/deal/change-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Args } from "@oclif/core";

import { BaseCommand, baseFlags } from "../../baseCommand.js";
import { commandObj } from "../../lib/commandObj.js";
import { NETWORK_FLAG, PRIV_KEY_FLAG } from "../../lib/const.js";
import { PRIV_KEY_FLAG, ENV_FLAG } from "../../lib/const.js";
import { dealUpdate } from "../../lib/deal.js";
import { initCli } from "../../lib/lifeCycle.js";
import { input } from "../../lib/prompt.js";
Expand All @@ -31,7 +31,7 @@ export default class ChangeApp extends BaseCommand<typeof ChangeApp> {
static override flags = {
...baseFlags,
...PRIV_KEY_FLAG,
...NETWORK_FLAG,
...ENV_FLAG,
};

static override args = {
Expand All @@ -55,10 +55,7 @@ export default class ChangeApp extends BaseCommand<typeof ChangeApp> {
(await input({ message: "Enter deal address" })),
appCID:
args["NEW-APP-CID"] ?? (await input({ message: "Enter new app CID" })),
network: await ensureChainNetwork({
maybeNetworkFromFlags: flags.network,
maybeDealsConfigNetwork: maybeFluenceConfig?.chainNetwork,
}),
network: await ensureChainNetwork(flags.env, maybeFluenceConfig),
privKey: flags["priv-key"],
});

Expand Down
9 changes: 3 additions & 6 deletions src/commands/deal/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Flags } from "@oclif/core";

import { BaseCommand, baseFlags } from "../../baseCommand.js";
import { commandObj } from "../../lib/commandObj.js";
import { NETWORK_FLAG, PRIV_KEY_FLAG } from "../../lib/const.js";
import { ENV_FLAG, PRIV_KEY_FLAG } from "../../lib/const.js";
import { dealCreate } from "../../lib/deal.js";
import { initCli } from "../../lib/lifeCycle.js";
import { ensureChainNetwork } from "../../lib/provider.js";
Expand All @@ -42,7 +42,7 @@ export default class Create extends BaseCommand<typeof Create> {
description: "Max workers in the deal",
default: 3,
}),
...NETWORK_FLAG,
...ENV_FLAG,
...PRIV_KEY_FLAG,
};

Expand All @@ -57,10 +57,7 @@ export default class Create extends BaseCommand<typeof Create> {
minWorkers: flags["min-workers"],
targetWorkers: flags["target-workers"],
privKey: flags["priv-key"],
chainNetwork: await ensureChainNetwork({
maybeNetworkFromFlags: flags.network,
maybeDealsConfigNetwork: maybeFluenceConfig?.chainNetwork,
}),
chainNetwork: await ensureChainNetwork(flags.env, maybeFluenceConfig),
});

commandObj.logToStderr(
Expand Down
17 changes: 7 additions & 10 deletions src/commands/deal/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { initNewWorkersConfig } from "../../lib/configs/project/workers.js";
import {
KEY_PAIR_FLAG,
PRIV_KEY_FLAG,
NETWORK_FLAG,
ENV_FLAG,
OFF_AQUA_LOGS_FLAG,
DEAL_CONFIG,
FLUENCE_CONFIG_FULL_FILE_NAME,
Expand Down Expand Up @@ -61,7 +61,7 @@ export default class Deploy extends BaseCommand<typeof Deploy> {
...KEY_PAIR_FLAG,
...OFF_AQUA_LOGS_FLAG,
...PRIV_KEY_FLAG,
...NETWORK_FLAG,
...ENV_FLAG,
...FLUENCE_CLIENT_FLAGS,
...IMPORT_FLAG,
...NO_BUILD_FLAG,
Expand All @@ -85,12 +85,8 @@ export default class Deploy extends BaseCommand<typeof Deploy> {
true,
);

const chainNetwork = await ensureChainNetwork({
maybeNetworkFromFlags: flags.network,
maybeDealsConfigNetwork: fluenceConfig.chainNetwork,
});

const chainNetworkId = DEAL_CONFIG[chainNetwork].chainId;
const chainNetwork = await ensureChainNetwork(flags.env, fluenceConfig);
const chainNetworkId = DEAL_CONFIG[chainNetwork].id;
const workersConfig = await initNewWorkersConfig();

const aquaImports = await ensureAquaImports({
Expand Down Expand Up @@ -140,7 +136,7 @@ export default class Deploy extends BaseCommand<typeof Deploy> {
name: workerName,
definition: appCID,
} of uploadResult.workers) {
const deal = fluenceConfig?.deals?.[workerName];
const deal = fluenceConfig.deals?.[workerName];
assert(deal !== undefined);
const { minWorkers = MIN_WORKERS, targetWorkers = TARGET_WORKERS } = deal;

Expand Down Expand Up @@ -263,7 +259,8 @@ export default class Deploy extends BaseCommand<typeof Deploy> {
: `\n\n${yamlDiffPatch("", {}, { "updated deals": updatedDeals })}`;

if (createdDealsText === "" && updatedDealsText === "") {
return commandObj.log("No updated or created deals");
commandObj.log("No updated or created deals");
return;
}

commandObj.log(
Expand Down
10 changes: 6 additions & 4 deletions src/commands/deal/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ import {
DIAL_TIMEOUT_FLAG_NAME,
TRACING_FLAG,
} from "../../lib/const.js";
import { formatAquaLogs } from "../../lib/helpers/formatLogs.js";
import { stringifyUnknown } from "../../lib/helpers/jsonStringify.js";
import {
formatAquaLogs,
stringifyUnknown,
commaSepStrToArr,
} from "../../lib/helpers/utils.js";
import {
disconnectFluenceClient,
initFluenceClient,
Expand Down Expand Up @@ -119,12 +122,11 @@ const getDealIdWorkerNameMap = async (
);

const workerNamesSet = Object.keys(deals);
const { parseWorkers } = await import("../../lib/deployWorkers.js");

const workersToGetLogsFor =
maybeWorkerNamesString === undefined
? workerNamesSet
: parseWorkers(maybeWorkerNamesString);
: commaSepStrToArr(maybeWorkerNamesString);

const workerNamesNotFoundInWorkersConfig = workersToGetLogsFor.filter(
(workerName) => {
Expand Down
10 changes: 3 additions & 7 deletions src/commands/deal/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Args } from "@oclif/core";

import { BaseCommand, baseFlags } from "../../baseCommand.js";
import { NETWORK_FLAG, PRIV_KEY_FLAG } from "../../lib/const.js";
import { ENV_FLAG, PRIV_KEY_FLAG } from "../../lib/const.js";
import { match } from "../../lib/deal.js";
import { initCli } from "../../lib/lifeCycle.js";
import { input } from "../../lib/prompt.js";
Expand All @@ -29,7 +29,7 @@ export default class Match extends BaseCommand<typeof Match> {
static override flags = {
...baseFlags,
...PRIV_KEY_FLAG,
...NETWORK_FLAG,
...ENV_FLAG,
};

static override args = {
Expand All @@ -47,11 +47,7 @@ export default class Match extends BaseCommand<typeof Match> {
const dealAddress =
args["DEAL-ADDRESS"] ?? (await input({ message: "Enter deal address" }));

const network = await ensureChainNetwork({
maybeNetworkFromFlags: flags.network,
maybeDealsConfigNetwork: maybeFluenceConfig?.chainNetwork,
});

const network = await ensureChainNetwork(flags.env, maybeFluenceConfig);
await match(network, flags["priv-key"], dealAddress);
}
}
73 changes: 73 additions & 0 deletions src/commands/default/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2023 Fluence Labs Limited
*
* 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 assert from "assert";

import { color } from "@oclif/color";

import { BaseCommand, baseFlags } from "../../baseCommand.js";
import { commandObj } from "../../lib/commandObj.js";
import { envConfig } from "../../lib/configs/globalConfigs.js";
import { ENV_ARG } from "../../lib/const.js";
import { initCli } from "../../lib/lifeCycle.js";
import {
ensureValidEnvFlag,
fluenceEnvPrompt,
ensureCustomRelays,
} from "../../lib/multiaddres.js";

export default class Peers extends BaseCommand<typeof Peers> {
static override description =
"Switch default Fluence Environment used in the current Fluence project";
static override examples = ["<%= config.bin %> <%= command.id %>"];
static override flags = {
...baseFlags,
};
static override args = {
...ENV_ARG,
};
async run(): Promise<void> {
const { args, fluenceConfig } = await initCli(
this,
await this.parse(Peers),
true,
);

assert(envConfig !== undefined, "this command requires fluence project");
const fluenceEnvFromArgs = await ensureValidEnvFlag(args.ENV);

const newFluenceEnv =
fluenceEnvFromArgs === undefined
? await fluenceEnvPrompt()
: fluenceEnvFromArgs;

if (
newFluenceEnv === "custom" &&
fluenceConfig.customFluenceEnv === undefined
) {
await ensureCustomRelays(fluenceConfig);
}

envConfig.fluenceEnv = newFluenceEnv;
await envConfig.$commit();

commandObj.log(
`Successfully set default fluence environment to ${color.yellow(
newFluenceEnv,
)}`,
);
}
}
Loading

0 comments on commit a09f39b

Please sign in to comment.