Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reworked Ruby class heirarchy.. Properties now work well

  • Loading branch information...
commit 0651b4bf912236d33be4231e132c342be96652c1 1 parent 8f6d785
Sean Bradly authored
39 README
View
@@ -2,3 +2,42 @@ vixR - A (new&improved) Ruby bridge to the VMware VIX API - 2009, Sean Bradly
This project and all files it includes are released under the BSD license.
+--------------------------------------------------------------------------------
+| Compiling / Installing
+--------------------------------------------------------------------------------
+ruby ./extconf.rb --with-vixapi-include=/usr/include/vmware-vix
+make
+make install
+
+--------------------------------------------------------------------------------
+| Developing
+--------------------------------------------------------------------------------
+
+Ruby API
+
+module VixAPI
+ def raw_C_vixapi_method_wrapper
+ end
+end
+
+module VixR
+
+ self.connect()
+ end
+
+ class Host # As returned by VixR.connect
+ end
+
+ class VM
+ end
+
+ class GuestOS < VM
+ end
+
+ class Property
+ end
+
+ class Snapshot
+ end
+
+end
16 vm.h → functions.h
View
@@ -1,14 +1,20 @@
-/* VM Power Functions */
+/* Auth/Session routines */
+VALUE _connect(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE);
+VALUE _disconnect(VALUE, VALUE);
+
+VALUE _open_vmx(VALUE, VALUE, VALUE);
+
+/* VM functions */
VALUE _power_on(VALUE, VALUE, VALUE);
VALUE _power_off(VALUE, VALUE, VALUE);
-VALUE _reset(VALUE, VALUE, VALUE);
VALUE _pause(VALUE, VALUE);
VALUE _suspend(VALUE, VALUE);
VALUE _unpause(VALUE, VALUE);
-
-/* Misc VM Functions */
+VALUE _reset(VALUE, VALUE, VALUE);
VALUE _delete(VALUE, VALUE, VALUE);
+VALUE _upgrade_vhardware(VALUE, VALUE);
VALUE _read_var(VALUE, VALUE, VALUE, VALUE);
VALUE _write_var(VALUE, VALUE, VALUE, VALUE, VALUE);
-VALUE _upgrade_vhardware(VALUE, VALUE);
+/* Misc functions */
+VALUE _getproperty(VALUE, VALUE, VALUE);
8 guest.c
View
@@ -107,7 +107,7 @@ _create_temp_file_in_guest(VALUE self, VALUE rvm)
VixError err;
char *tempfilepath;
- job = Vix_VM_CreateTempFileInGuest(vm, 0, VIX_INVALID_HANDLE, NULL, NULL);
+ job = VixVM_CreateTempFileInGuest(vm, 0, VIX_INVALID_HANDLE, NULL, NULL);
err = VixJob_Wait(job, VIX_PROPERTY_JOB_RESULT_ITEM_NAME, &tempfilepath, VIX_PROPERTY_NONE);
if(VIX_FAILED(err))
@@ -131,7 +131,7 @@ _delete_directory_in_guest(VALUE self, VALUE rvm, VALUE rpath)
char *path = StringValueCStr(rpath);
- job = Vix_VM_DeleteDirectoryInGuest(vm, path, 0, NULL, NULL);
+ job = VixVM_DeleteDirectoryInGuest(vm, path, 0, NULL, NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
if(VIX_FAILED(err))
@@ -155,7 +155,7 @@ _delete_file_in_guest(VALUE self, VALUE rvm, VALUE rpath)
char *path = StringValueCStr(rpath);
- job = Vix_VM_DeleteFileInGuest(vm, path, NULL, NULL);
+ job = VixVM_DeleteFileInGuest(vm, path, NULL, NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
if(VIX_FAILED(err))
@@ -179,7 +179,7 @@ _directory_exists_in_guest(VALUE self, VALUE rvm, VALUE rpath)
char *path = StringValueCStr(rpath);
- job = Vix_VM_DirectoryExistsInGuest(vm, path, NULL, NULL);
+ job = VixVM_DirectoryExistsInGuest(vm, path, NULL, NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
if(VIX_FAILED(err))
71 host.c
View
@@ -1,86 +1,87 @@
#include <stdio.h>
#include <string.h>
#include <ruby.h>
-#include "vix.h"
+#include <vix.h>
-#include "host.h"
+#include "functions.h"
/* _connect(user,pass,type): async connect */
VALUE
-_connect(int hosttype, const char *hostname, int port, const char *user, const char *pass)
+_connect(VALUE self, VALUE rhosttype, VALUE rhostname, VALUE rport, VALUE ruser, VALUE rpass)
{
VixHandle job = 0;
VixHandle session = VIX_INVALID_HANDLE;
VixError err;
- fprintf(stderr,"calling connect(ht:%d,host:%s,port:%d,user:%s,pass.length:%d);\n",hosttype,hostname,port,user,(pass)?strlen(pass):0);
+ char *hostname,*pass,*user,str[200];
+ int hosttype,port;
+
+ hosttype = NUM2INT(rhosttype);
+ hostname = (rhostname!=Qnil)?StringValueCStr(rhostname):NULL;
+ port = (rport!=Qnil)?NUM2INT(rport):0;
+ user = (ruser!=Qnil)?StringValueCStr(ruser):NULL;
+ pass = (rpass!=Qnil)?StringValueCStr(rpass):NULL;
+
+ //fprintf(stderr,"calling connect(ht:%d,host:%s,port:%d,user:%s,pass.length:%d);\n",hosttype,hostname,port,user,(pass)?strlen(pass):0);
/* Start the async connect */
job = VixHost_Connect(VIX_API_VERSION,
- hosttype,hostname,port,user,pass,
- /*VIX_HOSTOPTION_USE_EVENT_PUMP,*/ 0, VIX_INVALID_HANDLE, NULL,NULL
- );
+ hosttype,hostname,port,user,pass,
+ /*VIX_HOSTOPTION_USE_EVENT_PUMP,*/
+ 0, VIX_INVALID_HANDLE, NULL,NULL);
/* Wait on connect */
err = VixJob_Wait(job, VIX_PROPERTY_JOB_RESULT_HANDLE, &session, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(job);
- if(VIX_FAILED(err))
+ if(VIX_FAILED(err) || session == VIX_INVALID_HANDLE)
{
- fprintf(stderr,"Failed to connect to vmware host machine\n");
+ fprintf(stderr,"Failed to connect to vmware host machine: %s\n",
+ Vix_GetErrorText(err,NULL));
return Qnil;
}
- fprintf(stderr,"hostHandle is %d\n",session);
-
- /* @session */
- return INT2NUM(session);
+ sprintf(str,"VixAPI::Handle.new(%d)",session);
+ return rb_eval_string(str);
}
VALUE
-_connect_server(VALUE self, VALUE rhosttype, VALUE rhostname, VALUE rport, VALUE ruser, VALUE rpass)
+_disconnect(VALUE self, VALUE rhandle)
{
- char *hostname = StringValueCStr(rhostname);
- int hosttype = NUM2INT(rhosttype);
- int port = NUM2INT(rport);
- char *user = StringValueCStr(ruser);
- char *pass = StringValueCStr(rpass);
-
- return _connect(hosttype,hostname,port,user,pass);
+ VixHandle host = NUM2INT(rhandle);
+ VixHost_Disconnect(host);
+ return Qtrue;
}
-VALUE
-_connect_wkstn(VALUE self)
-{
- return _connect(VIX_SERVICEPROVIDER_VMWARE_WORKSTATION, NULL, 0, NULL, NULL);
-}
+
VALUE
-_vmx_open(VALUE self,VALUE rsession, VALUE rpath)
+_open_vmx(VALUE self,VALUE rhosthandle, VALUE rpath)
{
- char *path = StringValueCStr(rpath);
- int session = NUM2INT(rsession);
-
+ VixHandle host = NUM2INT(rb_iv_get(rhosthandle,"@handle"));
VixHandle job;
VixHandle vm = VIX_INVALID_HANDLE;
VixError err;
+ char *path = StringValueCStr(rpath);
+ char str[200];
- fprintf(stderr,"calling vmx_open(%d,\"%s\");\n",session,path);
+ // fprintf(stderr,"calling vmx_open(%d,\"%s\");\n",session,path);
- job = VixVM_Open(session,path,NULL,NULL);
+ job = VixVM_Open(host,path,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_JOB_RESULT_HANDLE, &vm, VIX_PROPERTY_NONE);
Vix_ReleaseHandle(job);
if(VIX_FAILED(err))
{
- fprintf(stderr,"Failed open vmx file %s: \n",path, Vix_GetErrorText(err,NULL));
+ fprintf(stderr,"Failed open vmx file (@%s): %s\n",path, Vix_GetErrorText(err,NULL));
return Qnil;
}
- fprintf(stderr,"VM handle is %d\n",vm);
+ // fprintf(stderr,"VM handle is %d\n",vm);
- return INT2NUM(vm);
+ sprintf(str,"VixAPI::Handle.new(%d)",(int)vm);
+ return rb_eval_string(str);
}
4 host.h
View
@@ -1,4 +0,0 @@
-VALUE _connect(int, const char *, int, const char *, const char *);
-VALUE _connect_server(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE);
-VALUE _connect_wkstn(VALUE);
-VALUE _vmx_open(VALUE, VALUE, VALUE);
284 vixapi.c
View
@@ -1,16 +1,156 @@
#include <stdio.h>
#include <ruby.h>
-
-#include "vix.h"
+#include <vix.h>
/* local methods */
-#include "vm.h"
-#include "host.h"
+#include "functions.h"
/*
* Globals
*/
static VALUE started = Qnil;
+static VALUE rb_cVixAPI; /* module VixAPI */
+
+VALUE _getproperty_int(VixHandle handle, int propid)
+{
+ VixError err;
+ int result;
+
+ err = Vix_GetProperties(handle,propid,&result,VIX_PROPERTY_NONE);
+ if (VIX_FAILED(err))
+ {
+ fprintf(stderr,"Error while attempting to retrieve INT property (id %d)\n",propid);
+ return Qnil;
+ }
+
+ return INT2NUM(result);
+}
+
+VALUE _getproperty_str(VixHandle handle, int propid)
+{
+ VixError err;
+ char *result;
+ VALUE retval;
+
+ err = Vix_GetProperties(handle,propid,&result,VIX_PROPERTY_NONE);
+ if (VIX_FAILED(err))
+ {
+ fprintf(stderr,"Error while attempting to retrieve string property (id %d)\n",propid);
+ return Qnil;
+ }
+
+ retval = rb_str_new2(result);
+ Vix_FreeBuffer(result);
+ return retval;
+}
+
+VALUE _getproperty_bool(VixHandle handle, int propid)
+{
+ VixError err;
+ char result;
+
+ err = Vix_GetProperties(handle,propid,&result,VIX_PROPERTY_NONE);
+ if (VIX_FAILED(err))
+ {
+ fprintf(stderr,"Error while attempting to retrieve BOOL property (id %d)\n",propid);
+ return Qnil;
+ }
+
+ //fprintf(stderr,"attempting to retrieve BOOL property (id %d), res=%d\n",propid,result);
+
+ return (result)?Qtrue:Qfalse;
+}
+
+VALUE _getproperty_handle(VixHandle handle, int propid)
+{
+ VixError err;
+ VixHandle result;
+ char str[200];
+
+ err = Vix_GetProperties(handle,propid,&result,VIX_PROPERTY_NONE);
+ if (VIX_FAILED(err))
+ {
+ fprintf(stderr,"Error while attempting to retrieve HANDLE property (id %d)\n",propid);
+ return Qnil;
+ }
+
+ sprintf(str,"VixAPI::Handle.new(%d)",result);
+ return rb_eval_string(str);
+}
+
+VALUE _getproperty_int64(VixHandle handle, int propid)
+{
+ VixError err;
+ int64 result;
+
+ err = Vix_GetProperties(handle,propid,&result,VIX_PROPERTY_NONE);
+ if (VIX_FAILED(err))
+ {
+ fprintf(stderr,"Error while attempting to retrieve INT64 property (id %d)\n",propid);
+ return Qnil;
+ }
+
+ return INT2NUM(result);
+}
+
+VALUE _getproperty_blob(VixHandle handle, int propid)
+{
+ VixError err;
+ VALUE retval;
+ struct {int len;char *data;} result;
+
+ err = Vix_GetProperties(handle,propid,&result,VIX_PROPERTY_NONE);
+ if (VIX_FAILED(err))
+ {
+ fprintf(stderr,"Error while attempting to retrieve BLOB property (id %d)\n",propid);
+ return Qnil;
+ }
+
+ retval = rb_str_new(result.data,result.len);
+ Vix_FreeBuffer(result.data);
+ return retval;
+}
+
+
+VALUE _getproperty(VALUE self, VALUE rhandleobj, VALUE rpropid)
+{
+ VixHandle handle;
+ VixError err;
+ VixPropertyType type;
+ int propid;
+
+ handle = NUM2INT(rb_iv_get(rhandleobj,"@handle"));
+ propid = NUM2INT(rpropid);
+
+ /* Get the type of data to return */
+ if(VIX_FAILED(err = Vix_GetPropertyType(handle,propid,&type)))
+ {
+ fprintf(stderr,"Could not determine property type: %s\n",Vix_GetErrorText(err,NULL));
+ return Qnil;
+ }
+
+ /* do diff things based on prop type */
+ switch(type)
+ {
+ case VIX_PROPERTYTYPE_INTEGER:
+ return _getproperty_int(handle,propid);
+ case VIX_PROPERTYTYPE_STRING:
+ return _getproperty_str(handle,propid);
+ case VIX_PROPERTYTYPE_BOOL:
+ return _getproperty_bool(handle,propid);
+ case VIX_PROPERTYTYPE_HANDLE:
+ return _getproperty_handle(handle,propid);
+ case VIX_PROPERTYTYPE_INT64:
+ return _getproperty_int64(handle,propid);
+ case VIX_PROPERTYTYPE_BLOB:
+ return _getproperty_blob(handle,propid);
+ case VIX_PROPERTYTYPE_ANY: default:
+ break;
+ }
+
+ fprintf(stderr,"Unknown Property Type (%d)\n",type);
+ return Qnil;
+}
VALUE
vix_start(VALUE self)
@@ -27,123 +167,31 @@ vix_start(VALUE self)
return Qnil;
}
-/*
-VALUE
-_vix_event_loop(VALUE self)
-{
- int session = NUM2INT(rb_iv_get(self,"@session"));
- Vix_PumpEvents(session,0);
- return Qnil;
-}
-*/
-
void
-Init_vixapi() {
- VALUE vixapi = rb_define_class("VixAPI", rb_cObject);
+Init_vixapi()
+{
+ rb_cVixAPI = rb_define_module("VixAPI");
/* global init function */
- rb_define_method(vixapi, "start", vix_start, 0);
-
- /* instance methods */
- rb_define_method(vixapi,"_connect_server",_connect_server,5);
- rb_define_method(vixapi,"_connect_wkstn",_connect_wkstn,0);
- rb_define_method(vixapi,"_vmx_open",_vmx_open,2);
- rb_define_method(vixapi,"_delete",_delete,2);
- rb_define_method(vixapi,"_read_var",_read_var,3);
- rb_define_method(vixapi,"_write_var",_write_var,4);
- rb_define_method(vixapi,"_upgrade_vhardware",_upgrade_vhardware,1);
- /* VM Power Methods */
- rb_define_method(vixapi,"_power_on",_power_on,2);
- rb_define_method(vixapi,"_power_off",_power_on,2);
- rb_define_method(vixapi,"_reset",_power_on,2);
- rb_define_method(vixapi,"_pause",_power_on,1);
- rb_define_method(vixapi,"_suspend",_power_on,1);
- rb_define_method(vixapi,"_unpause",_power_on,1);
-
+ rb_define_singleton_method(rb_cVixAPI, "start", vix_start, 0);
+
+ /* VixAPI wrapper methods */
+ rb_define_singleton_method(rb_cVixAPI,"_connect",_connect,5);
+ rb_define_singleton_method(rb_cVixAPI,"_disconnect",_disconnect,1);
+ rb_define_singleton_method(rb_cVixAPI,"_open_vmx",_open_vmx,2);
- /* rb_define_method(vixapi,"_vix_event_loop",_vix_event_loop); */
+ /* VM methods */
+ rb_define_singleton_method(rb_cVixAPI,"_power_on",_power_on,2);
+ rb_define_singleton_method(rb_cVixAPI,"_power_off",_power_off,2);
+ rb_define_singleton_method(rb_cVixAPI,"_pause",_pause,1);
+ rb_define_singleton_method(rb_cVixAPI,"_suspend",_suspend,1);
+ rb_define_singleton_method(rb_cVixAPI,"_unpause",_unpause,1);
+ rb_define_singleton_method(rb_cVixAPI,"_reset",_reset,2);
+ rb_define_singleton_method(rb_cVixAPI,"_delete",_delete,2);
+ rb_define_singleton_method(rb_cVixAPI,"_upgrade_vhardware",_upgrade_vhardware,1);
+ rb_define_singleton_method(rb_cVixAPI,"_read_var",_read_var,3);
+ rb_define_singleton_method(rb_cVixAPI,"_write_var",_write_var,4);
- /* define constants (ruby form of vix.h) */
- {
- VALUE constants = rb_define_module_under(vixapi,"Const");
- VALUE provider = rb_define_module_under(constants,"Provider");
- VALUE poweropt = rb_define_module_under(constants,"VMPowerOpt");
- VALUE vartype = rb_define_module_under(constants,"VarType");
- VALUE eventtype = rb_define_module_under(constants,"EventType");
- VALUE handletype = rb_define_module_under(constants,"HandleType");
- VALUE property = rb_define_module_under(constants,"PropertyType");
- VALUE props = rb_define_module_under(constants,"Property");
- VALUE propshost = rb_define_module_under(props, "Host");
- VALUE propsvm = rb_define_module_under(props, "VM");
- VALUE propssnap = rb_define_module_under(props,"Snapshot");
-
- /* NULL Handle */
- rb_define_const(constants, "InvalidHandle", INT2NUM(VIX_INVALID_HANDLE));
-
- /* Var Types */
- rb_define_const(vartype, "Guest", INT2NUM(VIX_VM_GUEST_VARIABLE));
- rb_define_const(vartype, "ConfigRuntime", INT2NUM(VIX_VM_CONFIG_RUNTIME_ONLY));
- rb_define_const(vartype, "GuestEnvironment", INT2NUM(VIX_GUEST_ENVIRONMENT_VARIABLE));
-
- /* Event Types */
- rb_define_const(eventtype, "JobCompleted", INT2NUM(VIX_EVENTTYPE_JOB_COMPLETED));
- rb_define_const(eventtype, "JobProgress", INT2NUM(VIX_EVENTTYPE_JOB_PROGRESS));
- rb_define_const(eventtype, "FindItem", INT2NUM(VIX_EVENTTYPE_FIND_ITEM));
-
- /* VixProperty Types */
- rb_define_const(property, "Any", INT2NUM(VIX_PROPERTYTYPE_ANY));
- rb_define_const(property, "Integer", INT2NUM(VIX_PROPERTYTYPE_INTEGER));
- rb_define_const(property, "String", INT2NUM(VIX_PROPERTYTYPE_STRING));
- rb_define_const(property, "Bool", INT2NUM(VIX_PROPERTYTYPE_BOOL));
- rb_define_const(property, "Handle", INT2NUM(VIX_PROPERTYTYPE_HANDLE));
- rb_define_const(property, "Int64", INT2NUM(VIX_PROPERTYTYPE_INT64));
- rb_define_const(property, "Blob", INT2NUM(VIX_PROPERTYTYPE_BLOB));
-
- /* VixProperties */
- rb_define_const(props, "None", INT2NUM(VIX_PROPERTY_NONE));
- rb_define_const(props, "MetadataContainer", INT2NUM(VIX_PROPERTY_META_DATA_CONTAINER));
- rb_define_const(props, "FoundItemLocation", INT2NUM(VIX_PROPERTY_FOUND_ITEM_LOCATION));
- rb_define_const(propshost, "HostType", INT2NUM(VIX_PROPERTY_HOST_HOSTTYPE));
- rb_define_const(propshost, "APIVersion", INT2NUM(VIX_PROPERTY_HOST_API_VERSION));
- rb_define_const(propsvm, "NumCPUs", INT2NUM(VIX_PROPERTY_VM_NUM_VCPUS));
- rb_define_const(propsvm, "VMXPath", INT2NUM(VIX_PROPERTY_VM_VMX_PATHNAME));
- rb_define_const(propsvm, "VMTeamPath", INT2NUM(VIX_PROPERTY_VM_VMTEAM_PATHNAME));
- rb_define_const(propsvm, "MemSize", INT2NUM(VIX_PROPERTY_VM_MEMORY_SIZE));
- rb_define_const(propsvm, "ReadOnly", INT2NUM(VIX_PROPERTY_VM_READ_ONLY));
- rb_define_const(propsvm, "InVMTeam", INT2NUM(VIX_PROPERTY_VM_IN_VMTEAM));
- rb_define_const(propsvm, "PowerState", INT2NUM(VIX_PROPERTY_VM_POWER_STATE));
- rb_define_const(propsvm, "ToolsState", INT2NUM(VIX_PROPERTY_VM_TOOLS_STATE));
- rb_define_const(propsvm, "IsRunning", INT2NUM(VIX_PROPERTY_VM_IS_RUNNING));
- rb_define_const(propsvm, "SupportedFeatures", INT2NUM(VIX_PROPERTY_VM_SUPPORTED_FEATURES));
- rb_define_const(propsvm, "IsRecording", INT2NUM(VIX_PROPERTY_VM_IS_RECORDING));
- rb_define_const(propsvm, "IsPlaying", INT2NUM(VIX_PROPERTY_VM_IS_REPLAYING));
- rb_define_const(propssnap, "DisplayName", INT2NUM(VIX_PROPERTY_SNAPSHOT_DISPLAYNAME));
- rb_define_const(propssnap, "Description", INT2NUM(VIX_PROPERTY_SNAPSHOT_DESCRIPTION));
- rb_define_const(propssnap, "PowerState", INT2NUM(VIX_PROPERTY_SNAPSHOT_POWERSTATE));
- rb_define_const(propssnap, "IsReplayable", INT2NUM(VIX_PROPERTY_SNAPSHOT_IS_REPLAYABLE));
-
- /* VixHandle types */
- rb_define_const(handletype, "None", INT2NUM(VIX_HANDLETYPE_NONE));
- rb_define_const(handletype, "Host", INT2NUM(VIX_HANDLETYPE_HOST));
- rb_define_const(handletype, "VM", INT2NUM(VIX_HANDLETYPE_VM));
- rb_define_const(handletype, "Network", INT2NUM(VIX_HANDLETYPE_NETWORK));
- rb_define_const(handletype, "Job", INT2NUM(VIX_HANDLETYPE_JOB));
- rb_define_const(handletype, "Snapshot", INT2NUM(VIX_HANDLETYPE_SNAPSHOT));
- rb_define_const(handletype, "PropertyList", INT2NUM(VIX_HANDLETYPE_PROPERTY_LIST));
- rb_define_const(handletype, "MetadataContainer", INT2NUM(VIX_HANDLETYPE_METADATA_CONTAINER));
-
- /* VMware host types */
- rb_define_const(provider, "Default", INT2NUM(VIX_SERVICEPROVIDER_DEFAULT));
- rb_define_const(provider, "Server1x", INT2NUM(VIX_SERVICEPROVIDER_VMWARE_SERVER));
- rb_define_const(provider, "Server2x", INT2NUM(VIX_SERVICEPROVIDER_VMWARE_VI_SERVER));
- rb_define_const(provider, "Workstation", INT2NUM(VIX_SERVICEPROVIDER_VMWARE_WORKSTATION));
-
- /* VMPower function options */
- rb_define_const(poweropt,"Normal", INT2NUM(VIX_VMPOWEROP_NORMAL));
- rb_define_const(poweropt,"FromGuest", INT2NUM(VIX_VMPOWEROP_FROM_GUEST));
- rb_define_const(poweropt,"SuppressSnapshot", INT2NUM(VIX_VMPOWEROP_SUPPRESS_SNAPSHOT_POWERON));
- rb_define_const(poweropt,"LaunchGUI", INT2NUM(VIX_VMPOWEROP_LAUNCH_GUI));
- rb_define_const(poweropt,"StartPaused", INT2NUM(VIX_VMPOWEROP_START_VM_PAUSED));
-
- }
+ /* Misc methods */
+ rb_define_singleton_method(rb_cVixAPI,"_getproperty",_getproperty,2);
}
68 vm.c
View
@@ -2,6 +2,8 @@
#include <ruby.h>
#include "vix.h"
+#include "functions.h"
+
/*
* VM Power Routines
*/
@@ -9,7 +11,7 @@
VALUE
_power_on(VALUE self, VALUE rvm, VALUE ropt)
{
- VixHandle vm = NUM2INT(rvm);
+ VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle"));
VixHandle job;
VixError err;
@@ -17,22 +19,21 @@ _power_on(VALUE self, VALUE rvm, VALUE ropt)
job = VixVM_PowerOn(vm,opt,VIX_INVALID_HANDLE,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
-
+ Vix_ReleaseHandle(job);
+
if(VIX_FAILED(err))
{
fprintf(stderr,"Failed to power on virtual machine: %s\n", Vix_GetErrorText(err,NULL));
return Qnil;
}
- Vix_ReleaseHandle(job);
-
return Qtrue;
}
VALUE
_power_off(VALUE self, VALUE rvm, VALUE ropt)
{
- VixHandle vm = NUM2INT(rvm);
+ VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle"));
VixHandle job;
VixError err;
@@ -40,27 +41,27 @@ _power_off(VALUE self, VALUE rvm, VALUE ropt)
job = VixVM_PowerOff(vm,opt,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
-
+ Vix_ReleaseHandle(job);
+
if(VIX_FAILED(err))
{
fprintf(stderr,"Failed to power off virtual machine: %s\n", Vix_GetErrorText(err,NULL));
return Qnil;
}
- Vix_ReleaseHandle(job);
-
return Qtrue;
}
VALUE
_pause(VALUE self, VALUE rvm)
{
- VixHandle vm = NUM2INT(rvm);
+ VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle"));
VixHandle job;
VixError err;
job = VixVM_Pause(vm,0,VIX_INVALID_HANDLE,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
+ Vix_ReleaseHandle(job);
if(VIX_FAILED(err))
{
@@ -68,19 +69,19 @@ _pause(VALUE self, VALUE rvm)
return Qnil;
}
- Vix_ReleaseHandle(job);
return Qtrue;
}
VALUE
_suspend(VALUE self, VALUE rvm)
{
- VixHandle vm = NUM2INT(rvm);
+ VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle"));
VixHandle job;
VixError err;
job = VixVM_Suspend(vm,0,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
+ Vix_ReleaseHandle(job);
if(VIX_FAILED(err))
{
@@ -88,19 +89,19 @@ _suspend(VALUE self, VALUE rvm)
return Qnil;
}
- Vix_ReleaseHandle(job);
return Qtrue;
}
VALUE
_unpause(VALUE self, VALUE rvm)
{
- VixHandle vm = NUM2INT(rvm);
+ VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle"));
VixHandle job;
VixError err;
job = VixVM_Unpause(vm,0,VIX_INVALID_HANDLE,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
+ Vix_ReleaseHandle(job);
if(VIX_FAILED(err))
{
@@ -108,14 +109,13 @@ _unpause(VALUE self, VALUE rvm)
return Qnil;
}
- Vix_ReleaseHandle(job);
return Qtrue;
}
VALUE
_reset(VALUE self, VALUE rvm, VALUE ropt)
{
- VixHandle vm = NUM2INT(rvm);
+ VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle"));
VixHandle job;
VixError err;
@@ -123,14 +123,13 @@ _reset(VALUE self, VALUE rvm, VALUE ropt)
job = VixVM_Reset(vm,opt,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
+ Vix_ReleaseHandle(job);
if(VIX_FAILED(err))
{
fprintf(stderr,"Failed to reset virtual machine: %s\n", Vix_GetErrorText(err,NULL));
return Qnil;
}
-
- Vix_ReleaseHandle(job);
return Qtrue;
}
@@ -142,37 +141,38 @@ _reset(VALUE self, VALUE rvm, VALUE ropt)
//TODO:Implement snapshot obj
//VALUE _clone(VALUE self, VALUE rvm, VALUE snapshot,
-/* NOT thouroughly tested */
+/* careful... NOT tested yet */
VALUE
_delete(VALUE self, VALUE rvm, VALUE opts)
{
- VixHandle vm = NUM2INT(rvm);
+ VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle"));
VixHandle job;
VixError err;
int opt = NUM2INT(opts);
- job = VixVM_Delete(vm,opts,NULL,NULL);
+ job = VixVM_Delete(vm,opt,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
+ Vix_ReleaseHandle(job);
if(VIX_FAILED(err))
{
fprintf(stderr,"Failed to delete virtual machine:%s\n",Vix_GetErrorText(err,NULL));
- Vix_ReleaseHandle(job);
return Qnil;
}
- Vix_ReleaseHandle(job);
return Qtrue;
}
VALUE
_read_var(VALUE self, VALUE rvm, VALUE rvartype, VALUE rname)
{
- VixHandle vm = NUM2INT(rvm);
+ VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle"));
VixHandle job;
VixError err;
+ VALUE retval;
+
int vartype = NUM2INT(rvartype);
char *name = StringValueCStr(rname);
int opts = 0;
@@ -180,26 +180,25 @@ _read_var(VALUE self, VALUE rvm, VALUE rvartype, VALUE rname)
job = VixVM_ReadVariable(vm,vartype,name,opts,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_JOB_RESULT_VM_VARIABLE_STRING, &var, VIX_PROPERTY_NONE);
+ Vix_ReleaseHandle(job);
if(VIX_FAILED(err))
{
fprintf(stderr,"Failed to read variable virtual machine:%s\n",Vix_GetErrorText(err,NULL));
- Vix_ReleaseHandle(job);
return Qnil;
}
- Vix_ReleaseHandle(job);
-
fprintf(stderr,"Read var \"%s\", got (%s)\n",name,var);
- /* This handle must be manually free'd elsewhere */
- return rb_str_new2(var);
+ retval = rb_str_new2(var);
+ Vix_FreeBuffer(var);
+ return retval;
}
VALUE
_write_var(VALUE self, VALUE rvm, VALUE rtype, VALUE rname, VALUE rval)
{
- VixHandle vm = NUM2INT(rvm);
+ VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle"));
VixHandle job;
VixError err;
@@ -209,36 +208,33 @@ _write_var(VALUE self, VALUE rvm, VALUE rtype, VALUE rname, VALUE rval)
job = VixVM_WriteVariable(vm,type,name,val,0,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
+ Vix_ReleaseHandle(job);
if(VIX_FAILED(err))
{
fprintf(stderr,"Failed to write variable (%s) val=(%s): %s\n",name,val,Vix_GetErrorText(err,NULL));
- Vix_ReleaseHandle(job);
return Qnil;
}
- Vix_ReleaseHandle(job);
-
return rval;
}
VALUE
_upgrade_vhardware(VALUE self, VALUE rvm)
{
- VixHandle vm = NUM2INT(rvm);
+ VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle"));
VixHandle job;
VixError err;
job = VixVM_UpgradeVirtualHardware(vm,0,NULL,NULL);
err = VixJob_Wait(job, VIX_PROPERTY_NONE);
-
+ Vix_ReleaseHandle(job);
+
if(VIX_FAILED(err))
{
fprintf(stderr,"Failed to upgrade the virtual hardware: %s\n",Vix_GetErrorText(err,NULL));
- Vix_ReleaseHandle(job);
return Qnil;
}
- Vix_ReleaseHandle(job);
return Qtrue;
}
463 vmware.rb
View
@@ -1,159 +1,344 @@
-require 'vixapi' # raw C extension mappings
-
-## Global Default Options
-$vixopt = {
- # connect opts
- :hosttype => VixAPI::Const::Provider::Workstation,
- :hostname => nil,
- :user => nil,
- :pass => nil,
- :port => 8222,
-
- # vm opts
- :vmxfile => nil,
- :showgui => false,
- :poweroffinguest => true # reset and poweroff will use the guest OS to shutdown
-}
-
-class VixAPI
- attr_accessor :opt
- attr_reader :session
-
- def initialize(opt={})
- @opt=$vixopt.merge(opt)
- end
+# Copyright (c) 2009 Sean Bradly
+#
+# This code is provided under the BSD license
+
+require 'vixapi' # vixapi.so, C extension wrappers
+
+
+# The VixAPI module is defined in C by vixapi.so this is just here so
+# there can be ruby method signatures.
+module VixAPI
+
+ def login_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def logout_from_guest(*args); raise "NOT IMPLEMENTED"; end
+
+ #
+ # Guest OS methods
+ #
+
+ def capture_screen_image(*args); raise "NOT IMPLEMENTED"; end
+ def install_tools(*args); raise "NOT IMPLEMENTED"; end
+ def wait_for_tools_in_guest(*args); raise "NOT IMPLEMENTED"; end
+
+ #
+ # Guest OS File methods
+ #
+
+ def copy_file_from_guest_to_host(*args); raise "NOT IMPLEMENTED"; end
+ def copy_file_from_host_to_guest(*args); raise "NOT IMPLEMENTED"; end
+ def create_directory_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def create_temp_file_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def delete_directory_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def delete_file_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def directory_exists_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def list_directory_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def file_exists_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def get_file_info_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def rename_file_in_guest(*args); raise "NOT IMPLEMENTED"; end
+
+ #
+ # Guest OS Process methods
+ #
+
+ def kill_process_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def list_processes_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def open_url_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def run_program_in_guest(*args); raise "NOT IMPLEMENTED"; end
+ def run_script_in_guest(*args); raise "NOT IMPLEMENTED"; end
+
+ #
+ # Guest OS Shared Folder methods
+ #
+
+ def enable_shared_folders(*args); raise "NOT IMPLEMENTED"; end
+ def add_shared_folder(*args); raise "NOT IMPLEMENTED"; end
+ def remove_shared_folder(*args); raise "NOT IMPLEMENTED"; end
+ # this will have to be abstracted into ruby-friendlyness
+ #def set_shared_folder_state(*args); raise "NOT IMPLEMENTED"; end
+ #def get_shared_folder_state(*args); raise "NOT IMPLEMENTED"; end
+ #def get_num_shared_folders(*args); raise "NOT IMPLEMENTED"; end
+
+ #
+ # Snapshot methods
+ #
+
+ # create a custom set of C functions to deal with this
+
+ #
+ # VM methods
+ #
+ def clone(*args); raise "NOT IMPLEMENTED"; end
- def connect(opt={})
- opt = @opt.merge(opt)
+ # Constants as defined in `/usr/include/vmware-vix/vix.h'
+ module Const
+ # Endpoint's host type (VMware Workstation, Server, Etc)
+ module Provider
+ Default = 1
+ Server1x = 2
+ Workstation = 3
+ Server2x = 10
+ end
+ # VixHandle properties types
+ module Property
+ module Host
+ HostType = 50
+ APIVersion = 51
+ end
+ module VM
+ NumVCPUs = 101
+ VMXPathname = 103
+ TeamPathname = 104
+ MemorySize = 106
+ ReadOnly = 107
+ InVMTeam = 128
+ PowerState = 129
+ ToolsState = 152
+ IsRunning = 196
+ SupportedFeatures = 197
+ IsRecording = 236
+ IsReplaying = 237
+ end
+ end
+ # on/off/reset options
+ module VMPowerOpt
+ Normal = 0x0000
+ FromGuest = 0x0004
+ SupressSnapshot = 0x0080
+ LaunchGUI = 0x0200
+ StartPaused = 0x1000
+ end
+ # Delete VM opt
+ module VM
+ DeleteDiskFiles = 0x0002
+ end
+ # R/W Variable types
+ module Var
+ Guest = 1
+ ConfigRuntime = 2
+ GuestEnvironment = 3
+ end
+ end
+
+ # Encapsulation for handles (mostly to make properties easy)
+ class Handle
+ attr_accessor :handle
+ def initialize(handle)
+ @handle = handle
+ end
+ def getproperty(property)
+ VixAPI._getproperty(self,property)
+ end
+ end
+
+end
+
+
+module VixR
+
+ ## Global Default Options (constant)
+ def self.opt
+ {
+ # connect opts
+ :hosttype => VixAPI::Const::Provider::Workstation,
+ :hostname => nil,
+ :user => nil,
+ :pass => nil,
+ :port => 8222,
+
+ # vm opts
+ :vmxfile => nil,
+ :showgui => false,
+ :fromguest => true, # reset and poweroff will use the guest OS to shutdown
+ :deletefile => false,
+ }
+ end
+
+ # VixR.connect() => VixR::Host
+ def self.connect(opt={})
+ opt = VixR.opt.merge(opt)
+ # Figure out Workstation vs. Server
opt[:hosttype] = VixAPI::Const::Provider::Server2x if opt[:hostname] =~ /^http(s)?:\/\/.*/
case opt[:hosttype]
- when Const::Provider::Workstation
- session = _connect_wkstn()
- when Const::Provider::Server2x
-
- session = _connect_server(
- VixAPI::Const::Provider::Server2x,
- opt[:hostname],
- opt[:port],
- opt[:user],
- opt[:pass]
- )
+ when VixAPI::Const::Provider::Workstation
+ session = VixAPI.connect()
+ when VixAPI::Const::Provider::Server2x
+ session = VixAPI.connect(VixAPI::Const::Provider::Server2x,
+ opt[:hostname],
+ opt[:port],
+ opt[:user],
+ opt[:pass])
else
raise "This VMware host provider type is not supported (you probably should use VMware Server 2.x)"
end
raise "could not get session" if not session
-
- @host = Host.new(self,session)
+
+ @host = Host.new(session.handle)
@host
end
- class VM
- def initialize(vixapi, vmhandle)
- @api = vixapi
- @vm = vmhandle
- end
- def power_on(opt)
- if not opt.class < Integer
- opts = @api.opt.merge(opt)
- opt = 0
- opt += Const::VMPowerOpt::LaunchGUI if opts[:showgui]
- opt += Const::VMPowerOpt::StartPaused if opts[:startpaused]
- end
- @api._power_on(@vm,opt)
- end
- def power_off(fromguest=@api.opt[:poweroffinguest])
- if fromguest
- op = Const::VMPowerOpt::FromGuest
- else
- op = Const::VMPowerOpt::Normal
- end
- @api._power_off(@vm,op)
- end
- def reset(fromguest=@api.opt[:poweroffinguest])
- if fromguest
- op = Const::VMPowerOpt::FromGuest
- else
- op = Const::VMPowerOpt::Normal
- end
- @api._reset(@vm,op)
- end
- def pause
- @api._pause(@vm)
- end
- def unpause
- @api._unpause(@vm)
- end
- def suspend
- @api._suspend(@vm)
- end
+ #
+ # Host class
+ #
+ class Host < VixAPI::Handle
- def write_var(name,val,type=Const::VarType::Guest)
- @api._write_var(@vm,type,name,val)
- end
- def read_var(name, type=Const::VarType::Guest)
- @api._read_var(@vm,type,name)
- end
- def upgrade_vhardware()
- @api._upgrade_vhardware(@vm)
- end
- def capture_screen_image
- @api._capture_screen_image(@vm)
- end
-
- def cp_to_host(src,dst)
- @api._copy_file_from_guest_to_host(@vm,src,dst)
- end
-
- def cp_to_guest(src,dst)
- @api._copy_file_from_host_to_guest(@vm,src,dst)
- end
-
- def mkdir_guest(path)
- @api._create_directory_in_guest(@vm, path)
- end
-
- def mktemp_guest
- @api._create_temp_file_in_guest(@vm)
- end
-
- def rmdir_guest
- @api._delete_directory_in_guest(@vm, path)
- end
-
- def rm_guest
- @api._delete_file_in_guest(@vm, path)
- end
-
- def directory_exists?
- @api._directory_exists_in_guest(@vm, path)
- end
- end
+ # open_vmx(vmxfilepath) => VixR::VM
+ def open_vmx(path=nil,opt={})
+ opt = VixR.opt.merge(opt)
+ path ||= opt[:vmxfile]
+ vmhandle = VixAPI._open_vmx(self,path)
+ raise "Could not open vmx file (#{path})" if not vmhandle
+ vm = VM.new(vmhandle.handle)
+ vm
+ end
+
+ # disconnect(hostHandle) => true
+ #
+ # This should automatically be called automatically when the program exits
+ def disconnect()
+ VixAPI._disconnect(@handle)
+ end
- class Host
- def initialize(vixapi,host)
- @api = vixapi
- @session = host
- end
- def vmx_open(path=opt[:vmxfile])
- vmhandle = @api._vmx_open(@session,path)
- raise "Could not open vmx file (#{path})" if not vmhandle
- vm = VM.new(@api,vmhandle)
- vm
- end
+ #
+ # Properties
+ #
+ def hosttype
+ self.getproperty(VixAPI::Const::Property::Host::HostType)
+ end
+
+ def api_version
+ self.getproperty(VixAPI::Const::Property::Host::APIVersion)
+ end
end
+
+ #
+ # VM class
+ #
+ class VM < VixAPI::Handle
+
+ # power_on(:showgui=>false, :startpaused=>false) => true
+ #
+ # Power on the VM
+ def power_on(opt={})
+ opt = VixR.opt.merge(opt)
+ opts = 0
+ opts |= VixAPI::Const::VMPowerOpt::LaunchGUI if opt[:showgui]
+ opts |= VixAPI::Const::VMPowerOpt::StartPaused if opt[:startpaused]
+ VixAPI._power_on(self,opts)
+ end
+
+ # power_off(:fromguest=>true) => true
+ def power_off(opt={})
+ opt = VixR.opt.merge(opt)
+ opts = 0
+ opts |= VixAPI::Const::VMPowerOpt::FromGuest if opt[:fromguest]
+ VixAPI._power_off(self,opts)
+ end
+
+ # pause()
+ def pause
+ VixAPI._pause(self)
+ end
+
+ # suspend()
+ def suspend
+ VixAPI._suspend(self)
+ end
+
+ # unpause()
+ def unpause
+ VixAPI._unpause(self)
+ end
+
+ # reset(:fromguest=>true)
+ def reset(opt={})
+ opt = VixR.opt.merge(opt)
+ opts = 0
+ opts |= VixAPI::Const::VMPowerOpt::FromGuest if opt[:fromguest]
+ VixAPI._reset(self,opts)
+ end
+
+ # delete(:deletefiles=>false)
+ def delete(opt={})
+ opt = VixR.opt.merge(opt)
+ opts = 0
+ opts |= VixAPI::Const::VM::DeleteDiskFiles if opt[:deletefiles]
+ VixAPI._delete(self,opts)
+ end
+
+ # upgrade_vhardware()
+ def upgrade_vhardware
+ VixAPI._upgrade_vhardware(self)
+ end
+
+ # read_var()
+ def read_var(name,vartype=VixAPI::Config::Var::Guest)
+ VixAPI._read_var(self,vartype,name)
+ end
+
+ # write_var()
+ def write_var(name,val,type=VixAPI::Config::Var::Guest)
+ VixAPI._write_var(self,type,name,val)
+ end
+
+ #
+ # Properties
+ #
+ def num_vcpus
+ self.getproperty(VixAPI::Const::Property::VM::NumVCPUs)
+ end
+
+ def vmx_path
+ self.getproperty(VixAPI::Const::Property::VM::VMXPathname)
+ end
+
+ def team_path
+ self.getproperty(VixAPI::Const::Property::VM::TeamPathname)
+ end
+
+ def mem_size
+ self.getproperty(VixAPI::Const::Property::VM::MemorySize)
+ end
+
+ def read_only?
+ self.getproperty(VixAPI::Const::Property::VM::ReadOnly)
+ end
+
+ def vm_team?
+ self.getproperty(VixAPI::Const::Property::VM::InVMTeam)
+ end
+
+ def power_state
+ self.getproperty(VixAPI::Const::Property::VM::PowerState)
+ end
+
+ def tools_state
+ self.getproperty(VixAPI::Const::Property::VM::ToolsState)
+ end
+
+ def running?
+ self.getproperty(VixAPI::Const::Property::VM::IsRunning)
+ end
+
+ def features
+ self.getproperty(VixAPI::Const::Property::VM::SupportedFeatures)
+ end
+
+ def recording?
+ self.getproperty(VixAPI::Const::Property::VM::IsRecording)
+ end
+
+ def replaying?
+ self.getproperty(VixAPI::Const::Property::VM::IsReplaying)
+ end
+ end
+
+
end
## init C runtime (only once)
-VixAPI.new.start
-
-# Test script
-vix = VixAPI.new
-host = vix.connect
-vm = host.vmx_open("/data/tmp/BPSLABS-W2K3-01/Windows Server 2003 Enterprise Edition.vmx")
-vm.read_var("poo")
-#vm.power_on(:showgui=>true)
+VixAPI.start
-#puts "Powered on"
+#VixAPI.connect
Please sign in to comment.
Something went wrong with that request. Please try again.