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 |
| 19 | from pyroute2 import IPRoute |
| 20 | from itertools import chain |
| 21 | from nsenter import Namespace |
| 22 | from docker import Client |
| 23 | from shutil import copy |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 24 | from OnosCtrl import OnosCtrl |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 25 | |
| 26 | class docker_netns(object): |
| 27 | |
| 28 | dckr = Client() |
| 29 | def __init__(self, name): |
| 30 | pid = int(self.dckr.inspect_container(name)['State']['Pid']) |
| 31 | if pid == 0: |
| 32 | raise Exception('no container named {0}'.format(name)) |
| 33 | self.pid = pid |
| 34 | |
| 35 | def __enter__(self): |
| 36 | pid = self.pid |
| 37 | if not os.path.exists('/var/run/netns'): |
| 38 | os.mkdir('/var/run/netns') |
| 39 | os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid)) |
| 40 | return str(pid) |
| 41 | |
| 42 | def __exit__(self, type, value, traceback): |
| 43 | pid = self.pid |
| 44 | os.unlink('/var/run/netns/{0}'.format(pid)) |
| 45 | |
| 46 | flatten = lambda l: chain.from_iterable(l) |
| 47 | |
| 48 | class Container(object): |
| 49 | dckr = Client() |
| 50 | def __init__(self, name, image, tag = 'latest', command = 'bash', quagga_config = None): |
| 51 | self.name = name |
| 52 | self.image = image |
| 53 | self.tag = tag |
| 54 | self.image_name = image + ':' + tag |
| 55 | self.id = None |
| 56 | self.command = command |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 57 | self.quagga_config = quagga_config |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 58 | |
| 59 | @classmethod |
| 60 | def build_image(cls, dockerfile, tag, force=True, nocache=False): |
| 61 | f = io.BytesIO(dockerfile.encode('utf-8')) |
| 62 | if force or not cls.image_exists(tag): |
| 63 | print('Build {0}...'.format(tag)) |
| 64 | for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache): |
| 65 | if 'stream' in line: |
| 66 | print(line['stream'].strip()) |
| 67 | |
| 68 | @classmethod |
| 69 | def image_exists(cls, name): |
| 70 | return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()] |
| 71 | |
| 72 | @classmethod |
| 73 | def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False): |
| 74 | port_bindings = None |
| 75 | binds = None |
| 76 | if port_list: |
| 77 | port_bindings = {} |
| 78 | for p in port_list: |
| 79 | port_bindings[str(p)] = str(p) |
| 80 | |
| 81 | if host_guest_map: |
| 82 | binds = [] |
| 83 | for h, g in host_guest_map: |
| 84 | binds.append('{0}:{1}'.format(h, g)) |
| 85 | |
| 86 | return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged) |
| 87 | |
| 88 | @classmethod |
| 89 | def cleanup(cls, image): |
A R Karthick | 09b1f4e | 2016-05-12 14:31:50 -0700 | [diff] [blame] | 90 | 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] | 91 | for cnt in cnt_list: |
| 92 | print('Cleaning container %s' %cnt['Id']) |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 93 | if cnt.has_key('State') and cnt['State'] == 'running': |
A R Karthick | 09b1f4e | 2016-05-12 14:31:50 -0700 | [diff] [blame] | 94 | cls.dckr.kill(cnt['Id']) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 95 | cls.dckr.remove_container(cnt['Id'], force=True) |
| 96 | |
| 97 | @classmethod |
| 98 | def remove_container(cls, name, force=True): |
| 99 | try: |
| 100 | cls.dckr.remove_container(name, force = force) |
| 101 | except: pass |
| 102 | |
| 103 | def exists(self): |
| 104 | return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers())) |
| 105 | |
| 106 | def img_exists(self): |
| 107 | return self.image_name in [ctn['RepoTags'][0] for ctn in self.dckr.images()] |
| 108 | |
| 109 | def ip(self): |
| 110 | cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers()) |
| 111 | cnt_settings = cnt_list.pop() |
| 112 | return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress'] |
| 113 | |
| 114 | def kill(self, remove = True): |
| 115 | self.dckr.kill(self.name) |
| 116 | self.dckr.remove_container(self.name, force=True) |
| 117 | |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 118 | def start(self, rm = True, ports = None, volumes = None, host_config = None, |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 119 | environment = None, tty = False, stdin_open = True): |
| 120 | |
| 121 | if rm and self.exists(): |
| 122 | print('Removing container:', self.name) |
| 123 | self.dckr.remove_container(self.name, force=True) |
| 124 | |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 125 | 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] | 126 | detach=True, name=self.name, |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 127 | environment = environment, |
| 128 | volumes = volumes, |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 129 | host_config = host_config, stdin_open=stdin_open, tty = tty) |
| 130 | self.dckr.start(container=self.name) |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 131 | if self.quagga_config: |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 132 | self.connect_to_br() |
| 133 | self.id = ctn['Id'] |
| 134 | return ctn |
| 135 | |
| 136 | def connect_to_br(self): |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 137 | index = 0 |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 138 | with docker_netns(self.name) as pid: |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 139 | for quagga_config in self.quagga_config: |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 140 | ip = IPRoute() |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 141 | br = ip.link_lookup(ifname=quagga_config['bridge']) |
| 142 | if len(br) == 0: |
Chetan Gaonker | 5a0fda3 | 2016-05-10 14:09:07 -0700 | [diff] [blame] | 143 | ip.link_create(ifname=quagga_config['bridge'], kind='bridge') |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 144 | br = ip.link_lookup(ifname=quagga_config['bridge']) |
| 145 | br = br[0] |
| 146 | ip.link('set', index=br, state='up') |
| 147 | ifname = '{0}-{1}'.format(self.name, index) |
| 148 | ifs = ip.link_lookup(ifname=ifname) |
| 149 | if len(ifs) > 0: |
| 150 | ip.link_remove(ifs[0]) |
| 151 | peer_ifname = '{0}-{1}'.format(pid, index) |
Chetan Gaonker | 5a0fda3 | 2016-05-10 14:09:07 -0700 | [diff] [blame] | 152 | ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname) |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 153 | host = ip.link_lookup(ifname=ifname)[0] |
| 154 | ip.link('set', index=host, master=br) |
| 155 | ip.link('set', index=host, state='up') |
| 156 | guest = ip.link_lookup(ifname=peer_ifname)[0] |
| 157 | ip.link('set', index=guest, net_ns_fd=pid) |
| 158 | with Namespace(pid, 'net'): |
| 159 | ip = IPRoute() |
| 160 | ip.link('set', index=guest, ifname='eth{}'.format(index+1)) |
| 161 | ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask']) |
| 162 | ip.link('set', index=guest, state='up') |
| 163 | index += 1 |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 164 | |
| 165 | def execute(self, cmd, tty = True, stream = False, shell = False): |
| 166 | res = 0 |
| 167 | if type(cmd) == str: |
| 168 | cmds = (cmd,) |
| 169 | else: |
| 170 | cmds = cmd |
| 171 | if shell: |
| 172 | for c in cmds: |
| 173 | res += os.system('docker exec {0} {1}'.format(self.name, c)) |
| 174 | return res |
| 175 | for c in cmds: |
| 176 | i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True) |
| 177 | self.dckr.exec_start(i['Id'], stream = stream, detach=True) |
| 178 | result = self.dckr.exec_inspect(i['Id']) |
| 179 | res += 0 if result['ExitCode'] == None else result['ExitCode'] |
| 180 | return res |
| 181 | |
Chetan Gaonker | 462d9fa | 2016-05-03 16:39:10 -0700 | [diff] [blame] | 182 | def get_mem(): |
| 183 | with open('/proc/meminfo', 'r') as fd: |
| 184 | meminfo = fd.readlines() |
| 185 | mem = 0 |
| 186 | for m in meminfo: |
| 187 | if m.startswith('MemTotal:') or m.startswith('SwapTotal:'): |
| 188 | mem += int(m.split(':')[1].strip().split()[0]) |
| 189 | |
Chetan Gaonker | c0421e8 | 2016-05-04 17:23:08 -0700 | [diff] [blame] | 190 | mem = max(mem/1024/1024/2, 1) |
Chetan Gaonker | 6d0a7b0 | 2016-05-03 16:57:28 -0700 | [diff] [blame] | 191 | mem = min(mem, 16) |
Chetan Gaonker | 462d9fa | 2016-05-03 16:39:10 -0700 | [diff] [blame] | 192 | return str(mem) + 'G' |
| 193 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 194 | class Onos(Container): |
| 195 | |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 196 | quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, ) |
Chetan Gaonker | 462d9fa | 2016-05-03 16:39:10 -0700 | [diff] [blame] | 197 | SYSTEM_MEMORY = (get_mem(),) * 2 |
| 198 | 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] | 199 | env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,vrouter', 'JAVA_OPTS' : JAVA_OPTS } |
| 200 | onos_cord_apps = ( ('cord-config', '1.0-SNAPSHOT'), |
| 201 | ('aaa', '1.0-SNAPSHOT'), |
| 202 | ('igmp', '1.0-SNAPSHOT'), |
| 203 | ) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 204 | ports = [ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ] |
| 205 | host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/onos-config') |
| 206 | guest_config_dir = '/root/onos/config' |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 207 | 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] | 208 | host_guest_map = ( (host_config_dir, guest_config_dir), ) |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 209 | NAME = 'cord-onos' |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 210 | |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 211 | def __init__(self, name = NAME, image = 'onosproject/onos', tag = 'latest', |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 212 | boot_delay = 60, restart = False, network_cfg = None): |
| 213 | if restart is True: |
| 214 | ##Find the right image to restart |
| 215 | running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers()) |
| 216 | if running_image: |
| 217 | image_name = running_image[0]['Image'] |
| 218 | try: |
| 219 | image = image_name.split(':')[0] |
| 220 | tag = image_name.split(':')[1] |
| 221 | except: pass |
| 222 | |
| 223 | super(Onos, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config) |
| 224 | if restart is True and self.exists(): |
| 225 | self.kill() |
| 226 | if not self.exists(): |
| 227 | self.remove_container(name, force=True) |
| 228 | host_config = self.create_host_config(port_list = self.ports, |
| 229 | host_guest_map = self.host_guest_map) |
| 230 | volumes = [] |
| 231 | for _,g in self.host_guest_map: |
| 232 | volumes.append(g) |
| 233 | if network_cfg is not None: |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 234 | json_data = json.dumps(network_cfg, indent=4) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 235 | with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f: |
| 236 | f.write(json_data) |
| 237 | print('Starting ONOS container %s' %self.name) |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 238 | self.start(ports = self.ports, environment = self.env, |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 239 | host_config = host_config, volumes = volumes, tty = True) |
| 240 | print('Waiting %d seconds for ONOS to boot' %(boot_delay)) |
| 241 | time.sleep(boot_delay) |
| 242 | |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 243 | self.install_cord_apps() |
| 244 | |
| 245 | @classmethod |
| 246 | def install_cord_apps(cls): |
| 247 | for app, version in cls.onos_cord_apps: |
| 248 | app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version) |
| 249 | ok, code = OnosCtrl.install_app(app_file) |
| 250 | ##app already installed (conflicts) |
| 251 | if code in [ 409 ]: |
| 252 | ok = True |
| 253 | print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install')) |
| 254 | time.sleep(2) |
| 255 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 256 | class Radius(Container): |
| 257 | ports = [ 1812, 1813 ] |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 258 | env = {'TIMEZONE':'America/Los_Angeles', |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 259 | 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password' |
| 260 | } |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 261 | 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] | 262 | guest_db_dir = os.path.join(os.path.sep, 'opt', 'db') |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 263 | 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] | 264 | guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius') |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 265 | start_command = os.path.join(guest_config_dir, 'start-radius.py') |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 266 | host_guest_map = ( (host_db_dir, guest_db_dir), |
| 267 | (host_config_dir, guest_config_dir) |
| 268 | ) |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 269 | IMAGE = 'cord-test/radius' |
| 270 | NAME = 'cord-radius' |
| 271 | |
| 272 | def __init__(self, name = NAME, image = IMAGE, tag = 'latest', |
| 273 | boot_delay = 10, restart = False, update = False): |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 274 | super(Radius, self).__init__(name, image, tag = tag, command = self.start_command) |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 275 | if update is True or not self.img_exists(): |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 276 | self.build_image(image) |
| 277 | if restart is True and self.exists(): |
| 278 | self.kill() |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 279 | if not self.exists(): |
| 280 | self.remove_container(name, force=True) |
| 281 | host_config = self.create_host_config(port_list = self.ports, |
| 282 | host_guest_map = self.host_guest_map) |
| 283 | volumes = [] |
| 284 | for _,g in self.host_guest_map: |
| 285 | volumes.append(g) |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 286 | self.start(ports = self.ports, environment = self.env, |
| 287 | volumes = volumes, |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 288 | host_config = host_config, tty = True) |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 289 | time.sleep(boot_delay) |
| 290 | |
| 291 | @classmethod |
| 292 | def build_image(cls, image): |
| 293 | print('Building Radius image %s' %image) |
| 294 | dockerfile = ''' |
| 295 | FROM hbouvier/docker-radius |
| 296 | MAINTAINER chetan@ciena.com |
| 297 | LABEL RUN docker pull hbouvier/docker-radius |
| 298 | LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius |
A R Karthick | c762df4 | 2016-05-25 10:09:21 -0700 | [diff] [blame] | 299 | RUN apt-get update && \ |
| 300 | apt-get -y install python python-pexpect strace |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 301 | WORKDIR /root |
| 302 | CMD ["/etc/freeradius/start-radius.py"] |
| 303 | ''' |
| 304 | super(Radius, cls).build_image(dockerfile, image) |
| 305 | print('Done building image %s' %image) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 306 | |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 307 | class Quagga(Container): |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 308 | quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 }, |
Chetan Gaonker | 8e25e1b | 2016-05-02 13:42:21 -0700 | [diff] [blame] | 309 | { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 }, |
| 310 | ) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 311 | ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ] |
| 312 | host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config') |
| 313 | guest_quagga_config = '/root/config' |
| 314 | quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf') |
| 315 | host_guest_map = ( (host_quagga_config, guest_quagga_config), ) |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 316 | IMAGE = 'cord-test/quagga' |
| 317 | NAME = 'cord-quagga' |
| 318 | |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 319 | def __init__(self, name = NAME, image = IMAGE, tag = 'latest', |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 320 | boot_delay = 15, restart = False, config_file = quagga_config_file, update = False): |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 321 | super(Quagga, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config) |
Chetan Gaonker | 503032a | 2016-05-12 12:06:29 -0700 | [diff] [blame] | 322 | if update is True or not self.img_exists(): |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 323 | self.build_image(image) |
| 324 | if restart is True and self.exists(): |
| 325 | self.kill() |
| 326 | if not self.exists(): |
| 327 | self.remove_container(name, force=True) |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 328 | host_config = self.create_host_config(port_list = self.ports, |
| 329 | host_guest_map = self.host_guest_map, |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 330 | privileged = True) |
| 331 | volumes = [] |
| 332 | for _,g in self.host_guest_map: |
| 333 | volumes.append(g) |
| 334 | self.start(ports = self.ports, |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 335 | host_config = host_config, |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 336 | volumes = volumes, tty = True) |
| 337 | print('Starting Quagga on container %s' %self.name) |
| 338 | self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file)) |
| 339 | time.sleep(boot_delay) |
| 340 | |
| 341 | @classmethod |
| 342 | def build_image(cls, image): |
Chetan Gaonker | 2a6601b | 2016-05-02 17:28:26 -0700 | [diff] [blame] | 343 | onos_quagga_ip = Onos.quagga_config[0]['ip'] |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 344 | print('Building Quagga image %s' %image) |
| 345 | dockerfile = ''' |
A R Karthick | 41adfce | 2016-06-10 09:51:25 -0700 | [diff] [blame] | 346 | FROM ubuntu:14.04 |
| 347 | MAINTAINER chetan@ciena.com |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 348 | WORKDIR /root |
| 349 | RUN useradd -M quagga |
| 350 | RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga |
| 351 | RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga |
| 352 | RUN apt-get update && apt-get install -qy git autoconf libtool gawk make telnet libreadline6-dev |
| 353 | RUN git clone git://git.sv.gnu.org/quagga.git quagga && \ |
| 354 | (cd quagga && git checkout HEAD && ./bootstrap.sh && \ |
| 355 | sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \ |
| 356 | ./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install) |
| 357 | RUN ldconfig |
| 358 | '''.format(onos_quagga_ip) |
| 359 | super(Quagga, cls).build_image(dockerfile, image) |
| 360 | print('Done building image %s' %image) |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 361 | |
| 362 | def reinitContainerClients(): |
| 363 | docker_netns.dckr = Client() |
| 364 | Container.dckr = Client() |