File operations

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.

Getting ready

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.

How to do it...

We have a bit of ground to cover, so let's start!

Writing a file

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:

  1. Create a new workflow and create the following variables:

    Name

    Type

    Section

    Use

    fileName

    String

    IN

    The name of the file, including its path

    fileContent

    Sting

    IN

    Some random content

  2. Add a scriptable task to the schema and enter the following script:
          //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(); 
    
  3. Run the workflow and check the result; you should have a file that contains something like this:
          Test 
          Test -:- Test 
    

Note

If the clean method is not used when opening an existing file, it will be opened for appending. Try it!

Reading a file

This part showcases how to read a file fully and line-by-line.

  1. Create a new workflow and create the following variables:

    Name

    Type

    Section

    Use

    fileName

    String

    IN

    The name of the file, including its path.

  2. Add a scriptable task to the schema and enter the following script:
          //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(); 
          } 
    
  3. Run the workflow and check the logs.

Getting information on files

This section showcases how to access information about a directory or file:

  1. Create a new workflow and create the following variables:

    Name

    Type

    Section

    Use

    directory

    String

    IN

    The name of the file, including its path.

  2. Add a scriptable task to the schema and enter the following script:
          //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 
    
  3. Run the workflow and enter a directory (such as /var/run/vco). Check the logs.

Creating, renaming, and deleting a file or directory

We now showcase how to create an empty file, rename it, and then delete it:

  1. Create a new workflow and create the following variables:

    Name

    Type

    Section

    Use

    directory

    String

    IN

    The name of the file, including its path.

  2. Add a scriptable task to the schema and enter the following script:
          //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()); 
          } 
    
  3. Run the workflow and supply a directory. A file will be created, renamed, and deleted.

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.

How it works...

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.

Executing scripts

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.

Shared directories

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.

Tip

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.

There's more...

The following points are worth knowing.

CSV files

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(); 

Doing things as root

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:

  • Use SSH with an SSL key to login to 127.0.0.1, as shown in the example workflow 02.01 Tuning the Appliance
  • Change /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]
  • Add vco to the root group

All 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.

See also

The example workflows are:

  • 09.02.1 Write a File
  • 09.02.2 Read a File
  • 09.02.3 Getting File information
  • 09.02.4 Creating, renaming, and deleting
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset