diff --git a/extconf.rb b/extconf.rb index 3cfc433..aaf24f6 100644 --- a/extconf.rb +++ b/extconf.rb @@ -1,6 +1,6 @@ require 'mkmf' -$CFLAGS = (ENV['CFLAGS']||"") + "-Wall" +$CFLAGS = (ENV['CFLAGS']||"") + "-Wall -g" $LDFLAGS = (ENV["LDFLAGS"]||"")+"-lvixAllProducts" have_library('vixAllProducts','VixHost_Connect') diff --git a/functions.h b/functions.h index 9fd38df..66dd9ae 100644 --- a/functions.h +++ b/functions.h @@ -23,6 +23,17 @@ VALUE _copy_file_from_guest_to_host(VALUE,VALUE,VALUE,VALUE); VALUE _copy_file_from_host_to_guest(VALUE,VALUE,VALUE,VALUE); VALUE _create_directory_in_guest(VALUE, VALUE, VALUE); VALUE _create_temp_file_in_guest(VALUE, VALUE); +VALUE _delete_directory_in_guest(VALUE, VALUE, VALUE); +VALUE _delete_file_in_guest(VALUE, VALUE, VALUE); +VALUE _directory_exists_in_guest(VALUE, VALUE, VALUE); +VALUE _login_in_guest(VALUE, VALUE, VALUE, VALUE); +VALUE _logout_from_guest(VALUE, VALUE); /* Misc functions */ VALUE _getproperty(VALUE, VALUE, VALUE); +VALUE _getproperty_int(VixHandle, int); +VALUE _getproperty_str(VixHandle, int); +VALUE _getproperty_bool(VixHandle, int); +VALUE _getproperty_handle(VixHandle, int); +VALUE _getproperty_int64(VixHandle, int); +VALUE _getproperty_blob(VixHandle, int); diff --git a/guest.c b/guest.c index 7385eef..41ea0e8 100644 --- a/guest.c +++ b/guest.c @@ -1,34 +1,39 @@ #include #include #include "vix.h" +#include "functions.h" VALUE _capture_screen_image(VALUE self, VALUE rvm) { - VixHandle vm = NUM2INT(rvm); + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); VixHandle job; VixError err; - + VALUE pngdata; + job = VixVM_CaptureScreenImage(vm, VIX_CAPTURESCREENFORMAT_PNG, VIX_INVALID_HANDLE, NULL, NULL); err = VixJob_Wait(job, VIX_PROPERTY_NONE); - + if(VIX_FAILED(err)) { - fprintf(stderr,"Failed to capture screen on virtual machine: %s\n", Vix_GetErrorText(err,NULL)); Vix_ReleaseHandle(job); + fprintf(stderr,"Failed to capture screen on virtual machine: %s\n", + Vix_GetErrorText(err,NULL)); return Qnil; } + /* Read the image from teh Job properties */ + pngdata = _getproperty_blob(job, VIX_PROPERTY_JOB_RESULT_SCREEN_IMAGE_DATA); Vix_ReleaseHandle(job); - - return Qtrue; + + return pngdata; } VALUE _wait_for_tools(VALUE self, VALUE rvm, VALUE rtimeout) { - VixHandle vm = NUM2INT(rvm); + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); VixHandle job; VixError err; @@ -37,20 +42,21 @@ _wait_for_tools(VALUE self, VALUE rvm, VALUE rtimeout) job = VixVM_WaitForToolsInGuest(vm,timeout,NULL,NULL); err = VixJob_Wait(job, VIX_PROPERTY_NONE); Vix_ReleaseHandle(job); - if(VIX_ERROR(err)) + + if(VIX_FAILED(err)) { - fprintf(stderr,"Error waiting for VMtools in guest: %n\n", - Vix_GetErrorTest(err,NULL)); + fprintf(stderr,"Error waiting for VMtools in guest: %s\n", + Vix_GetErrorText(err,NULL)); return Qnil; } - return Qtrue;o + return Qtrue; } VALUE _copy_file_from_guest_to_host(VALUE self, VALUE rvm, VALUE rsrc, VALUE rdst) { - VixHandle vm = NUM2INT(rvm); + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); VixHandle job; VixError err; @@ -76,7 +82,7 @@ _copy_file_from_guest_to_host(VALUE self, VALUE rvm, VALUE rsrc, VALUE rdst) VALUE _copy_file_from_host_to_guest(VALUE self, VALUE rvm, VALUE rsrc, VALUE rdst) { - VixHandle vm = NUM2INT(rvm); + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); VixHandle job; VixError err; @@ -103,7 +109,7 @@ _copy_file_from_host_to_guest(VALUE self, VALUE rvm, VALUE rsrc, VALUE rdst) VALUE _create_directory_in_guest(VALUE self, VALUE rvm, VALUE rpath) { - VixHandle vm = NUM2INT(rvm); + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); VixHandle job; VixError err; @@ -126,7 +132,7 @@ _create_directory_in_guest(VALUE self, VALUE rvm, VALUE rpath) VALUE _create_temp_file_in_guest(VALUE self, VALUE rvm) { - VixHandle vm = NUM2INT(rvm); + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); VixHandle job; VixError err; char *tempfilepath; @@ -150,7 +156,7 @@ _create_temp_file_in_guest(VALUE self, VALUE rvm) VALUE _delete_directory_in_guest(VALUE self, VALUE rvm, VALUE rpath) { - VixHandle vm = NUM2INT(rvm); + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); VixHandle job; VixError err; @@ -173,7 +179,7 @@ _delete_directory_in_guest(VALUE self, VALUE rvm, VALUE rpath) VALUE _delete_file_in_guest(VALUE self, VALUE rvm, VALUE rpath) { - VixHandle vm = NUM2INT(rvm); + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); VixHandle job; VixError err; @@ -181,23 +187,22 @@ _delete_file_in_guest(VALUE self, VALUE rvm, VALUE rpath) job = VixVM_DeleteFileInGuest(vm, path, NULL, NULL); err = VixJob_Wait(job, VIX_PROPERTY_NONE); + Vix_ReleaseHandle(job); if(VIX_FAILED(err)) { - fprintf(stderr,"Failed to delete file on guest: %s\n", Vix_GetErrorText(err,NULL)); - Vix_ReleaseHandle(job); + fprintf(stderr,"Failed to delete file on guest: %s\n", + Vix_GetErrorText(err,NULL)); return Qnil; } - Vix_ReleaseHandle(job); - return Qtrue; } VALUE _directory_exists_in_guest(VALUE self, VALUE rvm, VALUE rpath) { - VixHandle vm = NUM2INT(rvm); + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); VixHandle job; VixError err; @@ -205,19 +210,64 @@ _directory_exists_in_guest(VALUE self, VALUE rvm, VALUE rpath) job = VixVM_DirectoryExistsInGuest(vm, path, NULL, NULL); err = VixJob_Wait(job, VIX_PROPERTY_NONE); + Vix_ReleaseHandle(job); if(VIX_FAILED(err)) { - //fprintf(stderr,"Failed to delete file on guest: %s\n", Vix_GetErrorText(err,NULL)); - Vix_ReleaseHandle(job); + fprintf(stderr,"Failed to check directory on guest: %s\n", + Vix_GetErrorText(err,NULL)); + return Qnil; + } + + return Qtrue; +} + + +VALUE +_login_in_guest(VALUE self, VALUE rvm, VALUE ruser, VALUE rpass) +{ + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); + VixHandle job; + VixError err; + char *user=StringValueCStr(ruser); + char *pass=StringValueCStr(rpass); + + job = VixVM_LoginInGuest(vm, user, pass, 0, NULL, NULL); + err = VixJob_Wait(job, VIX_PROPERTY_NONE); + Vix_ReleaseHandle(job); + + if(VIX_FAILED(err)) + { + fprintf(stderr,"Failed to login on guest: %s\n", + Vix_GetErrorText(err,NULL)); return Qnil; } + + return Qtrue; +} +VALUE +_logout_from_guest(VALUE self, VALUE rvm) +{ + VixHandle vm = NUM2INT(rb_iv_get(rvm,"@handle")); + VixHandle job; + VixError err; + + job = VixVM_LogoutFromGuest(vm, NULL, NULL); + err = VixJob_Wait(job, VIX_PROPERTY_NONE); Vix_ReleaseHandle(job); + if(VIX_FAILED(err)) + { + fprintf(stderr,"Failed to logout of guest: %s\n", + Vix_GetErrorText(err,NULL)); + return Qnil; + } + return Qtrue; } + /* VALUE @@ -256,16 +306,6 @@ _list_processes_in_guest(VALUE self, VALUE rvm) { } -VALUE -_login_in_guest(VALUE self, VALUE rvm) -{ -} - -VALUE -_logout_from_guest(VALUE self, VALUE rvm) -{ -} - VALUE _open_url_in_guest(VALUE self, VALUE rvm) { diff --git a/properties.c b/properties.c new file mode 100644 index 0000000..8038572 --- /dev/null +++ b/properties.c @@ -0,0 +1,146 @@ +#include +#include +#include +#include "functions.h" + +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; + char *data; + int len; + + err = Vix_GetProperties(handle,propid,&len,&data,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(data,len); + Vix_FreeBuffer(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; +} diff --git a/vixapi.c b/vixapi.c index 178c854..a73687b 100644 --- a/vixapi.c +++ b/vixapi.c @@ -11,147 +11,6 @@ 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) { @@ -191,6 +50,11 @@ Init_vixapi() 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); + + /* VM Guest Methods */ + rb_define_singleton_method(rb_cVixAPI,"_capture_screen_image",_capture_screen_image,1); + rb_define_singleton_method(rb_cVixAPI,"_login_in_guest",_login_in_guest,3); + rb_define_singleton_method(rb_cVixAPI,"_logout_from_guest",_logout_from_guest,1); /* Misc methods */ rb_define_singleton_method(rb_cVixAPI,"_getproperty",_getproperty,2); diff --git a/vmware.rb b/vmware.rb index 25e80f1..98610a1 100644 --- a/vmware.rb +++ b/vmware.rb @@ -160,9 +160,10 @@ def self.connect(opt={}) opt[:hosttype] = VixAPI::Const::Provider::Server2x if opt[:hostname] =~ /^http(s)?:\/\/.*/ case opt[:hosttype] when VixAPI::Const::Provider::Workstation - session = VixAPI.connect() + session = VixAPI._connect(VixAPI::Const::Provider::Workstation, + nil,0,nil,nil) when VixAPI::Const::Provider::Server2x - session = VixAPI.connect(VixAPI::Const::Provider::Server2x, + session = VixAPI._connect(VixAPI::Const::Provider::Server2x, opt[:hostname], opt[:port], opt[:user], @@ -281,6 +282,28 @@ def write_var(name,val,type=VixAPI::Config::Var::Guest) VixAPI._write_var(self,type,name,val) end + # + # Guest / VM Tools Functions + # + + # screendump(file->nil).. if file non nil, save to file, else return bytes + def screendump(file=nil) + pngdata = VixAPI._capture_screen_image(self) + end + + # login(user=nil,pass=>nil) # => also reads :username, :password from $opt + def login(user=nil,pass=nil) + user = VixR.opt[:username] if not user + pass = VixR.opt[:password] if not pass + raise "You must provide a username and password via the options" if not user or not pass + VixAPI._login_in_guest(self,user,pass) + end + + # logout + def logout + VixAPI._logout_from_guest(self) + end + # # Properties #