-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: workers remove * up * one test * Apply automatic changes * fix * Apply automatic changes * fix * fix * up * fix * up * fix * check * check * fix * Apply automatic changes * areAllWorkersRemoved function * Apply automatic changes * fix * fix * up * up * fix * fix * fix * export * fix * fix * all tests * fix: npm not being found when installed with yarn [fixes DXJ-479] (#484) * fix: npm not being found when installed with yarn [fixes DXJ-479] * display both errors * improve error * check local npm exists * improve formatting * improve error message * chore(main): release fluence-cli 0.8.8 (#495) * chore(main): release fluence-cli 0.8.8 * Apply automatic changes --------- Co-authored-by: fluencebot <[email protected]> Co-authored-by: shamsartem <[email protected]> * up * improve --------- Co-authored-by: shamsartem <[email protected]> Co-authored-by: fluencebot <[email protected]> Co-authored-by: fluencebot <[email protected]>
- Loading branch information
1 parent
8321f24
commit 71acd94
Showing
8 changed files
with
398 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/** | ||
* 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 { color } from "@oclif/color"; | ||
import { Args } from "@oclif/core"; | ||
|
||
import { BaseCommand, baseFlags } from "../../baseCommand.js"; | ||
import { commandObj } from "../../lib/commandObj.js"; | ||
import type { RemoveArgWorkers } from "../../lib/compiled-aqua/installation-spell/deploy.js"; | ||
import { initNewWorkersConfig } from "../../lib/configs/project/workers.js"; | ||
import { | ||
KEY_PAIR_FLAG, | ||
PRIV_KEY_FLAG, | ||
OFF_AQUA_LOGS_FLAG, | ||
FLUENCE_CONFIG_FULL_FILE_NAME, | ||
FLUENCE_CLIENT_FLAGS, | ||
TRACING_FLAG, | ||
WORKERS_CONFIG_FULL_FILE_NAME, | ||
} from "../../lib/const.js"; | ||
import { parseWorkers } from "../../lib/deployWorkers.js"; | ||
import { | ||
disconnectFluenceClient, | ||
initFluenceClient, | ||
} from "../../lib/jsClient.js"; | ||
import { initCli } from "../../lib/lifeCycle.js"; | ||
|
||
export default class Remove extends BaseCommand<typeof Remove> { | ||
static override description = `Remove workers from hosts, described in 'hosts' property in ${WORKERS_CONFIG_FULL_FILE_NAME}`; | ||
static override examples = ["<%= config.bin %> <%= command.id %>"]; | ||
static override flags = { | ||
...baseFlags, | ||
...KEY_PAIR_FLAG, | ||
...OFF_AQUA_LOGS_FLAG, | ||
...PRIV_KEY_FLAG, | ||
...FLUENCE_CLIENT_FLAGS, | ||
...TRACING_FLAG, | ||
}; | ||
static override args = { | ||
"WORKER-NAMES": Args.string({ | ||
description: `Comma separated names of workers to remove. Example: "worker1,worker2" (by default all workers from 'hosts' property in ${WORKERS_CONFIG_FULL_FILE_NAME} are removed)`, | ||
}), | ||
}; | ||
async run(): Promise<void> { | ||
const { flags, fluenceConfig, args } = await initCli( | ||
this, | ||
await this.parse(Remove), | ||
true, | ||
); | ||
|
||
const workersConfig = await initNewWorkersConfig(); | ||
|
||
const { ensureAquaFileWithWorkerInfo } = await import( | ||
"../../lib/deployWorkers.js" | ||
); | ||
|
||
await initFluenceClient(flags, fluenceConfig); | ||
const { Fluence } = await import("@fluencelabs/js-client"); | ||
const relayId = (await Fluence.getClient()).getRelayPeerId(); | ||
|
||
if (workersConfig.hosts === undefined) { | ||
return commandObj.error( | ||
`There are no workers in ${FLUENCE_CONFIG_FULL_FILE_NAME}`, | ||
); | ||
} | ||
|
||
const workersToRemove = | ||
args["WORKER-NAMES"] === undefined | ||
? Object.keys(workersConfig.hosts) | ||
: parseWorkers(args["WORKER-NAMES"]); | ||
|
||
const removeArg: RemoveArgWorkers = { | ||
workers: Object.entries(workersConfig.hosts) | ||
.filter(([workerName]) => { | ||
return workersToRemove.includes(workerName); | ||
}) | ||
.map(([name, worker]): RemoveArgWorkers["workers"][number] => { | ||
return { | ||
definition: worker.definition, | ||
name, | ||
dummy_deal_id: worker.dummyDealId, | ||
installation_spells: worker.installation_spells, | ||
}; | ||
}), | ||
}; | ||
|
||
const removeResult = await remove(flags.tracing, removeArg); | ||
|
||
const newHosts = Object.fromEntries( | ||
Object.entries(workersConfig.hosts) | ||
.map(([name, { installation_spells: prevInstSp, ...rest }]) => { | ||
const currentWorkerResult = removeResult.find((r) => { | ||
return r.name === name; | ||
}); | ||
|
||
const removedWorkerIds = currentWorkerResult?.worker_ids ?? []; | ||
|
||
const notRemovedWorkers = prevInstSp.filter(({ worker_id }) => { | ||
const workerRemovedSuccessfully = | ||
removedWorkerIds.includes(worker_id); | ||
|
||
return !workerRemovedSuccessfully; | ||
}); | ||
|
||
return [ | ||
name, | ||
{ installation_spells: notRemovedWorkers, ...rest }, | ||
] as const; | ||
}) | ||
.filter(([, { installation_spells }]) => { | ||
return installation_spells.length > 0; | ||
}), | ||
); | ||
|
||
workersConfig.hosts = newHosts; | ||
|
||
if (Object.keys(workersConfig.hosts).length === 0) { | ||
delete workersConfig.hosts; | ||
} | ||
|
||
await workersConfig.$commit(); | ||
await ensureAquaFileWithWorkerInfo(workersConfig, fluenceConfig); | ||
const { yamlDiffPatch } = await import("yaml-diff-patch"); | ||
|
||
commandObj.log( | ||
`\n\n${color.yellow("Success!")}\n\nrelay: ${relayId}\n\n${yamlDiffPatch( | ||
"", | ||
{}, | ||
{ removed: removeResult }, | ||
)}`, | ||
); | ||
|
||
await disconnectFluenceClient(); | ||
} | ||
} | ||
|
||
async function remove(tracing: boolean, removeArg: RemoveArgWorkers) { | ||
if (tracing) { | ||
const { remove } = await import( | ||
"../../lib/compiled-aqua-with-tracing/installation-spell/deploy.js" | ||
); | ||
|
||
return remove(removeArg); | ||
} | ||
|
||
const { remove } = await import( | ||
"../../lib/compiled-aqua/installation-spell/deploy.js" | ||
); | ||
|
||
return remove(removeArg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.