Add connection verification

This commit is contained in:
Ilya Shakhat 2016-08-09 23:01:15 +03:00
parent 45acf32684
commit 87d0a2c708
4 changed files with 90 additions and 33 deletions

View File

@ -29,6 +29,22 @@ from os_failures import utils
LOG = logging.getLogger(__name__)
STATUS_OK = 'OK'
STATUS_FAILED = 'FAILED'
STATUS_UNREACHABLE = 'UNREACHABLE'
STATUS_SKIPPED = 'SKIPPED'
DEFAULT_ERROR_STATUSES = [STATUS_FAILED, STATUS_UNREACHABLE]
class AnsibleExecutionException(Exception):
pass
class AnsibleExecutionUnreachable(AnsibleExecutionException):
pass
def _light_rec(result):
for r in result:
c = copy.deepcopy(r)
@ -63,19 +79,19 @@ class MyCallback(callback_pkg.CallbackBase):
def v2_runner_on_failed(self, result, ignore_errors=False):
super(MyCallback, self).v2_runner_on_failed(result)
self._store(result, 'FAILED')
self._store(result, STATUS_FAILED)
def v2_runner_on_ok(self, result):
super(MyCallback, self).v2_runner_on_ok(result)
self._store(result, 'OK')
self._store(result, STATUS_OK)
def v2_runner_on_skipped(self, result):
super(MyCallback, self).v2_runner_on_skipped(result)
self._store(result, 'SKIPPED')
self._store(result, STATUS_SKIPPED)
def v2_runner_on_unreachable(self, result):
super(MyCallback, self).v2_runner_on_unreachable(result)
self._store(result, 'UNREACHABLE')
self._store(result, STATUS_UNREACHABLE)
Options = namedtuple('Options',
@ -151,6 +167,26 @@ class AnsibleRunner(object):
return result
def execute(self, hosts, task):
def execute(self, hosts, task, raise_on_statuses=DEFAULT_ERROR_STATUSES):
task_play = {'hosts': hosts, 'tasks': [task]}
return self.run([task_play])
result = self.run([task_play])
if raise_on_statuses:
errors = []
only_unreachable = True
for r in result:
if r['status'] in raise_on_statuses:
if r['status'] != STATUS_UNREACHABLE:
only_unreachable = False
errors.append(r)
if errors:
msg = 'Execution failed: %s' % ', '.join((
'(host: %s, status: %s)' % (r['host'], r['status']))
for r in errors)
ek = (AnsibleExecutionUnreachable if only_unreachable
else AnsibleExecutionException)
raise ek(msg)
return result

View File

@ -8,6 +8,13 @@ class CloudManagement(object):
def __init__(self):
self.power_management = None
def set_power_management(self, power_management):
self.power_management = power_management
@abc.abstractmethod
def verify(self):
pass
@abc.abstractmethod
def get_nodes(self):
pass
@ -15,6 +22,3 @@ class CloudManagement(object):
@abc.abstractmethod
def get_service(self, name):
pass
def set_power_management(self, power_management):
self.power_management = power_management

View File

@ -1,8 +1,10 @@
import abc
import json
import random
import six
from os_failures.ansible import runner
from os_failures.api import cloud_manager
from os_failures.api import cloud_management
from os_failures.api import node_collection
from os_failures.api import service
@ -38,35 +40,45 @@ class FuelNodeCollection(node_collection.NodeCollection):
self.power_management.poweroff([n['mac'] for n in self.hosts])
@six.add_metaclass(abc.ABCMeta)
class FuelService(service.Service):
def __init__(self, cloud_management=None, power_management=None,
name=None):
def __init__(self, cloud_management=None, power_management=None):
self.cloud_management = cloud_management
self.power_management = power_management
self.name = name
def _get_hosts(self):
def _get_cloud_nodes(self, role):
cloud_hosts = self.cloud_management.get_cloud_hosts()
return [n for n in cloud_hosts if 'controller' in n['roles']]
return [n for n in cloud_hosts if role in n['roles']]
def get_cloud_nodes_ips(self, role):
return [n['ip'] for n in self._get_cloud_nodes(role=role)]
def get_fuel_nodes(self, role):
hosts = self._get_cloud_nodes(role=role)
return FuelNodeCollection(cloud_management=self.cloud_management,
power_management=self.power_management,
hosts=hosts)
class KeystoneService(FuelService):
def get_nodes(self):
if self.name == 'keystone-api':
hosts = self._get_hosts()
return FuelNodeCollection(cloud_management=self.cloud_management,
power_management=self.power_management,
hosts=hosts)
return self.get_fuel_nodes(role='controller')
def stop(self):
if self.name == 'keystone-api':
task = {
'command': 'service apache2 restart'
}
ips = [n['ip'] for n in self._get_hosts()]
print(self.cloud_management.execute_on_cloud(ips, task))
task = {
'command': 'service apache2 restart'
}
ips = self.get_cloud_nodes_ips(role='controller')
print(self.cloud_management.execute_on_cloud(ips, task))
class FuelManagement(cloud_manager.CloudManagement):
SERVICE_NAME_TO_CLASS = {
'keystone-api': KeystoneService,
}
class FuelManagement(cloud_management.CloudManagement):
def __init__(self, params):
super(FuelManagement, self).__init__()
@ -77,13 +89,15 @@ class FuelManagement(cloud_manager.CloudManagement):
self.master_node_executor = runner.AnsibleRunner(
remote_user=self.username)
hosts = self.get_cloud_hosts()
print(hosts)
self.cloud_executor = runner.AnsibleRunner(
remote_user=self.username,
ssh_common_args='-o ProxyCommand="ssh -W %%h:%%p %s@%s"' %
(self.username, self.master_node_address))
def verify(self):
hosts = self.get_cloud_hosts()
print(hosts)
task = {'command': 'hostname'}
host_addrs = [n['ip'] for n in hosts]
print(self.execute_on_cloud(host_addrs, task))
@ -106,6 +120,7 @@ class FuelManagement(cloud_manager.CloudManagement):
hosts=hosts)
def get_service(self, name):
return FuelService(cloud_management=self,
power_management=self.power_management,
name=name)
if name in SERVICE_NAME_TO_CLASS:
klazz = SERVICE_NAME_TO_CLASS[name]
return klazz(cloud_management=self,
power_management=self.power_management)

View File

@ -34,6 +34,8 @@ def main():
}
}
client = os_failures.build_client(cloud_config)
client.verify()
service = client.get_service(name='keystone-api')
print(service)
service.stop()