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