Here we will explore how Orchestrator can interact with the filesystem of its operating system. We will also take a look at how to access a network share and execute local files.
Orchestrator needs to be able to access a directory on the local filesystem. To configure this access and set the access rights, take a look at the Configuring access to the local filesystem recipe in Chapter 2, Optimizing Orchestrator Configuration.
In my example, I will simply use the default Orchestrator file location that is set to rwx
the directory /var/run/vco/
.
In addition to this, you should have administrative (root) access to Orchestrator's operating system.
We have a bit of ground to cover, so let's start!
This part showcases how to write into a file with, and without, a line feed as well as how to append to an existing file:
Name |
Type |
Section |
Use |
|
String |
IN |
The name of the file, including its path |
|
Sting |
IN |
Some random content |
//FileWriter constructor var myFileWriter = new FileWriter(fileName); //open the file for writing myFileWriter.open(); //Empties existing file. Without it we append myFileWriter.clean(); //write a line into the file myFileWriter.writeLine(fileContent); //write without line feed myFileWriter.write(fileContent); myFileWriter.write(" -:- "); myFileWriter.write(fileContent); //write line feed myFileWriter.write(" "); //Close the file myFileWriter.close();
Test Test -:- Test
This part showcases how to read a file fully and line-by-line.
Name |
Type |
Section |
Use |
|
String |
IN |
The name of the file, including its path. |
//File reader constructor var myFileReader = new FileReader(fileName); // check if the file actually exists if (myFileReader.exists){ //Open the file myFileReader.open(); //read everything System.log(myFileReader.readAll()); // Close the file (undocumented in API). myFileReader.close(); System.log("----------------------"); //read line by line until the file is empty myFileReader.open(); do{ temp=myFileReader.readLine(); System.log(temp); } while(temp!=null); myFileReader.close(); }
This section showcases how to access information about a directory or file:
Name |
Type |
Section |
Use |
|
String |
IN |
The name of the file, including its path. |
//File constructor var myDir = new File(directory); //Does the file or directory exist and is it a directory? if (myDir.exists && myDir.isDir){ System.log("This is a directory. It Contains:"); //list the content of the directory, returns an array of Strings for each (fileInDir in myDir.list()) { //read file properties. Cause its strings we need to make them files first var temp = new File(fileInDir); System.log("FileName: "+ temp.name); System.log("Path: "+ temp.path); System.log("Directory: "+ temp.directory); System.log("FileExtention: "+ temp.extension); System.log("Readable: "+ temp.canRead()) System.log("Writeable: "+ temp.canWrite()); } //end of Foreach }//end of ifexists and isdir
/var/run/vco
). Check the logs.We now showcase how to create an empty file, rename it, and then delete it:
Name |
Type |
Section |
Use |
|
String |
IN |
The name of the file, including its path. |
//File Constructor var myDir = new File(directory); //is it a Directory if (myDir.isDir){ //create a new File Constructor var newFile = new File(Directory+"/TempFile"); //create an empty file newFile.createFile(); //Show directory System.log(myDir.list()); //Rename the file newFile.renameTo(Directory+"/RenamedFile") //Show directory System.log(myDir.list()); // new constructor as the file name has changed var renamedFile = new File(Directory+"/RenamedFile"); //Delete the File renamedFile.deleteFile(); //Show directory System.log(myDir.list()); }
If you want to create a directory, use the createDirectory
method. To rename and delete a directory, the renameTo
and deleteFile
methods are used, just as we have seen earlier with a file.
As you can see, file operations are quite easy and straightforward. They come in handy if you want to save or load XML or CSV content or anything else, for that matter.
Please note that Orchestrator regards a file and a directory the same when it comes to methods.
If you want to use a file to write logs, there is a special scripting class called LogFileWriter
. However, it is not much different from the FileWriter
class.
Directory dividers are different in Linux and Windows. Where Windows uses a backslash (), Linux uses a forward-slash (/). However, when we deal with file paths in Orchestrator, we use only the forward-slash.
You can execute scripts from the local OS using Orchestrator. To do that, Orchestrator needs access (x) to the folder where the script is located and the Orchestrator user needs to be able to read and execute (rx) it. You also need to allow Orchestrator to execute local files; see the System properties section in the Control Center titbits recipe in Chapter 2, Optimizing Orchestrator Configuration. Here is an example script that will execute the file script.bat
:
//prepare command var command = new Command("c:/var/run/vco/script.bat"); //execute the command command.execute(true); //get the return code var returnCode = command.result; //get the output of the command var returnOutput = command.output;
Please note that the script needs to be in a file location that Orchestrator can access and that Orchestrator will run as user vco
with the group vco
.
You can use the file writer to write to a shared directory. Check out the Configuring access to the local filesystem recipe in Chapter 2, Optimizing Orchestrator Configuration.
This is especially important in a clustered Orchestrator environment where storing local files isn't a good solution. You should use NFS or SMB shares; see the Configuring access to the local filesystem recipe in Chapter 2, Optimizing Orchestrator Configuration.
The following points are worth knowing.
Comma separated files are quite good for transferring data. For example, you could create a CSV in /var/run/vco
and then send it as a e-mail attachment. Here is an example:
var myFileWriter = new FileWriter(filename); myFileWriter.open(); //CSV header header="VMname,Memory,CPU"; myFileWriter.writeLine(header); //one line per VM for each (vm in vms) { line=vmName+","+vmMemory+","+vmCPU; myFileWriter.writeLine(line); } myFileWriter.close();
If you need to execute a command as root, or any other operation that requires you to use root, here are some ideas on how to do it:
127.0.0.1
, as shown in the example workflow 02.01 Tuning the Appliance
/etc/sudoers
and add vco
ALL=(ALL) NOPASSWD:ALL
, which will add vco
to the sudoer list and doesn't need a password. You the run commands with sudo
[command]
vco
to the root groupAll of these methods are more or less problematic. With opening SSH and root, you open up a potential attack vector. Using sudoers, you will need to make sure the setting stays after Orchestrator updates. Adding vco
to root is the same or worse; it makes it possible for Orchestrator to take over your Linux system, meaning Orchestrator users could alter the local Linux system or even break into other systems.