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