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 | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 17 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -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 | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 23 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 24 | # http://www.apache.org/licenses/LICENSE-2.0 |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 25 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -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 | # |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 32 | import os,time |
| 33 | import io |
| 34 | import json |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 35 | import yaml |
A.R Karthick | c4e474d | 2016-12-12 15:24:57 -0800 | [diff] [blame] | 36 | import errno |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 37 | import copy |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 38 | from pyroute2 import IPRoute |
A.R Karthick | c4e474d | 2016-12-12 15:24:57 -0800 | [diff] [blame] | 39 | from pyroute2.netlink import NetlinkError |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 40 | from itertools import chain |
| 41 | from nsenter import Namespace |
A R Karthick | 6f2ac6f | 2017-07-26 12:55:24 -0700 | [diff] [blame] | 42 | try: |
| 43 | from docker import APIClient as Client |
| 44 | except: |
| 45 | from docker import Client |
A R Karthick | 85eb186 | 2017-01-23 16:10:57 -0800 | [diff] [blame] | 46 | from docker import utils as dockerutils |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 47 | import shutil |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 48 | from OnosCtrl import OnosCtrl |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 49 | from OnosLog import OnosLog |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 50 | from OltConfig import OltConfig |
| 51 | from EapolAAA import radius_add_users, radius_restore_users |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 52 | from onosclidriver import OnosCliDriver |
A.R Karthick | c4e474d | 2016-12-12 15:24:57 -0800 | [diff] [blame] | 53 | from threadPool import ThreadPool |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 54 | from threading import Lock |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 55 | |
| 56 | class docker_netns(object): |
| 57 | |
| 58 | dckr = Client() |
| 59 | def __init__(self, name): |
| 60 | pid = int(self.dckr.inspect_container(name)['State']['Pid']) |
| 61 | if pid == 0: |
| 62 | raise Exception('no container named {0}'.format(name)) |
| 63 | self.pid = pid |
| 64 | |
| 65 | def __enter__(self): |
| 66 | pid = self.pid |
| 67 | if not os.path.exists('/var/run/netns'): |
| 68 | os.mkdir('/var/run/netns') |
| 69 | os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid)) |
| 70 | return str(pid) |
| 71 | |
| 72 | def __exit__(self, type, value, traceback): |
| 73 | pid = self.pid |
| 74 | os.unlink('/var/run/netns/{0}'.format(pid)) |
| 75 | |
| 76 | flatten = lambda l: chain.from_iterable(l) |
| 77 | |
| 78 | class Container(object): |
| 79 | dckr = Client() |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 80 | IMAGE_PREFIX = '' ##for saving global prefix for all test classes |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 81 | CONFIG_LOCK = Lock() |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 82 | |
| 83 | def __init__(self, name, image, prefix='', tag = 'candidate', command = 'bash', quagga_config = None): |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 84 | self.name = name |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 85 | self.prefix = prefix |
| 86 | if prefix: |
| 87 | self.prefix += '/' |
| 88 | image = '{}{}'.format(self.prefix, image) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 89 | self.image = image |
| 90 | self.tag = tag |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 91 | if tag: |
| 92 | self.image_name = image + ':' + tag |
| 93 | else: |
| 94 | self.image_name = image |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 95 | self.id = None |
| 96 | self.command = command |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 97 | self.quagga_config = quagga_config |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 98 | |
| 99 | @classmethod |
| 100 | def build_image(cls, dockerfile, tag, force=True, nocache=False): |
| 101 | f = io.BytesIO(dockerfile.encode('utf-8')) |
| 102 | if force or not cls.image_exists(tag): |
| 103 | print('Build {0}...'.format(tag)) |
| 104 | for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache): |
| 105 | if 'stream' in line: |
| 106 | print(line['stream'].strip()) |
| 107 | |
| 108 | @classmethod |
| 109 | def image_exists(cls, name): |
A R Karthick | e07fc3a | 2017-02-27 10:49:29 -0800 | [diff] [blame] | 110 | #return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()] |
| 111 | return name in list( flatten(ctn['RepoTags'] if ctn['RepoTags'] else '' for ctn in cls.dckr.images()) ) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 112 | |
| 113 | @classmethod |
| 114 | def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False): |
| 115 | port_bindings = None |
| 116 | binds = None |
| 117 | if port_list: |
| 118 | port_bindings = {} |
| 119 | for p in port_list: |
A R Karthick | 184945a | 2017-07-25 17:23:57 -0700 | [diff] [blame] | 120 | if type(p) is tuple: |
| 121 | port_bindings[str(p[0])] = str(p[1]) |
| 122 | else: |
| 123 | port_bindings[str(p)] = str(p) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 124 | |
| 125 | if host_guest_map: |
| 126 | binds = [] |
| 127 | for h, g in host_guest_map: |
| 128 | binds.append('{0}:{1}'.format(h, g)) |
| 129 | |
| 130 | return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged) |
| 131 | |
| 132 | @classmethod |
A R Karthick | 85eb186 | 2017-01-23 16:10:57 -0800 | [diff] [blame] | 133 | def connect_to_network(cls, name, network): |
| 134 | try: |
| 135 | cls.dckr.connect_container_to_network(name, network) |
A R Karthick | 85eb186 | 2017-01-23 16:10:57 -0800 | [diff] [blame] | 136 | except: |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 137 | connect_cmd = 'docker network connect %s %s' %(network, name) |
| 138 | os.system(connect_cmd) |
| 139 | return True |
A R Karthick | 85eb186 | 2017-01-23 16:10:57 -0800 | [diff] [blame] | 140 | |
| 141 | @classmethod |
| 142 | def create_network(cls, network, subnet = None, gateway = None): |
| 143 | ipam_config = None |
| 144 | if subnet is not None and gateway is not None: |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 145 | try: |
| 146 | ipam_pool = dockerutils.create_ipam_pool(subnet = subnet, gateway = gateway) |
| 147 | ipam_config = dockerutils.create_ipam_config(pool_configs = [ipam_pool]) |
| 148 | cls.dckr.create_network(network, driver='bridge', ipam = ipam_config) |
| 149 | except: |
| 150 | create_cmd = 'docker network create %s --subnet %s --gateway %s >/dev/null 2>&1' %(network, subnet, gateway) |
| 151 | os.system(create_cmd) |
A R Karthick | 85eb186 | 2017-01-23 16:10:57 -0800 | [diff] [blame] | 152 | |
| 153 | @classmethod |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 154 | def cleanup(cls, image): |
A R Karthick | 09b1f4e | 2016-05-12 14:31:50 -0700 | [diff] [blame] | 155 | cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True)) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 156 | for cnt in cnt_list: |
| 157 | print('Cleaning container %s' %cnt['Id']) |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 158 | if cnt.has_key('State') and cnt['State'] == 'running': |
A R Karthick | 09b1f4e | 2016-05-12 14:31:50 -0700 | [diff] [blame] | 159 | cls.dckr.kill(cnt['Id']) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 160 | cls.dckr.remove_container(cnt['Id'], force=True) |
| 161 | |
| 162 | @classmethod |
| 163 | def remove_container(cls, name, force=True): |
| 164 | try: |
| 165 | cls.dckr.remove_container(name, force = force) |
| 166 | except: pass |
| 167 | |
| 168 | def exists(self): |
| 169 | return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers())) |
| 170 | |
| 171 | def img_exists(self): |
A R Karthick | e07fc3a | 2017-02-27 10:49:29 -0800 | [diff] [blame] | 172 | #return self.image_name in [ctn['RepoTags'][0] if ctn['RepoTags'] else '' for ctn in self.dckr.images()] |
| 173 | return self.image_name in list( flatten(ctn['RepoTags'] if ctn['RepoTags'] else '' for ctn in self.dckr.images()) ) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 174 | |
A R Karthick | 7584457 | 2017-01-23 16:57:44 -0800 | [diff] [blame] | 175 | def ip(self, network = None): |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 176 | cnt_list = filter(lambda c: c['Names'][0] == '/{}'.format(self.name), self.dckr.containers()) |
| 177 | #if not cnt_list: |
| 178 | # cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers()) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 179 | cnt_settings = cnt_list.pop() |
A R Karthick | 7584457 | 2017-01-23 16:57:44 -0800 | [diff] [blame] | 180 | if network is not None and cnt_settings['NetworkSettings']['Networks'].has_key(network): |
| 181 | return cnt_settings['NetworkSettings']['Networks'][network]['IPAddress'] |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 182 | return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress'] |
| 183 | |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 184 | @classmethod |
| 185 | def ips(cls, image_name): |
| 186 | cnt_list = filter(lambda c: c['Image'] == image_name, cls.dckr.containers()) |
| 187 | ips = [ cnt['NetworkSettings']['Networks']['bridge']['IPAddress'] for cnt in cnt_list ] |
| 188 | return ips |
| 189 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 190 | def kill(self, remove = True): |
| 191 | self.dckr.kill(self.name) |
| 192 | self.dckr.remove_container(self.name, force=True) |
| 193 | |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 194 | def start(self, rm = True, ports = None, volumes = None, host_config = None, |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 195 | environment = None, tty = False, stdin_open = True, |
| 196 | network_disabled = False, network = None): |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 197 | |
| 198 | if rm and self.exists(): |
| 199 | print('Removing container:', self.name) |
| 200 | self.dckr.remove_container(self.name, force=True) |
| 201 | |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 202 | ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command, |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 203 | detach=True, name=self.name, |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 204 | environment = environment, |
| 205 | volumes = volumes, |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 206 | host_config = host_config, stdin_open=stdin_open, tty = tty, |
| 207 | network_disabled = network_disabled) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 208 | self.dckr.start(container=self.name) |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 209 | if network_disabled is False: |
| 210 | if network is not None: |
| 211 | self.connect_to_network(self.name, network) |
| 212 | if self.quagga_config: |
| 213 | self.connect_to_br(index = 1) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 214 | self.id = ctn['Id'] |
| 215 | return ctn |
| 216 | |
Thangavelu K S | ef6f0a5 | 2016-12-14 19:57:05 +0000 | [diff] [blame] | 217 | @classmethod |
| 218 | def pause_container(cls, image, delay): |
| 219 | cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True)) |
| 220 | for cnt in cnt_list: |
| 221 | print('Pause the container %s' %cnt['Id']) |
| 222 | if cnt.has_key('State') and cnt['State'] == 'running': |
| 223 | cls.dckr.pause(cnt['Id']) |
| 224 | if delay != 0: |
| 225 | time.sleep(delay) |
| 226 | for cnt in cnt_list: |
| 227 | print('Unpause the container %s' %cnt['Id']) |
| 228 | cls.dckr.unpause(cnt['Id']) |
| 229 | else: |
| 230 | print('Infinity time pause the container %s' %cnt['Id']) |
| 231 | return 'success' |
| 232 | |
A R Karthick | 5241473 | 2017-01-31 09:59:47 -0800 | [diff] [blame] | 233 | def connect_to_br(self, index = 0): |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 234 | self.CONFIG_LOCK.acquire() |
| 235 | try: |
| 236 | with docker_netns(self.name) as pid: |
| 237 | for quagga_config in self.quagga_config: |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 238 | ip = IPRoute() |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 239 | br = ip.link_lookup(ifname=quagga_config['bridge']) |
| 240 | if len(br) == 0: |
| 241 | try: |
| 242 | ip.link_create(ifname=quagga_config['bridge'], kind='bridge') |
| 243 | except NetlinkError as e: |
| 244 | err, _ = e.args |
| 245 | if err == errno.EEXIST: |
| 246 | pass |
| 247 | else: |
| 248 | raise NetlinkError(*e.args) |
| 249 | br = ip.link_lookup(ifname=quagga_config['bridge']) |
| 250 | br = br[0] |
| 251 | ip.link('set', index=br, state='up') |
A R Karthick | 5241473 | 2017-01-31 09:59:47 -0800 | [diff] [blame] | 252 | ifname = '{0}-{1}'.format(self.name[:12], index) |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 253 | ifs = ip.link_lookup(ifname=ifname) |
| 254 | if len(ifs) > 0: |
| 255 | ip.link_remove(ifs[0]) |
| 256 | peer_ifname = '{0}-{1}'.format(pid, index) |
| 257 | ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname) |
| 258 | host = ip.link_lookup(ifname=ifname)[0] |
| 259 | ip.link('set', index=host, master=br) |
| 260 | ip.link('set', index=host, state='up') |
| 261 | guest = ip.link_lookup(ifname=peer_ifname)[0] |
| 262 | ip.link('set', index=guest, net_ns_fd=pid) |
| 263 | with Namespace(pid, 'net'): |
| 264 | ip = IPRoute() |
| 265 | ip.link('set', index=guest, ifname='eth{}'.format(index+1)) |
| 266 | ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask']) |
| 267 | ip.link('set', index=guest, state='up') |
| 268 | index += 1 |
| 269 | finally: |
| 270 | self.CONFIG_LOCK.release() |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 271 | |
Thangavelu K S | ef6f0a5 | 2016-12-14 19:57:05 +0000 | [diff] [blame] | 272 | def execute(self, cmd, tty = True, stream = False, shell = False, detach = True): |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 273 | res = 0 |
| 274 | if type(cmd) == str: |
| 275 | cmds = (cmd,) |
| 276 | else: |
| 277 | cmds = cmd |
| 278 | if shell: |
| 279 | for c in cmds: |
| 280 | res += os.system('docker exec {0} {1}'.format(self.name, c)) |
| 281 | return res |
| 282 | for c in cmds: |
| 283 | i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True) |
A R Karthick | d6dd9b2 | 2017-02-24 15:17:22 -0800 | [diff] [blame] | 284 | s = self.dckr.exec_start(i['Id'], stream = stream, detach=detach, socket=True) |
| 285 | try: |
| 286 | s.close() |
| 287 | except: pass |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 288 | result = self.dckr.exec_inspect(i['Id']) |
| 289 | res += 0 if result['ExitCode'] == None else result['ExitCode'] |
| 290 | return res |
| 291 | |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 292 | def restart(self, timeout =10): |
| 293 | return self.dckr.restart(self.name, timeout) |
| 294 | |
A R Karthick | c69d73e | 2017-01-20 11:44:34 -0800 | [diff] [blame] | 295 | def get_mem(jvm_heap_size = None, instances = 1): |
A R Karthick | 1f90820 | 2016-11-16 17:32:20 -0800 | [diff] [blame] | 296 | if instances <= 0: |
| 297 | instances = 1 |
A R Karthick | c69d73e | 2017-01-20 11:44:34 -0800 | [diff] [blame] | 298 | heap_size = jvm_heap_size |
| 299 | heap_size_i = 0 |
| 300 | #sanitize the heap size config |
| 301 | if heap_size is not None: |
| 302 | if not heap_size.isdigit(): |
| 303 | try: |
| 304 | heap_size_i = int(heap_size[:-1]) |
| 305 | suffix = heap_size[-1] |
| 306 | if suffix == 'M': |
| 307 | heap_size_i /= 1024 #convert to gigs |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 308 | #allow to specific minimum heap size |
| 309 | if heap_size_i == 0: |
| 310 | return heap_size |
A R Karthick | c69d73e | 2017-01-20 11:44:34 -0800 | [diff] [blame] | 311 | except: |
| 312 | ##invalid suffix length probably. Fall back to default |
| 313 | heap_size = None |
| 314 | else: |
| 315 | heap_size_i = int(heap_size) |
| 316 | |
Chetan Gaonker | 462d9fa | 2016-05-03 16:39:10 -0700 | [diff] [blame] | 317 | with open('/proc/meminfo', 'r') as fd: |
| 318 | meminfo = fd.readlines() |
| 319 | mem = 0 |
| 320 | for m in meminfo: |
| 321 | if m.startswith('MemTotal:') or m.startswith('SwapTotal:'): |
| 322 | mem += int(m.split(':')[1].strip().split()[0]) |
| 323 | |
A R Karthick | 1f90820 | 2016-11-16 17:32:20 -0800 | [diff] [blame] | 324 | mem = max(mem/1024/1024/2/instances, 1) |
Chetan Gaonker | 6d0a7b0 | 2016-05-03 16:57:28 -0700 | [diff] [blame] | 325 | mem = min(mem, 16) |
A R Karthick | c69d73e | 2017-01-20 11:44:34 -0800 | [diff] [blame] | 326 | |
| 327 | if heap_size_i: |
| 328 | #we take the minimum of the provided heap size and max allowed heap size |
| 329 | heap_size_i = min(heap_size_i, mem) |
| 330 | else: |
| 331 | heap_size_i = mem |
| 332 | |
| 333 | return '{}G'.format(heap_size_i) |
Chetan Gaonker | 462d9fa | 2016-05-03 16:39:10 -0700 | [diff] [blame] | 334 | |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 335 | class OnosCord(Container): |
| 336 | """Use this when running the cord tester agent on the onos compute node""" |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 337 | onos_config_dir_guest = '/root/onos/config' |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 338 | synchronizer_map = { 'vtn' : { 'install': |
A R Karthick | 36a7593 | 2017-10-25 14:40:42 -0700 | [diff] [blame] | 339 | ('http://mavenrepo:8080/repository/org/opencord/cord-config/1.4.0-SNAPSHOT/cord-config-1.4.0-SNAPSHOT.oar', |
A R Karthick | 4d4f9e6 | 2017-10-30 10:30:21 -0700 | [diff] [blame] | 340 | 'http://mavenrepo:8080/repository/org/opencord/vtn/1.3.0/vtn-1.3.0.oar', |
A R Karthick | 36a7593 | 2017-10-25 14:40:42 -0700 | [diff] [blame] | 341 | 'http://mavenrepo:8080/repository/org/opencord/vtn/1.4.0-SNAPSHOT/vtn-1.4.0-SNAPSHOT.oar',), |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 342 | 'activate': |
| 343 | ('org.onosproject.ovsdb-base', 'org.onosproject.drivers.ovsdb', |
| 344 | 'org.onosproject.dhcp', 'org.onosproject.optical-model', |
| 345 | 'org.onosproject.openflow-base', 'org.onosproject.proxyarp', |
| 346 | 'org.onosproject.hostprovider'), |
| 347 | }, |
| 348 | 'fabric' : { 'activate': |
| 349 | ('org.onosproject.hostprovider', 'org.onosproject.optical-model', |
| 350 | 'org.onosproject.openflow-base', 'org.onosproject.vrouter', |
| 351 | 'org.onosproject.netcfghostprovider', 'org.onosproject.netcfglinksprovider', |
| 352 | 'org.onosproject.segmentrouting', 'org.onosproject.proxyarp'), |
| 353 | } |
| 354 | } |
A R Karthick | 36a7593 | 2017-10-25 14:40:42 -0700 | [diff] [blame] | 355 | tester_apps = ('http://mavenrepo:8080/repository/org/opencord/aaa/1.4.0-SNAPSHOT/aaa-1.4.0-SNAPSHOT.oar', |
| 356 | 'http://mavenrepo:8080/repository/org/opencord/igmp/1.4.0-SNAPSHOT/igmp-1.4.0-SNAPSHOT.oar',) |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 357 | |
A.R Karthick | ddf1277 | 2017-05-17 13:49:47 -0700 | [diff] [blame] | 358 | old_service_profile = '/opt/cord/orchestration/service-profile/cord-pod' |
A R Karthick | 49529c5 | 2017-05-19 09:43:01 -0700 | [diff] [blame] | 359 | cord_profile = '/opt/cord_profile' |
A.R Karthick | ddf1277 | 2017-05-17 13:49:47 -0700 | [diff] [blame] | 360 | |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 361 | def __init__(self, onos_ip, conf, service_profile, synchronizer, start = True, boot_delay = 5): |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 362 | if not os.access(conf, os.F_OK): |
| 363 | raise Exception('ONOS cord configuration location %s is invalid' %conf) |
A.R Karthick | ddf1277 | 2017-05-17 13:49:47 -0700 | [diff] [blame] | 364 | self.old_cord = False |
| 365 | if os.access(self.old_service_profile, os.F_OK): |
| 366 | self.old_cord = True |
A R Karthick | bd9b8a3 | 2016-07-21 09:56:45 -0700 | [diff] [blame] | 367 | self.onos_ip = onos_ip |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 368 | self.onos_cord_dir = conf |
A R Karthick | bd9b8a3 | 2016-07-21 09:56:45 -0700 | [diff] [blame] | 369 | self.boot_delay = boot_delay |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 370 | self.synchronizer = synchronizer |
| 371 | self.service_profile = service_profile |
| 372 | self.docker_yaml = os.path.join(conf, 'docker-compose.yml') |
| 373 | self.docker_yaml_saved = os.path.join(conf, 'docker-compose.yml.saved') |
| 374 | self.onos_config_dir = os.path.join(conf, 'config') |
| 375 | self.onos_cfg_save_loc = os.path.join(conf, 'network-cfg.json.saved') |
| 376 | instance_active = False |
| 377 | #if we have a wrapper onos instance already active, back out |
| 378 | if os.access(self.onos_config_dir, os.F_OK) or os.access(self.docker_yaml_saved, os.F_OK): |
| 379 | instance_active = True |
| 380 | else: |
| 381 | if start is True: |
| 382 | os.mkdir(self.onos_config_dir) |
| 383 | shutil.copy(self.docker_yaml, self.docker_yaml_saved) |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 384 | |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 385 | self.start_wrapper = instance_active is False and start is True |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 386 | ##update the docker yaml with the config volume |
| 387 | with open(self.docker_yaml, 'r') as f: |
| 388 | yaml_config = yaml.load(f) |
| 389 | image = yaml_config['services'].keys()[0] |
A R Karthick | 8983cb0 | 2017-06-09 11:32:53 -0700 | [diff] [blame] | 390 | cord_conf_dir_basename = os.path.basename(self.onos_cord_dir.replace('-', '').replace('_', '')) |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 391 | xos_onos_name = '{}_{}_1'.format(cord_conf_dir_basename, image) |
A R Karthick | 5778a79 | 2017-01-31 13:47:16 -0800 | [diff] [blame] | 392 | if not yaml_config['services'][image].has_key('volumes'): |
| 393 | yaml_config['services'][image]['volumes'] = [] |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 394 | volumes = yaml_config['services'][image]['volumes'] |
| 395 | config_volumes = filter(lambda e: e.find(self.onos_config_dir_guest) >= 0, volumes) |
| 396 | if not config_volumes: |
| 397 | config_volume = '{}:{}'.format(self.onos_config_dir, self.onos_config_dir_guest) |
| 398 | volumes.append(config_volume) |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 399 | if self.start_wrapper: |
| 400 | docker_yaml_changed = '{}-changed'.format(self.docker_yaml) |
| 401 | with open(docker_yaml_changed, 'w') as wf: |
| 402 | yaml.dump(yaml_config, wf) |
| 403 | os.rename(docker_yaml_changed, self.docker_yaml) |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 404 | self.volumes = volumes |
| 405 | |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 406 | ##Create an container instance of xos onos |
A R Karthick | 5241473 | 2017-01-31 09:59:47 -0800 | [diff] [blame] | 407 | super(OnosCord, self).__init__(xos_onos_name, image, tag = '', quagga_config = Onos.QUAGGA_CONFIG) |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 408 | self.last_cfg = None |
| 409 | if self.start_wrapper: |
| 410 | #fetch the current config of onos cord instance and save it |
| 411 | try: |
| 412 | self.last_cfg = OnosCtrl.get_config(controller = onos_ip) |
| 413 | json_data = json.dumps(self.last_cfg, indent=4) |
| 414 | with open(self.onos_cfg_save_loc, 'w') as f: |
| 415 | f.write(json_data) |
| 416 | except: |
| 417 | pass |
| 418 | #start the container back with the shared onos config volume |
| 419 | self.start() |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 420 | |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 421 | def cliEnter(self): |
| 422 | retries = 0 |
| 423 | while retries < 30: |
| 424 | cli = OnosCliDriver(controller = self.onos_ip, connect = True) |
| 425 | if cli.handle: |
| 426 | return cli |
| 427 | else: |
| 428 | retries += 1 |
A R Karthick | 72fcbc5 | 2017-03-06 12:35:17 -0800 | [diff] [blame] | 429 | time.sleep(3) |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 430 | |
| 431 | return None |
| 432 | |
| 433 | def cliExit(self, cli): |
| 434 | if cli: |
| 435 | cli.disconnect() |
| 436 | |
A.R Karthick | ddf1277 | 2017-05-17 13:49:47 -0700 | [diff] [blame] | 437 | def synchronize_fabric(self, cfg = None): |
| 438 | if self.old_cord is True: |
| 439 | cmds = [ 'cd {} && make {}'.format(self.old_service_profile, self.synchronizer), |
| 440 | 'sleep 30' |
| 441 | ] |
| 442 | for cmd in cmds: |
| 443 | try: |
| 444 | os.system(cmd) |
| 445 | except: |
| 446 | pass |
| 447 | |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 448 | def synchronize_vtn(self, cfg = None): |
A.R Karthick | ddf1277 | 2017-05-17 13:49:47 -0700 | [diff] [blame] | 449 | if self.old_cord is True: |
| 450 | cmds = [ 'cd {} && make {}'.format(self.old_service_profile, self.synchronizer), |
| 451 | 'sleep 30' |
| 452 | ] |
| 453 | for cmd in cmds: |
| 454 | try: |
| 455 | os.system(cmd) |
| 456 | except: |
| 457 | pass |
| 458 | return |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 459 | if cfg is None: |
| 460 | return |
| 461 | if not cfg.has_key('apps'): |
| 462 | return |
| 463 | if not cfg['apps'].has_key('org.opencord.vtn'): |
| 464 | return |
| 465 | vtn_neutron_cfg = cfg['apps']['org.opencord.vtn']['cordvtn']['openstack'] |
| 466 | password = vtn_neutron_cfg['password'] |
| 467 | endpoint = vtn_neutron_cfg['endpoint'] |
| 468 | user = vtn_neutron_cfg['user'] |
| 469 | tenant = vtn_neutron_cfg['tenant'] |
| 470 | vtn_host = cfg['apps']['org.opencord.vtn']['cordvtn']['nodes'][0]['hostname'] |
| 471 | cli = self.cliEnter() |
| 472 | if cli is None: |
| 473 | return |
| 474 | cli.cordVtnSyncNeutronStates(endpoint, password, tenant = tenant, user = user) |
| 475 | time.sleep(2) |
| 476 | cli.cordVtnNodeInit(vtn_host) |
| 477 | self.cliExit(cli) |
| 478 | |
| 479 | def synchronize(self, cfg_unlink = False): |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 480 | |
| 481 | if not self.synchronizer_map.has_key(self.synchronizer): |
| 482 | return |
| 483 | |
| 484 | install_list = () |
| 485 | if self.synchronizer_map[self.synchronizer].has_key('install'): |
| 486 | install_list = self.synchronizer_map[self.synchronizer]['install'] |
| 487 | |
| 488 | activate_list = () |
| 489 | if self.synchronizer_map[self.synchronizer].has_key('activate'): |
| 490 | activate_list = self.synchronizer_map[self.synchronizer]['activate'] |
| 491 | |
| 492 | for app_url in install_list: |
| 493 | print('Installing app from url: %s' %app_url) |
| 494 | OnosCtrl.install_app_from_url(None, None, app_url = app_url, onos_ip = self.onos_ip) |
| 495 | |
| 496 | for app in activate_list: |
| 497 | print('Activating app %s' %app) |
| 498 | OnosCtrl(app, controller = self.onos_ip).activate() |
| 499 | time.sleep(2) |
| 500 | |
| 501 | for app_url in self.tester_apps: |
| 502 | print('Installing tester app from url: %s' %app_url) |
| 503 | OnosCtrl.install_app_from_url(None, None, app_url = app_url, onos_ip = self.onos_ip) |
| 504 | |
A R Karthick | 72fcbc5 | 2017-03-06 12:35:17 -0800 | [diff] [blame] | 505 | cfg = None |
| 506 | #restore the saved config after applications are activated |
| 507 | if os.access(self.onos_cfg_save_loc, os.F_OK): |
| 508 | with open(self.onos_cfg_save_loc, 'r') as f: |
| 509 | cfg = json.load(f) |
| 510 | try: |
| 511 | OnosCtrl.config(cfg, controller = self.onos_ip) |
| 512 | if cfg_unlink is True: |
| 513 | os.unlink(self.onos_cfg_save_loc) |
| 514 | except: |
| 515 | pass |
| 516 | |
| 517 | if hasattr(self, 'synchronize_{}'.format(self.synchronizer)): |
| 518 | getattr(self, 'synchronize_{}'.format(self.synchronizer))(cfg = cfg) |
| 519 | |
| 520 | #now restart the xos synchronizer container |
A R Karthick | 49529c5 | 2017-05-19 09:43:01 -0700 | [diff] [blame] | 521 | cmd = None |
| 522 | if os.access('{}/onboarding-docker-compose/docker-compose.yml'.format(self.cord_profile), os.F_OK): |
| 523 | cmd = 'cd {}/onboarding-docker-compose && \ |
| 524 | docker-compose -p {} restart xos_synchronizer_{}'.format(self.cord_profile, |
| 525 | self.service_profile, |
| 526 | self.synchronizer) |
| 527 | else: |
| 528 | if os.access('{}/docker-compose.yml'.format(self.cord_profile), os.F_OK): |
| 529 | cmd = 'cd {} && \ |
| 530 | docker-compose -p {} restart {}-synchronizer'.format(self.cord_profile, |
| 531 | self.service_profile, |
| 532 | self.synchronizer) |
| 533 | if cmd is not None: |
| 534 | try: |
| 535 | print(cmd) |
| 536 | os.system(cmd) |
| 537 | except: |
| 538 | pass |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 539 | |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 540 | def start(self, restart = False, network_cfg = None): |
A R Karthick | 928ad62 | 2017-01-30 12:18:32 -0800 | [diff] [blame] | 541 | if network_cfg is not None: |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 542 | json_data = json.dumps(network_cfg, indent=4) |
| 543 | with open('{}/network-cfg.json'.format(self.onos_config_dir), 'w') as f: |
| 544 | f.write(json_data) |
A R Karthick | 5241473 | 2017-01-31 09:59:47 -0800 | [diff] [blame] | 545 | |
| 546 | #we avoid using docker-compose restart for now. |
| 547 | #since we don't want to retain the metadata across restarts |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 548 | #stop and start and synchronize the services before installing tester cord apps |
| 549 | cmds = [ 'cd {} && docker-compose down'.format(self.onos_cord_dir), |
| 550 | 'cd {} && docker-compose up -d'.format(self.onos_cord_dir), |
A R Karthick | bc89437 | 2017-05-12 16:34:08 -0700 | [diff] [blame] | 551 | 'sleep 150', |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 552 | ] |
| 553 | for cmd in cmds: |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 554 | try: |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 555 | print(cmd) |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 556 | os.system(cmd) |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 557 | except:pass |
A R Karthick | 5241473 | 2017-01-31 09:59:47 -0800 | [diff] [blame] | 558 | |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 559 | self.synchronize() |
A R Karthick | 5241473 | 2017-01-31 09:59:47 -0800 | [diff] [blame] | 560 | ##we could also connect container to default docker network but disabled for now |
| 561 | #Container.connect_to_network(self.name, 'bridge') |
A R Karthick | 5241473 | 2017-01-31 09:59:47 -0800 | [diff] [blame] | 562 | #connect container to the quagga bridge |
| 563 | self.connect_to_br(index = 0) |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 564 | print('Waiting %d seconds for ONOS instance to start' %self.boot_delay) |
A R Karthick | bd9b8a3 | 2016-07-21 09:56:45 -0700 | [diff] [blame] | 565 | time.sleep(self.boot_delay) |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 566 | |
| 567 | def build_image(self): |
| 568 | build_cmd = 'cd {} && docker-compose build'.format(self.onos_cord_dir) |
| 569 | os.system(build_cmd) |
| 570 | |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 571 | def restore(self, force = False): |
| 572 | restore = self.start_wrapper is True or force is True |
| 573 | if not restore: |
A.R Karthick | 263d3fc | 2017-01-27 12:52:53 -0800 | [diff] [blame] | 574 | return |
A R Karthick | 394976f | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 575 | #nothing to restore |
| 576 | if not os.access(self.docker_yaml_saved, os.F_OK): |
| 577 | return |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 578 | |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 579 | #restore the config files back. The synchronizer restore should bring the last config back |
| 580 | cmds = ['cd {} && docker-compose down'.format(self.onos_cord_dir), |
| 581 | 'rm -rf {}'.format(self.onos_config_dir), |
| 582 | 'mv {} {}'.format(self.docker_yaml_saved, self.docker_yaml), |
| 583 | 'cd {} && docker-compose up -d'.format(self.onos_cord_dir), |
A R Karthick | bc89437 | 2017-05-12 16:34:08 -0700 | [diff] [blame] | 584 | 'sleep 150', |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 585 | ] |
| 586 | for cmd in cmds: |
A.R Karthick | b17e202 | 2017-01-27 11:29:26 -0800 | [diff] [blame] | 587 | try: |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 588 | print(cmd) |
| 589 | os.system(cmd) |
A.R Karthick | b17e202 | 2017-01-27 11:29:26 -0800 | [diff] [blame] | 590 | except: pass |
| 591 | |
A R Karthick | 03bd281 | 2017-03-03 17:49:17 -0800 | [diff] [blame] | 592 | self.synchronize(cfg_unlink = True) |
A.R Karthick | b17e202 | 2017-01-27 11:29:26 -0800 | [diff] [blame] | 593 | |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 594 | class OnosCordStopWrapper(Container): |
| 595 | onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord') |
| 596 | docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml') |
| 597 | |
| 598 | def __init__(self): |
| 599 | if os.access(self.docker_yaml, os.F_OK): |
| 600 | with open(self.docker_yaml, 'r') as f: |
| 601 | yaml_config = yaml.load(f) |
| 602 | image = yaml_config['services'].keys()[0] |
| 603 | name = 'cordtestercord_{}_1'.format(image) |
| 604 | super(OnosCordStopWrapper, self).__init__(name, image, tag = '') |
| 605 | if self.exists(): |
| 606 | print('Killing container %s' %self.name) |
| 607 | self.kill() |
| 608 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 609 | class Onos(Container): |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 610 | QUAGGA_CONFIG = [ { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, ] |
A R Karthick | a2492c1 | 2016-12-16 10:31:51 -0800 | [diff] [blame] | 611 | MAX_INSTANCES = 3 |
A R Karthick | c69d73e | 2017-01-20 11:44:34 -0800 | [diff] [blame] | 612 | JVM_HEAP_SIZE = None |
Chetan Gaonker | 462d9fa | 2016-05-03 16:39:10 -0700 | [diff] [blame] | 613 | SYSTEM_MEMORY = (get_mem(),) * 2 |
A R Karthick | a2492c1 | 2016-12-16 10:31:51 -0800 | [diff] [blame] | 614 | INSTANCE_MEMORY = (get_mem(instances=MAX_INSTANCES),) * 2 |
A R Karthick | c69d73e | 2017-01-20 11:44:34 -0800 | [diff] [blame] | 615 | JAVA_OPTS_FORMAT = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode' |
| 616 | JAVA_OPTS_DEFAULT = JAVA_OPTS_FORMAT.format(*SYSTEM_MEMORY) #-XX:+PrintGCDetails -XX:+PrintGCTimeStamps' |
| 617 | JAVA_OPTS_CLUSTER_DEFAULT = JAVA_OPTS_FORMAT.format(*INSTANCE_MEMORY) |
A R Karthick | cf1a5d3 | 2017-10-05 16:04:43 -0700 | [diff] [blame] | 618 | env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,vrouter,hostprovider', 'JAVA_OPTS' : JAVA_OPTS_DEFAULT } |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 619 | onos_cord_apps = ( ['cord-config', '1.2-SNAPSHOT', 'org.opencord.config'], |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 620 | ['sadis-app', '3.0-SNAPSHOT', 'org.opencord.sadis'], |
| 621 | ['olt-app', '1.2-SNAPSHOT', 'org.onosproject.olt'], |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 622 | ['aaa', '1.2-SNAPSHOT', 'org.opencord.aaa'], |
| 623 | ['igmp', '1.2-SNAPSHOT', 'org.opencord.igmp'], |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 624 | ) |
A R Karthick | b608d40 | 2017-06-02 11:48:41 -0700 | [diff] [blame] | 625 | cord_apps_version_updated = False |
A R Karthick | 184945a | 2017-07-25 17:23:57 -0700 | [diff] [blame] | 626 | expose_port = False |
| 627 | expose_ports = [ 8181, 8101, 9876, 6653, 6633, 2000, 2620, 5005 ] |
| 628 | ports = [] |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 629 | setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup') |
| 630 | host_config_dir = os.path.join(setup_dir, 'onos-config') |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 631 | guest_config_dir = '/root/onos/config' |
A.R Karthick | dda2206 | 2017-02-09 14:39:20 -0800 | [diff] [blame] | 632 | guest_data_dir = '/root/onos/apache-karaf-3.0.8/data' |
| 633 | guest_log_file = '/root/onos/apache-karaf-3.0.8/data/log/karaf.log' |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 634 | onos_gen_partitions = os.path.join(setup_dir, 'onos-gen-partitions') |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 635 | onos_form_cluster = os.path.join(setup_dir, 'onos-form-cluster') |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 636 | cord_apps_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'apps') |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 637 | host_guest_map = ( (host_config_dir, guest_config_dir), ) |
A R Karthick | d52ca8a | 2017-07-24 17:38:55 -0700 | [diff] [blame] | 638 | ssl_key = None |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 639 | cluster_cfg = os.path.join(host_config_dir, 'cluster.json') |
| 640 | cluster_mode = False |
| 641 | cluster_instances = [] |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 642 | NAME = 'cord-onos' |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 643 | ##the ip of ONOS in default cluster.json in setup/onos-config |
| 644 | CLUSTER_CFG_IP = '172.17.0.2' |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 645 | IMAGE = 'onosproject/onos' |
| 646 | TAG = 'latest' |
| 647 | PREFIX = '' |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 648 | |
| 649 | @classmethod |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 650 | def generate_cluster_cfg(cls, ip): |
| 651 | if type(ip) in [ list, tuple ]: |
| 652 | ips = ' '.join(ip) |
| 653 | else: |
| 654 | ips = ip |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 655 | try: |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 656 | cmd = '{} {} {}'.format(cls.onos_gen_partitions, cls.cluster_cfg, ips) |
| 657 | os.system(cmd) |
| 658 | except: pass |
| 659 | |
| 660 | @classmethod |
| 661 | def form_cluster(cls, ips): |
| 662 | nodes = ' '.join(ips) |
| 663 | try: |
| 664 | cmd = '{} {}'.format(cls.onos_form_cluster, nodes) |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 665 | os.system(cmd) |
| 666 | except: pass |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 667 | |
A R Karthick | 9d48c65 | 2016-09-15 09:16:36 -0700 | [diff] [blame] | 668 | @classmethod |
| 669 | def cleanup_runtime(cls): |
| 670 | '''Cleanup ONOS runtime generated files''' |
| 671 | files = ( Onos.cluster_cfg, os.path.join(Onos.host_config_dir, 'network-cfg.json') ) |
| 672 | for f in files: |
| 673 | if os.access(f, os.F_OK): |
| 674 | try: |
| 675 | os.unlink(f) |
| 676 | except: pass |
| 677 | |
A R Karthick | ec2db32 | 2016-11-17 15:06:01 -0800 | [diff] [blame] | 678 | @classmethod |
| 679 | def get_data_map(cls, host_volume, guest_volume_dir): |
| 680 | host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume)) |
| 681 | if not os.path.exists(host_volume_dir): |
| 682 | os.mkdir(host_volume_dir) |
| 683 | return ( (host_volume_dir, guest_volume_dir), ) |
| 684 | |
| 685 | @classmethod |
| 686 | def remove_data_map(cls, host_volume, guest_volume_dir): |
| 687 | host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume)) |
| 688 | if os.path.exists(host_volume_dir): |
A.R Karthick | f184b34 | 2017-01-27 19:30:50 -0800 | [diff] [blame] | 689 | shutil.rmtree(host_volume_dir) |
A R Karthick | ec2db32 | 2016-11-17 15:06:01 -0800 | [diff] [blame] | 690 | |
A R Karthick | 973010f | 2017-02-06 16:41:51 -0800 | [diff] [blame] | 691 | @classmethod |
| 692 | def update_data_dir(cls, karaf): |
| 693 | Onos.guest_data_dir = '/root/onos/apache-karaf-{}/data'.format(karaf) |
| 694 | Onos.guest_log_file = '/root/onos/apache-karaf-{}/data/log/karaf.log'.format(karaf) |
| 695 | |
A R Karthick | d52ca8a | 2017-07-24 17:38:55 -0700 | [diff] [blame] | 696 | @classmethod |
| 697 | def update_ssl_key(cls, key): |
| 698 | if os.access(key, os.F_OK): |
| 699 | try: |
| 700 | shutil.copy(key, cls.host_config_dir) |
| 701 | cls.ssl_key = os.path.join(cls.host_config_dir, os.path.basename(key)) |
| 702 | except:pass |
| 703 | |
A R Karthick | 184945a | 2017-07-25 17:23:57 -0700 | [diff] [blame] | 704 | @classmethod |
| 705 | def set_expose_port(cls, flag): |
| 706 | cls.expose_port = flag |
| 707 | |
| 708 | def get_port_map(self, instance=0): |
| 709 | if self.expose_port is False: |
| 710 | return self.ports |
| 711 | return map(lambda p: (p, p + instance), self.expose_ports) |
| 712 | |
A R Karthick | ec2db32 | 2016-11-17 15:06:01 -0800 | [diff] [blame] | 713 | def remove_data_volume(self): |
| 714 | if self.data_map is not None: |
| 715 | self.remove_data_map(*self.data_map) |
| 716 | |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 717 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG, |
A R Karthick | ec2db32 | 2016-11-17 15:06:01 -0800 | [diff] [blame] | 718 | boot_delay = 20, restart = False, network_cfg = None, |
A R Karthick | 85eb186 | 2017-01-23 16:10:57 -0800 | [diff] [blame] | 719 | cluster = False, data_volume = None, async = False, quagga_config = None, |
A R Karthick | 184945a | 2017-07-25 17:23:57 -0700 | [diff] [blame] | 720 | network = None, instance = 0): |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 721 | if restart is True: |
| 722 | ##Find the right image to restart |
| 723 | running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers()) |
| 724 | if running_image: |
| 725 | image_name = running_image[0]['Image'] |
| 726 | try: |
| 727 | image = image_name.split(':')[0] |
| 728 | tag = image_name.split(':')[1] |
| 729 | except: pass |
| 730 | |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 731 | if quagga_config is None: |
| 732 | quagga_config = Onos.QUAGGA_CONFIG |
| 733 | super(Onos, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = quagga_config) |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 734 | self.boot_delay = boot_delay |
A R Karthick | ec2db32 | 2016-11-17 15:06:01 -0800 | [diff] [blame] | 735 | self.data_map = None |
A R Karthick | c69d73e | 2017-01-20 11:44:34 -0800 | [diff] [blame] | 736 | instance_memory = (get_mem(jvm_heap_size = Onos.JVM_HEAP_SIZE, instances = Onos.MAX_INSTANCES),) * 2 |
| 737 | self.env['JAVA_OPTS'] = self.JAVA_OPTS_FORMAT.format(*instance_memory) |
A R Karthick | 184945a | 2017-07-25 17:23:57 -0700 | [diff] [blame] | 738 | self.ports = self.get_port_map(instance = instance) |
A R Karthick | d52ca8a | 2017-07-24 17:38:55 -0700 | [diff] [blame] | 739 | if self.ssl_key: |
| 740 | key_files = ( os.path.join(self.guest_config_dir, os.path.basename(self.ssl_key)), ) * 2 |
| 741 | self.env['JAVA_OPTS'] += ' -DenableOFTLS=true -Djavax.net.ssl.keyStore={} -Djavax.net.ssl.keyStorePassword=222222 -Djavax.net.ssl.trustStore={} -Djavax.net.ssl.trustStorePassword=222222'.format(*key_files) |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 742 | if cluster is True: |
A R Karthick | ec2db32 | 2016-11-17 15:06:01 -0800 | [diff] [blame] | 743 | if data_volume is not None: |
| 744 | self.data_map = self.get_data_map(data_volume, self.guest_data_dir) |
| 745 | self.host_guest_map = self.host_guest_map + self.data_map |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 746 | if os.access(self.cluster_cfg, os.F_OK): |
| 747 | try: |
| 748 | os.unlink(self.cluster_cfg) |
| 749 | except: pass |
| 750 | |
| 751 | self.host_config = self.create_host_config(port_list = self.ports, |
| 752 | host_guest_map = self.host_guest_map) |
| 753 | self.volumes = [] |
| 754 | for _,g in self.host_guest_map: |
| 755 | self.volumes.append(g) |
| 756 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 757 | if restart is True and self.exists(): |
| 758 | self.kill() |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 759 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 760 | if not self.exists(): |
| 761 | self.remove_container(name, force=True) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 762 | host_config = self.create_host_config(port_list = self.ports, |
| 763 | host_guest_map = self.host_guest_map) |
| 764 | volumes = [] |
| 765 | for _,g in self.host_guest_map: |
| 766 | volumes.append(g) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 767 | if network_cfg is not None: |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 768 | json_data = json.dumps(network_cfg, indent=4) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 769 | with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f: |
| 770 | f.write(json_data) |
A.R Karthick | c4e474d | 2016-12-12 15:24:57 -0800 | [diff] [blame] | 771 | if cluster is False or async is False: |
| 772 | print('Starting ONOS container %s' %self.name) |
| 773 | self.start(ports = self.ports, environment = self.env, |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 774 | host_config = self.host_config, volumes = self.volumes, tty = True, |
| 775 | network = Radius.NETWORK) |
A.R Karthick | c4e474d | 2016-12-12 15:24:57 -0800 | [diff] [blame] | 776 | if not restart: |
| 777 | ##wait a bit before fetching IP to regenerate cluster cfg |
| 778 | time.sleep(5) |
| 779 | ip = self.ip() |
| 780 | ##Just a quick hack/check to ensure we don't regenerate in the common case. |
| 781 | ##As ONOS is usually the first test container that is started |
| 782 | if cluster is False: |
| 783 | if ip != self.CLUSTER_CFG_IP or not os.access(self.cluster_cfg, os.F_OK): |
| 784 | print('Regenerating ONOS cluster cfg for ip %s' %ip) |
| 785 | self.generate_cluster_cfg(ip) |
| 786 | self.kill() |
| 787 | self.remove_container(self.name, force=True) |
| 788 | print('Restarting ONOS container %s' %self.name) |
| 789 | self.start(ports = self.ports, environment = self.env, |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 790 | host_config = self.host_config, volumes = self.volumes, tty = True, |
| 791 | network = Radius.NETWORK) |
A.R Karthick | c4e474d | 2016-12-12 15:24:57 -0800 | [diff] [blame] | 792 | print('Waiting for ONOS to boot') |
| 793 | time.sleep(boot_delay) |
| 794 | self.wait_for_onos_start(self.ip()) |
| 795 | self.running = True |
| 796 | else: |
| 797 | self.running = False |
| 798 | else: |
| 799 | self.running = True |
| 800 | if self.running: |
| 801 | self.ipaddr = self.ip() |
| 802 | if cluster is False: |
| 803 | self.install_cord_apps(self.ipaddr) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 804 | |
A.R Karthick | c4e474d | 2016-12-12 15:24:57 -0800 | [diff] [blame] | 805 | @classmethod |
| 806 | def get_quagga_config(cls, instance = 0): |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 807 | quagga_config = copy.deepcopy(cls.QUAGGA_CONFIG) |
A.R Karthick | c4e474d | 2016-12-12 15:24:57 -0800 | [diff] [blame] | 808 | if instance == 0: |
| 809 | return quagga_config |
| 810 | ip = quagga_config[0]['ip'] |
| 811 | octets = ip.split('.') |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 812 | octets[3] = str((int(octets[3]) + instance) & 255) |
A.R Karthick | c4e474d | 2016-12-12 15:24:57 -0800 | [diff] [blame] | 813 | ip = '.'.join(octets) |
| 814 | quagga_config[0]['ip'] = ip |
| 815 | return quagga_config |
| 816 | |
| 817 | @classmethod |
| 818 | def start_cluster_async(cls, onos_instances): |
| 819 | instances = filter(lambda o: o.running == False, onos_instances) |
| 820 | if not instances: |
| 821 | return |
| 822 | tpool = ThreadPool(len(instances), queue_size = 1, wait_timeout = 1) |
| 823 | for onos in instances: |
| 824 | tpool.addTask(onos.start_async) |
| 825 | tpool.cleanUpThreads() |
| 826 | |
| 827 | def start_async(self): |
| 828 | print('Starting ONOS container %s' %self.name) |
| 829 | self.start(ports = self.ports, environment = self.env, |
| 830 | host_config = self.host_config, volumes = self.volumes, tty = True) |
| 831 | time.sleep(3) |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 832 | self.ipaddr = self.ip() |
A.R Karthick | c4e474d | 2016-12-12 15:24:57 -0800 | [diff] [blame] | 833 | print('Waiting for ONOS container %s to start' %self.name) |
| 834 | self.wait_for_onos_start(self.ipaddr) |
| 835 | self.running = True |
| 836 | print('ONOS container %s started' %self.name) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 837 | |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 838 | @classmethod |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 839 | def wait_for_onos_start(cls, ip, tries = 30): |
A R Karthick | 973010f | 2017-02-06 16:41:51 -0800 | [diff] [blame] | 840 | onos_log = OnosLog(host = ip, log_file = Onos.guest_log_file) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 841 | num_tries = 0 |
| 842 | started = None |
| 843 | while not started and num_tries < tries: |
| 844 | time.sleep(3) |
| 845 | started = onos_log.search_log_pattern('ApplicationManager .* Started') |
| 846 | num_tries += 1 |
| 847 | |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 848 | if not started: |
| 849 | print('ONOS did not start') |
| 850 | else: |
| 851 | print('ONOS started') |
| 852 | return started |
| 853 | |
| 854 | @classmethod |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 855 | def setup_cluster_deprecated(cls, onos_instances, image_name = None): |
| 856 | if not onos_instances or len(onos_instances) < 2: |
| 857 | return |
| 858 | ips = [] |
| 859 | if image_name is not None: |
| 860 | ips = Container.ips(image_name) |
| 861 | else: |
| 862 | for onos in onos_instances: |
| 863 | ips.append(onos.ipaddr) |
| 864 | Onos.cluster_instances = onos_instances |
| 865 | Onos.cluster_mode = True |
| 866 | ##regenerate the cluster json with the 3 instance ips before restarting them back |
| 867 | print('Generating cluster cfg for ONOS instances with ips %s' %ips) |
| 868 | Onos.generate_cluster_cfg(ips) |
| 869 | for onos in onos_instances: |
| 870 | onos.kill() |
| 871 | onos.remove_container(onos.name, force=True) |
| 872 | print('Restarting ONOS container %s for forming cluster' %onos.name) |
| 873 | onos.start(ports = onos.ports, environment = onos.env, |
| 874 | host_config = onos.host_config, volumes = onos.volumes, tty = True) |
| 875 | print('Waiting %d seconds for ONOS %s to boot' %(onos.boot_delay, onos.name)) |
| 876 | time.sleep(onos.boot_delay) |
| 877 | onos.ipaddr = onos.ip() |
| 878 | onos.install_cord_apps(onos.ipaddr) |
| 879 | |
| 880 | @classmethod |
| 881 | def setup_cluster(cls, onos_instances, image_name = None): |
| 882 | if not onos_instances or len(onos_instances) < 2: |
| 883 | return |
| 884 | ips = [] |
| 885 | if image_name is not None: |
| 886 | ips = Container.ips(image_name) |
| 887 | else: |
| 888 | for onos in onos_instances: |
| 889 | ips.append(onos.ipaddr) |
| 890 | Onos.cluster_instances = onos_instances |
| 891 | Onos.cluster_mode = True |
| 892 | ##regenerate the cluster json with the 3 instance ips before restarting them back |
| 893 | print('Forming cluster for ONOS instances with ips %s' %ips) |
| 894 | Onos.form_cluster(ips) |
| 895 | ##wait for the cluster to be formed |
| 896 | print('Waiting for the cluster to be formed') |
| 897 | time.sleep(60) |
| 898 | for onos in onos_instances: |
| 899 | onos.install_cord_apps(onos.ipaddr) |
| 900 | |
| 901 | @classmethod |
A R Karthick | e2c24bd | 2016-10-07 14:51:38 -0700 | [diff] [blame] | 902 | def add_cluster(cls, count = 1, network_cfg = None): |
| 903 | if not cls.cluster_instances or Onos.cluster_mode is False: |
| 904 | return |
| 905 | for i in range(count): |
A R Karthick | 184945a | 2017-07-25 17:23:57 -0700 | [diff] [blame] | 906 | instance = len(cls.cluster_instances) |
| 907 | name = '{}-{}'.format(Onos.NAME, instance+1) |
A R Karthick | e2c24bd | 2016-10-07 14:51:38 -0700 | [diff] [blame] | 908 | onos = cls(name = name, image = Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX, |
A R Karthick | 184945a | 2017-07-25 17:23:57 -0700 | [diff] [blame] | 909 | cluster = True, network_cfg = network_cfg, instance = instance) |
A R Karthick | e2c24bd | 2016-10-07 14:51:38 -0700 | [diff] [blame] | 910 | cls.cluster_instances.append(onos) |
| 911 | |
| 912 | cls.setup_cluster(cls.cluster_instances) |
| 913 | |
| 914 | @classmethod |
A.R Karthick | 2560f04 | 2016-11-30 14:38:52 -0800 | [diff] [blame] | 915 | def restart_cluster(cls, network_cfg = None, timeout = 10, setup = False): |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 916 | if cls.cluster_mode is False: |
| 917 | return |
| 918 | if not cls.cluster_instances: |
| 919 | return |
| 920 | |
| 921 | if network_cfg is not None: |
| 922 | json_data = json.dumps(network_cfg, indent=4) |
| 923 | with open('{}/network-cfg.json'.format(cls.host_config_dir), 'w') as f: |
| 924 | f.write(json_data) |
| 925 | |
A.R Karthick | 2560f04 | 2016-11-30 14:38:52 -0800 | [diff] [blame] | 926 | cls.cleanup_cluster() |
| 927 | if timeout > 0: |
| 928 | time.sleep(timeout) |
| 929 | |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 930 | #start the instances asynchronously |
| 931 | cls.start_cluster_async(cls.cluster_instances) |
| 932 | time.sleep(5) |
A.R Karthick | 2560f04 | 2016-11-30 14:38:52 -0800 | [diff] [blame] | 933 | ##form the cluster as appropriate |
| 934 | if setup is True: |
| 935 | cls.setup_cluster(cls.cluster_instances) |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 936 | else: |
| 937 | for onos in cls.cluster_instances: |
| 938 | onos.install_cord_apps(onos.ipaddr) |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 939 | |
| 940 | @classmethod |
| 941 | def cluster_ips(cls): |
| 942 | if cls.cluster_mode is False: |
| 943 | return [] |
| 944 | if not cls.cluster_instances: |
| 945 | return [] |
| 946 | ips = [ onos.ipaddr for onos in cls.cluster_instances ] |
| 947 | return ips |
| 948 | |
| 949 | @classmethod |
| 950 | def cleanup_cluster(cls): |
| 951 | if cls.cluster_mode is False: |
| 952 | return |
| 953 | if not cls.cluster_instances: |
| 954 | return |
| 955 | for onos in cls.cluster_instances: |
| 956 | if onos.exists(): |
| 957 | onos.kill() |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 958 | onos.running = False |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 959 | onos.remove_container(onos.name, force=True) |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 960 | |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 961 | @classmethod |
A R Karthick | de6b9dc | 2016-11-29 17:46:16 -0800 | [diff] [blame] | 962 | def restart_node(cls, node = None, network_cfg = None, timeout = 10): |
A R Karthick | 889d965 | 2016-10-03 14:13:45 -0700 | [diff] [blame] | 963 | if node is None: |
| 964 | cls(restart = True, network_cfg = network_cfg, image = cls.IMAGE, tag = cls.TAG) |
| 965 | else: |
| 966 | #Restarts a node in the cluster |
| 967 | valid_node = filter(lambda onos: node in [ onos.ipaddr, onos.name ], cls.cluster_instances) |
| 968 | if valid_node: |
| 969 | onos = valid_node.pop() |
| 970 | if onos.exists(): |
| 971 | onos.kill() |
| 972 | onos.remove_container(onos.name, force=True) |
A R Karthick | de6b9dc | 2016-11-29 17:46:16 -0800 | [diff] [blame] | 973 | if timeout > 0: |
| 974 | time.sleep(timeout) |
A R Karthick | 889d965 | 2016-10-03 14:13:45 -0700 | [diff] [blame] | 975 | print('Restarting ONOS container %s' %onos.name) |
| 976 | onos.start(ports = onos.ports, environment = onos.env, |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 977 | host_config = onos.host_config, volumes = onos.volumes, tty = True, |
| 978 | network = Radius.NETWORK) |
A R Karthick | 889d965 | 2016-10-03 14:13:45 -0700 | [diff] [blame] | 979 | onos.ipaddr = onos.ip() |
A.R Karthick | 2560f04 | 2016-11-30 14:38:52 -0800 | [diff] [blame] | 980 | onos.wait_for_onos_start(onos.ipaddr) |
| 981 | onos.install_cord_apps(onos.ipaddr) |
A R Karthick | 889d965 | 2016-10-03 14:13:45 -0700 | [diff] [blame] | 982 | |
| 983 | @classmethod |
A R Karthick | b608d40 | 2017-06-02 11:48:41 -0700 | [diff] [blame] | 984 | def cliEnter(cls, onos_ip = None): |
| 985 | retries = 0 |
| 986 | while retries < 10: |
| 987 | cli = OnosCliDriver(controller = onos_ip, connect = True) |
| 988 | if cli.handle: |
| 989 | return cli |
| 990 | else: |
| 991 | retries += 1 |
| 992 | time.sleep(3) |
| 993 | |
| 994 | return None |
| 995 | |
| 996 | @classmethod |
| 997 | def cliExit(cls, cli): |
| 998 | if cli: |
| 999 | cli.disconnect() |
| 1000 | |
| 1001 | @classmethod |
| 1002 | def getVersion(cls, onos_ip = None): |
| 1003 | cli = cls.cliEnter(onos_ip = onos_ip) |
| 1004 | try: |
| 1005 | summary = json.loads(cli.summary(jsonFormat = True)) |
| 1006 | except: |
| 1007 | cls.cliExit(cli) |
| 1008 | return '1.8.0' |
| 1009 | cls.cliExit(cli) |
| 1010 | return summary['version'] |
| 1011 | |
| 1012 | @classmethod |
| 1013 | def update_cord_apps_version(cls, onos_ip = None): |
| 1014 | if cls.cord_apps_version_updated == True: |
| 1015 | return |
| 1016 | version = cls.getVersion(onos_ip = onos_ip) |
| 1017 | major = int(version.split('.')[0]) |
| 1018 | minor = int(version.split('.')[1]) |
A R Karthick | 5b8310e | 2017-09-01 13:55:15 -0700 | [diff] [blame] | 1019 | try: |
| 1020 | patch = int(version.split('.')[2]) |
| 1021 | except: |
| 1022 | patch = 0 |
A R Karthick | b608d40 | 2017-06-02 11:48:41 -0700 | [diff] [blame] | 1023 | app_version = '1.2-SNAPSHOT' |
| 1024 | if major > 1: |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 1025 | app_version = '3.0-SNAPSHOT' |
A R Karthick | 5b8310e | 2017-09-01 13:55:15 -0700 | [diff] [blame] | 1026 | elif major == 1 and minor >= 10: |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 1027 | app_version = '3.0-SNAPSHOT' |
A R Karthick | 5b8310e | 2017-09-01 13:55:15 -0700 | [diff] [blame] | 1028 | if patch < 3: |
| 1029 | app_version = '1.2-SNAPSHOT' |
A R Karthick | b608d40 | 2017-06-02 11:48:41 -0700 | [diff] [blame] | 1030 | for apps in cls.onos_cord_apps: |
| 1031 | apps[1] = app_version |
| 1032 | cls.cord_apps_version_updated = True |
| 1033 | |
| 1034 | @classmethod |
A R Karthick | eaf1c4e | 2016-07-19 12:22:35 -0700 | [diff] [blame] | 1035 | def install_cord_apps(cls, onos_ip = None): |
A R Karthick | b608d40 | 2017-06-02 11:48:41 -0700 | [diff] [blame] | 1036 | cls.update_cord_apps_version(onos_ip = onos_ip) |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 1037 | for app, version,_ in cls.onos_cord_apps: |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 1038 | app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version) |
A R Karthick | eaf1c4e | 2016-07-19 12:22:35 -0700 | [diff] [blame] | 1039 | ok, code = OnosCtrl.install_app(app_file, onos_ip = onos_ip) |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 1040 | ##app already installed (conflicts) |
| 1041 | if code in [ 409 ]: |
| 1042 | ok = True |
| 1043 | print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install')) |
| 1044 | time.sleep(2) |
| 1045 | |
A R Karthick | 2178298 | 2017-10-02 10:49:22 -0700 | [diff] [blame] | 1046 | OnosCtrl.config_olt_component(controller = onos_ip) |
| 1047 | |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 1048 | @classmethod |
| 1049 | def activate_apps(cls, apps, onos_ip = None, deactivate = False): |
| 1050 | for app in apps: |
| 1051 | if deactivate is True: |
| 1052 | OnosCtrl(app, controller = onos_ip).deactivate() |
| 1053 | time.sleep(2) |
| 1054 | OnosCtrl(app, controller = onos_ip).activate() |
| 1055 | |
| 1056 | time.sleep(5) |
| 1057 | |
| 1058 | @classmethod |
| 1059 | def activate_cord_apps(cls, onos_ip = None, deactivate = True): |
| 1060 | cord_apps = map(lambda a: a[2], cls.onos_cord_apps) |
| 1061 | cls.activate_apps(cord_apps, onos_ip = onos_ip, deactivate = deactivate) |
| 1062 | |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 1063 | class OnosStopWrapper(Container): |
| 1064 | def __init__(self, name): |
| 1065 | super(OnosStopWrapper, self).__init__(name, Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX) |
| 1066 | if self.exists(): |
| 1067 | self.kill() |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 1068 | self.running = False |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 1069 | else: |
| 1070 | if Onos.cluster_mode is True: |
| 1071 | valid_node = filter(lambda onos: name in [ onos.ipaddr, onos.name ], Onos.cluster_instances) |
| 1072 | if valid_node: |
| 1073 | onos = valid_node.pop() |
| 1074 | if onos.exists(): |
| 1075 | onos.kill() |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 1076 | onos.running = False |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 1077 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 1078 | class Radius(Container): |
| 1079 | ports = [ 1812, 1813 ] |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 1080 | env = {'TIMEZONE':'America/Los_Angeles', |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 1081 | 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password' |
| 1082 | } |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 1083 | host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db') |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 1084 | guest_db_dir = os.path.join(os.path.sep, 'opt', 'db') |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 1085 | host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius') |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 1086 | guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius') |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 1087 | start_command = os.path.join(guest_config_dir, 'start-radius.py') |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 1088 | host_guest_map = ( (host_db_dir, guest_db_dir), |
| 1089 | (host_config_dir, guest_config_dir) |
| 1090 | ) |
A R Karthick | f7a613b | 2017-02-24 09:36:44 -0800 | [diff] [blame] | 1091 | IMAGE = 'cordtest/radius' |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 1092 | NAME = 'cord-radius' |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 1093 | NETWORK = 'cord-radius-test' |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 1094 | SOCKET_SUBNET = '11.0.0.0/24' |
| 1095 | SOCKET_SUBNET_PREFIX = '11.0.0' |
| 1096 | SOCKET_GATEWAY = '11.0.0.1' |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 1097 | |
| 1098 | @classmethod |
| 1099 | def create_network(cls, name = NETWORK): |
| 1100 | try: |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 1101 | Container.create_network(name, subnet = cls.SOCKET_SUBNET, gateway = cls.SOCKET_GATEWAY) |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 1102 | except: |
| 1103 | pass |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 1104 | |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 1105 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate', |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 1106 | boot_delay = 10, restart = False, update = False, network = None, |
| 1107 | network_disabled = False, olt_config = ''): |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 1108 | super(Radius, self).__init__(name, image, prefix = prefix, tag = tag, command = self.start_command) |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 1109 | if update is True or not self.img_exists(): |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 1110 | self.build_image(self.image_name) |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 1111 | if restart is True and self.exists(): |
| 1112 | self.kill() |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 1113 | else: |
| 1114 | subscribers = 10 |
| 1115 | if olt_config: |
| 1116 | port_map, _ = OltConfig(olt_config).olt_port_map() |
| 1117 | if port_map: |
| 1118 | subscribers = port_map['num_ports'] * len(port_map['switch_port_list']) |
| 1119 | radius_restore_users() |
| 1120 | radius_add_users(subscribers) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 1121 | if not self.exists(): |
| 1122 | self.remove_container(name, force=True) |
| 1123 | host_config = self.create_host_config(port_list = self.ports, |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 1124 | host_guest_map = self.host_guest_map, |
| 1125 | privileged = True) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 1126 | volumes = [] |
| 1127 | for _,g in self.host_guest_map: |
| 1128 | volumes.append(g) |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 1129 | self.start(ports = self.ports, environment = self.env, |
| 1130 | volumes = volumes, |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 1131 | host_config = host_config, tty = True, network_disabled = network_disabled) |
| 1132 | if network_disabled is False: |
| 1133 | Container.connect_to_network(self.name, self.NETWORK) |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 1134 | time.sleep(boot_delay) |
| 1135 | |
| 1136 | @classmethod |
| 1137 | def build_image(cls, image): |
| 1138 | print('Building Radius image %s' %image) |
| 1139 | dockerfile = ''' |
| 1140 | FROM hbouvier/docker-radius |
| 1141 | MAINTAINER chetan@ciena.com |
| 1142 | LABEL RUN docker pull hbouvier/docker-radius |
| 1143 | LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius |
A R Karthick | c762df4 | 2016-05-25 10:09:21 -0700 | [diff] [blame] | 1144 | RUN apt-get update && \ |
| 1145 | apt-get -y install python python-pexpect strace |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 1146 | WORKDIR /root |
| 1147 | CMD ["/etc/freeradius/start-radius.py"] |
| 1148 | ''' |
| 1149 | super(Radius, cls).build_image(dockerfile, image) |
| 1150 | print('Done building image %s' %image) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 1151 | |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 1152 | class Quagga(Container): |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 1153 | QUAGGA_CONFIG = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 }, |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 1154 | { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 }, |
| 1155 | ) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 1156 | ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ] |
| 1157 | host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config') |
| 1158 | guest_quagga_config = '/root/config' |
| 1159 | quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf') |
| 1160 | host_guest_map = ( (host_quagga_config, guest_quagga_config), ) |
A R Karthick | f7a613b | 2017-02-24 09:36:44 -0800 | [diff] [blame] | 1161 | IMAGE = 'cordtest/quagga' |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 1162 | NAME = 'cord-quagga' |
| 1163 | |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 1164 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate', |
A R Karthick | 85eb186 | 2017-01-23 16:10:57 -0800 | [diff] [blame] | 1165 | boot_delay = 15, restart = False, config_file = quagga_config_file, update = False, |
| 1166 | network = None): |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 1167 | super(Quagga, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.QUAGGA_CONFIG) |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 1168 | if update is True or not self.img_exists(): |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 1169 | self.build_image(self.image_name) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 1170 | if restart is True and self.exists(): |
| 1171 | self.kill() |
| 1172 | if not self.exists(): |
| 1173 | self.remove_container(name, force=True) |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 1174 | host_config = self.create_host_config(port_list = self.ports, |
| 1175 | host_guest_map = self.host_guest_map, |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 1176 | privileged = True) |
| 1177 | volumes = [] |
| 1178 | for _,g in self.host_guest_map: |
| 1179 | volumes.append(g) |
| 1180 | self.start(ports = self.ports, |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 1181 | host_config = host_config, |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 1182 | volumes = volumes, tty = True) |
A R Karthick | 85eb186 | 2017-01-23 16:10:57 -0800 | [diff] [blame] | 1183 | if network is not None: |
| 1184 | Container.connect_to_network(self.name, network) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 1185 | print('Starting Quagga on container %s' %self.name) |
| 1186 | self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file)) |
| 1187 | time.sleep(boot_delay) |
| 1188 | |
| 1189 | @classmethod |
| 1190 | def build_image(cls, image): |
A R Karthick | aa54a1c | 2016-12-15 11:42:08 -0800 | [diff] [blame] | 1191 | onos_quagga_ip = Onos.QUAGGA_CONFIG[0]['ip'] |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 1192 | print('Building Quagga image %s' %image) |
| 1193 | dockerfile = ''' |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 1194 | FROM ubuntu:14.04 |
| 1195 | MAINTAINER chetan@ciena.com |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 1196 | WORKDIR /root |
| 1197 | RUN useradd -M quagga |
| 1198 | RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga |
| 1199 | RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga |
A R Karthick | 973ea69 | 2016-10-17 12:23:02 -0700 | [diff] [blame] | 1200 | RUN apt-get update && apt-get install -qy git autoconf libtool gawk make telnet libreadline6-dev pkg-config protobuf-c-compiler |
ChetanGaonker | b5b46c6 | 2016-08-16 12:02:53 -0700 | [diff] [blame] | 1201 | RUN git clone git://git.savannah.nongnu.org/quagga.git quagga && \ |
A R Karthick | 8f69c2c | 2016-10-21 11:43:26 -0700 | [diff] [blame] | 1202 | (cd quagga && git checkout quagga-1.0.20160315 && ./bootstrap.sh && \ |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 1203 | sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \ |
| 1204 | ./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install) |
| 1205 | RUN ldconfig |
| 1206 | '''.format(onos_quagga_ip) |
| 1207 | super(Quagga, cls).build_image(dockerfile, image) |
| 1208 | print('Done building image %s' %image) |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 1209 | |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 1210 | class QuaggaStopWrapper(Container): |
| 1211 | def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'candidate'): |
| 1212 | super(QuaggaStopWrapper, self).__init__(name, image, prefix = Container.IMAGE_PREFIX, tag = tag) |
| 1213 | if self.exists(): |
| 1214 | self.kill() |
| 1215 | |
| 1216 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 1217 | def reinitContainerClients(): |
| 1218 | docker_netns.dckr = Client() |
| 1219 | Container.dckr = Client() |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1220 | |
| 1221 | class Xos(Container): |
| 1222 | setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup') |
| 1223 | TAG = 'latest' |
| 1224 | PREFIX = '' |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1225 | host_guest_map = None |
| 1226 | env = None |
| 1227 | ports = None |
| 1228 | volumes = None |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1229 | |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1230 | @classmethod |
| 1231 | def get_cmd(cls, img_name): |
| 1232 | cmd = cls.dckr.inspect_image(img_name)['Config']['Cmd'] |
| 1233 | return ' '.join(cmd) |
| 1234 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1235 | def __init__(self, name, image, prefix = PREFIX, tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1236 | boot_delay = 20, restart = False, network_cfg = None, update = False): |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1237 | if restart is True: |
| 1238 | ##Find the right image to restart |
| 1239 | running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers()) |
| 1240 | if running_image: |
| 1241 | image_name = running_image[0]['Image'] |
| 1242 | try: |
| 1243 | image = image_name.split(':')[0] |
| 1244 | tag = image_name.split(':')[1] |
| 1245 | except: pass |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1246 | super(Xos, self).__init__(name, image, prefix = prefix, tag = tag) |
| 1247 | if update is True or not self.img_exists(): |
| 1248 | self.build_image(self.image_name) |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1249 | self.command = self.get_cmd(self.image_name).strip() or None |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1250 | if restart is True and self.exists(): |
| 1251 | self.kill() |
| 1252 | if not self.exists(): |
| 1253 | self.remove_container(name, force=True) |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1254 | host_config = self.create_host_config(port_list = self.ports, |
| 1255 | host_guest_map = self.host_guest_map, |
| 1256 | privileged = True) |
| 1257 | print('Starting XOS container %s' %self.name) |
| 1258 | self.start(ports = self.ports, environment = self.env, host_config = host_config, |
| 1259 | volumes = self.volumes, tty = True) |
| 1260 | print('Waiting %d seconds for XOS Base Container to boot' %(boot_delay)) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1261 | time.sleep(boot_delay) |
| 1262 | |
| 1263 | @classmethod |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1264 | def build_image(cls, image, dockerfile_path, image_target = 'build'): |
| 1265 | cmd = 'cd {} && make {}'.format(dockerfile_path, image_target) |
| 1266 | print('Building XOS %s' %image) |
| 1267 | res = os.system(cmd) |
| 1268 | print('Done building image %s. Image build %s' %(image, 'successful' if res == 0 else 'failed')) |
| 1269 | return res |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1270 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1271 | class XosServer(Xos): |
| 1272 | ports = [8000,9998,9999] |
| 1273 | NAME = 'xos-server' |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1274 | IMAGE = 'xosproject/xos' |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1275 | BASE_IMAGE = 'xosproject/xos-base' |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1276 | TAG = 'latest' |
| 1277 | PREFIX = '' |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1278 | dockerfile_path = os.path.join(Xos.setup_dir, 'xos') |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1279 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1280 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1281 | boot_delay = 10, restart = False, network_cfg = None, update = False): |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1282 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1283 | |
| 1284 | @classmethod |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1285 | def build_image(cls, image = IMAGE): |
| 1286 | ##build the base image and then build the server image |
| 1287 | Xos.build_image(cls.BASE_IMAGE, cls.dockerfile_path, image_target = 'base') |
| 1288 | Xos.build_image(image, cls.dockerfile_path) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1289 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1290 | class XosSynchronizerOpenstack(Xos): |
| 1291 | ports = [2375,] |
| 1292 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer') |
| 1293 | NAME = 'xos-synchronizer' |
| 1294 | IMAGE = 'xosproject/xos-synchronizer-openstack' |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1295 | TAG = 'latest' |
| 1296 | PREFIX = '' |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1297 | host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1298 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1299 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1300 | tag = TAG, boot_delay = 20, restart = False, network_cfg = None, update = False): |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1301 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1302 | |
| 1303 | @classmethod |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1304 | def build_image(cls, image = IMAGE): |
| 1305 | XosServer.build_image() |
| 1306 | Xos.build_image(image, cls.dockerfile_path) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1307 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1308 | class XosSynchronizerOnboarding(Xos): |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1309 | NAME = 'xos-synchronizer-onboarding' |
| 1310 | IMAGE = 'xosproject/xos-synchronizer-onboarding' |
| 1311 | TAG = 'latest' |
| 1312 | PREFIX = '' |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1313 | dockerfile_path = os.path.join(Xos.setup_dir, 'onboarding_synchronizer') |
| 1314 | host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1315 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1316 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1317 | tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False): |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1318 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1319 | |
| 1320 | @classmethod |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1321 | def build_image(cls, image = IMAGE): |
| 1322 | XosSynchronizerOpenstack.build_image() |
| 1323 | Xos.build_image(image, cls.dockerfile_path) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1324 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1325 | class XosSynchronizerOpenvpn(Xos): |
| 1326 | NAME = 'xos-synchronizer-openvpn' |
| 1327 | IMAGE = 'xosproject/xos-openvpn' |
| 1328 | TAG = 'latest' |
| 1329 | PREFIX = '' |
| 1330 | dockerfile_path = os.path.join(Xos.setup_dir, 'openvpn') |
| 1331 | host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1332 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1333 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1334 | tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False): |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1335 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 1336 | |
| 1337 | @classmethod |
| 1338 | def build_image(cls, image = IMAGE): |
| 1339 | XosSynchronizerOpenstack.build_image() |
| 1340 | Xos.build_image(image, cls.dockerfile_path) |
| 1341 | |
| 1342 | class XosPostgresql(Xos): |
| 1343 | ports = [5432,] |
| 1344 | NAME = 'xos-db-postgres' |
| 1345 | IMAGE = 'xosproject/xos-postgres' |
| 1346 | TAG = 'latest' |
| 1347 | PREFIX = '' |
| 1348 | volumes = ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"] |
| 1349 | dockerfile_path = os.path.join(Xos.setup_dir, 'postgresql') |
| 1350 | |
| 1351 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1352 | tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False): |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1353 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 1354 | |
| 1355 | @classmethod |
| 1356 | def build_image(cls, image = IMAGE): |
| 1357 | Xos.build_image(image, cls.dockerfile_path) |
| 1358 | |
| 1359 | class XosSyndicateMs(Xos): |
| 1360 | ports = [8080,] |
| 1361 | env = None |
| 1362 | NAME = 'xos-syndicate-ms' |
| 1363 | IMAGE = 'xosproject/syndicate-ms' |
| 1364 | TAG = 'latest' |
| 1365 | PREFIX = '' |
| 1366 | dockerfile_path = os.path.join(Xos.setup_dir, 'syndicate-ms') |
| 1367 | |
| 1368 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1369 | boot_delay = 10, restart = False, network_cfg = None, update = False): |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 1370 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 1371 | |
| 1372 | @classmethod |
| 1373 | def build_image(cls, image = IMAGE): |
| 1374 | Xos.build_image(image, cls.dockerfile_path) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 1375 | |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 1376 | class XosSyncVtn(Xos): |
| 1377 | ports = [8080,] |
| 1378 | env = None |
| 1379 | NAME = 'xos-synchronizer-vtn' |
| 1380 | IMAGE = 'xosproject/xos-synchronizer-vtn' |
| 1381 | TAG = 'latest' |
| 1382 | PREFIX = '' |
| 1383 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtn') |
| 1384 | |
| 1385 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1386 | boot_delay = 10, restart = False, network_cfg = None, update = False): |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 1387 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 1388 | |
| 1389 | @classmethod |
| 1390 | def build_image(cls, image = IMAGE): |
| 1391 | Xos.build_image(image, cls.dockerfile_path) |
| 1392 | |
| 1393 | class XosSyncVtr(Xos): |
| 1394 | ports = [8080,] |
| 1395 | env = None |
| 1396 | NAME = 'xos-synchronizer-vtr' |
| 1397 | IMAGE = 'xosproject/xos-synchronizer-vtr' |
| 1398 | TAG = 'latest' |
| 1399 | PREFIX = '' |
| 1400 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtr') |
| 1401 | |
| 1402 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1403 | boot_delay = 10, restart = False, network_cfg = None, update = False): |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 1404 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 1405 | |
| 1406 | @classmethod |
| 1407 | def build_image(cls, image = IMAGE): |
| 1408 | Xos.build_image(image, cls.dockerfile_path) |
| 1409 | |
| 1410 | class XosSyncVsg(Xos): |
| 1411 | ports = [8080,] |
| 1412 | env = None |
| 1413 | NAME = 'xos-synchronizer-vsg' |
| 1414 | IMAGE = 'xosproject/xos-synchronizer-vsg' |
| 1415 | TAG = 'latest' |
| 1416 | PREFIX = '' |
| 1417 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vsg') |
| 1418 | |
| 1419 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1420 | boot_delay = 10, restart = False, network_cfg = None, update = False): |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 1421 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 1422 | |
| 1423 | @classmethod |
| 1424 | def build_image(cls, image = IMAGE): |
| 1425 | Xos.build_image(image, cls.dockerfile_path) |
| 1426 | |
| 1427 | |
| 1428 | class XosSyncOnos(Xos): |
| 1429 | ports = [8080,] |
| 1430 | env = None |
| 1431 | NAME = 'xos-synchronizer-onos' |
| 1432 | IMAGE = 'xosproject/xos-synchronizer-onos' |
| 1433 | TAG = 'latest' |
| 1434 | PREFIX = '' |
| 1435 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-onos') |
| 1436 | |
| 1437 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1438 | boot_delay = 30, restart = False, network_cfg = None, update = False): |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 1439 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 1440 | |
| 1441 | @classmethod |
| 1442 | def build_image(cls, image = IMAGE): |
| 1443 | Xos.build_image(image, cls.dockerfile_path) |
| 1444 | |
| 1445 | class XosSyncFabric(Xos): |
| 1446 | ports = [8080,] |
| 1447 | env = None |
| 1448 | NAME = 'xos-synchronizer-fabric' |
| 1449 | IMAGE = 'xosproject/xos-synchronizer-fabric' |
| 1450 | TAG = 'latest' |
| 1451 | PREFIX = '' |
| 1452 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-fabric') |
| 1453 | |
| 1454 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 1455 | boot_delay = 30, restart = False, network_cfg = None, update = False): |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 1456 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 1457 | |
| 1458 | @classmethod |
| 1459 | def build_image(cls, image = IMAGE): |
| 1460 | Xos.build_image(image, cls.dockerfile_path) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 1461 | |
| 1462 | if __name__ == '__main__': |
| 1463 | onos = Onos(boot_delay = 10, restart = True) |