blob: a3afab4f2912779d9ec445497b66e5e923662d4f [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
A.R Karthickc4e474d2016-12-12 15:24:57 -080020import errno
A R Karthickaa54a1c2016-12-15 11:42:08 -080021import copy
Chetan Gaonker3533faa2016-04-25 17:50:14 -070022from pyroute2 import IPRoute
A.R Karthickc4e474d2016-12-12 15:24:57 -080023from pyroute2.netlink import NetlinkError
Chetan Gaonker3533faa2016-04-25 17:50:14 -070024from itertools import chain
25from nsenter import Namespace
26from docker import Client
A R Karthick85eb1862017-01-23 16:10:57 -080027from docker import utils as dockerutils
A.R Karthickf184b342017-01-27 19:30:50 -080028import shutil
A.R Karthick95d044e2016-06-10 18:44:36 -070029from OnosCtrl import OnosCtrl
A R Karthick19aaf5c2016-11-09 17:47:57 -080030from OnosLog import OnosLog
A.R Karthickc4e474d2016-12-12 15:24:57 -080031from threadPool import ThreadPool
A R Karthickaa54a1c2016-12-15 11:42:08 -080032from threading import Lock
Chetan Gaonker3533faa2016-04-25 17:50:14 -070033
34class docker_netns(object):
35
36 dckr = Client()
37 def __init__(self, name):
38 pid = int(self.dckr.inspect_container(name)['State']['Pid'])
39 if pid == 0:
40 raise Exception('no container named {0}'.format(name))
41 self.pid = pid
42
43 def __enter__(self):
44 pid = self.pid
45 if not os.path.exists('/var/run/netns'):
46 os.mkdir('/var/run/netns')
47 os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid))
48 return str(pid)
49
50 def __exit__(self, type, value, traceback):
51 pid = self.pid
52 os.unlink('/var/run/netns/{0}'.format(pid))
53
54flatten = lambda l: chain.from_iterable(l)
55
56class Container(object):
57 dckr = Client()
A R Karthick07608ef2016-08-23 16:51:19 -070058 IMAGE_PREFIX = '' ##for saving global prefix for all test classes
A R Karthickaa54a1c2016-12-15 11:42:08 -080059 CONFIG_LOCK = Lock()
A R Karthick07608ef2016-08-23 16:51:19 -070060
61 def __init__(self, name, image, prefix='', tag = 'candidate', command = 'bash', quagga_config = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -070062 self.name = name
A R Karthick07608ef2016-08-23 16:51:19 -070063 self.prefix = prefix
64 if prefix:
65 self.prefix += '/'
66 image = '{}{}'.format(self.prefix, image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -070067 self.image = image
68 self.tag = tag
A R Karthickd44cea12016-07-20 12:16:41 -070069 if tag:
70 self.image_name = image + ':' + tag
71 else:
72 self.image_name = image
Chetan Gaonker3533faa2016-04-25 17:50:14 -070073 self.id = None
74 self.command = command
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -070075 self.quagga_config = quagga_config
Chetan Gaonker3533faa2016-04-25 17:50:14 -070076
77 @classmethod
78 def build_image(cls, dockerfile, tag, force=True, nocache=False):
79 f = io.BytesIO(dockerfile.encode('utf-8'))
80 if force or not cls.image_exists(tag):
81 print('Build {0}...'.format(tag))
82 for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache):
83 if 'stream' in line:
84 print(line['stream'].strip())
85
86 @classmethod
87 def image_exists(cls, name):
A R Karthicke07fc3a2017-02-27 10:49:29 -080088 #return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()]
89 return name in list( flatten(ctn['RepoTags'] if ctn['RepoTags'] else '' for ctn in cls.dckr.images()) )
Chetan Gaonker3533faa2016-04-25 17:50:14 -070090
91 @classmethod
92 def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False):
93 port_bindings = None
94 binds = None
95 if port_list:
96 port_bindings = {}
97 for p in port_list:
98 port_bindings[str(p)] = str(p)
99
100 if host_guest_map:
101 binds = []
102 for h, g in host_guest_map:
103 binds.append('{0}:{1}'.format(h, g))
104
105 return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged)
106
107 @classmethod
A R Karthick85eb1862017-01-23 16:10:57 -0800108 def connect_to_network(cls, name, network):
109 try:
110 cls.dckr.connect_container_to_network(name, network)
111 return True
112 except:
113 return False
114
115 @classmethod
116 def create_network(cls, network, subnet = None, gateway = None):
117 ipam_config = None
118 if subnet is not None and gateway is not None:
119 ipam_pool = dockerutils.create_ipam_pool(subnet = subnet, gateway = gateway)
120 ipam_config = dockerutils.create_ipam_config(pool_configs = [ipam_pool])
121 cls.dckr.create_network(network, driver='bridge', ipam = ipam_config)
122
123 @classmethod
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700124 def cleanup(cls, image):
A R Karthick09b1f4e2016-05-12 14:31:50 -0700125 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700126 for cnt in cnt_list:
127 print('Cleaning container %s' %cnt['Id'])
A.R Karthick95d044e2016-06-10 18:44:36 -0700128 if cnt.has_key('State') and cnt['State'] == 'running':
A R Karthick09b1f4e2016-05-12 14:31:50 -0700129 cls.dckr.kill(cnt['Id'])
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700130 cls.dckr.remove_container(cnt['Id'], force=True)
131
132 @classmethod
133 def remove_container(cls, name, force=True):
134 try:
135 cls.dckr.remove_container(name, force = force)
136 except: pass
137
138 def exists(self):
139 return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers()))
140
141 def img_exists(self):
A R Karthicke07fc3a2017-02-27 10:49:29 -0800142 #return self.image_name in [ctn['RepoTags'][0] if ctn['RepoTags'] else '' for ctn in self.dckr.images()]
143 return self.image_name in list( flatten(ctn['RepoTags'] if ctn['RepoTags'] else '' for ctn in self.dckr.images()) )
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700144
A R Karthick75844572017-01-23 16:57:44 -0800145 def ip(self, network = None):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700146 cnt_list = filter(lambda c: c['Names'][0] == '/{}'.format(self.name), self.dckr.containers())
147 #if not cnt_list:
148 # cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700149 cnt_settings = cnt_list.pop()
A R Karthick75844572017-01-23 16:57:44 -0800150 if network is not None and cnt_settings['NetworkSettings']['Networks'].has_key(network):
151 return cnt_settings['NetworkSettings']['Networks'][network]['IPAddress']
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700152 return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress']
153
A R Karthick2b93d6a2016-09-06 15:19:09 -0700154 @classmethod
155 def ips(cls, image_name):
156 cnt_list = filter(lambda c: c['Image'] == image_name, cls.dckr.containers())
157 ips = [ cnt['NetworkSettings']['Networks']['bridge']['IPAddress'] for cnt in cnt_list ]
158 return ips
159
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700160 def kill(self, remove = True):
161 self.dckr.kill(self.name)
162 self.dckr.remove_container(self.name, force=True)
163
A R Karthick41adfce2016-06-10 09:51:25 -0700164 def start(self, rm = True, ports = None, volumes = None, host_config = None,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700165 environment = None, tty = False, stdin_open = True):
166
167 if rm and self.exists():
168 print('Removing container:', self.name)
169 self.dckr.remove_container(self.name, force=True)
170
A R Karthick41adfce2016-06-10 09:51:25 -0700171 ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700172 detach=True, name=self.name,
A R Karthick41adfce2016-06-10 09:51:25 -0700173 environment = environment,
174 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700175 host_config = host_config, stdin_open=stdin_open, tty = tty)
176 self.dckr.start(container=self.name)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700177 if self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700178 self.connect_to_br()
179 self.id = ctn['Id']
180 return ctn
181
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000182 @classmethod
183 def pause_container(cls, image, delay):
184 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
185 for cnt in cnt_list:
186 print('Pause the container %s' %cnt['Id'])
187 if cnt.has_key('State') and cnt['State'] == 'running':
188 cls.dckr.pause(cnt['Id'])
189 if delay != 0:
190 time.sleep(delay)
191 for cnt in cnt_list:
192 print('Unpause the container %s' %cnt['Id'])
193 cls.dckr.unpause(cnt['Id'])
194 else:
195 print('Infinity time pause the container %s' %cnt['Id'])
196 return 'success'
197
A R Karthick52414732017-01-31 09:59:47 -0800198 def connect_to_br(self, index = 0):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800199 self.CONFIG_LOCK.acquire()
200 try:
201 with docker_netns(self.name) as pid:
202 for quagga_config in self.quagga_config:
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700203 ip = IPRoute()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800204 br = ip.link_lookup(ifname=quagga_config['bridge'])
205 if len(br) == 0:
206 try:
207 ip.link_create(ifname=quagga_config['bridge'], kind='bridge')
208 except NetlinkError as e:
209 err, _ = e.args
210 if err == errno.EEXIST:
211 pass
212 else:
213 raise NetlinkError(*e.args)
214 br = ip.link_lookup(ifname=quagga_config['bridge'])
215 br = br[0]
216 ip.link('set', index=br, state='up')
A R Karthick52414732017-01-31 09:59:47 -0800217 ifname = '{0}-{1}'.format(self.name[:12], index)
A R Karthickaa54a1c2016-12-15 11:42:08 -0800218 ifs = ip.link_lookup(ifname=ifname)
219 if len(ifs) > 0:
220 ip.link_remove(ifs[0])
221 peer_ifname = '{0}-{1}'.format(pid, index)
222 ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname)
223 host = ip.link_lookup(ifname=ifname)[0]
224 ip.link('set', index=host, master=br)
225 ip.link('set', index=host, state='up')
226 guest = ip.link_lookup(ifname=peer_ifname)[0]
227 ip.link('set', index=guest, net_ns_fd=pid)
228 with Namespace(pid, 'net'):
229 ip = IPRoute()
230 ip.link('set', index=guest, ifname='eth{}'.format(index+1))
231 ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask'])
232 ip.link('set', index=guest, state='up')
233 index += 1
234 finally:
235 self.CONFIG_LOCK.release()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700236
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000237 def execute(self, cmd, tty = True, stream = False, shell = False, detach = True):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700238 res = 0
239 if type(cmd) == str:
240 cmds = (cmd,)
241 else:
242 cmds = cmd
243 if shell:
244 for c in cmds:
245 res += os.system('docker exec {0} {1}'.format(self.name, c))
246 return res
247 for c in cmds:
248 i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True)
A R Karthickd6dd9b22017-02-24 15:17:22 -0800249 s = self.dckr.exec_start(i['Id'], stream = stream, detach=detach, socket=True)
250 try:
251 s.close()
252 except: pass
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700253 result = self.dckr.exec_inspect(i['Id'])
254 res += 0 if result['ExitCode'] == None else result['ExitCode']
255 return res
256
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700257 def restart(self, timeout =10):
258 return self.dckr.restart(self.name, timeout)
259
A R Karthickc69d73e2017-01-20 11:44:34 -0800260def get_mem(jvm_heap_size = None, instances = 1):
A R Karthick1f908202016-11-16 17:32:20 -0800261 if instances <= 0:
262 instances = 1
A R Karthickc69d73e2017-01-20 11:44:34 -0800263 heap_size = jvm_heap_size
264 heap_size_i = 0
265 #sanitize the heap size config
266 if heap_size is not None:
267 if not heap_size.isdigit():
268 try:
269 heap_size_i = int(heap_size[:-1])
270 suffix = heap_size[-1]
271 if suffix == 'M':
272 heap_size_i /= 1024 #convert to gigs
A.R Karthick99044822017-02-09 14:04:20 -0800273 #allow to specific minimum heap size
274 if heap_size_i == 0:
275 return heap_size
A R Karthickc69d73e2017-01-20 11:44:34 -0800276 except:
277 ##invalid suffix length probably. Fall back to default
278 heap_size = None
279 else:
280 heap_size_i = int(heap_size)
281
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700282 with open('/proc/meminfo', 'r') as fd:
283 meminfo = fd.readlines()
284 mem = 0
285 for m in meminfo:
286 if m.startswith('MemTotal:') or m.startswith('SwapTotal:'):
287 mem += int(m.split(':')[1].strip().split()[0])
288
A R Karthick1f908202016-11-16 17:32:20 -0800289 mem = max(mem/1024/1024/2/instances, 1)
Chetan Gaonker6d0a7b02016-05-03 16:57:28 -0700290 mem = min(mem, 16)
A R Karthickc69d73e2017-01-20 11:44:34 -0800291
292 if heap_size_i:
293 #we take the minimum of the provided heap size and max allowed heap size
294 heap_size_i = min(heap_size_i, mem)
295 else:
296 heap_size_i = mem
297
298 return '{}G'.format(heap_size_i)
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700299
A R Karthickd44cea12016-07-20 12:16:41 -0700300class OnosCord(Container):
301 """Use this when running the cord tester agent on the onos compute node"""
A R Karthickd44cea12016-07-20 12:16:41 -0700302 onos_config_dir_guest = '/root/onos/config'
A R Karthickf6ef89b2017-02-01 11:32:19 -0800303 tester_apps = ( 'org.onosproject.proxyarp', 'org.onosproject.hostprovider' )
A R Karthickd44cea12016-07-20 12:16:41 -0700304
A R Karthick52414732017-01-31 09:59:47 -0800305 def __init__(self, onos_ip, conf, service_profile, synchronizer, start = True, boot_delay = 25):
A.R Karthickf184b342017-01-27 19:30:50 -0800306 if not os.access(conf, os.F_OK):
307 raise Exception('ONOS cord configuration location %s is invalid' %conf)
308 if not os.access(service_profile, os.F_OK):
309 raise Exception('ONOS cord service profile location is not accessible' %service_profile)
A R Karthickbd9b8a32016-07-21 09:56:45 -0700310 self.onos_ip = onos_ip
A.R Karthickf184b342017-01-27 19:30:50 -0800311 self.onos_cord_dir = conf
A R Karthickbd9b8a32016-07-21 09:56:45 -0700312 self.boot_delay = boot_delay
A.R Karthickf184b342017-01-27 19:30:50 -0800313 self.synchronizer = synchronizer
314 self.service_profile = service_profile
315 self.docker_yaml = os.path.join(conf, 'docker-compose.yml')
316 self.docker_yaml_saved = os.path.join(conf, 'docker-compose.yml.saved')
317 self.onos_config_dir = os.path.join(conf, 'config')
318 self.onos_cfg_save_loc = os.path.join(conf, 'network-cfg.json.saved')
319 instance_active = False
320 #if we have a wrapper onos instance already active, back out
321 if os.access(self.onos_config_dir, os.F_OK) or os.access(self.docker_yaml_saved, os.F_OK):
322 instance_active = True
323 else:
324 if start is True:
325 os.mkdir(self.onos_config_dir)
326 shutil.copy(self.docker_yaml, self.docker_yaml_saved)
A R Karthickd44cea12016-07-20 12:16:41 -0700327
A.R Karthickf184b342017-01-27 19:30:50 -0800328 self.start_wrapper = instance_active is False and start is True
A R Karthickd44cea12016-07-20 12:16:41 -0700329 ##update the docker yaml with the config volume
330 with open(self.docker_yaml, 'r') as f:
331 yaml_config = yaml.load(f)
332 image = yaml_config['services'].keys()[0]
A.R Karthickf184b342017-01-27 19:30:50 -0800333 cord_conf_dir_basename = os.path.basename(self.onos_cord_dir.replace('-', ''))
334 xos_onos_name = '{}_{}_1'.format(cord_conf_dir_basename, image)
A R Karthick5778a792017-01-31 13:47:16 -0800335 if not yaml_config['services'][image].has_key('volumes'):
336 yaml_config['services'][image]['volumes'] = []
A R Karthickd44cea12016-07-20 12:16:41 -0700337 volumes = yaml_config['services'][image]['volumes']
338 config_volumes = filter(lambda e: e.find(self.onos_config_dir_guest) >= 0, volumes)
339 if not config_volumes:
340 config_volume = '{}:{}'.format(self.onos_config_dir, self.onos_config_dir_guest)
341 volumes.append(config_volume)
A.R Karthickf184b342017-01-27 19:30:50 -0800342 if self.start_wrapper:
343 docker_yaml_changed = '{}-changed'.format(self.docker_yaml)
344 with open(docker_yaml_changed, 'w') as wf:
345 yaml.dump(yaml_config, wf)
346 os.rename(docker_yaml_changed, self.docker_yaml)
A R Karthickd44cea12016-07-20 12:16:41 -0700347 self.volumes = volumes
348
A R Karthickd44cea12016-07-20 12:16:41 -0700349 ##Create an container instance of xos onos
A R Karthick52414732017-01-31 09:59:47 -0800350 super(OnosCord, self).__init__(xos_onos_name, image, tag = '', quagga_config = Onos.QUAGGA_CONFIG)
A.R Karthickf184b342017-01-27 19:30:50 -0800351 self.last_cfg = None
352 if self.start_wrapper:
353 #fetch the current config of onos cord instance and save it
354 try:
355 self.last_cfg = OnosCtrl.get_config(controller = onos_ip)
356 json_data = json.dumps(self.last_cfg, indent=4)
357 with open(self.onos_cfg_save_loc, 'w') as f:
358 f.write(json_data)
359 except:
360 pass
361 #start the container back with the shared onos config volume
362 self.start()
A R Karthickd44cea12016-07-20 12:16:41 -0700363
364 def start(self, restart = False, network_cfg = None):
A R Karthick928ad622017-01-30 12:18:32 -0800365 if network_cfg is not None:
A R Karthickd44cea12016-07-20 12:16:41 -0700366 json_data = json.dumps(network_cfg, indent=4)
367 with open('{}/network-cfg.json'.format(self.onos_config_dir), 'w') as f:
368 f.write(json_data)
A R Karthick52414732017-01-31 09:59:47 -0800369
370 #we avoid using docker-compose restart for now.
371 #since we don't want to retain the metadata across restarts
372 if True:
A.R Karthickf184b342017-01-27 19:30:50 -0800373 #stop and start and synchronize the services before installing tester cord apps
374 cmds = [ 'cd {} && docker-compose down'.format(self.onos_cord_dir),
375 'cd {} && docker-compose up -d'.format(self.onos_cord_dir),
376 'sleep 45',
377 'cd {} && make {}'.format(self.service_profile, self.synchronizer)
378 ]
379 for cmd in cmds:
380 try:
381 print(cmd)
382 os.system(cmd)
383 except:pass
384 Onos.install_cord_apps(onos_ip = self.onos_ip)
A R Karthickf6ef89b2017-02-01 11:32:19 -0800385 self.activate_apps()
A.R Karthickf184b342017-01-27 19:30:50 -0800386 else:
387 cmd = 'cd {} && docker-compose restart'.format(self.onos_cord_dir)
388 try:
389 os.system(cmd)
390 except: pass
A R Karthick52414732017-01-31 09:59:47 -0800391
392 ##we could also connect container to default docker network but disabled for now
393 #Container.connect_to_network(self.name, 'bridge')
394
395 #connect container to the quagga bridge
396 self.connect_to_br(index = 0)
A.R Karthickf184b342017-01-27 19:30:50 -0800397 print('Waiting %d seconds for ONOS instance to start' %self.boot_delay)
A R Karthickbd9b8a32016-07-21 09:56:45 -0700398 time.sleep(self.boot_delay)
A R Karthickd44cea12016-07-20 12:16:41 -0700399
A R Karthickdb5a5fc2017-02-01 16:40:43 -0800400 def activate_apps(self):
401 for app in self.tester_apps:
A R Karthickf6ef89b2017-02-01 11:32:19 -0800402 print('Activating ONOS app %s' %(app))
A R Karthickdb5a5fc2017-02-01 16:40:43 -0800403 OnosCtrl(app, controller = self.onos_ip).activate()
A R Karthickf6ef89b2017-02-01 11:32:19 -0800404 time.sleep(2)
405
A R Karthickd44cea12016-07-20 12:16:41 -0700406 def build_image(self):
407 build_cmd = 'cd {} && docker-compose build'.format(self.onos_cord_dir)
408 os.system(build_cmd)
409
A.R Karthickf184b342017-01-27 19:30:50 -0800410 def restore(self, force = False):
411 restore = self.start_wrapper is True or force is True
412 if not restore:
A.R Karthick263d3fc2017-01-27 12:52:53 -0800413 return
A R Karthick394976f2017-01-31 14:25:16 -0800414 #nothing to restore
415 if not os.access(self.docker_yaml_saved, os.F_OK):
416 return
A.R Karthickf184b342017-01-27 19:30:50 -0800417 #restore the config files back. The synchronizer restore should bring the last config back
418 cmds = ['cd {} && docker-compose down'.format(self.onos_cord_dir),
419 'rm -rf {}'.format(self.onos_config_dir),
420 'mv {} {}'.format(self.docker_yaml_saved, self.docker_yaml),
421 'cd {} && docker-compose up -d'.format(self.onos_cord_dir),
422 'sleep 45',
423 'cd {} && make {}'.format(self.service_profile, self.synchronizer)
424 ]
425 for cmd in cmds:
A.R Karthickb17e2022017-01-27 11:29:26 -0800426 try:
A.R Karthickf184b342017-01-27 19:30:50 -0800427 print(cmd)
428 os.system(cmd)
A.R Karthickb17e2022017-01-27 11:29:26 -0800429 except: pass
430
A.R Karthickf184b342017-01-27 19:30:50 -0800431 #We may not have to restore the config but still it should match synchronizer last config
432 if os.access(self.onos_cfg_save_loc, os.F_OK):
433 with open(self.onos_cfg_save_loc, 'r') as f:
434 cfg = json.load(f)
435 try:
436 OnosCtrl.config(cfg, controller = self.onos_ip)
437 os.unlink(self.onos_cfg_save_loc)
438 except:
439 pass
A.R Karthickb17e2022017-01-27 11:29:26 -0800440
A.R Karthick1700e0e2016-10-06 18:16:57 -0700441class OnosCordStopWrapper(Container):
442 onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord')
443 docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml')
444
445 def __init__(self):
446 if os.access(self.docker_yaml, os.F_OK):
447 with open(self.docker_yaml, 'r') as f:
448 yaml_config = yaml.load(f)
449 image = yaml_config['services'].keys()[0]
450 name = 'cordtestercord_{}_1'.format(image)
451 super(OnosCordStopWrapper, self).__init__(name, image, tag = '')
452 if self.exists():
453 print('Killing container %s' %self.name)
454 self.kill()
455
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700456class Onos(Container):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800457 QUAGGA_CONFIG = [ { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, ]
A R Karthicka2492c12016-12-16 10:31:51 -0800458 MAX_INSTANCES = 3
A R Karthickc69d73e2017-01-20 11:44:34 -0800459 JVM_HEAP_SIZE = None
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700460 SYSTEM_MEMORY = (get_mem(),) * 2
A R Karthicka2492c12016-12-16 10:31:51 -0800461 INSTANCE_MEMORY = (get_mem(instances=MAX_INSTANCES),) * 2
A R Karthickc69d73e2017-01-20 11:44:34 -0800462 JAVA_OPTS_FORMAT = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'
463 JAVA_OPTS_DEFAULT = JAVA_OPTS_FORMAT.format(*SYSTEM_MEMORY) #-XX:+PrintGCDetails -XX:+PrintGCTimeStamps'
464 JAVA_OPTS_CLUSTER_DEFAULT = JAVA_OPTS_FORMAT.format(*INSTANCE_MEMORY)
465 env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,vrouter', 'JAVA_OPTS' : JAVA_OPTS_DEFAULT }
A.R Karthickdfeadb02016-11-30 17:55:51 -0800466 onos_cord_apps = ( ('cord-config', '1.1-SNAPSHOT'),
467 ('aaa', '1.1-SNAPSHOT'),
468 ('igmp', '1.1-SNAPSHOT'),
469 #('vtn', '1.1-SNAPSHOT'),
A.R Karthick95d044e2016-06-10 18:44:36 -0700470 )
A.R Karthickdda22062017-02-09 14:39:20 -0800471 ports = [] #[ 8181, 8101, 9876, 6653, 6633, 2000, 2620, 5005 ]
A R Karthickf2f4ca62016-08-17 10:34:08 -0700472 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
473 host_config_dir = os.path.join(setup_dir, 'onos-config')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700474 guest_config_dir = '/root/onos/config'
A.R Karthickdda22062017-02-09 14:39:20 -0800475 guest_data_dir = '/root/onos/apache-karaf-3.0.8/data'
476 guest_log_file = '/root/onos/apache-karaf-3.0.8/data/log/karaf.log'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700477 onos_gen_partitions = os.path.join(setup_dir, 'onos-gen-partitions')
A R Karthick2b93d6a2016-09-06 15:19:09 -0700478 onos_form_cluster = os.path.join(setup_dir, 'onos-form-cluster')
A.R Karthick95d044e2016-06-10 18:44:36 -0700479 cord_apps_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'apps')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700480 host_guest_map = ( (host_config_dir, guest_config_dir), )
A R Karthick2b93d6a2016-09-06 15:19:09 -0700481 cluster_cfg = os.path.join(host_config_dir, 'cluster.json')
482 cluster_mode = False
483 cluster_instances = []
Chetan Gaonker503032a2016-05-12 12:06:29 -0700484 NAME = 'cord-onos'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700485 ##the ip of ONOS in default cluster.json in setup/onos-config
486 CLUSTER_CFG_IP = '172.17.0.2'
A R Karthick07608ef2016-08-23 16:51:19 -0700487 IMAGE = 'onosproject/onos'
488 TAG = 'latest'
489 PREFIX = ''
A R Karthickf2f4ca62016-08-17 10:34:08 -0700490
491 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700492 def generate_cluster_cfg(cls, ip):
493 if type(ip) in [ list, tuple ]:
494 ips = ' '.join(ip)
495 else:
496 ips = ip
A R Karthickf2f4ca62016-08-17 10:34:08 -0700497 try:
A R Karthick2b93d6a2016-09-06 15:19:09 -0700498 cmd = '{} {} {}'.format(cls.onos_gen_partitions, cls.cluster_cfg, ips)
499 os.system(cmd)
500 except: pass
501
502 @classmethod
503 def form_cluster(cls, ips):
504 nodes = ' '.join(ips)
505 try:
506 cmd = '{} {}'.format(cls.onos_form_cluster, nodes)
A R Karthickf2f4ca62016-08-17 10:34:08 -0700507 os.system(cmd)
508 except: pass
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700509
A R Karthick9d48c652016-09-15 09:16:36 -0700510 @classmethod
511 def cleanup_runtime(cls):
512 '''Cleanup ONOS runtime generated files'''
513 files = ( Onos.cluster_cfg, os.path.join(Onos.host_config_dir, 'network-cfg.json') )
514 for f in files:
515 if os.access(f, os.F_OK):
516 try:
517 os.unlink(f)
518 except: pass
519
A R Karthickec2db322016-11-17 15:06:01 -0800520 @classmethod
521 def get_data_map(cls, host_volume, guest_volume_dir):
522 host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume))
523 if not os.path.exists(host_volume_dir):
524 os.mkdir(host_volume_dir)
525 return ( (host_volume_dir, guest_volume_dir), )
526
527 @classmethod
528 def remove_data_map(cls, host_volume, guest_volume_dir):
529 host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume))
530 if os.path.exists(host_volume_dir):
A.R Karthickf184b342017-01-27 19:30:50 -0800531 shutil.rmtree(host_volume_dir)
A R Karthickec2db322016-11-17 15:06:01 -0800532
A R Karthick973010f2017-02-06 16:41:51 -0800533 @classmethod
534 def update_data_dir(cls, karaf):
535 Onos.guest_data_dir = '/root/onos/apache-karaf-{}/data'.format(karaf)
536 Onos.guest_log_file = '/root/onos/apache-karaf-{}/data/log/karaf.log'.format(karaf)
537
A R Karthickec2db322016-11-17 15:06:01 -0800538 def remove_data_volume(self):
539 if self.data_map is not None:
540 self.remove_data_map(*self.data_map)
541
A.R Karthick1700e0e2016-10-06 18:16:57 -0700542 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthickec2db322016-11-17 15:06:01 -0800543 boot_delay = 20, restart = False, network_cfg = None,
A R Karthick85eb1862017-01-23 16:10:57 -0800544 cluster = False, data_volume = None, async = False, quagga_config = None,
545 network = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700546 if restart is True:
547 ##Find the right image to restart
548 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
549 if running_image:
550 image_name = running_image[0]['Image']
551 try:
552 image = image_name.split(':')[0]
553 tag = image_name.split(':')[1]
554 except: pass
555
A R Karthickaa54a1c2016-12-15 11:42:08 -0800556 if quagga_config is None:
557 quagga_config = Onos.QUAGGA_CONFIG
558 super(Onos, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = quagga_config)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700559 self.boot_delay = boot_delay
A R Karthickec2db322016-11-17 15:06:01 -0800560 self.data_map = None
A R Karthickc69d73e2017-01-20 11:44:34 -0800561 instance_memory = (get_mem(jvm_heap_size = Onos.JVM_HEAP_SIZE, instances = Onos.MAX_INSTANCES),) * 2
562 self.env['JAVA_OPTS'] = self.JAVA_OPTS_FORMAT.format(*instance_memory)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700563 if cluster is True:
564 self.ports = []
A R Karthickec2db322016-11-17 15:06:01 -0800565 if data_volume is not None:
566 self.data_map = self.get_data_map(data_volume, self.guest_data_dir)
567 self.host_guest_map = self.host_guest_map + self.data_map
A R Karthick2b93d6a2016-09-06 15:19:09 -0700568 if os.access(self.cluster_cfg, os.F_OK):
569 try:
570 os.unlink(self.cluster_cfg)
571 except: pass
572
573 self.host_config = self.create_host_config(port_list = self.ports,
574 host_guest_map = self.host_guest_map)
575 self.volumes = []
576 for _,g in self.host_guest_map:
577 self.volumes.append(g)
578
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700579 if restart is True and self.exists():
580 self.kill()
A R Karthick2b93d6a2016-09-06 15:19:09 -0700581
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700582 if not self.exists():
583 self.remove_container(name, force=True)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700584 host_config = self.create_host_config(port_list = self.ports,
585 host_guest_map = self.host_guest_map)
586 volumes = []
587 for _,g in self.host_guest_map:
588 volumes.append(g)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700589 if network_cfg is not None:
A R Karthick81acbff2016-06-17 14:45:16 -0700590 json_data = json.dumps(network_cfg, indent=4)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700591 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
592 f.write(json_data)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800593 if cluster is False or async is False:
594 print('Starting ONOS container %s' %self.name)
595 self.start(ports = self.ports, environment = self.env,
596 host_config = self.host_config, volumes = self.volumes, tty = True)
597 if not restart:
598 ##wait a bit before fetching IP to regenerate cluster cfg
599 time.sleep(5)
600 ip = self.ip()
601 ##Just a quick hack/check to ensure we don't regenerate in the common case.
602 ##As ONOS is usually the first test container that is started
603 if cluster is False:
604 if ip != self.CLUSTER_CFG_IP or not os.access(self.cluster_cfg, os.F_OK):
605 print('Regenerating ONOS cluster cfg for ip %s' %ip)
606 self.generate_cluster_cfg(ip)
607 self.kill()
608 self.remove_container(self.name, force=True)
609 print('Restarting ONOS container %s' %self.name)
610 self.start(ports = self.ports, environment = self.env,
611 host_config = self.host_config, volumes = self.volumes, tty = True)
612 print('Waiting for ONOS to boot')
613 time.sleep(boot_delay)
614 self.wait_for_onos_start(self.ip())
615 self.running = True
616 else:
617 self.running = False
618 else:
619 self.running = True
620 if self.running:
621 self.ipaddr = self.ip()
622 if cluster is False:
623 self.install_cord_apps(self.ipaddr)
A R Karthick19aaf5c2016-11-09 17:47:57 -0800624
A.R Karthickc4e474d2016-12-12 15:24:57 -0800625 @classmethod
626 def get_quagga_config(cls, instance = 0):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800627 quagga_config = copy.deepcopy(cls.QUAGGA_CONFIG)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800628 if instance == 0:
629 return quagga_config
630 ip = quagga_config[0]['ip']
631 octets = ip.split('.')
A R Karthickaa54a1c2016-12-15 11:42:08 -0800632 octets[3] = str((int(octets[3]) + instance) & 255)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800633 ip = '.'.join(octets)
634 quagga_config[0]['ip'] = ip
635 return quagga_config
636
637 @classmethod
638 def start_cluster_async(cls, onos_instances):
639 instances = filter(lambda o: o.running == False, onos_instances)
640 if not instances:
641 return
642 tpool = ThreadPool(len(instances), queue_size = 1, wait_timeout = 1)
643 for onos in instances:
644 tpool.addTask(onos.start_async)
645 tpool.cleanUpThreads()
646
647 def start_async(self):
648 print('Starting ONOS container %s' %self.name)
649 self.start(ports = self.ports, environment = self.env,
650 host_config = self.host_config, volumes = self.volumes, tty = True)
651 time.sleep(3)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700652 self.ipaddr = self.ip()
A.R Karthickc4e474d2016-12-12 15:24:57 -0800653 print('Waiting for ONOS container %s to start' %self.name)
654 self.wait_for_onos_start(self.ipaddr)
655 self.running = True
656 print('ONOS container %s started' %self.name)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700657
A R Karthick2b93d6a2016-09-06 15:19:09 -0700658 @classmethod
A R Karthick19aaf5c2016-11-09 17:47:57 -0800659 def wait_for_onos_start(cls, ip, tries = 30):
A R Karthick973010f2017-02-06 16:41:51 -0800660 onos_log = OnosLog(host = ip, log_file = Onos.guest_log_file)
A R Karthick19aaf5c2016-11-09 17:47:57 -0800661 num_tries = 0
662 started = None
663 while not started and num_tries < tries:
664 time.sleep(3)
665 started = onos_log.search_log_pattern('ApplicationManager .* Started')
666 num_tries += 1
667
A R Karthick19aaf5c2016-11-09 17:47:57 -0800668 if not started:
669 print('ONOS did not start')
670 else:
671 print('ONOS started')
672 return started
673
674 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700675 def setup_cluster_deprecated(cls, onos_instances, image_name = None):
676 if not onos_instances or len(onos_instances) < 2:
677 return
678 ips = []
679 if image_name is not None:
680 ips = Container.ips(image_name)
681 else:
682 for onos in onos_instances:
683 ips.append(onos.ipaddr)
684 Onos.cluster_instances = onos_instances
685 Onos.cluster_mode = True
686 ##regenerate the cluster json with the 3 instance ips before restarting them back
687 print('Generating cluster cfg for ONOS instances with ips %s' %ips)
688 Onos.generate_cluster_cfg(ips)
689 for onos in onos_instances:
690 onos.kill()
691 onos.remove_container(onos.name, force=True)
692 print('Restarting ONOS container %s for forming cluster' %onos.name)
693 onos.start(ports = onos.ports, environment = onos.env,
694 host_config = onos.host_config, volumes = onos.volumes, tty = True)
695 print('Waiting %d seconds for ONOS %s to boot' %(onos.boot_delay, onos.name))
696 time.sleep(onos.boot_delay)
697 onos.ipaddr = onos.ip()
698 onos.install_cord_apps(onos.ipaddr)
699
700 @classmethod
701 def setup_cluster(cls, onos_instances, image_name = None):
702 if not onos_instances or len(onos_instances) < 2:
703 return
704 ips = []
705 if image_name is not None:
706 ips = Container.ips(image_name)
707 else:
708 for onos in onos_instances:
709 ips.append(onos.ipaddr)
710 Onos.cluster_instances = onos_instances
711 Onos.cluster_mode = True
712 ##regenerate the cluster json with the 3 instance ips before restarting them back
713 print('Forming cluster for ONOS instances with ips %s' %ips)
714 Onos.form_cluster(ips)
715 ##wait for the cluster to be formed
716 print('Waiting for the cluster to be formed')
717 time.sleep(60)
718 for onos in onos_instances:
719 onos.install_cord_apps(onos.ipaddr)
720
721 @classmethod
A R Karthicke2c24bd2016-10-07 14:51:38 -0700722 def add_cluster(cls, count = 1, network_cfg = None):
723 if not cls.cluster_instances or Onos.cluster_mode is False:
724 return
725 for i in range(count):
726 name = '{}-{}'.format(Onos.NAME, len(cls.cluster_instances)+1)
727 onos = cls(name = name, image = Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX,
728 cluster = True, network_cfg = network_cfg)
729 cls.cluster_instances.append(onos)
730
731 cls.setup_cluster(cls.cluster_instances)
732
733 @classmethod
A.R Karthick2560f042016-11-30 14:38:52 -0800734 def restart_cluster(cls, network_cfg = None, timeout = 10, setup = False):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700735 if cls.cluster_mode is False:
736 return
737 if not cls.cluster_instances:
738 return
739
740 if network_cfg is not None:
741 json_data = json.dumps(network_cfg, indent=4)
742 with open('{}/network-cfg.json'.format(cls.host_config_dir), 'w') as f:
743 f.write(json_data)
744
A.R Karthick2560f042016-11-30 14:38:52 -0800745 cls.cleanup_cluster()
746 if timeout > 0:
747 time.sleep(timeout)
748
A R Karthickaa54a1c2016-12-15 11:42:08 -0800749 #start the instances asynchronously
750 cls.start_cluster_async(cls.cluster_instances)
751 time.sleep(5)
A.R Karthick2560f042016-11-30 14:38:52 -0800752 ##form the cluster as appropriate
753 if setup is True:
754 cls.setup_cluster(cls.cluster_instances)
A R Karthickaa54a1c2016-12-15 11:42:08 -0800755 else:
756 for onos in cls.cluster_instances:
757 onos.install_cord_apps(onos.ipaddr)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700758
759 @classmethod
760 def cluster_ips(cls):
761 if cls.cluster_mode is False:
762 return []
763 if not cls.cluster_instances:
764 return []
765 ips = [ onos.ipaddr for onos in cls.cluster_instances ]
766 return ips
767
768 @classmethod
769 def cleanup_cluster(cls):
770 if cls.cluster_mode is False:
771 return
772 if not cls.cluster_instances:
773 return
774 for onos in cls.cluster_instances:
775 if onos.exists():
776 onos.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800777 onos.running = False
A R Karthick2b93d6a2016-09-06 15:19:09 -0700778 onos.remove_container(onos.name, force=True)
A R Karthickd44cea12016-07-20 12:16:41 -0700779
A.R Karthick95d044e2016-06-10 18:44:36 -0700780 @classmethod
A R Karthickde6b9dc2016-11-29 17:46:16 -0800781 def restart_node(cls, node = None, network_cfg = None, timeout = 10):
A R Karthick889d9652016-10-03 14:13:45 -0700782 if node is None:
783 cls(restart = True, network_cfg = network_cfg, image = cls.IMAGE, tag = cls.TAG)
784 else:
785 #Restarts a node in the cluster
786 valid_node = filter(lambda onos: node in [ onos.ipaddr, onos.name ], cls.cluster_instances)
787 if valid_node:
788 onos = valid_node.pop()
789 if onos.exists():
790 onos.kill()
791 onos.remove_container(onos.name, force=True)
A R Karthickde6b9dc2016-11-29 17:46:16 -0800792 if timeout > 0:
793 time.sleep(timeout)
A R Karthick889d9652016-10-03 14:13:45 -0700794 print('Restarting ONOS container %s' %onos.name)
795 onos.start(ports = onos.ports, environment = onos.env,
796 host_config = onos.host_config, volumes = onos.volumes, tty = True)
A R Karthick889d9652016-10-03 14:13:45 -0700797 onos.ipaddr = onos.ip()
A.R Karthick2560f042016-11-30 14:38:52 -0800798 onos.wait_for_onos_start(onos.ipaddr)
799 onos.install_cord_apps(onos.ipaddr)
A R Karthick889d9652016-10-03 14:13:45 -0700800
801 @classmethod
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700802 def install_cord_apps(cls, onos_ip = None):
A.R Karthick95d044e2016-06-10 18:44:36 -0700803 for app, version in cls.onos_cord_apps:
804 app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version)
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700805 ok, code = OnosCtrl.install_app(app_file, onos_ip = onos_ip)
A.R Karthick95d044e2016-06-10 18:44:36 -0700806 ##app already installed (conflicts)
807 if code in [ 409 ]:
808 ok = True
809 print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install'))
810 time.sleep(2)
811
A.R Karthick1700e0e2016-10-06 18:16:57 -0700812class OnosStopWrapper(Container):
813 def __init__(self, name):
814 super(OnosStopWrapper, self).__init__(name, Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX)
815 if self.exists():
816 self.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800817 self.running = False
A.R Karthick1700e0e2016-10-06 18:16:57 -0700818 else:
819 if Onos.cluster_mode is True:
820 valid_node = filter(lambda onos: name in [ onos.ipaddr, onos.name ], Onos.cluster_instances)
821 if valid_node:
822 onos = valid_node.pop()
823 if onos.exists():
824 onos.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800825 onos.running = False
A.R Karthick1700e0e2016-10-06 18:16:57 -0700826
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700827class Radius(Container):
828 ports = [ 1812, 1813 ]
A R Karthick41adfce2016-06-10 09:51:25 -0700829 env = {'TIMEZONE':'America/Los_Angeles',
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700830 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
831 }
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700832 host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700833 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700834 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700835 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700836 start_command = os.path.join(guest_config_dir, 'start-radius.py')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700837 host_guest_map = ( (host_db_dir, guest_db_dir),
838 (host_config_dir, guest_config_dir)
839 )
A R Karthickf7a613b2017-02-24 09:36:44 -0800840 IMAGE = 'cordtest/radius'
Chetan Gaonker503032a2016-05-12 12:06:29 -0700841 NAME = 'cord-radius'
842
A R Karthick07608ef2016-08-23 16:51:19 -0700843 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
A R Karthick85eb1862017-01-23 16:10:57 -0800844 boot_delay = 10, restart = False, update = False, network = None):
A R Karthick07608ef2016-08-23 16:51:19 -0700845 super(Radius, self).__init__(name, image, prefix = prefix, tag = tag, command = self.start_command)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700846 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700847 self.build_image(self.image_name)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700848 if restart is True and self.exists():
849 self.kill()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700850 if not self.exists():
851 self.remove_container(name, force=True)
852 host_config = self.create_host_config(port_list = self.ports,
853 host_guest_map = self.host_guest_map)
854 volumes = []
855 for _,g in self.host_guest_map:
856 volumes.append(g)
A R Karthick41adfce2016-06-10 09:51:25 -0700857 self.start(ports = self.ports, environment = self.env,
858 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700859 host_config = host_config, tty = True)
A R Karthick85eb1862017-01-23 16:10:57 -0800860 if network is not None:
861 Container.connect_to_network(self.name, network)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700862 time.sleep(boot_delay)
863
864 @classmethod
865 def build_image(cls, image):
866 print('Building Radius image %s' %image)
867 dockerfile = '''
868FROM hbouvier/docker-radius
869MAINTAINER chetan@ciena.com
870LABEL RUN docker pull hbouvier/docker-radius
871LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius
A R Karthickc762df42016-05-25 10:09:21 -0700872RUN apt-get update && \
873 apt-get -y install python python-pexpect strace
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700874WORKDIR /root
875CMD ["/etc/freeradius/start-radius.py"]
876'''
877 super(Radius, cls).build_image(dockerfile, image)
878 print('Done building image %s' %image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700879
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700880class Quagga(Container):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800881 QUAGGA_CONFIG = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 },
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700882 { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 },
883 )
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700884 ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ]
885 host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config')
886 guest_quagga_config = '/root/config'
887 quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
888 host_guest_map = ( (host_quagga_config, guest_quagga_config), )
A R Karthickf7a613b2017-02-24 09:36:44 -0800889 IMAGE = 'cordtest/quagga'
Chetan Gaonker503032a2016-05-12 12:06:29 -0700890 NAME = 'cord-quagga'
891
A R Karthick07608ef2016-08-23 16:51:19 -0700892 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
A R Karthick85eb1862017-01-23 16:10:57 -0800893 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False,
894 network = None):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800895 super(Quagga, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.QUAGGA_CONFIG)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700896 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700897 self.build_image(self.image_name)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700898 if restart is True and self.exists():
899 self.kill()
900 if not self.exists():
901 self.remove_container(name, force=True)
A R Karthick41adfce2016-06-10 09:51:25 -0700902 host_config = self.create_host_config(port_list = self.ports,
903 host_guest_map = self.host_guest_map,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700904 privileged = True)
905 volumes = []
906 for _,g in self.host_guest_map:
907 volumes.append(g)
908 self.start(ports = self.ports,
A R Karthick41adfce2016-06-10 09:51:25 -0700909 host_config = host_config,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700910 volumes = volumes, tty = True)
A R Karthick85eb1862017-01-23 16:10:57 -0800911 if network is not None:
912 Container.connect_to_network(self.name, network)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700913 print('Starting Quagga on container %s' %self.name)
914 self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
915 time.sleep(boot_delay)
916
917 @classmethod
918 def build_image(cls, image):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800919 onos_quagga_ip = Onos.QUAGGA_CONFIG[0]['ip']
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700920 print('Building Quagga image %s' %image)
921 dockerfile = '''
A R Karthick41adfce2016-06-10 09:51:25 -0700922FROM ubuntu:14.04
923MAINTAINER chetan@ciena.com
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700924WORKDIR /root
925RUN useradd -M quagga
926RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga
927RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga
A R Karthick973ea692016-10-17 12:23:02 -0700928RUN apt-get update && apt-get install -qy git autoconf libtool gawk make telnet libreadline6-dev pkg-config protobuf-c-compiler
ChetanGaonkerb5b46c62016-08-16 12:02:53 -0700929RUN git clone git://git.savannah.nongnu.org/quagga.git quagga && \
A R Karthick8f69c2c2016-10-21 11:43:26 -0700930(cd quagga && git checkout quagga-1.0.20160315 && ./bootstrap.sh && \
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700931sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \
932./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install)
933RUN ldconfig
934'''.format(onos_quagga_ip)
935 super(Quagga, cls).build_image(dockerfile, image)
936 print('Done building image %s' %image)
A R Karthick81acbff2016-06-17 14:45:16 -0700937
A.R Karthick1700e0e2016-10-06 18:16:57 -0700938class QuaggaStopWrapper(Container):
939 def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'candidate'):
940 super(QuaggaStopWrapper, self).__init__(name, image, prefix = Container.IMAGE_PREFIX, tag = tag)
941 if self.exists():
942 self.kill()
943
944
A R Karthick81acbff2016-06-17 14:45:16 -0700945def reinitContainerClients():
946 docker_netns.dckr = Client()
947 Container.dckr = Client()
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700948
949class Xos(Container):
950 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
951 TAG = 'latest'
952 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700953 host_guest_map = None
954 env = None
955 ports = None
956 volumes = None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700957
A R Karthick6e80afd2016-10-10 16:03:12 -0700958 @classmethod
959 def get_cmd(cls, img_name):
960 cmd = cls.dckr.inspect_image(img_name)['Config']['Cmd']
961 return ' '.join(cmd)
962
A R Karthicke3bde962016-09-27 15:06:35 -0700963 def __init__(self, name, image, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700964 boot_delay = 20, restart = False, network_cfg = None, update = False):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700965 if restart is True:
966 ##Find the right image to restart
967 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
968 if running_image:
969 image_name = running_image[0]['Image']
970 try:
971 image = image_name.split(':')[0]
972 tag = image_name.split(':')[1]
973 except: pass
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700974 super(Xos, self).__init__(name, image, prefix = prefix, tag = tag)
975 if update is True or not self.img_exists():
976 self.build_image(self.image_name)
A R Karthick6e80afd2016-10-10 16:03:12 -0700977 self.command = self.get_cmd(self.image_name).strip() or None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700978 if restart is True and self.exists():
979 self.kill()
980 if not self.exists():
981 self.remove_container(name, force=True)
A R Karthicke3bde962016-09-27 15:06:35 -0700982 host_config = self.create_host_config(port_list = self.ports,
983 host_guest_map = self.host_guest_map,
984 privileged = True)
985 print('Starting XOS container %s' %self.name)
986 self.start(ports = self.ports, environment = self.env, host_config = host_config,
987 volumes = self.volumes, tty = True)
988 print('Waiting %d seconds for XOS Base Container to boot' %(boot_delay))
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700989 time.sleep(boot_delay)
990
991 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700992 def build_image(cls, image, dockerfile_path, image_target = 'build'):
993 cmd = 'cd {} && make {}'.format(dockerfile_path, image_target)
994 print('Building XOS %s' %image)
995 res = os.system(cmd)
996 print('Done building image %s. Image build %s' %(image, 'successful' if res == 0 else 'failed'))
997 return res
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700998
A R Karthicke3bde962016-09-27 15:06:35 -0700999class XosServer(Xos):
1000 ports = [8000,9998,9999]
1001 NAME = 'xos-server'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001002 IMAGE = 'xosproject/xos'
A R Karthicke3bde962016-09-27 15:06:35 -07001003 BASE_IMAGE = 'xosproject/xos-base'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001004 TAG = 'latest'
1005 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -07001006 dockerfile_path = os.path.join(Xos.setup_dir, 'xos')
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001007
A R Karthicke3bde962016-09-27 15:06:35 -07001008 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001009 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001010 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001011
1012 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -07001013 def build_image(cls, image = IMAGE):
1014 ##build the base image and then build the server image
1015 Xos.build_image(cls.BASE_IMAGE, cls.dockerfile_path, image_target = 'base')
1016 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001017
A R Karthicke3bde962016-09-27 15:06:35 -07001018class XosSynchronizerOpenstack(Xos):
1019 ports = [2375,]
1020 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer')
1021 NAME = 'xos-synchronizer'
1022 IMAGE = 'xosproject/xos-synchronizer-openstack'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001023 TAG = 'latest'
1024 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -07001025 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001026
A R Karthicke3bde962016-09-27 15:06:35 -07001027 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -07001028 tag = TAG, boot_delay = 20, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001029 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001030
1031 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -07001032 def build_image(cls, image = IMAGE):
1033 XosServer.build_image()
1034 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001035
A R Karthicke3bde962016-09-27 15:06:35 -07001036class XosSynchronizerOnboarding(Xos):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001037 NAME = 'xos-synchronizer-onboarding'
1038 IMAGE = 'xosproject/xos-synchronizer-onboarding'
1039 TAG = 'latest'
1040 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -07001041 dockerfile_path = os.path.join(Xos.setup_dir, 'onboarding_synchronizer')
1042 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001043
A R Karthicke3bde962016-09-27 15:06:35 -07001044 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -07001045 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001046 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001047
1048 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -07001049 def build_image(cls, image = IMAGE):
1050 XosSynchronizerOpenstack.build_image()
1051 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001052
A R Karthicke3bde962016-09-27 15:06:35 -07001053class XosSynchronizerOpenvpn(Xos):
1054 NAME = 'xos-synchronizer-openvpn'
1055 IMAGE = 'xosproject/xos-openvpn'
1056 TAG = 'latest'
1057 PREFIX = ''
1058 dockerfile_path = os.path.join(Xos.setup_dir, 'openvpn')
1059 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001060
A R Karthicke3bde962016-09-27 15:06:35 -07001061 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -07001062 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001063 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1064
1065 @classmethod
1066 def build_image(cls, image = IMAGE):
1067 XosSynchronizerOpenstack.build_image()
1068 Xos.build_image(image, cls.dockerfile_path)
1069
1070class XosPostgresql(Xos):
1071 ports = [5432,]
1072 NAME = 'xos-db-postgres'
1073 IMAGE = 'xosproject/xos-postgres'
1074 TAG = 'latest'
1075 PREFIX = ''
1076 volumes = ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
1077 dockerfile_path = os.path.join(Xos.setup_dir, 'postgresql')
1078
1079 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -07001080 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001081 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1082
1083 @classmethod
1084 def build_image(cls, image = IMAGE):
1085 Xos.build_image(image, cls.dockerfile_path)
1086
1087class XosSyndicateMs(Xos):
1088 ports = [8080,]
1089 env = None
1090 NAME = 'xos-syndicate-ms'
1091 IMAGE = 'xosproject/syndicate-ms'
1092 TAG = 'latest'
1093 PREFIX = ''
1094 dockerfile_path = os.path.join(Xos.setup_dir, 'syndicate-ms')
1095
1096 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001097 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001098 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1099
1100 @classmethod
1101 def build_image(cls, image = IMAGE):
1102 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001103
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001104class XosSyncVtn(Xos):
1105 ports = [8080,]
1106 env = None
1107 NAME = 'xos-synchronizer-vtn'
1108 IMAGE = 'xosproject/xos-synchronizer-vtn'
1109 TAG = 'latest'
1110 PREFIX = ''
1111 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtn')
1112
1113 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001114 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001115 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1116
1117 @classmethod
1118 def build_image(cls, image = IMAGE):
1119 Xos.build_image(image, cls.dockerfile_path)
1120
1121class XosSyncVtr(Xos):
1122 ports = [8080,]
1123 env = None
1124 NAME = 'xos-synchronizer-vtr'
1125 IMAGE = 'xosproject/xos-synchronizer-vtr'
1126 TAG = 'latest'
1127 PREFIX = ''
1128 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtr')
1129
1130 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001131 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001132 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1133
1134 @classmethod
1135 def build_image(cls, image = IMAGE):
1136 Xos.build_image(image, cls.dockerfile_path)
1137
1138class XosSyncVsg(Xos):
1139 ports = [8080,]
1140 env = None
1141 NAME = 'xos-synchronizer-vsg'
1142 IMAGE = 'xosproject/xos-synchronizer-vsg'
1143 TAG = 'latest'
1144 PREFIX = ''
1145 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vsg')
1146
1147 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001148 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001149 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1150
1151 @classmethod
1152 def build_image(cls, image = IMAGE):
1153 Xos.build_image(image, cls.dockerfile_path)
1154
1155
1156class XosSyncOnos(Xos):
1157 ports = [8080,]
1158 env = None
1159 NAME = 'xos-synchronizer-onos'
1160 IMAGE = 'xosproject/xos-synchronizer-onos'
1161 TAG = 'latest'
1162 PREFIX = ''
1163 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-onos')
1164
1165 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001166 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001167 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1168
1169 @classmethod
1170 def build_image(cls, image = IMAGE):
1171 Xos.build_image(image, cls.dockerfile_path)
1172
1173class XosSyncFabric(Xos):
1174 ports = [8080,]
1175 env = None
1176 NAME = 'xos-synchronizer-fabric'
1177 IMAGE = 'xosproject/xos-synchronizer-fabric'
1178 TAG = 'latest'
1179 PREFIX = ''
1180 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-fabric')
1181
1182 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001183 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001184 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1185
1186 @classmethod
1187 def build_image(cls, image = IMAGE):
1188 Xos.build_image(image, cls.dockerfile_path)
A R Karthick19aaf5c2016-11-09 17:47:57 -08001189
1190if __name__ == '__main__':
1191 onos = Onos(boot_delay = 10, restart = True)