Skip to content

Commit

Permalink
ovirtlago: add 'ovirt start'/'ovirt stop'
Browse files Browse the repository at this point in the history
1. Add 'start_all_vms' method.
2. Add 'lago ovirt start/stop'
  • Loading branch information
nvgoldin committed Mar 20, 2017
1 parent fcdc165 commit 8d9888a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 36 deletions.
40 changes: 40 additions & 0 deletions ovirtlago/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import ovirtlago
from lago.plugins.cli import (CLIPlugin, cli_plugin, cli_plugin_add_argument)
from lago.utils import (in_prefix, with_logging)
from lago.log_utils import LogTask
from lago.config import config as lago_config

LOGGER = logging.getLogger('ovirt-cli')
Expand Down Expand Up @@ -196,13 +197,52 @@ def do_ovirt_stop_vms(prefix, **kwargs):
prefix.virt_env.engine_vm().stop_all_vms()


@cli_plugin(help='Start all VMs that are down')
@in_ovirt_prefix
@with_logging
def do_ovirt_start_vms(prefix, **kwargs):
prefix.virt_env.engine_vm().start_all_vms()


@cli_plugin(help='Print oVirt setup status')
@in_ovirt_prefix
@with_logging
def do_ovirt_status(prefix, **kwargs):
prefix.virt_env.engine_vm().status()


@cli_plugin(
help=(
'Start the environment, put all hosts in activate mode'
)
)
@in_ovirt_prefix
@with_logging
def do_ovirt_start(prefix, **kwargs):
with LogTask('Starting oVirt environment'):
prefix.start()
with LogTask('Activating Engine Hosts'):
prefix.virt_env.engine_vm().start_all_hosts()


@cli_plugin(
help=(
'Stop all Engine VMs, put all hosts in maintenance and turn off '
' Lago VMs.'
)
)
@in_ovirt_prefix
@with_logging
def do_ovirt_stop(prefix, **kwargs):
with LogTask('Stopping oVirt environment'):
with LogTask('Stopping Engine VMs'):
prefix.virt_env.engine_vm().stop_all_vms()
with LogTask('Putting hosts in maintenance mode'):
prefix.virt_env.engine_vm().stop_all_hosts()
with LogTask('Shutdown Lago VMs'):
prefix.shutdown()


