3 Replies Latest reply on Jul 27, 2018 2:05 AM by tschoergez

    vRO 7.4 - executing curl using the Command class - how to format the command?

    Czernobog Hot Shot

      Hello,

      I need to build a workflow which executes a command on the embedded vRO 7.4 instance in vRA, with the purpose to export and import blueprint content between tenants.

      Executing the curl command directly using ssh works without problems, but I cannot get the API call to execute successfully when consctructing the command in a scriptable task.

      Here's the code:

       

      var cmd = see_below
      var command = new Command(cmd);
      command.execute(true); // execute and wait = true
      System.log("LOG: cmd: " + cmd); // for debugging
      var commandOutput = command.output;
      System.log("LOG: command output: " + commandOutput);
      

       

      and here is the string I use for the cmd:

       

      var cmd = 'curl --insecure -s -H "Accept: application/zip" -H "Authorization: Bearer ' + token + '" https://fqdn/content-management-service/api/packages/' + packageID + ' -o "' + filePath + '"';
      

      result: HTTP Status 401 Unauthorized

       

      var cmd = 'curl --insecure -s -H "Content-Type:application/zip" -H "Authorization: Bearer ' + token + '" https://fqdn/content-management-service/api/packages/' + packageID + ' -o "file=@' + filePath + '"';
      

      result: 400 Bad request

       

      var cmd = "curl --insecure -s -H 'Content-Type:application/zip' -H 'Authorization: Bearer " + token + "' https://fqdn/content-management-service/api/packages/" + packageID + " -o file=@" + filePath;
      

      result: HTTP Status 401 Unauthorized

       

      The auth token, packageID and filePath are inputs of the scriptable task and the values are correctly filled in the command string. I can ran each of the above directly in the console and the call executes with no issues.

       

      What would be the correct formatting to do a successful command execution?

        • 1. Re: vRO 7.4 - executing curl using the Command class - how to format the command?
          tschoergez Master
          vExpertUser ModeratorsVMware Employees

          string escaping hell :-)

          Not a solution, but my lazy workaround for such situations: Create a one-liner bash script that does the curl command and takes command line input parameters just for the variable parts. That usually helps avoiding too much escaping need.

          Alternatively you can create a bit more complex script that reads the parameter from a file, and then in the workflow use the filewriter object to write that file (which contains the variables, or the complete script), then with the Command.execute() just run this script without the need for any parameters.

          Joerg

          1 person found this helpful
          • 2. Re: vRO 7.4 - executing curl using the Command class - how to format the command?
            Czernobog Hot Shot

            Create a one-liner bash script that does the curl command and takes command line input parameters just for the variable parts.

            Thanks, that did the trick. I have saved the curl command as an sh script an execute it during workflow runtime with the needed parameters.

            Ideally I would do that stuff using only the REST plugin, but I cannot get it to work with input parameters other then string:(

             

            edit:

            Here's the relevant part of the workflow, it still needs some error checking but it works:

            inputs: fileName, packageID, token
            // create temp file with the package content and sh script with curl command
                var tempDir = System.getTempDirectory();
                // filepath for pkg file
                    var pkgFileName = "/vRApkg-" + fileName; // filename for the package, used in script
                    var pkgFilePath = tempDir + pkgFileName;
                // filepath for sh script
                    var shFileName = "/vRApkg-" + fileName + "-tmp.sh"; // filename of the sh script file
                    var shFilePath = tempDir + shFileName;
            // create wrapper sh script for curl command, since string encoding makes executing curl via Command class impossible
                var shFileContent = 'curl --insecure -s -H "Accept: application/zip" -H "Authorization: Bearer $1" https://FQDN/content-management-service/api/packages/$2 -o $3';
                //$1 = token, $2 = packageID, $3 = pkgFilePath
                System.log("LOG: shell script content: " + shFileContent);
                var fileWriter = new FileWriter(shFilePath);
                fileWriter.open();
                fileWriter.writeLine(shFileContent); 
                fileWriter.close();
                
            // execute wrapper sh script
                System.log("LOG: execute shell script in path: " + shFilePath);
                
                var command1 = new Command("chmod 755 " + shFilePath); // make file executable
                command1.execute(true); // execute and wait = true
                if (command1.output.length != 0) { System.log("LOG: command1 output: " + command1.output) };
                
                var command2Content = shFilePath + " " + token + " " + packageID + " " + pkgFilePath;
                // System.log("LOG: command2Content: " + command2Content); // for debugging only!!
                var command2 = new Command(command2Content); // execute sh script
                command2.execute(true); // execute and wait = true
                if (command2.output.length != 0) { System.log("LOG: command2 output: " + command2.output) };
                
                System.log("LOG: script execution complete");
            
            // delete temporary sh file after script execution
                var myFile = new File(shFilePath);
                myFile.deleteFile();
            
            • 3. Re: vRO 7.4 - executing curl using the Command class - how to format the command?
              tschoergez Master
              User ModeratorsvExpertVMware Employees

              Yes, unfortunately the REST Plugin does not support handling of binary data yet. If you would find such functionality helpful, you might create a SR with a Feature Request, to get it filed :-)

              Joerg