[ovirt-users] Shutdown all VM's command line

Giorgio Biacchi giorgio at di.unimi.it
Thu Jan 11 11:47:02 UTC 2018


On 01/11/2018 11:44 AM, Kapetanakis Giannis wrote:
> On 10/01/18 22:11, Wesley Stewart wrote:
>> Marcelo,
>>
>> I would greatly appreciate seeing a script!  It would be an excellent chance for me to learn a bit about using ovirt from the command line as well!
> 
> I'm using something like this with ovirt-shell
> 
> vm_shutdown:
> #!/bin/sh
> LOG=/root/ovirt/vm_shutdown_log
> echo `date` >> $LOG
> /usr/bin/ovirt-shell -f /root/ovirt/vm_shutdown_script >> $LOG
> echo "" >> $LOG
> 
> vm_shutdown_script:
> list vms --kwargs status-state=up|grep name | sed s/'name       :'/'action vm'/ | sed -e 's/$/ shutdown/' > /root/ovirt/new_vm_shutdown_script
> file /root/ovirt/new_vm_shutdown_script
> 
> new_vm_shutdown_script now lists entries like this:
> action vm vm1 shutdown
> action vm vm2 shutdown
> etc.
> 
> G
> 
> 
> _______________________________________________
> Users mailing list
> Users at ovirt.org
> http://lists.ovirt.org/mailman/listinfo/users
> 

You can use python SDK.

Somthing like this should work

#!/usr/bin/env python

import ovirtsdk4 as sdk

ovaddress = "<fill correct URL here>"
username="admin at internal"
password="*********"

connection = sdk.Connection(
   url=ovaddress,
   username=username,
   password=password,
   ca_file='ca.crt',
   insecure=True
)

system_service = connection.system_service()
vms_service = system_service.vms_service()
vms = vms_service.list()

for vm in vms:
         vm_service = vms_service.vm_service(vm.id)
         vm_service.shutdown()

connection.close()

-- 
gb

PGP Key: http://pgp.mit.edu/
Primary key fingerprint: C510 0765 943E EBED A4F2 69D3 16CC DC90 B9CB 0F34


More information about the Users mailing list