vRO script will read text file containing name of VMs, snapshots to be deleted.
To read the text file in vRO script, first of all you need to assign permission to path to which script can read text file. Task is quite simple, you need to append a line containing path along with permission to file named "js-io-rights.conf". Below are the steps.
Modify the js-io-rights.conf file:
Code has been modified by me as per mine requirement. It is taken from internet.
To read the text file in vRO script, first of all you need to assign permission to path to which script can read text file. Task is quite simple, you need to append a line containing path along with permission to file named "js-io-rights.conf". Below are the steps.
Modify the js-io-rights.conf file:
- vi /etc/vco/app-server/js-io-rights.conf
- Press the i key on the keyboard
- Copy & paste the following line to the end file:
- +rwx /tmp
- Press the esc key on the keyboard
- Type in :wq! and press the Enter key
- restart vRO server with command "service vco-server restart"
----------------------------------------Start code of script----------------------------------
var line,tempVM,i,vmname;
var myFile = new FileReader(Path);
myFile.open();
data = myFile.readAll();
var lines = new Array();
var fvms = new Array();
lines = data.split(/\r\n|\n\r/);
System.log(lines);
var vms = VcPlugin.getAllVirtualMachines();
for (line in lines )
{
tempVM = lines[line];
//System.log("2nd line " + tempVM);
for (i in vms)
{
vmname = vms[i];
//System.log(tempVM + " : " + vmname.name);
if (tempVM.match(vmname.name))
{
System.log(tempVM + vms[i].name + " find");
vm = vmname;
//System.log(vm.snapshot);
var allSnaps = new Array();
var allParents;
var snapshot = vm.snapshot;
var rootSnapshotList = snapshot.rootSnapshotList;
for each (var rsnap in rootSnapshotList)
{
getSnapshotsOfVM(rsnap);
}
for each (var snap in allSnaps)
{
System.log("VM Name: " + snap.vm.name);
System.log("Snapshot Name: " + snap.name);
System.log("Snapshot creation time: " + snap.createTime.toLocaleString());
System.log("Snapshot is old, removing...");
var task = snap.snapshot.removeSnapshot_Task(false,true);
var actionResult = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task);
}
}
}
}
function getSnapshotsOfVM(snapshotTree)
{
allSnaps.push(snapshotTree);
var childTrees = snapshotTree.childSnapshotList;
if(childTrees != null) {
for(var index in childTrees) {
if(childTrees[index] != null) {
getSnapshotsOfVM(childTrees[index]);
}
}
}
}
var myFile = new FileReader(Path);
myFile.open();
data = myFile.readAll();
var lines = new Array();
var fvms = new Array();
lines = data.split(/\r\n|\n\r/);
System.log(lines);
var vms = VcPlugin.getAllVirtualMachines();
for (line in lines )
{
tempVM = lines[line];
//System.log("2nd line " + tempVM);
for (i in vms)
{
vmname = vms[i];
//System.log(tempVM + " : " + vmname.name);
if (tempVM.match(vmname.name))
{
System.log(tempVM + vms[i].name + " find");
vm = vmname;
//System.log(vm.snapshot);
var allSnaps = new Array();
var allParents;
var snapshot = vm.snapshot;
var rootSnapshotList = snapshot.rootSnapshotList;
for each (var rsnap in rootSnapshotList)
{
getSnapshotsOfVM(rsnap);
}
for each (var snap in allSnaps)
{
System.log("VM Name: " + snap.vm.name);
System.log("Snapshot Name: " + snap.name);
System.log("Snapshot creation time: " + snap.createTime.toLocaleString());
System.log("Snapshot is old, removing...");
var task = snap.snapshot.removeSnapshot_Task(false,true);
var actionResult = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task);
}
}
}
}
function getSnapshotsOfVM(snapshotTree)
{
allSnaps.push(snapshotTree);
var childTrees = snapshotTree.childSnapshotList;
if(childTrees != null) {
for(var index in childTrees) {
if(childTrees[index] != null) {
getSnapshotsOfVM(childTrees[index]);
}
}
}
}
----------------------------------------End code of script----------------------------------
Code has been modified by me as per mine requirement. It is taken from internet.