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