@cli_plugin(
help=(
'Collect logs from VMs, list of collected logs '
Expand Down
87 changes: 51 additions & 36 deletions ovirtlago/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import os
import time
import warnings

import lago
import lago.vm
import functools
import logging

from lago.config import config as lago_config

import ovirtsdk.api
Expand All @@ -39,6 +41,8 @@
testlib,
)

LOGGER = logging.getLogger(__name__)


class OvirtVirtEnv(lago.virt.VirtEnv):
def __init__(self, prefix, vm_specs, net_spec):
Expand Down Expand Up @@ -184,9 +188,11 @@ def get_api_v3(self):
self._api_v3 = self._get_api(api_ver=3)
return self._api_v3

def get_api_v4(self):
def get_api_v4(self, check=False):
if self._api_v4 is None or not self._api_v4.test():
self._api_v4 = self._get_api(api_ver=4)
if check and self._api_v4 is None:
raise RuntimeError('Could not connect to engine')
return self._api_v4

def add_iso(self, path):
Expand Down Expand Up @@ -219,33 +225,52 @@ def engine_setup(self, config=None):
if result.code != 0:
raise RuntimeError('Failed to setup the engine')

def stop_all_vms(self):
api = self.get_api_v4()
if api is None:
raise RuntimeError('Could not connect to engine')

def _search_vms(self, api, query):
vms_service = api.system_service().vms_service()
vms = vms_service.list(search='status=up')
if vms:
for v in vms:
vm_service = vms_service.vm_service(v.id)
vm_service.stop()
time.sleep(10)
return [vm.id for vm in vms_service.list(search=query)]

def _vm_is_down():
vm_srv = vms_service.vm_service(v.id)
vm_obj = vm_srv.get()
if vm_obj.status == sdk4.types.VmStatus.DOWN:
return True
def start_all_vms(self):
api = self.get_api_v4(check=True)
vms_service = api.system_service().vms_service()
ids = self._search_vms(api, query='status=down')
[vms_service.vm_service(id).start() for id in ids]

def _vm_is_up(id):
vm_srv = vms_service.vm_service(id)
vm = vm_srv.get()
if vm.status == sdk4.types.VmStatus.UP:
LOGGER.debug('Engine VM ID %s, is UP', id)
return True

for id in ids:
testlib.assert_true_within(
functools.partial(
_vm_is_up, id=id
), timeout=5 * 60
)

for v in vms:
testlib.assert_true_within(_vm_is_down, timeout=5 * 60)
def stop_all_vms(self):
api = self.get_api_v4(check=True)
vms_service = api.system_service().vms_service()
ids = self._search_vms(api, query='status=up')
[vms_service.vm_service(id).stop() for id in ids]

def _vm_is_down(id):
vm_srv = vms_service.vm_service(id)
vm = vm_srv.get()
if vm.status == sdk4.types.VmStatus.DOWN:
LOGGER.debug('Engine VM ID %s, is down', id)
return True

for id in ids:
testlib.assert_true_within(
functools.partial(
_vm_is_down, id=id
), timeout=5 * 60
)

def stop_all_hosts(self):
api = self.get_api_v4()
if api is None:
raise RuntimeError('Could not connect to engine')

api = self.get_api_v4(check=True)
hosts_service = api.system_service().hosts_service()
hosts = hosts_service.list(search='status=up')
if hosts:
Expand All @@ -260,7 +285,6 @@ def _host_is_maint():
host_obj = h_service.get()
if host_obj.status == sdk4.types.HostStatus.MAINTENANCE:
return True

if host_obj.status == sdk4.types.HostStatus.NON_OPERATIONAL:
raise RuntimeError(
'Host %s is in non operational state' % h.name
Expand All @@ -276,10 +300,7 @@ def _host_is_maint():
testlib.assert_true_within(_host_is_maint, timeout=5 * 60)

def start_all_hosts(self):
api = self.get_api_v4()
if api is None:
raise RuntimeError('Could not connect to engine')

api = self.get_api_v4(check=True)
hosts_service = api.system_service().hosts_service()
hosts = hosts_service.list(search='status=maintenance')
if hosts:
Expand All @@ -300,18 +321,12 @@ def _host_is_up():
)
elif host_obj.status == sdk4.types.HostStatus.INSTALL_FAILED:
raise RuntimeError('Host %s installation failed' % h.name)
elif host_obj.status == sdk4.types.HostStatus.NON_RESPONSIVE:
raise RuntimeError(
'Host %s is in non responsive state' % h.name
)

for h in hosts:
testlib.assert_true_within(_host_is_up, timeout=5 * 60)

def status(self):
api = self.get_api_v4()
if api is None:
raise RuntimeError('Could not connect to engine')
api = self.get_api_v4(check=True)

sys_service = api.system_service().get()
print("Version: %s" % sys_service.product_info.version.full_version)
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ lago.plugins.ovirt.cli =
collect=ovirtlago.cmd:do_ovirt_collect
deploy=ovirtlago.cmd:do_deploy
engine-setup=ovirtlago.cmd:do_ovirt_engine_setup
start=ovirtlago.cmd:do_ovirt_start
stop=ovirtlago.cmd:do_ovirt_stop
start-hosts=ovirtlago.cmd:do_ovirt_start_hosts
stop-hosts=ovirtlago.cmd:do_ovirt_stop_hosts
stop-vms=ovirtlago.cmd:do_ovirt_stop_vms
start-vms=ovirtlago.cmd:do_ovirt_start_vms
status=ovirtlago.cmd:do_ovirt_status
reposetup=ovirtlago.cmd:do_ovirt_reposetup
runtest=ovirtlago.cmd:do_ovirt_runtest
Expand Down

0 comments on commit 8d9888a

Please sign in to comment.