Using API to automate VM OS Drive replacement
-
Hi All,
API Question: Can I automate this process a bit more ?
- Use the API to clone (fast) a drive (not the whole VM, just a drive) ?
- Use the API to shut down a VM, delete the OS drive, install the previously cloned drive, start the VM up again ?
Background: I have 150+ VM's where they have a multi drive setup:
- OS Drive. This is based on my template OS. Only the server name, IP address and DB Server config is different
- Data, Storage and Backup Drives. These three are unique to each instance.
To roll out OS and/or DB updates, I have a process where I:
- Prepare the template OS.
- Clone the template VM (normally in batches of 10-20 at a time)
- Delete the cloned templates, keeping the OS drive.
- For each of the 150+ VM's
i. Shut it down
ii. Delete the OS drive
iii. Add one of the cloned OS drives
iv. rename the drive name as appropriate
v. start up the VM - On first start, the new OS drive has a script which runs to automatically set the server name, IP address and DB Server config (each VM's unique settings are saved in a config file on one of the other drives which my config script reads from).
I've been using this process for a number of years, 3 or 4 times a year - previously with the Windows XenCenter. This time round I tried using XO instead, but there was one step that was painful.
On XenCenter, after making 20x clones of the template. I can select all 20 and delete in one go. Before the actual delete, I can select to NOT delete the OS drive.
With XO, the only way I could figure out how to do this was individually editing each clone to disconnect the drive.The other difference is, with XenCenter the original copy was always listed last. With XO, the original copy appeared to be in the middle of the list and more than once I ended up adding the original copy to a machine by mistake and only discovering when I went to make more clones and discovering the drive name was renamed to one of the VM's I'd just updated. I worked around this by renaming it after doing the clones, then renaming it back before creating more clones.
Cheers
Kent. -
Hi,
I'm not sure to understand why you are using clones to update your OS in the first place. Why not using Ansible or any similar tool to have automation at the OS level directly?
-
It's fast (individual server down time is ~5 minutes), it's been a rock solid reliable process in all the years I've been doing it, plus it's relatively simple to do. It also allows doing rollouts like what I did in the weekend, which was a clean install of Windows Server 2022 and a major update to our DB software.
There are also some aspects of the DB updating process which are less than reliable. -
Well, to answer the original question, sure you can 100% do everything you can do in the UI with the API or the CLI. The web UI is "just" a client to the API.
-
Is there a way to clone a drive without having to also clone the whole VM ?
Also, is there a way (via the XO interface) to delete the VM and not delete the drive assigned to it like is possible with XenCenter ?
-
Yes, you should start reading this and explore all the functions via the CLI, it's probably the easiest approach: https://xen-orchestra.com/docs/architecture.html#xo-cli-cli
About VM removal without the disks, I'm not sure, let me ask @julien-f
-
Hi @olivierlambert ,
Okay - I spent some time today and have my head around the cli basics. So far I've worked out how to read the object list to get the VM's, VDI's and VBD's.
I now exploring around automating my build process.
I've worked out how to clone the VM, then get the VBDs and VDI's of the drives attached to that VM.
I can even see via the xo-cli that I can delete the VM and keep the drive.Questions:
2) Most items have both a 'uuid' and an 'id', but as far as I can tell, they are always the same. Is there any difference ?-
What is the best way to remove the existing OS drive and add the newly cloned drive ? (I wonder if I can just edit the VBD record to change the VDI it points to, but I think I'm missing a step in the process.
-
Is this documented and I've just not found it yet. The link above gives the same as the xo-cli does. and the xo-cli list-commands gives the commands, but minimal documentation on how to use.
thanks
Kent. -
-
@julien-f will come back to you when he can
-
Hi @olivierlambert ,
Okay, I made some progress today and have worked out the steps to remove, delete or add drives to a VM.
Removed drive from the server attached to and also deleted the disk itself
xo-cli vdi.delete id=<the id of the VDI>
Removed drive from the server attached to, but didn't delete the disk!
xo-cli vdb.delete id=<the id of the VBD>
Added a drive to the server
xo-cli vm.attachDisk vdi=<the id of the VDI> vm=<the id of VM>
If was the last one that I was missing. I'm still not sure of the difference between the 'uuid' and 'id' fields, but will use the 'id' as this seems to be what the commands are wanting.
Thanks
Kent.