blob: 63443b9232a0d224f9d206582942cffdb827d9b9 [file] [log] [blame]
A R Karthick41adfce2016-06-10 09:51:25 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# 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 Karthick41adfce2016-06-10 09:51:25 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick41adfce2016-06-10 09:51:25 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# 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 Gaonker3533faa2016-04-25 17:50:14 -070016import os,time
17import io
18import json
A R Karthickd44cea12016-07-20 12:16:41 -070019import yaml
Chetan Gaonker3533faa2016-04-25 17:50:14 -070020from pyroute2 import IPRoute
21from itertools import chain
22from nsenter import Namespace
23from docker import Client
24from shutil import copy
A.R Karthick95d044e2016-06-10 18:44:36 -070025from OnosCtrl import OnosCtrl
Chetan Gaonker3533faa2016-04-25 17:50:14 -070026
27class 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
47flatten = lambda l: chain.from_iterable(l)
48
49class Container(object):
50 dckr = Client()
A R Karthick07608ef2016-08-23 16:51:19 -070051 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 Gaonker3533faa2016-04-25 17:50:14 -070054 self.name = name
A R Karthick07608ef2016-08-23 16:51:19 -070055 self.prefix = prefix
56 if prefix:
57 self.prefix += '/'
58 image = '{}{}'.format(self.prefix, image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -070059 self.image = image
60 self.tag = tag
A R Karthickd44cea12016-07-20 12:16:41 -070061 if tag:
62 self.image_name = image + ':' + tag
63 else:
64 self.image_name = image
Chetan Gaonker3533faa2016-04-25 17:50:14 -070065 self.id = None
66 self.command = command
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -070067 self.quagga_config = quagga_config
Chetan Gaonker3533faa2016-04-25 17:50:14 -070068
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 Karthick09b1f4e2016-05-12 14:31:50 -0700100 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700101 for cnt in cnt_list:
102 print('Cleaning container %s' %cnt['Id'])
A.R Karthick95d044e2016-06-10 18:44:36 -0700103 if cnt.has_key('State') and cnt['State'] == 'running':
A R Karthick09b1f4e2016-05-12 14:31:50 -0700104 cls.dckr.kill(cnt['Id'])
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700105 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 Karthick6d98a592016-08-24 15:16:46 -0700117 return self.image_name in [ctn['RepoTags'][0] if ctn['RepoTags'] else '' for ctn in self.dckr.images()]
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700118
119 def ip(self):
120 cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
121 cnt_settings = cnt_list.pop()
122 return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress']
123
124 def kill(self, remove = True):
125 self.dckr.kill(self.name)
126 self.dckr.remove_container(self.name, force=True)
127
A R Karthick41adfce2016-06-10 09:51:25 -0700128 def start(self, rm = True, ports = None, volumes = None, host_config = None,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700129 environment = None, tty = False, stdin_open = True):
130
131 if rm and self.exists():
132 print('Removing container:', self.name)
133 self.dckr.remove_container(self.name, force=True)
134
A R Karthick41adfce2016-06-10 09:51:25 -0700135 ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700136 detach=True, name=self.name,
A R Karthick41adfce2016-06-10 09:51:25 -0700137 environment = environment,
138 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700139 host_config = host_config, stdin_open=stdin_open, tty = tty)
140 self.dckr.start(container=self.name)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700141 if self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700142 self.connect_to_br()
143 self.id = ctn['Id']
144 return ctn
145
146 def connect_to_br(self):
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700147 index = 0
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700148 with docker_netns(self.name) as pid:
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700149 for quagga_config in self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700150 ip = IPRoute()
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700151 br = ip.link_lookup(ifname=quagga_config['bridge'])
152 if len(br) == 0:
Chetan Gaonker5a0fda32016-05-10 14:09:07 -0700153 ip.link_create(ifname=quagga_config['bridge'], kind='bridge')
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700154 br = ip.link_lookup(ifname=quagga_config['bridge'])
155 br = br[0]
156 ip.link('set', index=br, state='up')
157 ifname = '{0}-{1}'.format(self.name, index)
158 ifs = ip.link_lookup(ifname=ifname)
159 if len(ifs) > 0:
160 ip.link_remove(ifs[0])
161 peer_ifname = '{0}-{1}'.format(pid, index)
Chetan Gaonker5a0fda32016-05-10 14:09:07 -0700162 ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700163 host = ip.link_lookup(ifname=ifname)[0]
164 ip.link('set', index=host, master=br)
165 ip.link('set', index=host, state='up')
166 guest = ip.link_lookup(ifname=peer_ifname)[0]
167 ip.link('set', index=guest, net_ns_fd=pid)
168 with Namespace(pid, 'net'):
169 ip = IPRoute()
170 ip.link('set', index=guest, ifname='eth{}'.format(index+1))
171 ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask'])
172 ip.link('set', index=guest, state='up')
173 index += 1
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700174
175 def execute(self, cmd, tty = True, stream = False, shell = False):
176 res = 0
177 if type(cmd) == str:
178 cmds = (cmd,)
179 else:
180 cmds = cmd
181 if shell:
182 for c in cmds:
183 res += os.system('docker exec {0} {1}'.format(self.name, c))
184 return res
185 for c in cmds:
186 i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True)
187 self.dckr.exec_start(i['Id'], stream = stream, detach=True)
188 result = self.dckr.exec_inspect(i['Id'])
189 res += 0 if result['ExitCode'] == None else result['ExitCode']
190 return res
191
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700192 def restart(self, timeout =10):
193 return self.dckr.restart(self.name, timeout)
194
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700195def get_mem():
196 with open('/proc/meminfo', 'r') as fd:
197 meminfo = fd.readlines()
198 mem = 0
199 for m in meminfo:
200 if m.startswith('MemTotal:') or m.startswith('SwapTotal:'):
201 mem += int(m.split(':')[1].strip().split()[0])
202
Chetan Gaonkerc0421e82016-05-04 17:23:08 -0700203 mem = max(mem/1024/1024/2, 1)
Chetan Gaonker6d0a7b02016-05-03 16:57:28 -0700204 mem = min(mem, 16)
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700205 return str(mem) + 'G'
206
A R Karthickd44cea12016-07-20 12:16:41 -0700207class OnosCord(Container):
208 """Use this when running the cord tester agent on the onos compute node"""
209 onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord')
210 onos_config_dir_guest = '/root/onos/config'
211 onos_config_dir = os.path.join(onos_cord_dir, 'config')
212 docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml')
213
A R Karthickbd9b8a32016-07-21 09:56:45 -0700214 def __init__(self, onos_ip, conf, boot_delay = 60):
215 self.onos_ip = onos_ip
A R Karthickd44cea12016-07-20 12:16:41 -0700216 self.cord_conf_dir = conf
A R Karthickbd9b8a32016-07-21 09:56:45 -0700217 self.boot_delay = boot_delay
A R Karthickd44cea12016-07-20 12:16:41 -0700218 if os.access(self.cord_conf_dir, os.F_OK) and not os.access(self.onos_cord_dir, os.F_OK):
219 os.mkdir(self.onos_cord_dir)
220 os.mkdir(self.onos_config_dir)
221 ##copy the config file from cord-tester-config
222 cmd = 'cp {}/* {}'.format(self.cord_conf_dir, self.onos_cord_dir)
223 os.system(cmd)
224
225 ##update the docker yaml with the config volume
226 with open(self.docker_yaml, 'r') as f:
227 yaml_config = yaml.load(f)
228 image = yaml_config['services'].keys()[0]
229 name = 'cordtestercord_{}_1'.format(image)
230 volumes = yaml_config['services'][image]['volumes']
231 config_volumes = filter(lambda e: e.find(self.onos_config_dir_guest) >= 0, volumes)
232 if not config_volumes:
233 config_volume = '{}:{}'.format(self.onos_config_dir, self.onos_config_dir_guest)
234 volumes.append(config_volume)
235 docker_yaml_changed = '{}-changed'.format(self.docker_yaml)
236 with open(docker_yaml_changed, 'w') as wf:
237 yaml.dump(yaml_config, wf)
238
239 os.rename(docker_yaml_changed, self.docker_yaml)
240 self.volumes = volumes
241
242 super(OnosCord, self).__init__(name, image, tag = '')
243 cord_conf_dir_basename = os.path.basename(self.cord_conf_dir.replace('-', ''))
244 self.xos_onos_name = '{}_{}_1'.format(cord_conf_dir_basename, image)
245 ##Create an container instance of xos onos
246 self.xos_onos = Container(self.xos_onos_name, image, tag = '')
247
248 def start(self, restart = False, network_cfg = None):
249 if restart is True:
250 if self.exists():
251 ##Kill the existing instance
252 print('Killing container %s' %self.name)
253 self.kill()
254 if self.xos_onos.exists():
255 print('Killing container %s' %self.xos_onos.name)
256 self.xos_onos.kill()
257
258 if network_cfg is not None:
259 json_data = json.dumps(network_cfg, indent=4)
260 with open('{}/network-cfg.json'.format(self.onos_config_dir), 'w') as f:
261 f.write(json_data)
262
263 #start the container using docker-compose
264 cmd = 'cd {} && docker-compose up -d'.format(self.onos_cord_dir)
265 os.system(cmd)
A R Karthickbd9b8a32016-07-21 09:56:45 -0700266 #Delay to make sure ONOS fully boots
267 time.sleep(self.boot_delay)
268 Onos.install_cord_apps(onos_ip = self.onos_ip)
A R Karthickd44cea12016-07-20 12:16:41 -0700269
270 def build_image(self):
271 build_cmd = 'cd {} && docker-compose build'.format(self.onos_cord_dir)
272 os.system(build_cmd)
273
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700274class Onos(Container):
275
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700276 quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, )
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700277 SYSTEM_MEMORY = (get_mem(),) * 2
278 JAVA_OPTS = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'.format(*SYSTEM_MEMORY)#-XX:+PrintGCDetails -XX:+PrintGCTimeStamps'
A.R Karthick95d044e2016-06-10 18:44:36 -0700279 env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,vrouter', 'JAVA_OPTS' : JAVA_OPTS }
280 onos_cord_apps = ( ('cord-config', '1.0-SNAPSHOT'),
281 ('aaa', '1.0-SNAPSHOT'),
282 ('igmp', '1.0-SNAPSHOT'),
A R Karthickbd9b8a32016-07-21 09:56:45 -0700283 ('vtn', '1.0-SNAPSHOT'),
A.R Karthick95d044e2016-06-10 18:44:36 -0700284 )
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700285 ports = [ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ]
A R Karthickf2f4ca62016-08-17 10:34:08 -0700286 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
287 host_config_dir = os.path.join(setup_dir, 'onos-config')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700288 guest_config_dir = '/root/onos/config'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700289 onos_gen_partitions = os.path.join(setup_dir, 'onos-gen-partitions')
A.R Karthick95d044e2016-06-10 18:44:36 -0700290 cord_apps_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'apps')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700291 host_guest_map = ( (host_config_dir, guest_config_dir), )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700292 NAME = 'cord-onos'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700293 ##the ip of ONOS in default cluster.json in setup/onos-config
294 CLUSTER_CFG_IP = '172.17.0.2'
A R Karthick07608ef2016-08-23 16:51:19 -0700295 IMAGE = 'onosproject/onos'
296 TAG = 'latest'
297 PREFIX = ''
A R Karthickf2f4ca62016-08-17 10:34:08 -0700298
299 @classmethod
300 def onos_generate_cluster_cfg(cls, ip):
301 try:
302 cmd = '{} {}/cluster.json {}'.format(cls.onos_gen_partitions, cls.host_config_dir, ip)
303 os.system(cmd)
304 except: pass
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700305
A R Karthick07608ef2016-08-23 16:51:19 -0700306 def __init__(self, name = NAME, image = 'onosproject/onos', prefix = '', tag = 'latest',
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700307 boot_delay = 60, restart = False, network_cfg = None):
308 if restart is True:
309 ##Find the right image to restart
310 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
311 if running_image:
312 image_name = running_image[0]['Image']
313 try:
314 image = image_name.split(':')[0]
315 tag = image_name.split(':')[1]
316 except: pass
317
A R Karthick07608ef2016-08-23 16:51:19 -0700318 super(Onos, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.quagga_config)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700319 if restart is True and self.exists():
320 self.kill()
321 if not self.exists():
322 self.remove_container(name, force=True)
323 host_config = self.create_host_config(port_list = self.ports,
324 host_guest_map = self.host_guest_map)
325 volumes = []
326 for _,g in self.host_guest_map:
327 volumes.append(g)
328 if network_cfg is not None:
A R Karthick81acbff2016-06-17 14:45:16 -0700329 json_data = json.dumps(network_cfg, indent=4)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700330 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
331 f.write(json_data)
332 print('Starting ONOS container %s' %self.name)
A R Karthick41adfce2016-06-10 09:51:25 -0700333 self.start(ports = self.ports, environment = self.env,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700334 host_config = host_config, volumes = volumes, tty = True)
A R Karthickf2f4ca62016-08-17 10:34:08 -0700335 if not restart:
336 ##wait a bit before fetching IP to regenerate cluster cfg
337 time.sleep(5)
338 ip = self.ip()
339 ##Just a quick hack/check to ensure we don't regenerate in the common case.
340 ##As ONOS is usually the first test container that is started
341 if ip != self.CLUSTER_CFG_IP:
342 print('Regenerating ONOS cluster cfg for ip %s' %ip)
343 self.onos_generate_cluster_cfg(ip)
344 self.kill()
345 self.remove_container(self.name, force=True)
346 print('Restarting ONOS container %s' %self.name)
347 self.start(ports = self.ports, environment = self.env,
348 host_config = host_config, volumes = volumes, tty = True)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700349 print('Waiting %d seconds for ONOS to boot' %(boot_delay))
350 time.sleep(boot_delay)
351
A R Karthickd44cea12016-07-20 12:16:41 -0700352 self.install_cord_apps()
353
A.R Karthick95d044e2016-06-10 18:44:36 -0700354 @classmethod
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700355 def install_cord_apps(cls, onos_ip = None):
A.R Karthick95d044e2016-06-10 18:44:36 -0700356 for app, version in cls.onos_cord_apps:
357 app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version)
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700358 ok, code = OnosCtrl.install_app(app_file, onos_ip = onos_ip)
A.R Karthick95d044e2016-06-10 18:44:36 -0700359 ##app already installed (conflicts)
360 if code in [ 409 ]:
361 ok = True
362 print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install'))
363 time.sleep(2)
364
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700365class Radius(Container):
366 ports = [ 1812, 1813 ]
A R Karthick41adfce2016-06-10 09:51:25 -0700367 env = {'TIMEZONE':'America/Los_Angeles',
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700368 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
369 }
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700370 host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700371 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700372 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700373 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700374 start_command = os.path.join(guest_config_dir, 'start-radius.py')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700375 host_guest_map = ( (host_db_dir, guest_db_dir),
376 (host_config_dir, guest_config_dir)
377 )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700378 IMAGE = 'cord-test/radius'
379 NAME = 'cord-radius'
380
A R Karthick07608ef2016-08-23 16:51:19 -0700381 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
Chetan Gaonker503032a2016-05-12 12:06:29 -0700382 boot_delay = 10, restart = False, update = False):
A R Karthick07608ef2016-08-23 16:51:19 -0700383 super(Radius, self).__init__(name, image, prefix = prefix, tag = tag, command = self.start_command)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700384 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700385 self.build_image(self.image_name)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700386 if restart is True and self.exists():
387 self.kill()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700388 if not self.exists():
389 self.remove_container(name, force=True)
390 host_config = self.create_host_config(port_list = self.ports,
391 host_guest_map = self.host_guest_map)
392 volumes = []
393 for _,g in self.host_guest_map:
394 volumes.append(g)
A R Karthick41adfce2016-06-10 09:51:25 -0700395 self.start(ports = self.ports, environment = self.env,
396 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700397 host_config = host_config, tty = True)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700398 time.sleep(boot_delay)
399
400 @classmethod
401 def build_image(cls, image):
402 print('Building Radius image %s' %image)
403 dockerfile = '''
404FROM hbouvier/docker-radius
405MAINTAINER chetan@ciena.com
406LABEL RUN docker pull hbouvier/docker-radius
407LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius
A R Karthickc762df42016-05-25 10:09:21 -0700408RUN apt-get update && \
409 apt-get -y install python python-pexpect strace
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700410WORKDIR /root
411CMD ["/etc/freeradius/start-radius.py"]
412'''
413 super(Radius, cls).build_image(dockerfile, image)
414 print('Done building image %s' %image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700415
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700416class Quagga(Container):
A R Karthick41adfce2016-06-10 09:51:25 -0700417 quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 },
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700418 { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 },
419 )
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700420 ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ]
421 host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config')
422 guest_quagga_config = '/root/config'
423 quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
424 host_guest_map = ( (host_quagga_config, guest_quagga_config), )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700425 IMAGE = 'cord-test/quagga'
426 NAME = 'cord-quagga'
427
A R Karthick07608ef2016-08-23 16:51:19 -0700428 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
Chetan Gaonker503032a2016-05-12 12:06:29 -0700429 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False):
A R Karthick07608ef2016-08-23 16:51:19 -0700430 super(Quagga, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.quagga_config)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700431 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700432 self.build_image(self.image_name)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700433 if restart is True and self.exists():
434 self.kill()
435 if not self.exists():
436 self.remove_container(name, force=True)
A R Karthick41adfce2016-06-10 09:51:25 -0700437 host_config = self.create_host_config(port_list = self.ports,
438 host_guest_map = self.host_guest_map,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700439 privileged = True)
440 volumes = []
441 for _,g in self.host_guest_map:
442 volumes.append(g)
443 self.start(ports = self.ports,
A R Karthick41adfce2016-06-10 09:51:25 -0700444 host_config = host_config,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700445 volumes = volumes, tty = True)
446 print('Starting Quagga on container %s' %self.name)
447 self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
448 time.sleep(boot_delay)
449
450 @classmethod
451 def build_image(cls, image):
Chetan Gaonker2a6601b2016-05-02 17:28:26 -0700452 onos_quagga_ip = Onos.quagga_config[0]['ip']
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700453 print('Building Quagga image %s' %image)
454 dockerfile = '''
A R Karthick41adfce2016-06-10 09:51:25 -0700455FROM ubuntu:14.04
456MAINTAINER chetan@ciena.com
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700457WORKDIR /root
458RUN useradd -M quagga
459RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga
460RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga
461RUN apt-get update && apt-get install -qy git autoconf libtool gawk make telnet libreadline6-dev
ChetanGaonkerb5b46c62016-08-16 12:02:53 -0700462RUN git clone git://git.savannah.nongnu.org/quagga.git quagga && \
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700463(cd quagga && git checkout HEAD && ./bootstrap.sh && \
464sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \
465./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install)
466RUN ldconfig
467'''.format(onos_quagga_ip)
468 super(Quagga, cls).build_image(dockerfile, image)
469 print('Done building image %s' %image)
A R Karthick81acbff2016-06-17 14:45:16 -0700470
471def reinitContainerClients():
472 docker_netns.dckr = Client()
473 Container.dckr = Client()