Skip to content
This repository has been archived by the owner on Feb 18, 2025. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into read-dir-bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Yemi Kelani committed Feb 15, 2024
2 parents a64cd3b + c771dae commit fb2be1d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ function executeSimpleSdkCommand(command: string, session: Session, outputChanne
})
.catch(e => {
session.operationPending = false;
showErrorMessage(`Failure executing Delete Operator command: RC ${e}`);
showErrorMessage(`Failure executing Delete Operator command: ${e}`);
});
break;
}
Expand Down Expand Up @@ -931,7 +931,7 @@ function executeSimpleSdkCommand(command: string, session: Session, outputChanne
})
.catch(e => {
session.operationPending = false;
showErrorMessage(`Failure executing Redeploy Collection command: RC ${e}`);
showErrorMessage(`Failure executing Redeploy Collection command: ${e}`);
});
break;
}
Expand All @@ -949,7 +949,7 @@ function executeSimpleSdkCommand(command: string, session: Session, outputChanne
})
.catch(e => {
session.operationPending = false;
showErrorMessage(`Failure executing Redeploy Operator command: RC ${e}`);
showErrorMessage(`Failure executing Redeploy Operator command: ${e}`);
});
break;
}
Expand Down Expand Up @@ -1014,7 +1014,7 @@ function executeSdkCommandWithUserInput(command: string, session: Session, outpu
})
.catch(e => {
session.operationPending = false;
showErrorMessage(`Failure executing Create Operator command: RC ${e}`);
showErrorMessage(`Failure executing Create Operator command: ${e}`);
});
}
}
Expand Down
66 changes: 50 additions & 16 deletions src/shellCommands/ocSdkCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type HTTPS = typeof https;
export class OcSdkCommand {
constructor(private pwd?: string | undefined) {}

private commandOutput: string = "";

/**
* Executes the requested command
* @param cmd - The command to be executed
Expand All @@ -25,7 +27,7 @@ export class OcSdkCommand {
* @param logPath - Log path to store command output
* @returns - A Promise containing the the return code of the executed command
*/
private async run(cmd: string, args?: Array<string>, outputChannel?: vscode.OutputChannel, logPath?: string, callbackFunction?: (outputValue: string) => void): Promise<any> {
private async run(cmd: string, args?: Array<string>, outputChannel?: vscode.OutputChannel, logPath?: string): Promise<any> {
process.env.PWD = this.pwd;
let outputValue = "";

Expand Down Expand Up @@ -65,14 +67,13 @@ export class OcSdkCommand {
return reject(error.message);
});
childProcess.on("close", (code: number) => {
this.commandOutput = outputValue;

if (code) {
if (code !== 0) {
return reject(code);
}
} else {
if (callbackFunction !== undefined) {
callbackFunction(outputValue);
}
return resolve(code);
}
});
Expand Down Expand Up @@ -173,20 +174,16 @@ export class OcSdkCommand {
*/
async runOcSdkVersion(outputChannel?: vscode.OutputChannel, logPath?: string): Promise<string | undefined> {
const galaxyNamespace = getAnsibleGalaxySettings(AnsibleGalaxySettings.ansibleGalaxyNamespace) as string;
let versionInstalled: string;

// ansible-galaxy collection list | grep ibm.operator_collection_sdk
const cmd: string = "ansible-galaxy";
let args: Array<string> = ["collection", "list", "|", "grep", `${galaxyNamespace}.operator_collection_sdk`];

let setVersionInstalled = (outputValue: string) => {
versionInstalled = outputValue.split(" ")?.filter(item => {
return item.length;
})?.[1]; // item in [1] is the version
};
const args: Array<string> = ["collection", "list", "|", "grep", `${galaxyNamespace}.operator_collection_sdk`];

return this.run(cmd, args, outputChannel, logPath, setVersionInstalled)
return this.run(cmd, args, outputChannel, logPath)
.then(() => {
const versionInstalled = this.commandOutput.split(" ")?.filter(item => {
return item.length;
})?.[1]; // item in [1] is the version
return versionInstalled;
})
.catch(e => {
Expand Down Expand Up @@ -267,7 +264,17 @@ export class OcSdkCommand {
process.env.ANSIBLE_JINJA2_NATIVE = "true";
const cmd: string = "ansible-playbook";
args = args.concat("ibm.operator_collection_sdk.create_operator");
return this.run(cmd, args, outputChannel, logPath);

return new Promise(async (resolve, reject) => {
this.run(cmd, args, outputChannel, logPath)
.then(() => {
resolve(this.commandOutput);
})
.catch(returnCode => {
const errorMessage = `(RC: ${returnCode}) ${getFinalPlaybookTaskFailure(this.commandOutput)}`;
reject(errorMessage);
});
});
}

/**
Expand Down Expand Up @@ -319,10 +326,20 @@ export class OcSdkCommand {
* @param logPath - Log path to store command output
* @returns - A Promise container the return code of the command being executed
*/
private executeSimpleCommand(command: string, outputChannel?: vscode.OutputChannel, logPath?: string): Promise<any> {
private async executeSimpleCommand(command: string, outputChannel?: vscode.OutputChannel, logPath?: string): Promise<any> {
const cmd: string = "ansible-playbook";
let args: Array<string> = [command];
return this.run(cmd, args, outputChannel, logPath);

return new Promise(async (resolve, reject) => {
this.run(cmd, args, outputChannel, logPath)
.then(() => {
resolve(this.commandOutput);
})
.catch(returnCode => {
const errorMessage = `(RC: ${returnCode}) ${getFinalPlaybookTaskFailure(this.commandOutput)}`;
reject(errorMessage);
});
});
}
}

Expand Down Expand Up @@ -375,3 +392,20 @@ async function getRequest(apiUrl: string): Promise<string | undefined> {
function getLatestCollectionVersion(jsonData: any): string | undefined {
return jsonData?.data?.collection?.latest_version?.version ?? jsonData?.data[0]?.version;
}

function getFinalPlaybookTaskFailure(stdOutput: string): string | undefined {
const playbookTasksFailureRegex = /FAILED! => {.*}/g;
const playbookTasksFailures = stdOutput.match(playbookTasksFailureRegex);
const finalFailureObject = playbookTasksFailures?.[playbookTasksFailures.length - 1]?.replace("FAILED! => ", "");
if (finalFailureObject) {
const defaultAnsibleErrorMessage = "non-zero return code";
const finalFailureMessage = JSON.parse(finalFailureObject)?.["msg"];

if (finalFailureMessage?.includes(defaultAnsibleErrorMessage)) {
return JSON.parse(finalFailureObject)?.["stderr_lines"];
}

return finalFailureMessage;
}
return stdOutput;
}

0 comments on commit fb2be1d

Please sign in to comment.