blob: 5bad546314558a9205ccdedf7eff7da8b8f59d0e [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
A R Karthickab366442017-01-17 15:42:20 -080066 self.users = [ user ]
67 if 'vagrant' not in self.users:
68 self.users.append('vagrant')
69 if 'ubuntu' not in self.users:
70 self.users.append('ubuntu')
A R Karthick07608ef2016-08-23 16:51:19 -070071 self.passwd = passwd
72 self.key_file = key_file
73 self.verbose = verbose
74 self.client = SSHClient()
75 self.client.load_system_host_keys()
76 self.client.set_missing_host_key_policy(AutoAddPolicy())
77
78 def run_cmd(self, node, neighbor, cmd, simulation = False):
79 if simulation is True:
80 Fabric.entropy = Fabric.entropy ^ 1
81 return bool(Fabric.entropy)
82 if node == self.cur_node:
83 res = os.system(cmd)
84 return res == 0
A R Karthickab366442017-01-17 15:42:20 -080085 ssh_user = None
86 for user in self.users:
87 try:
88 self.client.connect(node, username = user, key_filename = self.key_file, timeout = 5)
89 ssh_user = user
90 break
91 except:
92 continue
93
94 if ssh_user is None:
A R Karthick07608ef2016-08-23 16:51:19 -070095 print('Unable to ssh to node %s for neighbor %s' %(node, neighbor))
96 return False
A R Karthickab366442017-01-17 15:42:20 -080097 else:
98 if self.verbose:
99 print('ssh connection to node %s with user %s' %(node, ssh_user))
A R Karthick07608ef2016-08-23 16:51:19 -0700100 channel = self.client.get_transport().open_session()
101 channel.exec_command(cmd)
102 status = channel.recv_exit_status()
103 channel.close()
104 if self.verbose:
105 print('Cmd %s returned with status %d on node %s for neighbor %s' %(cmd, status, node, neighbor))
106 return status == 0
107
108 def ping_neighbor(self, node, neighbor):
109 cmd = 'ping -c 1 -w 2 {}'.format(neighbor)
110 return self.run_cmd(node, neighbor, cmd, Fabric.simulation)
111
112 def ping_neighbors(self):
113 result_map = []
114 for n in self.node_list:
115 for adj in self.node_list:
116 if adj == n:
117 continue
118 res = self.ping_neighbor(n, adj)
119 result_map.append((n,adj,res))
120
121 ##report
122 if self.verbose:
123 for node, neighbor, res in result_map:
124 print('Ping from node %s to neighbor %s returned %s\n' %(node, neighbor, res))
125
126 failed_nodes = filter(lambda f: f[2] == False, result_map)
127 return failed_nodes
128
129if __name__ == '__main__':
130 if len(sys.argv) > 1:
131 nodes_file = sys.argv[1]
132 with open(nodes_file, 'r') as fd:
133 nodes = json.load(fd)
134 node_list = nodes['node_list']
135 else:
136 m = FabricMAAS()
137 node_list = m.get_node_list()
138 print('Node list: %s' %node_list)
A R Karthickab366442017-01-17 15:42:20 -0800139 key_file = os.getenv('SSH_KEY_FILE', None)
140 Fabric.simulation = True if key_file is None else False
141 fab = Fabric(node_list, verbose = True, key_file = key_file)
A R Karthick07608ef2016-08-23 16:51:19 -0700142 failed_nodes = fab.ping_neighbors()
143 if failed_nodes:
144 print('Failed nodes: %s' %failed_nodes)
145 for node, neighbor, _ in failed_nodes:
146 print('Ping from node %s to neighbor %s Failed' %(node, neighbor))
147 else:
148 print('Fabric test between nodes %s is successful' %node_list)