Skip to content

Commit

Permalink
Add server commands to CLI plug-in
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Johnson <[email protected]>
  • Loading branch information
t1m0thyj committed Jan 29, 2025
1 parent 980f0fb commit f2b603f
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/cli/src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
*
*/

import * as path from "node:path";
import type { ICommandOptionDefinition } from "@zowe/imperative";
import { ZSshClient } from "zowe-native-proto-sdk";

// biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
export class Constants {
public static readonly OPT_SERVER_PATH: ICommandOptionDefinition = {
name: "server-path",
aliases: ["sp"],
description: "The remote path of the Zowe SSH server. Defaults to '~/.zowe-server'.",
description: `The remote path of the Zowe SSH server. Defaults to '${ZSshClient.DEFAULT_SERVER_PATH}'.`,
type: "string",
required: false,
};

public static readonly ZSSH_BIN_DIR = path.join(__dirname, "..", "bin");
}
35 changes: 35 additions & 0 deletions packages/cli/src/server/Server.definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

import type { ICommandDefinition } from "@zowe/imperative";
import { SshSession } from "@zowe/zos-uss-for-zowe-sdk";
import { Constants } from "../Constants";
import { ServerInstallDefinition } from "./install/Install.definition";
import { ServerUninstallDefinition } from "./uninstall/Uninstall.definition";

const ServerDefinition: ICommandDefinition = {
name: "server",
aliases: ["srv"],
summary: "Manage the Zowe SSH server",
description: "Manage the Zowe SSH server",
type: "group",
children: [ServerInstallDefinition, ServerUninstallDefinition],
passOn: [
{
property: "options",
value: [...SshSession.SSH_CONNECTION_OPTIONS, Constants.OPT_SERVER_PATH],
merge: true,
ignoreNodes: [{ type: "group" }],
},
],
};

export = ServerDefinition;
32 changes: 32 additions & 0 deletions packages/cli/src/server/install/Install.definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

import type { ICommandDefinition } from "@zowe/imperative";

export const ServerInstallDefinition: ICommandDefinition = {
handler: __dirname + "/Install.handler",
type: "command",
name: "install",
aliases: ["up"],
summary: "Install Zowe SSH server on z/OS host",
description: "Install Zowe SSH server on z/OS host",
examples: [
{
description: "Install Zowe SSH server in the default location",
options: "",
},
{
description: 'Install Zowe SSH server in the directory "/tmp/zowe-server"',
options: '--server-path "/tmp/zowe-server"',
},
],
profile: { optional: ["ssh"] },
};
30 changes: 30 additions & 0 deletions packages/cli/src/server/install/Install.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

import { ConfigUtils, ICommandHandler, IHandlerParameters } from "@zowe/imperative";
import { ZSshClient, ZSshUtils } from "zowe-native-proto-sdk";
import { Constants } from "../../Constants";

export default class ServerInstallHandler implements ICommandHandler {
public async process(params: IHandlerParameters): Promise<void> {
const session = ZSshUtils.buildSession(params.arguments);
const serverPath = params.arguments.serverPath ?? ZSshClient.DEFAULT_SERVER_PATH;
params.response.progress.startSpinner("Deploying Zowe SSH server...");
try {
await ZSshUtils.installServer(session, serverPath, Constants.ZSSH_BIN_DIR);
} finally {
params.response.progress.endSpinner();
}
params.response.console.log(
`Installed Zowe SSH server on ${ConfigUtils.getActiveProfileName("ssh", params.arguments)}`,
);
}
}
32 changes: 32 additions & 0 deletions packages/cli/src/server/uninstall/Uninstall.definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

import type { ICommandDefinition } from "@zowe/imperative";

export const ServerUninstallDefinition: ICommandDefinition = {
handler: __dirname + "/Uninstall.handler",
type: "command",
name: "uninstall",
aliases: ["rm"],
summary: "Uninstall Zowe SSH server on z/OS host",
description: "Uninstall Zowe SSH server on z/OS host",
examples: [
{
description: "Uninstall Zowe SSH server from the default location",
options: "",
},
{
description: 'Uninstall Zowe SSH server from the directory "/tmp/zowe-server"',
options: '--server-path "/tmp/zowe-server"',
},
],
profile: { optional: ["ssh"] },
};
25 changes: 25 additions & 0 deletions packages/cli/src/server/uninstall/Uninstall.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

import { ConfigUtils, ICommandHandler, IHandlerParameters } from "@zowe/imperative";
import { ZSshClient, ZSshUtils } from "zowe-native-proto-sdk";
import { Constants } from "../../Constants";

export default class ServerUninstallHandler implements ICommandHandler {
public async process(params: IHandlerParameters): Promise<void> {
const session = ZSshUtils.buildSession(params.arguments);
const serverPath = params.arguments.serverPath ?? ZSshClient.DEFAULT_SERVER_PATH;
await ZSshUtils.uninstallServer(session, serverPath, Constants.ZSSH_BIN_DIR);
params.response.console.log(
`Uninstalled Zowe SSH server from ${ConfigUtils.getActiveProfileName("ssh", params.arguments)}`,
);
}
}
2 changes: 2 additions & 0 deletions packages/sdk/src/ZSshClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class ZSshClient extends AbstractRpcClient implements Disposable {
reject(err);
} else {
stream.stderr.on("data", (chunk: Buffer) => {
// const EBCDIC = require("ebcdic-ascii").default;
// chunk = Buffer.from(new EBCDIC("1047").toEBCDIC(chunk.toString("hex")), "hex");
console.log("STDERR:", chunk.toString());
});
resolve(stream);
Expand Down
8 changes: 6 additions & 2 deletions packages/sdk/src/ZSshUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export class ZSshUtils {
if (err) {
reject(err);
} else {
ZSshUtils.uploadDir(sftp, localDir, serverPath.replace(/^~/, ".")).then(resolve, reject);
ZSshUtils.uploadDir(sftp, localDir, serverPath.replace(/^~/, "."))
.then(resolve, reject)
.finally(() => client.end());
}
});
});
Expand All @@ -73,7 +75,9 @@ export class ZSshUtils {
if (err) {
reject(err);
} else {
ZSshUtils.safeRemoveDir(sftp, localDir, serverPath.replace(/^~/, ".")).then(resolve, reject);
ZSshUtils.safeRemoveDir(sftp, localDir, serverPath.replace(/^~/, "."))
.then(resolve, reject)
.finally(() => client.end());
}
});
});
Expand Down

0 comments on commit f2b603f

Please sign in to comment.