Copying a File From a Resource
Element to a Guest VM
This workflow
is useful as a utility workflow that can convert a file stored in a resource
element into a file on a guest VM. This is done first by saving the file
from the resource element to the local file system of the vRealize Orchestrator
appliance. From there it is copied into a guest VM’s file system using
the VMware Host Guest Filesystem (HGFS) interface of VM Tools. In some
secure environments this functionality may be blocked in the VMX settings of
the virtual machine, so it is a good idea to check to make sure HGFS is not
blocked before attempting to use this interface.
Assuming that
using HGFS to copy files to the guest VM is not a problem, then this workflow
serves as a good way to copy script files from resource elements to virtual
machines. A benefit of this approach is that network connectivity is not
required from the vRealize Orchestrator instance to the guest VM, making it a
better candidate than SSH in certain instances. If HGFS is blocked but
connectivity from the vRealize Orchestrator appliance to the guest VM is not,
then SSH may be a better candidate as the transfer method.
There are other ways to accomplish this functionality, such as placing the
files on a commonly accessible HTTPS web server and copying it locally to the
VM from that location. Similarly, files can be pre-staged in VM templates
to make them accessible, although this approach is not recommended because it
couples build artifacts with VM templates meaning that any change to a build
artifact would require a change to the VM template. A third way is to
store files on a commonly accessible NFS or CIFS share, issue commands to the
VM to mount this share at build time, and copy or execute scripts from that
location at runtime. Any of these approaches are valid, and which one is
ultimately chosen depends on the specific characteristics of the environment in
which the task is to be accomplished.
CREATE THE WORKFLOW
Create a new
workflow with the name “Copy Resource To Guest”. We are going to place
this workflow in the /Examples/Tools/Scripts folder.
INPUTS
The workflow
should have five inputs:
Input
|
Type
|
Description
|
TargetResource
|
ResourceElement
|
The resource element containing the file that should be
copied to the guest VM
|
TargetDirectoryOnGuest
|
String
|
The directory in the guest VM’s filesystem where the
target resource should be placed.
|
VMUsername
|
String
|
Admin username for the machine that has permissions to
execute a process in the operating system
|
VMPassword
|
SecureString
|
The password for the admin username provided in the
vmUsername input
|
VM
|
VC:VirtualMachine
|
A reference to the virtual machine in which we want to
execute a program
|
Figure 1.
Workflow inputs for the "Copy Resource To Guest" workflow
In the workflow
designer, these inputs will look as follows:
Figure 2.
Inputs for the "Copy Resource To Guest" workflow
ATTRIBUTES
We will be
assigning five attributes for this workflow:
Attribute
|
Type
|
Description
|
Value
|
outFilename
|
String
|
Stores the full filename on the vCO server after it is
written from resource element locally
|
[Blank]
|
guestfilePath
|
String
|
Stores the full filename on the guest VM operating
system that will be used to write the file to the guest.
|
[Blank]
|
overwrite
|
Boolean
|
Overwrite the file if it already exists
|
Yes/True
|
copyResult
|
Boolean
|
Will be true if the copy operation was successful
|
No/False
|
errorCode
|
String
|
Return error string if there is an error copying the
file
|
“Guest file copy operation failed!”
|
Figure 3.
Attributes for the "Copy Resource To Guest" workflow
In the workflow
designer, these will look as follows:
Figure 4.
Attributes for the "Copy Resource To Guest" workflow
OUTPUTS
This workflow
has one output, which is the Guest file name of the file copied to the guest.
Output
|
Type
|
Description
|
GuestFileName
|
String
|
Filename of the file copied to the guest VM.
|
Figure 5.
Workflow outputs for the "Copy Resource To Guest" workflow
In the workflow
designer, the outputs will look as follows:
Figure 6.
Outputs for the "Copy Resource To Guest" workflow
SCHEMA
The workflow
consists of multiple steps. First we will write the resource to a file
locally on the vRealize Orchestrator appliance. Then we will initiate a
copy of that file from the vRealize Orchestrator appliance to the guest virtual
machine. If the copy was unsuccessful, we’ll throw an error. If it
was successful, we’ll set the output of the workflow to the guest file name and
finish.
The schema we
will build will look like this:
Figure 7.
Workflow schema for the "Copy Resource To Guest" workflow
Write
Resource
Create the
Write Resource step by dragging and dropping a new Scriptable task node on to
the schema canvas between the start and end nodes. Set the name of the
step to “Write Resource”.
Wire the
inputs, attributes, and outputs in the following manner.
Inputs
Local Parameter
|
Source Parameter
|
Type
|
TargetResource
|
TargetResource
|
ResourceElement
|
TargetDirectoryOnGuest
|
TargetDirectoryOnGuest
|
String
|
Outputs
Local Parameter
|
Source Parameter
|
Type
|
outFileName
|
outFileName
|
String
|
guestFilePath
|
guestFilePath
|
String
|
The visual
binding on the “Write Resource” step should look as follows:
Figure 8.
Visual binding for the "Write Resource" step in the "Copy
Resource To Guest" workflow
For the
scripting content of the Write Resource task, enter the following block of code
into the scripting tab:
var useForwardSlash
var slash
if
outFileName
guestFilePath
= TargetDirectoryOnGuest.indexOf(
"/") > -1 || TargetDirectoryOnGuest.indexOf(
"\\") == -1;
var slash
=
"/";
if
(!
useForwardSlash)
slash =
"\\";
outFileName
=
"/var/run/vco/" +
TargetResource.
name;
guestFilePath
=
TargetDirectoryOnGuest +
slash +
TargetResource.
name;
TargetResource.writeContentToFile(
outFileName);
Copy
File From vCO
Create the Copy
file from vCO step by dragging and dropping a new Workflow element node on to
the canvas between the Write Resource task and the workflow end node.
Search for “Copy file from vCO” to find the workflow which should be referenced
in this step. Double-click it to accept it on to the canvas.
Wire the
inputs, attributes, and outputs in the following manner.
Inputs
Local Parameter
|
Source Parameter
|
Type
|
vmUsername
|
VMUsername
|
String
|
vmPassword
|
VMPassword
|
SecureString
|
Vm
|
VM
|
VC:VirtualMachine
|
vcoPath
|
outFileName
|
String
|
guestFilePath
|
guestFilePath
|
String
|
overwrite
|
overwrite
|
Boolean
|
Outputs
Local Parameter
|
Source Parameter
|
Type
|
Result
|
copyResult
|
boolean
|
The visual
binding on the “Copy file from vCO” step should look as follows:
Figure 9.
Visual binding for the "Copy file from vCO" step in the "Copy
Resource To Guest" workflow
Decision
The decision in
this workflow throws an error if necessary or continues to successful
completion. If the copyResult attribute has a value of true, then the
operation completed successfully and we don’t throw an error. If it is
false, then we throw an error.
To create the
decision step, drag a Decision node on to the canvas between the Copy file from
vCO step and the workflow finish node. Delete the new workflow completion
node that has been added to the failure path of the new Decision node by
clicking the failure path and hitting the delete key.
Drag a Throw exception node on top of the Decision node to add the Throw
exception step to the failure path of the decision node.
Click the
Decision tab within the Decision step. Select the copyResult attribute as
the decision variable. Leave the drop-down condition set to “is true”
The decision
tab of the Decision step should look as follows:
Figure 10.
Decision tab of the Decision step of the "Copy Resource To Guest"
workflow
Click the Throw
exception step. Within the Exception tab, bind the errorCode attribute to
the exception binding. This will cause the step to throw the error string
contained within the errorCode variable.
Set
Workflow Output
Create the Set
Workflow Output step by dragging a Scriptable task node on to the canvase
between the Decision step and the workflow end node. Set the name of the
step to “Set Workflow Output”.
The purpose of
this step is simply to translate an attribute variable to an output
variable. We are going to take the guestFilePath attribute and copy its
value to the output variable called GuestFileName.
Wire the inputs
and outputs as follows.
Inputs
Local Parameter
|
Source Parameter
|
Type
|
guestFilePath
|
guestFilePath
|
String
|
Outputs
Local Parameter
|
Source Parameter
|
Type
|
GuestFileName
|
GuestFileName
|
string
|
The visual
binding for the Set Workflow Output step should look as follows:
Figure 11.
Visual binding for the "Set Workflow Output" step of the "Copy
Resource To Guest" workflow
Enter the
following script into the Scripting tab:
GuestFileName
= guestFilePath;
Save the workflow and test
its execution in your environment by creating a resource element with some text
content. Execute the workflow and verify that the resource element chosen
is copied as a file to the guest virtual machine.
Comments
Post a Comment