|
| 1 | +#!/usr/bin/python3 |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +import argparse |
| 5 | +import socket |
| 6 | +import requests |
| 7 | +import json |
| 8 | +import os.path |
| 9 | +from os import path |
| 10 | + |
| 11 | +VERSION = '1.0' |
| 12 | +POLYCUBED_ADDR = 'localhost' |
| 13 | +POLYCUBED_PORT = 9000 |
| 14 | +REQUESTS_TIMEOUT = 5 #seconds |
| 15 | + |
| 16 | +polycubed_endpoint = 'http://{}:{}/polycube/v1' |
| 17 | + |
| 18 | + |
| 19 | +def main(): |
| 20 | + global polycubed_endpoint |
| 21 | + args = parseArguments() |
| 22 | + |
| 23 | + addr = args['address'] |
| 24 | + port = args['port'] |
| 25 | + |
| 26 | + cube_name = args['cube_name'] |
| 27 | + interface_name = args['peer_interface'] |
| 28 | + path_to_dataplane = args['path_to_configuration'] |
| 29 | + |
| 30 | + dataplane = None |
| 31 | + |
| 32 | + if not path.isfile(path_to_dataplane): |
| 33 | + print(f'File {path_to_dataplane} does not exist.') |
| 34 | + exit(1) |
| 35 | + else: |
| 36 | + with open(path_to_dataplane) as json_file: |
| 37 | + dataplane = json.load(json_file) |
| 38 | + |
| 39 | + polycubed_endpoint = polycubed_endpoint.format(addr, port) |
| 40 | + |
| 41 | + checkConnectionToDaemon() |
| 42 | + |
| 43 | + already_exists, cube = checkIfServiceExists(cube_name) |
| 44 | + |
| 45 | + if already_exists: |
| 46 | + print(f'Dynmon {cube_name} already exist') |
| 47 | + attached_interface = cube['parent'] |
| 48 | + if attached_interface: |
| 49 | + if attached_interface != interface_name: |
| 50 | + detach_from_interface(cube_name, attached_interface) |
| 51 | + attach_to_interface(cube_name, interface_name) |
| 52 | + else: |
| 53 | + attach_to_interface(cube_name, interface_name) |
| 54 | + |
| 55 | + injectNewDataplane(cube_name, dataplane) |
| 56 | + |
| 57 | + else: |
| 58 | + createInstance(cube_name, dataplane) |
| 59 | + attach_to_interface(cube_name, interface_name) |
| 60 | + |
| 61 | + |
| 62 | +def checkConnectionToDaemon(): |
| 63 | + try: |
| 64 | + requests.get(polycubed_endpoint, timeout=REQUESTS_TIMEOUT) |
| 65 | + except requests.exceptions.ConnectionError: |
| 66 | + print('Connection error: unable to connect to polycube daemon.') |
| 67 | + exit(1) |
| 68 | + except requests.exceptions.Timeout: |
| 69 | + print('Timeout error: unable to connect to polycube daemon.') |
| 70 | + exit(1) |
| 71 | + except requests.exceptions.RequestException: |
| 72 | + print('Error: unable to connect to polycube daemon.') |
| 73 | + exit(1) |
| 74 | + |
| 75 | + |
| 76 | +def checkIfServiceExists(cube_name): |
| 77 | + try: |
| 78 | + response = requests.get(f'{polycubed_endpoint}/dynmon/{cube_name}', timeout=REQUESTS_TIMEOUT) |
| 79 | + response.raise_for_status() |
| 80 | + return True, json.loads(response.content) |
| 81 | + except requests.exceptions.HTTPError: |
| 82 | + return False, None |
| 83 | + except requests.exceptions.ConnectionError: |
| 84 | + print('Connection error: unable to connect to polycube daemon.') |
| 85 | + exit(1) |
| 86 | + except requests.exceptions.Timeout: |
| 87 | + print('Timeout error: unable to connect to polycube daemon.') |
| 88 | + exit(1) |
| 89 | + except requests.exceptions.RequestException: |
| 90 | + print('Error: unable to connect to polycube daemon.') |
| 91 | + exit(1) |
| 92 | + |
| 93 | + |
| 94 | +def injectNewDataplane(cube_name, dataplane): |
| 95 | + try: |
| 96 | + print('Injecting the new dataplane') |
| 97 | + response = requests.put(f'{polycubed_endpoint}/dynmon/{cube_name}/dataplane', |
| 98 | + json.dumps(dataplane), |
| 99 | + timeout=REQUESTS_TIMEOUT) |
| 100 | + response.raise_for_status() |
| 101 | + except requests.exceptions.HTTPError: |
| 102 | + print(f'Error: {response.content.decode("UTF-8")}') |
| 103 | + exit(1) |
| 104 | + except requests.exceptions.ConnectionError: |
| 105 | + print('Connection error: unable to connect to polycube daemon.') |
| 106 | + exit(1) |
| 107 | + except requests.exceptions.Timeout: |
| 108 | + print('Timeout error: unable to connect to polycube daemon.') |
| 109 | + exit(1) |
| 110 | + except requests.exceptions.RequestException: |
| 111 | + print('Error: unable to connect to polycube daemon.') |
| 112 | + exit(1) |
| 113 | + |
| 114 | + |
| 115 | +def createInstance(cube_name, dataplane): |
| 116 | + try: |
| 117 | + print(f'Creating new dynmon instance named {cube_name}') |
| 118 | + response = requests.put(f'{polycubed_endpoint}/dynmon/{cube_name}', |
| 119 | + json.dumps({'dataplane': dataplane}), |
| 120 | + timeout=REQUESTS_TIMEOUT) |
| 121 | + response.raise_for_status() |
| 122 | + except requests.exceptions.HTTPError: |
| 123 | + print(f'Error: {response.content.decode("UTF-8")}') |
| 124 | + exit(1) |
| 125 | + except requests.exceptions.ConnectionError: |
| 126 | + print('Connection error: unable to connect to polycube daemon.') |
| 127 | + exit(1) |
| 128 | + except requests.exceptions.Timeout: |
| 129 | + print('Timeout error: unable to connect to polycube daemon.') |
| 130 | + exit(1) |
| 131 | + except requests.exceptions.RequestException: |
| 132 | + print('Error: unable to connect to polycube daemon.') |
| 133 | + exit(1) |
| 134 | + |
| 135 | + |
| 136 | +def detach_from_interface(cube_name, interface): |
| 137 | + try: |
| 138 | + print(f'Detaching {cube_name} from {interface}') |
| 139 | + response = requests.post(f'{polycubed_endpoint}/detach', |
| 140 | + json.dumps({'cube': cube_name, 'port': interface}), |
| 141 | + timeout=REQUESTS_TIMEOUT) |
| 142 | + response.raise_for_status() |
| 143 | + except requests.exceptions.HTTPError: |
| 144 | + print(f'Error: {response.content.decode("UTF-8")}') |
| 145 | + exit(1) |
| 146 | + except requests.exceptions.ConnectionError: |
| 147 | + print('Connection error: unable to connect to polycube daemon.') |
| 148 | + exit(1) |
| 149 | + except requests.exceptions.Timeout: |
| 150 | + print('Timeout error: unable to connect to polycube daemon.') |
| 151 | + exit(1) |
| 152 | + except requests.exceptions.RequestException: |
| 153 | + print('Error: unable to connect to polycube daemon.') |
| 154 | + exit(1) |
| 155 | + |
| 156 | + |
| 157 | +def attach_to_interface(cube_name, interface): |
| 158 | + try: |
| 159 | + print(f'Attaching {cube_name} to {interface}') |
| 160 | + response = requests.post(f'{polycubed_endpoint}/attach', |
| 161 | + json.dumps({'cube': cube_name, 'port': interface}), |
| 162 | + timeout=REQUESTS_TIMEOUT) |
| 163 | + response.raise_for_status() |
| 164 | + except requests.exceptions.HTTPError: |
| 165 | + print(f'Error: {response.content.decode("UTF-8")}') |
| 166 | + exit(1) |
| 167 | + except requests.exceptions.ConnectionError: |
| 168 | + print('Connection error: unable to connect to polycube daemon.') |
| 169 | + exit(1) |
| 170 | + except requests.exceptions.Timeout: |
| 171 | + print('Timeout error: unable to connect to polycube daemon.') |
| 172 | + exit(1) |
| 173 | + except requests.exceptions.RequestException: |
| 174 | + print('Error: unable to connect to polycube daemon.') |
| 175 | + exit(1) |
| 176 | + |
| 177 | + |
| 178 | +def parseArguments(): |
| 179 | + parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 180 | + parser.add_argument('cube_name', help='indicates the name of the cube', type=str) |
| 181 | + parser.add_argument('peer_interface', help='indicates the network interface to connect the cube to', type=str) |
| 182 | + parser.add_argument('path_to_configuration', |
| 183 | + help='''indicates the path to the json file which contains the new dataplane configuration |
| 184 | + which contains the new dataplane code and the metadata associated to the exported metrics''', |
| 185 | + type=str) |
| 186 | + parser.add_argument('-a', '--address', help='set the polycube daemon ip address', type=str, default=POLYCUBED_ADDR) |
| 187 | + parser.add_argument('-p', '--port', help='set the polycube daemon port', type=int, default=POLYCUBED_PORT) |
| 188 | + parser.add_argument('-v', '--version', action='version', version=showVersion()) |
| 189 | + return parser.parse_args().__dict__ |
| 190 | + |
| 191 | + |
| 192 | +def showVersion(): |
| 193 | + return '%(prog)s - Version ' + VERSION |
| 194 | + |
| 195 | + |
| 196 | +if __name__ == '__main__': |
| 197 | + main() |
0 commit comments