Hi all,
I'm attempting to download an ISO via REST API. I'm currently trying to save the file locally within the vRA appliance to the vRO temp directory (/var/lib/vco/app-server/temp), where I can then upload it to a datastore, mount it to a VM, etc.
I have the rest of it working, but the actual saving the ISO isn't working quite correctly. I've tried using the FileWriter.write and File.write methods to take the REST response content as string and write it to a file. It works, but the file size of the ISO that gets saved off doesn't match the file size of the ISO when manually downloaded, so something isn't converting quite correctly.
I've started looking at the MimeAttachment object now to see if that will get me where I'm trying to go, but I'm not sure if that's the best route either. Is there an easier or better way to accomplish what I'm trying to do? Any assistance would be greatly appreciated.
Thanks!
ISO files are binary content which may (ie. due to special characters, etc.) has no 1:1 representation as JS string.
I'd recommend to download ISO files using curl command which has proper handling for binary content. Here is a sample snippet showing how to download the ISO file of DamnSmallLinux and store it to vRO temp directory:
var cmd = new Command("curl http://distro.ibiblio.org/damnsmall/current/dsl-4.4.10.iso -o /var/lib/vco/app-server/temp/dsl-4.4.10.iso -f");
var r = cmd.execute(true);
if (r != 0) {
System.error("Something went wrong with download; status code " + r);
} else {
System.log("Download completed successfully");
}
The curl command syntax is:
curl <iso-url> -o <outfile> -f
where
<iso-url> is the URL of the ISO file to download
-o option specifies the file where to store the downloaded content
-f option is needed to set result code to non-zero if download fail
If the download site requires authentication, you can pass user credentials using option -u <username>:<password>
ISO files are binary content which may (ie. due to special characters, etc.) has no 1:1 representation as JS string.
I'd recommend to download ISO files using curl command which has proper handling for binary content. Here is a sample snippet showing how to download the ISO file of DamnSmallLinux and store it to vRO temp directory:
var cmd = new Command("curl http://distro.ibiblio.org/damnsmall/current/dsl-4.4.10.iso -o /var/lib/vco/app-server/temp/dsl-4.4.10.iso -f");
var r = cmd.execute(true);
if (r != 0) {
System.error("Something went wrong with download; status code " + r);
} else {
System.log("Download completed successfully");
}
The curl command syntax is:
curl <iso-url> -o <outfile> -f
where
<iso-url> is the URL of the ISO file to download
-o option specifies the file where to store the downloaded content
-f option is needed to set result code to non-zero if download fail
If the download site requires authentication, you can pass user credentials using option -u <username>:<password>
Thanks for the response! Is there a way to do it without using the Command class? Use of that will require changing the "com.vmware.js.allow-local-process" property to "True", which could be considered decreasing overall system security.
An alternative would be to try vRO Download plug-in (author Joerg Lew) which can be downloaded from https://communities.vmware.com/docs/DOC-31015
Thanks again for the response. I've been messing with this for a little bit now, and after making various degrees of progress using various methods (attempting to use a PowerShell host to do the REST command to download the binary, trying the Download plugin, etc), I'm circling back to your initial suggestion to using the Command scripting class.
The vRO 7 documentation states that "By setting the com.vmware.js.allow-local-process system property to true, you allow the Command scripting class to write anywhere in the file system. This property overrides any file system access permissions that you set in the js-io-rights.conf file for the Command scripting class only. The file system access permissions that you set in the js-io-rights.conf file still apply to all scripting classes other than Command."
What I'm seeing, however, is that any command that tries to create a file in the file system (via curl or even something as simple as date >> newfile) results in errors. Manually running the commands while logged into the vRA appliance, of course, works. Is there something else I'm missing that I need to do to allow these commands to work?
Thanks again for any assistance.
Hmm, I'm not sure if the documentation if fully correct here. Need to double check it.
To my knowledge, the correct settings are:
As for date >> newfile - I suppose >> is interpreted not as stdout redirect but as just one additional argument. Could you try some commands that are not using shell-specific stuff?
I figured out what I was doing. I put double quotes around certain parts of my command I wanted to run. It apparently doesn't like that when using the Command scripting class, even though the command worked fine when running it from the shell. When I changed it from:
var cmd = new Command('curl -X GET -H "Accept: application/json" "https://resturl" -o "/var/lib/vco/app-server/temp/TESTVM1.iso" -u "username:password" -k')
to
var cmd = new Command("curl -X GET -H Accept:application/json https://resturl -o /var/lib/vco/app-server/temp/TESTVM1.iso -u username:password -k")
It worked as expected.
I'll go ahead and mark your initial response as the answer. Had I formatted my command as you had demonstrated there, I wouldn't have ran into any issue. Thanks again for all of your help!