From 74e8662019f5373d22989adde7f9d550bb7394ff Mon Sep 17 00:00:00 2001 From: Jeff Lu Date: Wed, 21 Feb 2024 11:58:50 -0500 Subject: [PATCH] refactor unregisterMeshAgent --- package.json | 2 +- src/commands/deploy.ts | 4 ++-- src/common/src/hzn.ts | 28 +++++++++++++++------------ src/common/src/interface/hzn-model.ts | 3 +++ src/common/src/utils.ts | 6 ++++-- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index ec9304b..ce782ee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hzn-cli", - "version": "0.7.5", + "version": "0.7.6", "description": "Open Horizon CLI toolkit helps streamline the process of preparing node agents and perform tasks between orgs environments", "main": "./build/index.js", "bin": { diff --git a/src/commands/deploy.ts b/src/commands/deploy.ts index 8ef9f90..d19383b 100644 --- a/src/commands/deploy.ts +++ b/src/commands/deploy.ts @@ -5,7 +5,7 @@ import { existsSync } from 'fs'; import { Hzn, utils } from '../common/src/hzn'; import { customRun, IHznParam, justRun, loop, promptForUpdate, runDirectly } from '../common/src/interface'; -import { IAutoParam } from '../common/src/interface/hzn-model'; +import { IAutoParam, justRunCliOptional } from '../common/src/interface/hzn-model'; import type { Arguments, CommandBuilder } from 'yargs'; type Options = { @@ -91,7 +91,7 @@ export const handler = (argv: Arguments): void => { } as IHznParam; const hzn = new Hzn(hznModel); - hzn.init() + hzn.init(justRunCliOptional.indexOf(action) >= 0) .subscribe({ complete: () => { if(loop.includes(action)) { diff --git a/src/common/src/hzn.ts b/src/common/src/hzn.ts index 76a61b6..e96c488 100644 --- a/src/common/src/hzn.ts +++ b/src/common/src/hzn.ts @@ -48,7 +48,7 @@ export class Hzn { this.mmsPattern = param.mmsPattern; } - init() { + init(cliOptional = false) { return new Observable((observer) => { this.envVar.init() .subscribe({ @@ -102,16 +102,20 @@ export class Hzn { } }) } else { - this.preInstallHznCli() - .subscribe({ - complete: () => { - console.log('done installing hzn.'); - observer.complete(); - }, - error: (err) => { - observer.error(err); - } - }) + if(cliOptional == true) { + observer.complete(); + } else { + this.preInstallHznCli() + .subscribe({ + complete: () => { + console.log('done installing hzn.'); + observer.complete(); + }, + error: (err) => { + observer.error(err); + } + }) + } } } else { observer.error(err); @@ -210,7 +214,7 @@ export class Hzn { return utils.registerMeshAgent(); } unregisterMeshAgent() { - return utils.unregisterMeshAgent(); + return this.param.name.length > 0 ? utils.unregisterMeshAgent(this.param) : of('Please specify agent name') } unregisterAgent() { return utils.unregisterAgent(true) diff --git a/src/common/src/interface/hzn-model.ts b/src/common/src/interface/hzn-model.ts index 2f68234..f229f0a 100644 --- a/src/common/src/interface/hzn-model.ts +++ b/src/common/src/interface/hzn-model.ts @@ -6,6 +6,9 @@ export const justRun = [ 'register', 'removeDeploymentPolicy', 'removeObject', 'removeOrg', 'removeNode', 'removeService', 'reviewPolicy', 'reviewServiceDefinition', 'unregisterMeshAgent', "registerMeshAgent" ]; +export const justRunCliOptional = [ + 'registerMeshAgent', 'unregisterMeshAgent' +]; export const promptForUpdate = [ 'setup', 'test', 'buildAndPublish', 'buildPublishAndRegister', 'buildMMSImage', 'buildServiceImage', 'editDeploymentPoicy', 'editNodePolicy', 'editServicePolicy', 'publishAndRegister', diff --git a/src/common/src/utils.ts b/src/common/src/utils.ts index a52f9e4..ca72081 100644 --- a/src/common/src/utils.ts +++ b/src/common/src/utils.ts @@ -979,9 +979,11 @@ export class Utils { uninstallK3s() { return this.shell(`sudo systemctl stop k3s && /usr/local/bin/k3s-uninstall.sh`); } - unregisterMeshAgent() { + unregisterMeshAgent(params: IHznParam) { const pEnv = process.env; - const arg = `curl -sL --insecure -u $HZN_ORG_ID/$HZN_EXCHANGE_USER_AUTH -X DELETE ${pEnv.MESH_ENDPOINT}/v1/orgs/${pEnv.HZN_ORG_ID}/nodes/${pEnv.HZN_DEVICE_ID}`; + //const arg = `curl -sL --insecure -u $HZN_ORG_ID/$HZN_EXCHANGE_USER_AUTH -X DELETE ${pEnv.MESH_ENDPOINT}/v1/orgs/${pEnv.HZN_ORG_ID}/nodes/${pEnv.HZN_DEVICE_ID}`; + //const arg = `kubectl -n ohmesh3-frontend-ns exec -i agent-769d687ff9-8kssh -- hzn unregister -r -f --timeout 3`; + const arg = `kubectl -n ${pEnv.AGENT_NAMESPACE} exec -i ${params.name} -- hzn unregister -r -f --timeout 3`; return this.shell(arg); } installK3s(params: IAutoParam) {