Function: Remove a Virtual Machine from Chef using Knife via SSH

by Simon Sparks · 5 September 2025

To use this function add it to the class library file named ChefService.ts

GitHub Repository: https://github.com/SimonSparksUK/Orchestrator

Filename: ChefService.ts

Description: Orchestrator Function to remove a virtual machines from Chef using Knife via SSH

Function ChefDecomission

public ChefDecomission(arrHostName: string[], strUsername: string, strPassword: string): void {
    let strChefFQDN: string = "chef.cloudbuildtools.com";

    try {
        let objSSHSession: SSHSession = new SSHSession(strChefFQDN, strUsername);
        objSSHSession.connectWithPassword(strPassword)

        arrHostName.forEach((strHostName: string) => {

            objSSHSession.executeCommand("knife node delete " + strHostName.toLowerCase() + ".cloudbuildtools.com -y -c /home/cloudbuildtools/client.rb -u vcoflow -k /home/cloudbuildtools/client.pem", true);

            this.objLogger.info("SSH Executing: knife node delete '" + strHostName.toLowerCase() + ".cloudbuildtools.com'");

            let strErrorText: string = objSSHSession.error;
            let intExitCode: number = objSSHSession.exitCode;
            let strOutputText: string = objSSHSession.output;
            strOutputText = strOutputText.replace(/\n/, "");

            if (intExitCode == 0) {
                this.objLogger.info("SSH Output: " + strOutputText);
            }
            else {
                if (strErrorText.search(/404 \"Not Found\"/) > -1) {
                    this.objLogger.info("SSH Error: Node " + strHostName.toLowerCase() + ".cloudbuildtools.com not found on Chef Server");
                }
                else {
                    this.objLogger.info(strErrorText);
                }
            }

            objSSHSession.executeCommand("knife client delete " + strHostName.toLowerCase() + ".cloudbuildtools.com -y -c /home/cloudbuildtools/client.rb -u vcoflow -k /home/cloudbuildtools/client.pem", true);

            this.objLogger.info("SSH Executing: knife client delete '" + strHostName.toLowerCase() + ".cloudbuildtools.com'");

            strErrorText = objSSHSession.error;
            intExitCode = objSSHSession.exitCode;
            strOutputText = objSSHSession.output;
            strOutputText = strOutputText.replace(/\n/, "");

            if (intExitCode == 0) {
                this.objLogger.info("SSH Output: " + strOutputText);
            }
            else {
                if (strErrorText.search(/404 \"Not Found\"/) > -1) {
                    this.objLogger.info("SSH Error: Client " + strHostName.toLowerCase() + ".cloudbuildtools.com not found on Chef Server");
                }
                else {
                    this.objLogger.info(strErrorText);
                }
            }
        });

        objSSHSession.disconnect();
        objSSHSession = null;
    }
    catch (strException) {
        this.objLogger.info("Exception: " + strException);
        this.objLogger.info("Unable to SSH to " + strChefFQDN + " to run the 'knife' commands to clean up Chef");
    }
}

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like