Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding the .ts file for commands/internal/container/prestop #3110

Open
wants to merge 2 commits into
base: v2.x/staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions bin/commands/internal/container/prestop/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 * as std from 'std';
import * as fs from '../../../../libs/fs';
import * as common from '../../../../libs/common';
import * as node from '../../../../libs/node';
import * as shell from '../../../../libs/shell';
import * as config from '../../../../libs/config';
import * as component from '../../../../libs/component';

export function execute() {
node.requireNode();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may not want this right here. in the shell script, it was assumed most everything required node.
we see in the library file that the catalog thing uses curl.js... so why not put the requireNode() over there
i believe the requireNode() function caches the answer about if node exists, so if you call it multiple times it should not harm performance.


let pod_name = shell.execOutSync('hostname', '-s').out?.toLowerCase();

common.printLevel0Message('Delete APIML static definitions written by current pod ' + pod_name);
// Validation
common.requireZoweYaml();

//load environment
config.loadEnvironmentVariables();

if (!std.getenv('ZWE_RUN_IN_CONTAINER')){
common.printErrorAndExit('Error ZWEL0123E: This function is only available in Zowe Containerization deployment.', undefined, 157);
}

let zwe_static_definition_dir = std.getenv('ZWE_STATIC_DEFINITIONS_DIR');
let zwe_cli_parameter_hs_instance = std.getenv('ZWE_CLI_PARAMETER_HA_INSTANCE');
if(fs.directoryExists(zwe_static_definition_dir) && pod_name !== undefined){
let result = shell.execOutSync('sh', '-c', `cd ${zwe_cli_parameter_hs_instance} && ls -l 2>&1`);
if (result.out) {
common.printMessage("- deleting");
shell.execOutSync('sh', '-c', `rm -f *.${zwe_cli_parameter_hs_instance}.* 2>&1`);
common.printMessage("- refreshing api catalog");
let apicatalog_host = `api-catalog-service.${std.getenv('ZWE_POD_NAMESPACE')? std.getenv('ZWE_POD_NAMESPACE'): 'zowe'}.svc.${std.getenv('ZWE_POD_CLUSTERNAME')? std.getenv('ZWE_POD_CLUSTERNAME'): 'cluster.local'}`
let success =component.refreshStaticRegistration(apicatalog_host,
std.getenv('ZWE_components_api_catalog_port'),
std.getenv('ZWE_zowe_certificate_pem_key'),
std.getenv('ZWE_zowe_certificate_pem_certificate'),
std.getenv('ZWE_zowe_certificate_pem_certificateAuthorities'));
Comment on lines +46 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these come from zowe.yaml.
so, instead please use the configmgr to get them, because it has more validation than just requesting a string from an env var.
For example,

common.requireZoweYaml();
const ZOWE_CONFIG=config.getZoweConfig();
const catalogPort=ZOWE_CONFIG.zowe.components.api-catalog ? ZOWE_CONFIG.zowe.components.api-catalog.port : undefined;

if something is required by schema, you can guarantee it is in the object ZOWE_CONFIG there. but, a lot of these parameters are marked as optional (on purpose or accident) so we need to check them.

if (success) {
common.printFormattedDebug("ZWELS", "zwe-internal-container-prestop,execute", ` refreshStaticRegistration success`);
} else {
common.printFormattedError("Error ZWEL0142E", "zwe-internal-container-prestop,execute", `processComponentDiscoverySharedLibs failure`);
}
} else {
common.printMessage("- nothing to delete"); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bracket in a weird place?

}
}


// exit message
common.printLevel1Message(`APIML static registrations are refreshed successfully.`);
}
31 changes: 28 additions & 3 deletions bin/libs/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,24 +905,49 @@ export function processComponentDiscoverySharedLibs(componentDir: string): boole
}
return true;
}
/*

const gatewayHost = std.getenv('ZWE_GATEWAY_HOST');
const haInstanceHostname = std.getenv('ZWE_haInstance_hostname');
const catalogPort = Number(std.getenv('ZWE_components_api_catalog_port'));
const zoweCertificatePemKey = std.getenv('ZWE_zowe_certificate_pem_key');
const zoweCertificatePemCertificate = std.getenv('ZWE_zowe_certificate_pem_certificate');
const zoweCertificatePemCertificateAuthorities = std.getenv('ZWE_zowe_certificate_pem_certificateAuthorities');
const zoweRuntimeDirectory = std.getenv('ZWE_zowe_runtimeDirectory');
//TODO implement refreshStaticRegistration

export function refreshStaticRegistration(apimlcatalogHost: string=gatewayHost, apimlcatalogPort: number= catalogPort,
authKey: string=zoweCertificatePemKey, authCert: string=zoweCertificatePemCertificate,
caCert: string=zoweCertificatePemCertificateAuthorities): number{
caCert: string=zoweCertificatePemCertificateAuthorities): boolean{
if (!apimlcatalogHost) {
if (haInstanceHostname) {
apimlcatalogHost = haInstanceHostname;
} else {
apimlcatalogHost = 'localhost';
}
}
let utils_dir= `${zoweRuntimeDirectory}/bin/utils`;
common.printTrace('calling API Catalog /static-api/refresh to refresh static registrations');
let shellReturn = shell.execOutSync('sh', '-c', `${std.getenv('NODE_HOME')}/bin/node" \
"${utils_dir}/curl.js" \
"https://${apimlcatalogHost}:${apimlcatalogPort}/apicatalog/static-api/refresh" \
-X POST \
--key "${authKey}" \
--cert "${authCert}" \
--cacert "${caCert}"`);
if (shellReturn.rc == 0) {
common.printTrace(' * Exit code: '+ shellReturn.rc);
common.printTrace(' * Output:');
if (shellReturn.out){
common.printTrace(stringlib.paddingLeft(shellReturn.out, " "))
}
} else {
common.printError(' * Exit code: '+ shellReturn.rc);
common.printError('');
if (shellReturn.out){
common.printError(stringlib.paddingLeft(shellReturn.out, " "))
}
common.printErrorAndExit('Error ZWEL0142E: Failed to refresh APIML static registrations.', undefined, 142);
}
return shellReturn.rc == 0 ? true : false;
}
*/