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 |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 20 | from pyroute2 import IPRoute |
| 21 | from itertools import chain |
| 22 | from nsenter import Namespace |
| 23 | from docker import Client |
| 24 | from shutil import copy |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 25 | from OnosCtrl import OnosCtrl |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 26 | |
| 27 | class docker_netns(object): |
| 28 | |
| 29 | dckr = Client() |
| 30 | def __init__(self, name): |
| 31 | pid = int(self.dckr.inspect_container(name)['State']['Pid']) |
| 32 | if pid == 0: |
| 33 | raise Exception('no container named {0}'.format(name)) |
| 34 | self.pid = pid |
| 35 | |
| 36 | def __enter__(self): |
| 37 | pid = self.pid |
| 38 | if not os.path.exists('/var/run/netns'): |
| 39 | os.mkdir('/var/run/netns') |
| 40 | os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid)) |
| 41 | return str(pid) |
| 42 | |
| 43 | def __exit__(self, type, value, traceback): |
| 44 | pid = self.pid |
| 45 | os.unlink('/var/run/netns/{0}'.format(pid)) |
| 46 | |
| 47 | flatten = lambda l: chain.from_iterable(l) |
| 48 | |
| 49 | class Container(object): |
| 50 | dckr = Client() |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 51 | IMAGE_PREFIX = '' ##for saving global prefix for all test classes |
| 52 | |
| 53 | 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] | 54 | self.name = name |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 55 | self.prefix = prefix |
| 56 | if prefix: |
| 57 | self.prefix += '/' |
| 58 | image = '{}{}'.format(self.prefix, image) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 59 | self.image = image |
| 60 | self.tag = tag |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 61 | if tag: |
| 62 | self.image_name = image + ':' + tag |
| 63 | else: |
| 64 | self.image_name = image |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 65 | self.id = None |
| 66 | self.command = command |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 67 | self.quagga_config = quagga_config |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 68 | |
| 69 | @classmethod |
| 70 | def build_image(cls, dockerfile, tag, force=True, nocache=False): |
| 71 | f = io.BytesIO(dockerfile.encode('utf-8')) |
| 72 | if force or not cls.image_exists(tag): |
| 73 | print('Build {0}...'.format(tag)) |
| 74 | for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache): |
| 75 | if 'stream' in line: |
| 76 | print(line['stream'].strip()) |
| 77 | |
| 78 | @classmethod |
| 79 | def image_exists(cls, name): |
| 80 | return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()] |
| 81 | |
| 82 | @classmethod |
| 83 | def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False): |
| 84 | port_bindings = None |
| 85 | binds = None |
| 86 | if port_list: |
| 87 | port_bindings = {} |
| 88 | for p in port_list: |
| 89 | port_bindings[str(p)] = str(p) |
| 90 | |
| 91 | if host_guest_map: |
| 92 | binds = [] |
| 93 | for h, g in host_guest_map: |
| 94 | binds.append('{0}:{1}'.format(h, g)) |
| 95 | |
| 96 | return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged) |
| 97 | |
| 98 | @classmethod |
| 99 | def cleanup(cls, image): |
A R Karthick | 09b1f4e | 2016-05-12 14:31:50 -0700 | [diff] [blame] | 100 | 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] | 101 | for cnt in cnt_list: |
| 102 | print('Cleaning container %s' %cnt['Id']) |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 103 | if cnt.has_key('State') and cnt['State'] == 'running': |
A R Karthick | 09b1f4e | 2016-05-12 14:31:50 -0700 | [diff] [blame] | 104 | cls.dckr.kill(cnt['Id']) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 105 | cls.dckr.remove_container(cnt['Id'], force=True) |
| 106 | |
| 107 | @classmethod |
| 108 | def remove_container(cls, name, force=True): |
| 109 | try: |
| 110 | cls.dckr.remove_container(name, force = force) |
| 111 | except: pass |
| 112 | |
| 113 | def exists(self): |
| 114 | return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers())) |
| 115 | |
| 116 | def img_exists(self): |
A R Karthick | 6d98a59 | 2016-08-24 15:16:46 -0700 | [diff] [blame] | 117 | 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] | 118 | |
| 119 | def ip(self): |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 120 | cnt_list = filter(lambda c: c['Names'][0] == '/{}'.format(self.name), self.dckr.containers()) |
| 121 | #if not cnt_list: |
| 122 | # 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] | 123 | cnt_settings = cnt_list.pop() |
| 124 | return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress'] |
| 125 | |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 126 | @classmethod |
| 127 | def ips(cls, image_name): |
| 128 | cnt_list = filter(lambda c: c['Image'] == image_name, cls.dckr.containers()) |
| 129 | ips = [ cnt['NetworkSettings']['Networks']['bridge']['IPAddress'] for cnt in cnt_list ] |
| 130 | return ips |
| 131 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 132 | def kill(self, remove = True): |
| 133 | self.dckr.kill(self.name) |
| 134 | self.dckr.remove_container(self.name, force=True) |
| 135 | |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 136 | def start(self, rm = True, ports = None, volumes = None, host_config = None, |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 137 | environment = None, tty = False, stdin_open = True): |
| 138 | |
| 139 | if rm and self.exists(): |
| 140 | print('Removing container:', self.name) |
| 141 | self.dckr.remove_container(self.name, force=True) |
| 142 | |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 143 | 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] | 144 | detach=True, name=self.name, |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 145 | environment = environment, |
| 146 | volumes = volumes, |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 147 | host_config = host_config, stdin_open=stdin_open, tty = tty) |
| 148 | self.dckr.start(container=self.name) |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 149 | if self.quagga_config: |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 150 | self.connect_to_br() |
| 151 | self.id = ctn['Id'] |
| 152 | return ctn |
| 153 | |
| 154 | def connect_to_br(self): |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 155 | index = 0 |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 156 | with docker_netns(self.name) as pid: |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 157 | for quagga_config in self.quagga_config: |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 158 | ip = IPRoute() |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 159 | br = ip.link_lookup(ifname=quagga_config['bridge']) |
| 160 | if len(br) == 0: |
Chetan Gaonker | 5a0fda3 | 2016-05-10 14:09:07 -0700 | [diff] [blame] | 161 | ip.link_create(ifname=quagga_config['bridge'], kind='bridge') |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 162 | br = ip.link_lookup(ifname=quagga_config['bridge']) |
| 163 | br = br[0] |
| 164 | ip.link('set', index=br, state='up') |
| 165 | ifname = '{0}-{1}'.format(self.name, index) |
| 166 | ifs = ip.link_lookup(ifname=ifname) |
| 167 | if len(ifs) > 0: |
| 168 | ip.link_remove(ifs[0]) |
| 169 | peer_ifname = '{0}-{1}'.format(pid, index) |
Chetan Gaonker | 5a0fda3 | 2016-05-10 14:09:07 -0700 | [diff] [blame] | 170 | ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname) |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 171 | host = ip.link_lookup(ifname=ifname)[0] |
| 172 | ip.link('set', index=host, master=br) |
| 173 | ip.link('set', index=host, state='up') |
| 174 | guest = ip.link_lookup(ifname=peer_ifname)[0] |
| 175 | ip.link('set', index=guest, net_ns_fd=pid) |
| 176 | with Namespace(pid, 'net'): |
| 177 | ip = IPRoute() |
| 178 | ip.link('set', index=guest, ifname='eth{}'.format(index+1)) |
| 179 | ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask']) |
| 180 | ip.link('set', index=guest, state='up') |
| 181 | index += 1 |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 182 | |
| 183 | def execute(self, cmd, tty = True, stream = False, shell = False): |
| 184 | res = 0 |
| 185 | if type(cmd) == str: |
| 186 | cmds = (cmd,) |
| 187 | else: |
| 188 | cmds = cmd |
| 189 | if shell: |
| 190 | for c in cmds: |
| 191 | res += os.system('docker exec {0} {1}'.format(self.name, c)) |
| 192 | return res |
| 193 | for c in cmds: |
| 194 | i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True) |
| 195 | self.dckr.exec_start(i['Id'], stream = stream, detach=True) |
| 196 | result = self.dckr.exec_inspect(i['Id']) |
| 197 | res += 0 if result['ExitCode'] == None else result['ExitCode'] |
| 198 | return res |
| 199 | |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 200 | def restart(self, timeout =10): |
| 201 | return self.dckr.restart(self.name, timeout) |
| 202 | |
Chetan Gaonker | 462d9fa | 2016-05-03 16:39:10 -0700 | [diff] [blame] | 203 | def get_mem(): |
| 204 | with open('/proc/meminfo', 'r') as fd: |
| 205 | meminfo = fd.readlines() |
| 206 | mem = 0 |
| 207 | for m in meminfo: |
| 208 | if m.startswith('MemTotal:') or m.startswith('SwapTotal:'): |
| 209 | mem += int(m.split(':')[1].strip().split()[0]) |
| 210 | |
Chetan Gaonker | c0421e8 | 2016-05-04 17:23:08 -0700 | [diff] [blame] | 211 | mem = max(mem/1024/1024/2, 1) |
Chetan Gaonker | 6d0a7b0 | 2016-05-03 16:57:28 -0700 | [diff] [blame] | 212 | mem = min(mem, 16) |
Chetan Gaonker | 462d9fa | 2016-05-03 16:39:10 -0700 | [diff] [blame] | 213 | return str(mem) + 'G' |
| 214 | |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 215 | class OnosCord(Container): |
| 216 | """Use this when running the cord tester agent on the onos compute node""" |
| 217 | onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord') |
| 218 | onos_config_dir_guest = '/root/onos/config' |
| 219 | onos_config_dir = os.path.join(onos_cord_dir, 'config') |
| 220 | docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml') |
| 221 | |
A R Karthick | bd9b8a3 | 2016-07-21 09:56:45 -0700 | [diff] [blame] | 222 | def __init__(self, onos_ip, conf, boot_delay = 60): |
| 223 | self.onos_ip = onos_ip |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 224 | self.cord_conf_dir = conf |
A R Karthick | bd9b8a3 | 2016-07-21 09:56:45 -0700 | [diff] [blame] | 225 | self.boot_delay = boot_delay |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 226 | if os.access(self.cord_conf_dir, os.F_OK) and not os.access(self.onos_cord_dir, os.F_OK): |
| 227 | os.mkdir(self.onos_cord_dir) |
| 228 | os.mkdir(self.onos_config_dir) |
| 229 | ##copy the config file from cord-tester-config |
| 230 | cmd = 'cp {}/* {}'.format(self.cord_conf_dir, self.onos_cord_dir) |
| 231 | os.system(cmd) |
| 232 | |
| 233 | ##update the docker yaml with the config volume |
| 234 | with open(self.docker_yaml, 'r') as f: |
| 235 | yaml_config = yaml.load(f) |
| 236 | image = yaml_config['services'].keys()[0] |
| 237 | name = 'cordtestercord_{}_1'.format(image) |
| 238 | volumes = yaml_config['services'][image]['volumes'] |
| 239 | config_volumes = filter(lambda e: e.find(self.onos_config_dir_guest) >= 0, volumes) |
| 240 | if not config_volumes: |
| 241 | config_volume = '{}:{}'.format(self.onos_config_dir, self.onos_config_dir_guest) |
| 242 | volumes.append(config_volume) |
| 243 | docker_yaml_changed = '{}-changed'.format(self.docker_yaml) |
| 244 | with open(docker_yaml_changed, 'w') as wf: |
| 245 | yaml.dump(yaml_config, wf) |
| 246 | |
| 247 | os.rename(docker_yaml_changed, self.docker_yaml) |
| 248 | self.volumes = volumes |
| 249 | |
| 250 | super(OnosCord, self).__init__(name, image, tag = '') |
| 251 | cord_conf_dir_basename = os.path.basename(self.cord_conf_dir.replace('-', '')) |
| 252 | self.xos_onos_name = '{}_{}_1'.format(cord_conf_dir_basename, image) |
| 253 | ##Create an container instance of xos onos |
| 254 | self.xos_onos = Container(self.xos_onos_name, image, tag = '') |
| 255 | |
| 256 | def start(self, restart = False, network_cfg = None): |
| 257 | if restart is True: |
| 258 | if self.exists(): |
| 259 | ##Kill the existing instance |
| 260 | print('Killing container %s' %self.name) |
| 261 | self.kill() |
| 262 | if self.xos_onos.exists(): |
| 263 | print('Killing container %s' %self.xos_onos.name) |
| 264 | self.xos_onos.kill() |
| 265 | |
| 266 | if network_cfg is not None: |
| 267 | json_data = json.dumps(network_cfg, indent=4) |
| 268 | with open('{}/network-cfg.json'.format(self.onos_config_dir), 'w') as f: |
| 269 | f.write(json_data) |
| 270 | |
| 271 | #start the container using docker-compose |
| 272 | cmd = 'cd {} && docker-compose up -d'.format(self.onos_cord_dir) |
| 273 | os.system(cmd) |
A R Karthick | bd9b8a3 | 2016-07-21 09:56:45 -0700 | [diff] [blame] | 274 | #Delay to make sure ONOS fully boots |
| 275 | time.sleep(self.boot_delay) |
| 276 | Onos.install_cord_apps(onos_ip = self.onos_ip) |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 277 | |
| 278 | def build_image(self): |
| 279 | build_cmd = 'cd {} && docker-compose build'.format(self.onos_cord_dir) |
| 280 | os.system(build_cmd) |
| 281 | |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 282 | class OnosCordStopWrapper(Container): |
| 283 | onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord') |
| 284 | docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml') |
| 285 | |
| 286 | def __init__(self): |
| 287 | if os.access(self.docker_yaml, os.F_OK): |
| 288 | with open(self.docker_yaml, 'r') as f: |
| 289 | yaml_config = yaml.load(f) |
| 290 | image = yaml_config['services'].keys()[0] |
| 291 | name = 'cordtestercord_{}_1'.format(image) |
| 292 | super(OnosCordStopWrapper, self).__init__(name, image, tag = '') |
| 293 | if self.exists(): |
| 294 | print('Killing container %s' %self.name) |
| 295 | self.kill() |
| 296 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 297 | class Onos(Container): |
| 298 | |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 299 | quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, ) |
Chetan Gaonker | 462d9fa | 2016-05-03 16:39:10 -0700 | [diff] [blame] | 300 | SYSTEM_MEMORY = (get_mem(),) * 2 |
| 301 | JAVA_OPTS = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'.format(*SYSTEM_MEMORY)#-XX:+PrintGCDetails -XX:+PrintGCTimeStamps' |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 302 | env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,vrouter', 'JAVA_OPTS' : JAVA_OPTS } |
| 303 | onos_cord_apps = ( ('cord-config', '1.0-SNAPSHOT'), |
| 304 | ('aaa', '1.0-SNAPSHOT'), |
| 305 | ('igmp', '1.0-SNAPSHOT'), |
A R Karthick | edab01c | 2016-09-08 14:05:44 -0700 | [diff] [blame] | 306 | #('vtn', '1.0-SNAPSHOT'), |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 307 | ) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 308 | ports = [ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ] |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 309 | setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup') |
| 310 | host_config_dir = os.path.join(setup_dir, 'onos-config') |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 311 | guest_config_dir = '/root/onos/config' |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 312 | onos_gen_partitions = os.path.join(setup_dir, 'onos-gen-partitions') |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 313 | onos_form_cluster = os.path.join(setup_dir, 'onos-form-cluster') |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 314 | 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] | 315 | host_guest_map = ( (host_config_dir, guest_config_dir), ) |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 316 | cluster_cfg = os.path.join(host_config_dir, 'cluster.json') |
| 317 | cluster_mode = False |
| 318 | cluster_instances = [] |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 319 | NAME = 'cord-onos' |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 320 | ##the ip of ONOS in default cluster.json in setup/onos-config |
| 321 | CLUSTER_CFG_IP = '172.17.0.2' |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 322 | IMAGE = 'onosproject/onos' |
| 323 | TAG = 'latest' |
| 324 | PREFIX = '' |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 325 | |
| 326 | @classmethod |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 327 | def generate_cluster_cfg(cls, ip): |
| 328 | if type(ip) in [ list, tuple ]: |
| 329 | ips = ' '.join(ip) |
| 330 | else: |
| 331 | ips = ip |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 332 | try: |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 333 | cmd = '{} {} {}'.format(cls.onos_gen_partitions, cls.cluster_cfg, ips) |
| 334 | os.system(cmd) |
| 335 | except: pass |
| 336 | |
| 337 | @classmethod |
| 338 | def form_cluster(cls, ips): |
| 339 | nodes = ' '.join(ips) |
| 340 | try: |
| 341 | cmd = '{} {}'.format(cls.onos_form_cluster, nodes) |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 342 | os.system(cmd) |
| 343 | except: pass |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 344 | |
A R Karthick | 9d48c65 | 2016-09-15 09:16:36 -0700 | [diff] [blame] | 345 | @classmethod |
| 346 | def cleanup_runtime(cls): |
| 347 | '''Cleanup ONOS runtime generated files''' |
| 348 | files = ( Onos.cluster_cfg, os.path.join(Onos.host_config_dir, 'network-cfg.json') ) |
| 349 | for f in files: |
| 350 | if os.access(f, os.F_OK): |
| 351 | try: |
| 352 | os.unlink(f) |
| 353 | except: pass |
| 354 | |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 355 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG, |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 356 | boot_delay = 60, restart = False, network_cfg = None, cluster = False): |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 357 | if restart is True: |
| 358 | ##Find the right image to restart |
| 359 | running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers()) |
| 360 | if running_image: |
| 361 | image_name = running_image[0]['Image'] |
| 362 | try: |
| 363 | image = image_name.split(':')[0] |
| 364 | tag = image_name.split(':')[1] |
| 365 | except: pass |
| 366 | |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 367 | super(Onos, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.quagga_config) |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 368 | self.boot_delay = boot_delay |
| 369 | if cluster is True: |
| 370 | self.ports = [] |
| 371 | if os.access(self.cluster_cfg, os.F_OK): |
| 372 | try: |
| 373 | os.unlink(self.cluster_cfg) |
| 374 | except: pass |
| 375 | |
| 376 | self.host_config = self.create_host_config(port_list = self.ports, |
| 377 | host_guest_map = self.host_guest_map) |
| 378 | self.volumes = [] |
| 379 | for _,g in self.host_guest_map: |
| 380 | self.volumes.append(g) |
| 381 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 382 | if restart is True and self.exists(): |
| 383 | self.kill() |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 384 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 385 | if not self.exists(): |
| 386 | self.remove_container(name, force=True) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 387 | host_config = self.create_host_config(port_list = self.ports, |
| 388 | host_guest_map = self.host_guest_map) |
| 389 | volumes = [] |
| 390 | for _,g in self.host_guest_map: |
| 391 | volumes.append(g) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 392 | if network_cfg is not None: |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 393 | json_data = json.dumps(network_cfg, indent=4) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 394 | with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f: |
| 395 | f.write(json_data) |
| 396 | print('Starting ONOS container %s' %self.name) |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 397 | self.start(ports = self.ports, environment = self.env, |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 398 | host_config = self.host_config, volumes = self.volumes, tty = True) |
A R Karthick | f2f4ca6 | 2016-08-17 10:34:08 -0700 | [diff] [blame] | 399 | if not restart: |
| 400 | ##wait a bit before fetching IP to regenerate cluster cfg |
| 401 | time.sleep(5) |
| 402 | ip = self.ip() |
| 403 | ##Just a quick hack/check to ensure we don't regenerate in the common case. |
| 404 | ##As ONOS is usually the first test container that is started |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 405 | if cluster is False: |
| 406 | if ip != self.CLUSTER_CFG_IP or not os.access(self.cluster_cfg, os.F_OK): |
| 407 | print('Regenerating ONOS cluster cfg for ip %s' %ip) |
| 408 | self.generate_cluster_cfg(ip) |
| 409 | self.kill() |
| 410 | self.remove_container(self.name, force=True) |
| 411 | print('Restarting ONOS container %s' %self.name) |
| 412 | self.start(ports = self.ports, environment = self.env, |
| 413 | host_config = self.host_config, volumes = self.volumes, tty = True) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 414 | print('Waiting %d seconds for ONOS to boot' %(boot_delay)) |
| 415 | time.sleep(boot_delay) |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 416 | self.ipaddr = self.ip() |
| 417 | if cluster is False: |
| 418 | self.install_cord_apps(self.ipaddr) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 419 | |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 420 | @classmethod |
| 421 | def setup_cluster_deprecated(cls, onos_instances, image_name = None): |
| 422 | if not onos_instances or len(onos_instances) < 2: |
| 423 | return |
| 424 | ips = [] |
| 425 | if image_name is not None: |
| 426 | ips = Container.ips(image_name) |
| 427 | else: |
| 428 | for onos in onos_instances: |
| 429 | ips.append(onos.ipaddr) |
| 430 | Onos.cluster_instances = onos_instances |
| 431 | Onos.cluster_mode = True |
| 432 | ##regenerate the cluster json with the 3 instance ips before restarting them back |
| 433 | print('Generating cluster cfg for ONOS instances with ips %s' %ips) |
| 434 | Onos.generate_cluster_cfg(ips) |
| 435 | for onos in onos_instances: |
| 436 | onos.kill() |
| 437 | onos.remove_container(onos.name, force=True) |
| 438 | print('Restarting ONOS container %s for forming cluster' %onos.name) |
| 439 | onos.start(ports = onos.ports, environment = onos.env, |
| 440 | host_config = onos.host_config, volumes = onos.volumes, tty = True) |
| 441 | print('Waiting %d seconds for ONOS %s to boot' %(onos.boot_delay, onos.name)) |
| 442 | time.sleep(onos.boot_delay) |
| 443 | onos.ipaddr = onos.ip() |
| 444 | onos.install_cord_apps(onos.ipaddr) |
| 445 | |
| 446 | @classmethod |
| 447 | def setup_cluster(cls, onos_instances, image_name = None): |
| 448 | if not onos_instances or len(onos_instances) < 2: |
| 449 | return |
| 450 | ips = [] |
| 451 | if image_name is not None: |
| 452 | ips = Container.ips(image_name) |
| 453 | else: |
| 454 | for onos in onos_instances: |
| 455 | ips.append(onos.ipaddr) |
| 456 | Onos.cluster_instances = onos_instances |
| 457 | Onos.cluster_mode = True |
| 458 | ##regenerate the cluster json with the 3 instance ips before restarting them back |
| 459 | print('Forming cluster for ONOS instances with ips %s' %ips) |
| 460 | Onos.form_cluster(ips) |
| 461 | ##wait for the cluster to be formed |
| 462 | print('Waiting for the cluster to be formed') |
| 463 | time.sleep(60) |
| 464 | for onos in onos_instances: |
| 465 | onos.install_cord_apps(onos.ipaddr) |
| 466 | |
| 467 | @classmethod |
A R Karthick | e2c24bd | 2016-10-07 14:51:38 -0700 | [diff] [blame] | 468 | def add_cluster(cls, count = 1, network_cfg = None): |
| 469 | if not cls.cluster_instances or Onos.cluster_mode is False: |
| 470 | return |
| 471 | for i in range(count): |
| 472 | name = '{}-{}'.format(Onos.NAME, len(cls.cluster_instances)+1) |
| 473 | onos = cls(name = name, image = Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX, |
| 474 | cluster = True, network_cfg = network_cfg) |
| 475 | cls.cluster_instances.append(onos) |
| 476 | |
| 477 | cls.setup_cluster(cls.cluster_instances) |
| 478 | |
| 479 | @classmethod |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 480 | def restart_cluster(cls, network_cfg = None): |
| 481 | if cls.cluster_mode is False: |
| 482 | return |
| 483 | if not cls.cluster_instances: |
| 484 | return |
| 485 | |
| 486 | if network_cfg is not None: |
| 487 | json_data = json.dumps(network_cfg, indent=4) |
| 488 | with open('{}/network-cfg.json'.format(cls.host_config_dir), 'w') as f: |
| 489 | f.write(json_data) |
| 490 | |
| 491 | for onos in cls.cluster_instances: |
| 492 | if onos.exists(): |
| 493 | onos.kill() |
| 494 | onos.remove_container(onos.name, force=True) |
| 495 | print('Restarting ONOS container %s' %onos.name) |
| 496 | onos.start(ports = onos.ports, environment = onos.env, |
| 497 | host_config = onos.host_config, volumes = onos.volumes, tty = True) |
| 498 | print('Waiting %d seconds for ONOS %s to boot' %(onos.boot_delay, onos.name)) |
| 499 | time.sleep(onos.boot_delay) |
| 500 | onos.ipaddr = onos.ip() |
| 501 | |
| 502 | ##form the cluster |
| 503 | cls.setup_cluster(cls.cluster_instances) |
| 504 | |
| 505 | @classmethod |
| 506 | def cluster_ips(cls): |
| 507 | if cls.cluster_mode is False: |
| 508 | return [] |
| 509 | if not cls.cluster_instances: |
| 510 | return [] |
| 511 | ips = [ onos.ipaddr for onos in cls.cluster_instances ] |
| 512 | return ips |
| 513 | |
| 514 | @classmethod |
| 515 | def cleanup_cluster(cls): |
| 516 | if cls.cluster_mode is False: |
| 517 | return |
| 518 | if not cls.cluster_instances: |
| 519 | return |
| 520 | for onos in cls.cluster_instances: |
| 521 | if onos.exists(): |
| 522 | onos.kill() |
| 523 | onos.remove_container(onos.name, force=True) |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 524 | |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 525 | @classmethod |
A R Karthick | 889d965 | 2016-10-03 14:13:45 -0700 | [diff] [blame] | 526 | def restart_node(cls, node = None, network_cfg = None): |
| 527 | if node is None: |
| 528 | cls(restart = True, network_cfg = network_cfg, image = cls.IMAGE, tag = cls.TAG) |
| 529 | else: |
| 530 | #Restarts a node in the cluster |
| 531 | valid_node = filter(lambda onos: node in [ onos.ipaddr, onos.name ], cls.cluster_instances) |
| 532 | if valid_node: |
| 533 | onos = valid_node.pop() |
| 534 | if onos.exists(): |
| 535 | onos.kill() |
| 536 | onos.remove_container(onos.name, force=True) |
| 537 | print('Restarting ONOS container %s' %onos.name) |
| 538 | onos.start(ports = onos.ports, environment = onos.env, |
| 539 | host_config = onos.host_config, volumes = onos.volumes, tty = True) |
| 540 | print('Waiting %d seconds for ONOS %s to boot' %(onos.boot_delay, onos.name)) |
| 541 | time.sleep(onos.boot_delay) |
| 542 | onos.ipaddr = onos.ip() |
| 543 | |
| 544 | @classmethod |
A R Karthick | eaf1c4e | 2016-07-19 12:22:35 -0700 | [diff] [blame] | 545 | def install_cord_apps(cls, onos_ip = None): |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 546 | for app, version in cls.onos_cord_apps: |
| 547 | app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version) |
A R Karthick | eaf1c4e | 2016-07-19 12:22:35 -0700 | [diff] [blame] | 548 | ok, code = OnosCtrl.install_app(app_file, onos_ip = onos_ip) |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 549 | ##app already installed (conflicts) |
| 550 | if code in [ 409 ]: |
| 551 | ok = True |
| 552 | print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install')) |
| 553 | time.sleep(2) |
| 554 | |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 555 | class OnosStopWrapper(Container): |
| 556 | def __init__(self, name): |
| 557 | super(OnosStopWrapper, self).__init__(name, Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX) |
| 558 | if self.exists(): |
| 559 | self.kill() |
| 560 | else: |
| 561 | if Onos.cluster_mode is True: |
| 562 | valid_node = filter(lambda onos: name in [ onos.ipaddr, onos.name ], Onos.cluster_instances) |
| 563 | if valid_node: |
| 564 | onos = valid_node.pop() |
| 565 | if onos.exists(): |
| 566 | onos.kill() |
| 567 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 568 | class Radius(Container): |
| 569 | ports = [ 1812, 1813 ] |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 570 | env = {'TIMEZONE':'America/Los_Angeles', |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 571 | 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password' |
| 572 | } |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 573 | 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] | 574 | guest_db_dir = os.path.join(os.path.sep, 'opt', 'db') |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 575 | 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] | 576 | guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius') |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 577 | start_command = os.path.join(guest_config_dir, 'start-radius.py') |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 578 | host_guest_map = ( (host_db_dir, guest_db_dir), |
| 579 | (host_config_dir, guest_config_dir) |
| 580 | ) |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 581 | IMAGE = 'cord-test/radius' |
| 582 | NAME = 'cord-radius' |
| 583 | |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 584 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate', |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 585 | boot_delay = 10, restart = False, update = False): |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 586 | 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] | 587 | if update is True or not self.img_exists(): |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 588 | self.build_image(self.image_name) |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 589 | if restart is True and self.exists(): |
| 590 | self.kill() |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 591 | if not self.exists(): |
| 592 | self.remove_container(name, force=True) |
| 593 | host_config = self.create_host_config(port_list = self.ports, |
| 594 | host_guest_map = self.host_guest_map) |
| 595 | volumes = [] |
| 596 | for _,g in self.host_guest_map: |
| 597 | volumes.append(g) |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 598 | self.start(ports = self.ports, environment = self.env, |
| 599 | volumes = volumes, |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 600 | host_config = host_config, tty = True) |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 601 | time.sleep(boot_delay) |
| 602 | |
| 603 | @classmethod |
| 604 | def build_image(cls, image): |
| 605 | print('Building Radius image %s' %image) |
| 606 | dockerfile = ''' |
| 607 | FROM hbouvier/docker-radius |
| 608 | MAINTAINER chetan@ciena.com |
| 609 | LABEL RUN docker pull hbouvier/docker-radius |
| 610 | LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius |
A R Karthick | c762df4 | 2016-05-25 10:09:21 -0700 | [diff] [blame] | 611 | RUN apt-get update && \ |
| 612 | apt-get -y install python python-pexpect strace |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 613 | WORKDIR /root |
| 614 | CMD ["/etc/freeradius/start-radius.py"] |
| 615 | ''' |
| 616 | super(Radius, cls).build_image(dockerfile, image) |
| 617 | print('Done building image %s' %image) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 618 | |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 619 | class Quagga(Container): |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 620 | quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 }, |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 621 | { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 }, |
| 622 | ) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 623 | ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ] |
| 624 | host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config') |
| 625 | guest_quagga_config = '/root/config' |
| 626 | quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf') |
| 627 | host_guest_map = ( (host_quagga_config, guest_quagga_config), ) |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 628 | IMAGE = 'cord-test/quagga' |
| 629 | NAME = 'cord-quagga' |
| 630 | |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 631 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate', |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 632 | boot_delay = 15, restart = False, config_file = quagga_config_file, update = False): |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 633 | 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] | 634 | if update is True or not self.img_exists(): |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 635 | self.build_image(self.image_name) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 636 | if restart is True and self.exists(): |
| 637 | self.kill() |
| 638 | if not self.exists(): |
| 639 | self.remove_container(name, force=True) |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 640 | host_config = self.create_host_config(port_list = self.ports, |
| 641 | host_guest_map = self.host_guest_map, |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 642 | privileged = True) |
| 643 | volumes = [] |
| 644 | for _,g in self.host_guest_map: |
| 645 | volumes.append(g) |
| 646 | self.start(ports = self.ports, |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 647 | host_config = host_config, |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 648 | volumes = volumes, tty = True) |
| 649 | print('Starting Quagga on container %s' %self.name) |
| 650 | self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file)) |
| 651 | time.sleep(boot_delay) |
| 652 | |
| 653 | @classmethod |
| 654 | def build_image(cls, image): |
Chetan Gaonker | 2a6601b | 2016-05-02 17:28:26 -0700 | [diff] [blame] | 655 | onos_quagga_ip = Onos.quagga_config[0]['ip'] |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 656 | print('Building Quagga image %s' %image) |
| 657 | dockerfile = ''' |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 658 | FROM ubuntu:14.04 |
| 659 | MAINTAINER chetan@ciena.com |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 660 | WORKDIR /root |
| 661 | RUN useradd -M quagga |
| 662 | RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga |
| 663 | RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga |
A R Karthick | 973ea69 | 2016-10-17 12:23:02 -0700 | [diff] [blame] | 664 | 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] | 665 | RUN git clone git://git.savannah.nongnu.org/quagga.git quagga && \ |
A R Karthick | 8f69c2c | 2016-10-21 11:43:26 -0700 | [diff] [blame] | 666 | (cd quagga && git checkout quagga-1.0.20160315 && ./bootstrap.sh && \ |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 667 | sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \ |
| 668 | ./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install) |
| 669 | RUN ldconfig |
| 670 | '''.format(onos_quagga_ip) |
| 671 | super(Quagga, cls).build_image(dockerfile, image) |
| 672 | print('Done building image %s' %image) |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 673 | |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 674 | class QuaggaStopWrapper(Container): |
| 675 | def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'candidate'): |
| 676 | super(QuaggaStopWrapper, self).__init__(name, image, prefix = Container.IMAGE_PREFIX, tag = tag) |
| 677 | if self.exists(): |
| 678 | self.kill() |
| 679 | |
| 680 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 681 | def reinitContainerClients(): |
| 682 | docker_netns.dckr = Client() |
| 683 | Container.dckr = Client() |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 684 | |
| 685 | class Xos(Container): |
| 686 | setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup') |
| 687 | TAG = 'latest' |
| 688 | PREFIX = '' |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 689 | host_guest_map = None |
| 690 | env = None |
| 691 | ports = None |
| 692 | volumes = None |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 693 | |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 694 | @classmethod |
| 695 | def get_cmd(cls, img_name): |
| 696 | cmd = cls.dckr.inspect_image(img_name)['Config']['Cmd'] |
| 697 | return ' '.join(cmd) |
| 698 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 699 | def __init__(self, name, image, prefix = PREFIX, tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 700 | boot_delay = 20, restart = False, network_cfg = None, update = False): |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 701 | if restart is True: |
| 702 | ##Find the right image to restart |
| 703 | running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers()) |
| 704 | if running_image: |
| 705 | image_name = running_image[0]['Image'] |
| 706 | try: |
| 707 | image = image_name.split(':')[0] |
| 708 | tag = image_name.split(':')[1] |
| 709 | except: pass |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 710 | super(Xos, self).__init__(name, image, prefix = prefix, tag = tag) |
| 711 | if update is True or not self.img_exists(): |
| 712 | self.build_image(self.image_name) |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 713 | self.command = self.get_cmd(self.image_name).strip() or None |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 714 | if restart is True and self.exists(): |
| 715 | self.kill() |
| 716 | if not self.exists(): |
| 717 | self.remove_container(name, force=True) |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 718 | host_config = self.create_host_config(port_list = self.ports, |
| 719 | host_guest_map = self.host_guest_map, |
| 720 | privileged = True) |
| 721 | print('Starting XOS container %s' %self.name) |
| 722 | self.start(ports = self.ports, environment = self.env, host_config = host_config, |
| 723 | volumes = self.volumes, tty = True) |
| 724 | print('Waiting %d seconds for XOS Base Container to boot' %(boot_delay)) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 725 | time.sleep(boot_delay) |
| 726 | |
| 727 | @classmethod |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 728 | def build_image(cls, image, dockerfile_path, image_target = 'build'): |
| 729 | cmd = 'cd {} && make {}'.format(dockerfile_path, image_target) |
| 730 | print('Building XOS %s' %image) |
| 731 | res = os.system(cmd) |
| 732 | print('Done building image %s. Image build %s' %(image, 'successful' if res == 0 else 'failed')) |
| 733 | return res |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 734 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 735 | class XosServer(Xos): |
| 736 | ports = [8000,9998,9999] |
| 737 | NAME = 'xos-server' |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 738 | IMAGE = 'xosproject/xos' |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 739 | BASE_IMAGE = 'xosproject/xos-base' |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 740 | TAG = 'latest' |
| 741 | PREFIX = '' |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 742 | dockerfile_path = os.path.join(Xos.setup_dir, 'xos') |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 743 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 744 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 745 | boot_delay = 10, restart = False, network_cfg = None, update = False): |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 746 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 747 | |
| 748 | @classmethod |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 749 | def build_image(cls, image = IMAGE): |
| 750 | ##build the base image and then build the server image |
| 751 | Xos.build_image(cls.BASE_IMAGE, cls.dockerfile_path, image_target = 'base') |
| 752 | Xos.build_image(image, cls.dockerfile_path) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 753 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 754 | class XosSynchronizerOpenstack(Xos): |
| 755 | ports = [2375,] |
| 756 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer') |
| 757 | NAME = 'xos-synchronizer' |
| 758 | IMAGE = 'xosproject/xos-synchronizer-openstack' |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 759 | TAG = 'latest' |
| 760 | PREFIX = '' |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 761 | host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 762 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 763 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 764 | 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] | 765 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 766 | |
| 767 | @classmethod |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 768 | def build_image(cls, image = IMAGE): |
| 769 | XosServer.build_image() |
| 770 | Xos.build_image(image, cls.dockerfile_path) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 771 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 772 | class XosSynchronizerOnboarding(Xos): |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 773 | NAME = 'xos-synchronizer-onboarding' |
| 774 | IMAGE = 'xosproject/xos-synchronizer-onboarding' |
| 775 | TAG = 'latest' |
| 776 | PREFIX = '' |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 777 | dockerfile_path = os.path.join(Xos.setup_dir, 'onboarding_synchronizer') |
| 778 | host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 779 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 780 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 781 | 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] | 782 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 783 | |
| 784 | @classmethod |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 785 | def build_image(cls, image = IMAGE): |
| 786 | XosSynchronizerOpenstack.build_image() |
| 787 | Xos.build_image(image, cls.dockerfile_path) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 788 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 789 | class XosSynchronizerOpenvpn(Xos): |
| 790 | NAME = 'xos-synchronizer-openvpn' |
| 791 | IMAGE = 'xosproject/xos-openvpn' |
| 792 | TAG = 'latest' |
| 793 | PREFIX = '' |
| 794 | dockerfile_path = os.path.join(Xos.setup_dir, 'openvpn') |
| 795 | host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 796 | |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 797 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 798 | 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] | 799 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 800 | |
| 801 | @classmethod |
| 802 | def build_image(cls, image = IMAGE): |
| 803 | XosSynchronizerOpenstack.build_image() |
| 804 | Xos.build_image(image, cls.dockerfile_path) |
| 805 | |
| 806 | class XosPostgresql(Xos): |
| 807 | ports = [5432,] |
| 808 | NAME = 'xos-db-postgres' |
| 809 | IMAGE = 'xosproject/xos-postgres' |
| 810 | TAG = 'latest' |
| 811 | PREFIX = '' |
| 812 | volumes = ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"] |
| 813 | dockerfile_path = os.path.join(Xos.setup_dir, 'postgresql') |
| 814 | |
| 815 | def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 816 | 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] | 817 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 818 | |
| 819 | @classmethod |
| 820 | def build_image(cls, image = IMAGE): |
| 821 | Xos.build_image(image, cls.dockerfile_path) |
| 822 | |
| 823 | class XosSyndicateMs(Xos): |
| 824 | ports = [8080,] |
| 825 | env = None |
| 826 | NAME = 'xos-syndicate-ms' |
| 827 | IMAGE = 'xosproject/syndicate-ms' |
| 828 | TAG = 'latest' |
| 829 | PREFIX = '' |
| 830 | dockerfile_path = os.path.join(Xos.setup_dir, 'syndicate-ms') |
| 831 | |
| 832 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 833 | boot_delay = 10, restart = False, network_cfg = None, update = False): |
A R Karthick | e3bde96 | 2016-09-27 15:06:35 -0700 | [diff] [blame] | 834 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 835 | |
| 836 | @classmethod |
| 837 | def build_image(cls, image = IMAGE): |
| 838 | Xos.build_image(image, cls.dockerfile_path) |
ChetanGaonker | 2c0e9bb | 2016-09-21 13:38:37 -0700 | [diff] [blame] | 839 | |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 840 | class XosSyncVtn(Xos): |
| 841 | ports = [8080,] |
| 842 | env = None |
| 843 | NAME = 'xos-synchronizer-vtn' |
| 844 | IMAGE = 'xosproject/xos-synchronizer-vtn' |
| 845 | TAG = 'latest' |
| 846 | PREFIX = '' |
| 847 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtn') |
| 848 | |
| 849 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 850 | boot_delay = 10, restart = False, network_cfg = None, update = False): |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 851 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 852 | |
| 853 | @classmethod |
| 854 | def build_image(cls, image = IMAGE): |
| 855 | Xos.build_image(image, cls.dockerfile_path) |
| 856 | |
| 857 | class XosSyncVtr(Xos): |
| 858 | ports = [8080,] |
| 859 | env = None |
| 860 | NAME = 'xos-synchronizer-vtr' |
| 861 | IMAGE = 'xosproject/xos-synchronizer-vtr' |
| 862 | TAG = 'latest' |
| 863 | PREFIX = '' |
| 864 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtr') |
| 865 | |
| 866 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 867 | boot_delay = 10, restart = False, network_cfg = None, update = False): |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 868 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 869 | |
| 870 | @classmethod |
| 871 | def build_image(cls, image = IMAGE): |
| 872 | Xos.build_image(image, cls.dockerfile_path) |
| 873 | |
| 874 | class XosSyncVsg(Xos): |
| 875 | ports = [8080,] |
| 876 | env = None |
| 877 | NAME = 'xos-synchronizer-vsg' |
| 878 | IMAGE = 'xosproject/xos-synchronizer-vsg' |
| 879 | TAG = 'latest' |
| 880 | PREFIX = '' |
| 881 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vsg') |
| 882 | |
| 883 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 884 | boot_delay = 10, restart = False, network_cfg = None, update = False): |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 885 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 886 | |
| 887 | @classmethod |
| 888 | def build_image(cls, image = IMAGE): |
| 889 | Xos.build_image(image, cls.dockerfile_path) |
| 890 | |
| 891 | |
| 892 | class XosSyncOnos(Xos): |
| 893 | ports = [8080,] |
| 894 | env = None |
| 895 | NAME = 'xos-synchronizer-onos' |
| 896 | IMAGE = 'xosproject/xos-synchronizer-onos' |
| 897 | TAG = 'latest' |
| 898 | PREFIX = '' |
| 899 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-onos') |
| 900 | |
| 901 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 902 | boot_delay = 30, restart = False, network_cfg = None, update = False): |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 903 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 904 | |
| 905 | @classmethod |
| 906 | def build_image(cls, image = IMAGE): |
| 907 | Xos.build_image(image, cls.dockerfile_path) |
| 908 | |
| 909 | class XosSyncFabric(Xos): |
| 910 | ports = [8080,] |
| 911 | env = None |
| 912 | NAME = 'xos-synchronizer-fabric' |
| 913 | IMAGE = 'xosproject/xos-synchronizer-fabric' |
| 914 | TAG = 'latest' |
| 915 | PREFIX = '' |
| 916 | dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-fabric') |
| 917 | |
| 918 | def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG, |
A R Karthick | 6e80afd | 2016-10-10 16:03:12 -0700 | [diff] [blame] | 919 | boot_delay = 30, restart = False, network_cfg = None, update = False): |
ChetanGaonker | c220e0d | 2016-10-05 05:06:25 -0700 | [diff] [blame] | 920 | Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update) |
| 921 | |
| 922 | @classmethod |
| 923 | def build_image(cls, image = IMAGE): |
| 924 | Xos.build_image(image, cls.dockerfile_path) |