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