Merge "Initial add of VES job file"
[releng.git] / modules / opnfv / deployment / apex / adapter.py
1 ##############################################################################
2 # Copyright (c) 2017 Ericsson AB and others.
3 # Author: Jose Lausuch (jose.lausuch@ericsson.com)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import re
11
12 from opnfv.deployment import manager
13 from opnfv.utils import opnfv_logger as logger
14 from opnfv.utils import ssh_utils
15
16 logger = logger.Logger(__name__).getLogger()
17
18
19 class ApexAdapter(manager.DeploymentHandler):
20
21     def __init__(self, installer_ip, installer_user, pkey_file):
22         super(ApexAdapter, self).__init__(installer='apex',
23                                           installer_ip=installer_ip,
24                                           installer_user=installer_user,
25                                           installer_pwd=None,
26                                           pkey_file=pkey_file)
27
28     def get_nodes(self):
29         nodes = []
30         cmd = "source /home/stack/stackrc;openstack server list"
31         output = self.installer_node.run_cmd(cmd)
32         lines = output.rsplit('\n')
33         if len(lines) < 4:
34             logger.info("No nodes found in the deployment.")
35             return None
36
37         for line in lines:
38             roles = []
39             if any(x in line for x in ['-----', 'Networks']):
40                 continue
41             if 'controller' in line:
42                 roles.append(manager.Role.CONTROLLER)
43             if 'compute' in line:
44                 roles.append(manager.Role.COMPUTE)
45             if 'opendaylight' in line.lower():
46                 roles.append(manager.Role.ODL)
47
48             fields = line.split('|')
49             id = re.sub('[!| ]', '', fields[1]).encode()
50             name = re.sub('[!| ]', '', fields[2]).encode()
51             status_node = re.sub('[!| ]', '', fields[3]).encode().lower()
52             ip = re.sub('[!| ctlplane=]', '', fields[4]).encode()
53
54             ssh_client = None
55             if 'active' in status_node:
56                 status = manager.NodeStatus.STATUS_OK
57                 ssh_client = ssh_utils.get_ssh_client(hostname=ip,
58                                                       username='heat-admin',
59                                                       pkey_file=self.pkey_file)
60             elif 'error' in status_node:
61                 status = manager.NodeStatus.STATUS_ERROR
62             elif 'off' in status_node:
63                 status = manager.NodeStatus.STATUS_OFFLINE
64             else:
65                 status = manager.NodeStatus.STATUS_INACTIVE
66
67             node = manager.Node(id, ip, name, status, roles, ssh_client)
68             nodes.append(node)
69
70         return nodes
71
72     def get_openstack_version(self):
73         cmd = 'source overcloudrc;sudo nova-manage version'
74         result = self.installer_node.run_cmd(cmd)
75         return result
76
77     def get_sdn_version(self):
78         cmd_descr = ("sudo yum info opendaylight 2>/dev/null|"
79                      "grep Description|sed 's/^.*\: //'")
80         cmd_ver = ("sudo yum info opendaylight 2>/dev/null|"
81                    "grep Version|sed 's/^.*\: //'")
82         description = None
83         for node in self.nodes:
84             if node.is_controller():
85                 description = node.run_cmd(cmd_descr)
86                 version = node.run_cmd(cmd_ver)
87                 break
88
89         if description is None:
90             return None
91         else:
92             return description + ':' + version
93
94     def get_deployment_status(self):
95         cmd = 'source stackrc;openstack stack list|grep CREATE_COMPLETE'
96         result = self.installer_node.run_cmd(cmd)
97         if result is None or len(result) == 0:
98             return 'failed'
99         else:
100             return 'active'