VMware {code} Community
VIGuru
Contributor
Contributor
Jump to solution

My first Perl App

Hello,

Is it possible to create a script using the perl sdk and execute it via apache or IIS ? I wish to create

a script which can be executed via IIS or Apache to deploy a vm from a template via a browser.

regards

newbie

0 Kudos
1 Solution

Accepted Solutions
stumpr
Virtuoso
Virtuoso
Jump to solution

I'm not familiar with that particular appliance, but a standard apache installation typically has the cgi configured. Just need to place the script into the cgi-bin directory.

You'll need to install the VI Perl Toolkit from VMware (Windows or Linux). Be sure to follow the instructions for the dependencies.

Reuben Stump | http://www.virtuin.com | @ReubenStump

View solution in original post

0 Kudos
10 Replies
lamw
Community Manager
Community Manager
Jump to solution

Yes, that is possible. Do a search online for Perl and CGI, if you have a vSphere SDK for Perl scrip that already does the VM deployment, then the only piece left is to have it be executed via some webpage of your design.

Here are a bunch of canned VMware scripts that's included with the vSphere SDK for Perl: http://www.vmware.com/support/developer/viperltoolkit/viperl40/doc/vsperl_util_index.html, one that may of be interest is vmclone.pl. If the default scripts don't provide what you need you can always re-write or update the script.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

Definitely follow up on the links Iamw provided. But I do this all the time. Just install the VI Perl Toolkit into your web server, then use mod_perl or Perl CGI. Pretty straightforward actually. Just have to format your output as HTML vs text. It's actually lot easier when you start using the Perl CGI/HTML modules.

If you want to get fancy with mod_perl, you can preload the VMware modules as well, but I doubt you'll have a high traffic site that needs that sort of optimization.

Your web server will need to be able to communicate to the ESX host(s) or vCenter servers on 443.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
VIGuru
Contributor
Contributor
Jump to solution

thanks very much guys

: would you be able to give me a hello world example except instead of hello world just a simple vmware response such as the virtual center hostname or the number of vms etc returned to the browser?

regards

0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

Sure, here's a very basic "Hello VI Perl" type example. It just gets the About info from the Service Content managed object. This is assuming the web server you're on is *Nix based and has cgi-bin and perl installed.

#!/usr/bin/perl

use warnings;
use strict;

use VMware::VIRuntime;

Opts::set_option("username", "administrator");
Opts::set_option("password", "VMware1");
Opts::set_option("url", "https://192.168.73.133/sdk");

Opts::parse();
Opts::validate();

Util::connect();

my $service_content = Vim::get_service_content();

my $full_name = $service_content->about->fullName;

my $html = "<HTML><BODY>$full_name<BR></BODY></HTML>"; 

Util::disconnect();

print "Content-type: text/html\n\n";
print $html;

In my case I get back the full name value from the serviceContent->about object.

VMware vCenter Server 4.0.0 build-208111

Note the Opts::set_option lines. In the case of the Service Content, a login isn't required, but you'll need that information for more interesting work. You could also use a configuration file, look through the vSphere SDK for Perl Programming Guide. If you were accepting the login information as a form from a web page, you'd just parse the variables and set_option for simplicity.

Obviously this will get a bit more complex as you do more interesting things, for example, you'll want to use eval { } blocks to catch errors from the SDK and handle them in a HTML safe manner.

Another place you may want to look is the vSphere Client Plugins Documentation. There will be a little more setup work, but you'll be able to integrate your cgi based tools directly into the vCenter UI.

If you want something with embedded perl for doing more complex web pages, look at Perl-Mason. I've used it in the past and it's pretty solid. I know VMware is working on a new plugin framework called Flex I believe. This is supposed to replace web and C# plugins in the future, but I'm not sure when that is slated to be released. I do believe the View 4.5 Beta web UIs use the new Flex framework, so it may be getting close.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
VIGuru
Contributor
Contributor
Jump to solution

thanks for that example. Is this example unix specific? I tried running the script on IIS7.0 using event handlers that point to the perl.exe that shipped with vsphere cli.(perl sdk)

This code works fine:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "Hello world!\n\n";

print "Press the Enter key to exit.\n";

But when I run your .pl script I get the following:

HTTP Error 502.2 - Bad Gateway

The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "Can't load 'C:/vspherecli/Perl/site/lib/auto/XML/LibXML/Common/Common.dll' for module XML::LibXML::Common: load_file:The specified module could not be found at C:/vspherecli/Perl/lib/DynaLoader.pm line 230. at C:/vspherecli/Perl/site/lib/XML/LibXML.pm line 12 Compilation failed in require at C:/vspherecli/Perl/site/lib/XML/LibXML.pm line 12. BEGIN failedcompilation aborted at C:/vspherecli/Perl/site/lib/XML/LibXML.pm line 12. Compilation failed in require at C:/vspherecli/Perl/lib/VMware/VICommon.pm line 11. BEGIN failedcompilation aborted at C:/vspherecli/Perl/lib/VMware/VICommon.pm line 11. Compilation failed in require at C:/vspherecli/Perl/lib/VMware/VIRuntime.pm line 15. Compilation failed in require at C:\inetpub\wwwroot\cgi-bin\test2.pl line 6. BEGIN failed--compilation aborted at C:\inetpub\wwwroot\cgi-bin\test2.pl line 6. ".

0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

Looks like a problem with your Perl Toolkit installation or your Perl environment. Can you run some of the sample scripts from the Perl Toolkit outside of the IIS CGI (just run them at the command line on the same server)?

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
VIGuru
Contributor
Contributor
Jump to solution

Yep definately something wrong there. Im going to drop winblowz for this project. If I download the appliance

http://www.vmware.com/appliances/directory/va/178973/resources

Can I use that appliances webserver to execute the perl scripts. Or can you tell me what your environment

is and I will build that up.

regards

B

0 Kudos
stumpr
Virtuoso
Virtuoso
Jump to solution

I'm not familiar with that particular appliance, but a standard apache installation typically has the cgi configured. Just need to place the script into the cgi-bin directory.

You'll need to install the VI Perl Toolkit from VMware (Windows or Linux). Be sure to follow the instructions for the dependencies.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
VIGuru
Contributor
Contributor
Jump to solution

Thanks I'll try that

0 Kudos
VIGuru
Contributor
Contributor
Jump to solution

kablamo!!!

Got it working wrong paths in environment variables.

My Environemnt:

Windows vista, IIS7, vSphere CLI Perl SDK (which is activeperl from activstate)

Steps to get it working:

configure IIS Event Handliers as per doco but ensure vsphere cli install directory contained no spaces.

set path variables for all paths referenced in the error code posted previously in this article.

0 Kudos