seTTembrinY
Contributor
Contributor

vCO and reading/writing Files

Jump to solution

Hi Community,

I am quite a newbie with vCO, so sorry for the maybe basic question.

I created a workflow for automatic deployment and customization of VMs. Work pretty well, template is cloned, sysprep, vCPUs and RAM set and so an. But the final customization tasks is, to assign a unique IP which is from a list in a simple and plain text file. vCO should open the file, take the first IP from the first line, remove it and close the file (DHCP is not an option). I can open and read the file with the "File Reader" and ".readall" options, but thats not exactly what I want. I tried some ".readline" which results in "cannot find function readline" and after I that  I thought about spliting the entire string which resulted in "split not defined". I really not sure if this is the right way.

Can somebody please guide me in the right direction, maybe with some code snippets?

Thank you very much!

S.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
tschoergez
Leadership
Leadership

ok, here's another example, which reads the complete file into one single string, and uses split() to split-up the different lines into elements of an array:

var fr = new FileReader("C://vcoscripts/demo.txt");
fr.open();

var complete = fr.readAll();
System.debug("complete: " + complete);
var lines = new Array();
lines= complete.split(/\r\n|\r|\n/);
System.debug("lines: " + lines);
for (var linenr in lines) {
    var currentLine = lines[linenr];
    System.debug("currentLine: " + currentLine);
    }

With this you can use standard array-methods (push(), pop()), which might be easier-to-use.

The RegExp to split up the lines I got from :

http://stackoverflow.com/questions/1155678/javascript-string-newline-character

(good read...)

edit: I forgot: with Array.strip() you can delete an element from an array (in your case the used IP-Address before writing it back to file via FileWriter...)

Regards,

Joerg

www.vcoportal.de - Orchestrate. Your Cloud.

View solution in original post

0 Kudos
11 Replies
tschoergez
Leadership
Leadership

Hi!

JavaScript is case-sensitive, so the Method os called readLine()...

So some example:

var fr = new FileReader("C://vcoscripts/demo.txt");
fr.open();

var firstLine = fr.readLine();

System.debug("firstLine: " + firstLine);

Unfortunately, it's not documented how to check if there are more lines, to loop through the whole file (as you usually do in Java).

Maybe it's better to read the complete file at once and separate it later.... (especially if the source is XML, CSV,....typed)

Regards,

Joerg

www.vcoportal.de - Orchestrate. Your Cloud.
seTTembrinY
Contributor
Contributor

Am I that stupid? 😉 case sensitve... Thank you very much!

So half of the work is done, then I just have to remove the first line from the file and save it... any more ideas?

0 Kudos
tschoergez
Leadership
Leadership

ok, here's another example, which reads the complete file into one single string, and uses split() to split-up the different lines into elements of an array:

var fr = new FileReader("C://vcoscripts/demo.txt");
fr.open();

var complete = fr.readAll();
System.debug("complete: " + complete);
var lines = new Array();
lines= complete.split(/\r\n|\r|\n/);
System.debug("lines: " + lines);
for (var linenr in lines) {
    var currentLine = lines[linenr];
    System.debug("currentLine: " + currentLine);
    }

With this you can use standard array-methods (push(), pop()), which might be easier-to-use.

The RegExp to split up the lines I got from :

http://stackoverflow.com/questions/1155678/javascript-string-newline-character

(good read...)

edit: I forgot: with Array.strip() you can delete an element from an array (in your case the used IP-Address before writing it back to file via FileWriter...)

Regards,

Joerg

www.vcoportal.de - Orchestrate. Your Cloud.

View solution in original post

0 Kudos
seTTembrinY
Contributor
Contributor

great! I already got my needed 1th line and a temp-file with the remaining lines! Now I just looking for a way to delete the old file and rename/move the temp-file to the old file...

0 Kudos
cdecanini_
VMware Employee
VMware Employee

var file = new File(fullName);
file.deleteFile();

Christophe.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
seTTembrinY
Contributor
Contributor

Yes!!! It works! Thank you very much you two!

0 Kudos
tmalaher
Contributor
Contributor

I can't find the rename functionality in the API.

How did you end up renaming the temp file?

0 Kudos
cdecanini_
VMware Employee
VMware Employee

I guess he created a new file with the same content as the source one with a new name and deleted the original file.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
0 Kudos
tmalaher
Contributor
Contributor

Ah. I'm trying to set up a situation where VCO creates a file (slowly, because it's collecting data from mutliple database queries and web hits) that will be picked up by another app. If VCO isn't done writing the file, then the other app may grab it before it's ready. In the past I've done this by creating it in another directory, or by renaming it from xxx.tmp to xxx.dat when I'm done writing to the file. But with no move/rename this isn't possible. I *could* copy the initial file to the final file, which would be much faster than the initial file creation, but there's still an (admittedly short) window of vulnerability.

So to confirm: there is *no* move/rename ability in the File API?

0 Kudos
tschoergez
Leadership
Leadership

According to the API docs there is no copy/move for the file-object 😞

That means, you have to do some workarounds, like

- creating your own vCO plugin that provide an File object with all methods you like (might be some effort) 🙂

- change the logic of the integration with that other system: you somehow have to build a (atomic) signal that the workflow has finished writing the file.

For instance, you could create an other empty "iAmReady" file once the workflow has written the data file.

however, this means you have to change the other system which reads the file.

Is that even possible?

Cheers,

Joerg

www.vcoportal.de - Orchestrate. Your Cloud.
0 Kudos
cdecanini_
VMware Employee
VMware Employee

It may be be possible to call the java method directy from javascript (for that you first need to edit the file that defines what java classes can be used by vco as documented in the vco guide.

Envoyé de mon iPhone

Le 10 sept. 2012 à 16:57, "Jörg Lew" <communities-emailer@vmware.com<mailto:communities-emailer@vmware.com>> a écrit :

VMware Communities<http://communities.vmware.com/index.jspa>

vCO and reading/writing Files

reply from Jörg Lew<http://communities.vmware.com/people/tschoergez> in Orchestrator - View the full discussion<http://communities.vmware.com/message/2111834#2111834

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
0 Kudos