blob: c7a3e218c4304d1b6a1c3a4685f4703fbdaee27f [file] [log] [blame]
A R Karthick07608ef2016-08-23 16:51:19 -07001#
2# Copyright 2016-present Ciena Corporation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import os, sys
17import json
18import platform
19import subprocess
20from apiclient.maas_client import MAASOAuth, MAASDispatcher, MAASClient
21from paramiko import SSHClient, WarningPolicy, AutoAddPolicy
22from CordTestServer import CORD_TEST_HOST
23
24class FabricMAAS(object):
25 head_node = os.getenv('HEAD_NODE', CORD_TEST_HOST)
26 maas_url = 'http://{}/MAAS/api/1.0/'.format(head_node)
27
28 def __init__(self, api_key = None, url = maas_url):
29 if api_key == None:
30 self.api_key = self.get_api_key()
31 else:
32 self.api_key = api_key
33 self.auth = MAASOAuth(*self.api_key.split(':'))
34 self.url = url
35 self.client = MAASClient(self.auth, MAASDispatcher(), self.url)
36
37 @classmethod
38 def get_api_key(cls):
39 api_key = os.getenv('MAAS_API_KEY', None)
40 if api_key:
41 return api_key
42 cmd = ['maas-region-admin', 'apikey', '--username=cord']
43 try:
44 p = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
45 except:
46 return 'UNKNOWN'
47 out, err = p.communicate()
48 if err:
49 raise Exception('Cannot get api key for MAAS')
50 return out.strip()
51
52 def get_node_list(self):
53 nodes = self.client.get(u'nodes/', 'list').read()
54 node_list = json.loads(nodes)
55 hosts = [ self.head_node ] + map(lambda n: n['hostname'], node_list)
56 return hosts
57
58class Fabric(object):
59 entropy = 1
60 simulation = False
61 def __init__(self, node_list, user = 'ubuntu', passwd = 'ubuntu', key_file = None, verbose = False):
62 self.cur_node = None
63 if Fabric.simulation:
64 self.cur_node = FabricMAAS.head_node
65 self.node_list = node_list
66 self.user = user
67 self.passwd = passwd
68 self.key_file = key_file
69 self.verbose = verbose
70 self.client = SSHClient()
71 self.client.load_system_host_keys()
72 self.client.set_missing_host_key_policy(AutoAddPolicy())
73
74 def run_cmd(self, node, neighbor, cmd, simulation = False):
75 if simulation is True:
76 Fabric.entropy = Fabric.entropy ^ 1
77 return bool(Fabric.entropy)
78 if node == self.cur_node:
79 res = os.system(cmd)
80 return res == 0
81 try:
82 self.client.connect(node, username = self.user, key_filename = self.key_file, timeout = 5)
83 except:
84 print('Unable to ssh to node %s for neighbor %s' %(node, neighbor))
85 return False
86 channel = self.client.get_transport().open_session()
87 channel.exec_command(cmd)
88 status = channel.recv_exit_status()
89 channel.close()
90 if self.verbose:
91 print('Cmd %s returned with status %d on node %s for neighbor %s' %(cmd, status, node, neighbor))
92 return status == 0
93
94 def ping_neighbor(self, node, neighbor):
95 cmd = 'ping -c 1 -w 2 {}'.format(neighbor)
96 return self.run_cmd(node, neighbor, cmd, Fabric.simulation)
97
98 def ping_neighbors(self):
99 result_map = []
100 for n in self.node_list:
101 for adj in self.node_list:
102 if adj == n:
103 continue
104 res = self.ping_neighbor(n, adj)
105 result_map.append((n,adj,res))
106
107 ##report
108 if self.verbose:
109 for node, neighbor, res in result_map:
110 print('Ping from node %s to neighbor %s returned %s\n' %(node, neighbor, res))
111
112 failed_nodes = filter(lambda f: f[2] == False, result_map)
113 return failed_nodes
114
115if __name__ == '__main__':
116 if len(sys.argv) > 1:
117 nodes_file = sys.argv[1]
118 with open(nodes_file, 'r') as fd:
119 nodes = json.load(fd)
120 node_list = nodes['node_list']
121 else:
122 m = FabricMAAS()
123 node_list = m.get_node_list()
124 print('Node list: %s' %node_list)
125 Fabric.simulation = True
126 fab = Fabric(node_list, verbose = False)
127 failed_nodes = fab.ping_neighbors()
128 if failed_nodes:
129 print('Failed nodes: %s' %failed_nodes)
130 for node, neighbor, _ in failed_nodes:
131 print('Ping from node %s to neighbor %s Failed' %(node, neighbor))
132 else:
133 print('Fabric test between nodes %s is successful' %node_list)