Development

PySphere script to clone a template into multiple VMs with post processing

PySphere script to clone a template into multiple VMs with post processing

*There is a better/newer version of this script, for more details, check this post: https://dellaert.org/2014/02/03/multi-clone-py-multi-threaded-cloning-of-a-template-to-multiple-vms/ – Or via github: https://github.com/pdellaert/vSphere-Python *

For an internal test setup i needed to be able to deploy multiple VMs from a template without to much hassle. So i started thinking that the best way to approach this, would be using a script. At first i thought i had to do this using PowerCLI as this is the preferred VMware way of scripting.

Luckily i came across the wonderful site of PySphere, which is a Python library that provides tools to access and administer a VMware vSphere setup. As Python wasn’t my strong suite, i was in a bit of a dilemma, i had almost no experience with either of those languages, so which to go for. Altho PowerCLI/Powershell has a lot more possibilities, as it is maintained and developed by VMware itself, Python had the great advantage i could do it all in a more familiar environment (Linux). It’s also closer to the languages i know than Powershell is. So i decided to just go for Python and see if it got me where i wanted to go.

This script deploys multiple VMs from a single template, you can specify how many and what basename the VMs should have. Each VM gets a name starting with the basename and a number which increments with each new VM. You are able to specify at what number it should start. You can also specify in which resource pool the VMs should be placed.

And as a final feature, you can specify a script which should be called after the VM has successfully booted and the guest OS has initiated it’s network interface. This script will be called with two arguments: the VM name and it’s IP. You can even specify it may only return a valid IPv6 address (i needed this to deploy VMs in an IPv6-only test environment).

The output if run with the help argument (-h):

To run the script, the command should look something like this:

And finally, the script itself, you can also download it on Github:

The script is probably a work in progress, as there are a lot of possibilities for improvement, if you have any requests feel free to contact me!

This is the first Python script i’ve ever written, so forgive me if i made some basic mistakes against best practices. Feel free to submit a patch.

PHP 5.3.2 DateTime diff() issue

PHP 5.3.2 DateTime diff() issue

It seems that PHP 5.3.2 ( 5.3.2-1ubuntu4.10 ) has a small issue when using the DateTime diff() method.

Got this code:

This code results in:

It seems the diff statement changes the starting date to a date before. This is kind of an issue, i need that difference in a project between days, weeks, months and even years. And the issue get’s worse if you use weeks (the difference becomes 14 days instead of 7) in the interval or months (61 instead of 30 or 31). So i decided to use a quickfix to calculate the difference, cause this was not working…

The workaround needed to change depending on the mode variable (day, week, month or year). For day and week it could just be 1 and 7 days, but the amount of days in a month and years changes per month and per year (leap years). So i came up with:

I decided to use the mktime function, because the getTimestamp() gave some issues concerning timezones (i think ;))

 Update

Dries Verachtert pointed me to a comment on the page of the DateTime sub method. The short of it:

If you use diff() after sub(), the effects of the sub() will be repeated on the date object.
It doesn’t matter if the object is the one diffed or doing the diffing (i.e. which object you call diff() from).
Note that using add() instead of sub() does NOT have the same effect.
This is particularly undesirable — in this example you make a datetime, use sub() to make it a relative time in the past, and then date->diff() to confirm the difference. But the diff() inadvertendly makes the difference 2x.

Strange issue, but i can tell you it’s fixed in 5.3.3, as i have running that on an internal test server, and the code does a perfect job on that.