blob: 908ef1be84d152ccf540d14dbe124487fad9a8d2 [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 Karthickec2db322016-11-17 15:06:01 -080028from shutil import rmtree
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):
88 return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()]
89
90 @classmethod
91 def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False):
92 port_bindings = None
93 binds = None
94 if port_list:
95 port_bindings = {}
96 for p in port_list:
97 port_bindings[str(p)] = str(p)
98
99 if host_guest_map:
100 binds = []
101 for h, g in host_guest_map:
102 binds.append('{0}:{1}'.format(h, g))
103
104 return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged)
105
106 @classmethod
A R Karthick85eb1862017-01-23 16:10:57 -0800107 def connect_to_network(cls, name, network):
108 try:
109 cls.dckr.connect_container_to_network(name, network)
110 return True
111 except:
112 return False
113
114 @classmethod
115 def create_network(cls, network, subnet = None, gateway = None):
116 ipam_config = None
117 if subnet is not None and gateway is not None:
118 ipam_pool = dockerutils.create_ipam_pool(subnet = subnet, gateway = gateway)
119 ipam_config = dockerutils.create_ipam_config(pool_configs = [ipam_pool])
120 cls.dckr.create_network(network, driver='bridge', ipam = ipam_config)
121
122 @classmethod
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700123 def cleanup(cls, image):
A R Karthick09b1f4e2016-05-12 14:31:50 -0700124 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700125 for cnt in cnt_list:
126 print('Cleaning container %s' %cnt['Id'])
A.R Karthick95d044e2016-06-10 18:44:36 -0700127 if cnt.has_key('State') and cnt['State'] == 'running':
A R Karthick09b1f4e2016-05-12 14:31:50 -0700128 cls.dckr.kill(cnt['Id'])
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700129 cls.dckr.remove_container(cnt['Id'], force=True)
130
131 @classmethod
132 def remove_container(cls, name, force=True):
133 try:
134 cls.dckr.remove_container(name, force = force)
135 except: pass
136
137 def exists(self):
138 return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers()))
139
140 def img_exists(self):
A R Karthick6d98a592016-08-24 15:16:46 -0700141 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 -0700142
143 def ip(self):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700144 cnt_list = filter(lambda c: c['Names'][0] == '/{}'.format(self.name), self.dckr.containers())
145 #if not cnt_list:
146 # cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700147 cnt_settings = cnt_list.pop()
148 return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress']
149
A R Karthick2b93d6a2016-09-06 15:19:09 -0700150 @classmethod
151 def ips(cls, image_name):
152 cnt_list = filter(lambda c: c['Image'] == image_name, cls.dckr.containers())
153 ips = [ cnt['NetworkSettings']['Networks']['bridge']['IPAddress'] for cnt in cnt_list ]
154 return ips
155
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700156 def kill(self, remove = True):
157 self.dckr.kill(self.name)
158 self.dckr.remove_container(self.name, force=True)
159
A R Karthick41adfce2016-06-10 09:51:25 -0700160 def start(self, rm = True, ports = None, volumes = None, host_config = None,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700161 environment = None, tty = False, stdin_open = True):
162
163 if rm and self.exists():
164 print('Removing container:', self.name)
165 self.dckr.remove_container(self.name, force=True)
166
A R Karthick41adfce2016-06-10 09:51:25 -0700167 ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700168 detach=True, name=self.name,
A R Karthick41adfce2016-06-10 09:51:25 -0700169 environment = environment,
170 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700171 host_config = host_config, stdin_open=stdin_open, tty = tty)
172 self.dckr.start(container=self.name)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700173 if self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700174 self.connect_to_br()
175 self.id = ctn['Id']
176 return ctn
177
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000178 @classmethod
179 def pause_container(cls, image, delay):
180 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
181 for cnt in cnt_list:
182 print('Pause the container %s' %cnt['Id'])
183 if cnt.has_key('State') and cnt['State'] == 'running':
184 cls.dckr.pause(cnt['Id'])
185 if delay != 0:
186 time.sleep(delay)
187 for cnt in cnt_list:
188 print('Unpause the container %s' %cnt['Id'])
189 cls.dckr.unpause(cnt['Id'])
190 else:
191 print('Infinity time pause the container %s' %cnt['Id'])
192 return 'success'
193
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700194 def connect_to_br(self):
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700195 index = 0
A R Karthickaa54a1c2016-12-15 11:42:08 -0800196 self.CONFIG_LOCK.acquire()
197 try:
198 with docker_netns(self.name) as pid:
199 for quagga_config in self.quagga_config:
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700200 ip = IPRoute()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800201 br = ip.link_lookup(ifname=quagga_config['bridge'])
202 if len(br) == 0:
203 try:
204 ip.link_create(ifname=quagga_config['bridge'], kind='bridge')
205 except NetlinkError as e:
206 err, _ = e.args
207 if err == errno.EEXIST:
208 pass
209 else:
210 raise NetlinkError(*e.args)
211 br = ip.link_lookup(ifname=quagga_config['bridge'])
212 br = br[0]
213 ip.link('set', index=br, state='up')
214 ifname = '{0}-{1}'.format(self.name, index)
215 ifs = ip.link_lookup(ifname=ifname)
216 if len(ifs) > 0:
217 ip.link_remove(ifs[0])
218 peer_ifname = '{0}-{1}'.format(pid, index)
219 ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname)
220 host = ip.link_lookup(ifname=ifname)[0]
221 ip.link('set', index=host, master=br)
222 ip.link('set', index=host, state='up')
223 guest = ip.link_lookup(ifname=peer_ifname)[0]
224 ip.link('set', index=guest, net_ns_fd=pid)
225 with Namespace(pid, 'net'):
226 ip = IPRoute()
227 ip.link('set', index=guest, ifname='eth{}'.format(index+1))
228 ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask'])
229 ip.link('set', index=guest, state='up')
230 index += 1
231 finally:
232 self.CONFIG_LOCK.release()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700233
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000234 def execute(self, cmd, tty = True, stream = False, shell = False, detach = True):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700235 res = 0
236 if type(cmd) == str:
237 cmds = (cmd,)
238 else:
239 cmds = cmd
240 if shell:
241 for c in cmds:
242 res += os.system('docker exec {0} {1}'.format(self.name, c))
243 return res
244 for c in cmds:
245 i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True)
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000246 self.dckr.exec_start(i['Id'], stream = stream, detach=detach)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700247 result = self.dckr.exec_inspect(i['Id'])
248 res += 0 if result['ExitCode'] == None else result['ExitCode']
249 return res
250
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700251 def restart(self, timeout =10):
252 return self.dckr.restart(self.name, timeout)
253
A R Karthickc69d73e2017-01-20 11:44:34 -0800254def get_mem(jvm_heap_size = None, instances = 1):
A R Karthick1f908202016-11-16 17:32:20 -0800255 if instances <= 0:
256 instances = 1
A R Karthickc69d73e2017-01-20 11:44:34 -0800257 heap_size = jvm_heap_size
258 heap_size_i = 0
259 #sanitize the heap size config
260 if heap_size is not None:
261 if not heap_size.isdigit():
262 try:
263 heap_size_i = int(heap_size[:-1])
264 suffix = heap_size[-1]
265 if suffix == 'M':
266 heap_size_i /= 1024 #convert to gigs
267 except:
268 ##invalid suffix length probably. Fall back to default
269 heap_size = None
270 else:
271 heap_size_i = int(heap_size)
272
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700273 with open('/proc/meminfo', 'r') as fd:
274 meminfo = fd.readlines()
275 mem = 0
276 for m in meminfo:
277 if m.startswith('MemTotal:') or m.startswith('SwapTotal:'):
278 mem += int(m.split(':')[1].strip().split()[0])
279
A R Karthick1f908202016-11-16 17:32:20 -0800280 mem = max(mem/1024/1024/2/instances, 1)
Chetan Gaonker6d0a7b02016-05-03 16:57:28 -0700281 mem = min(mem, 16)
A R Karthickc69d73e2017-01-20 11:44:34 -0800282
283 if heap_size_i:
284 #we take the minimum of the provided heap size and max allowed heap size
285 heap_size_i = min(heap_size_i, mem)
286 else:
287 heap_size_i = mem
288
289 return '{}G'.format(heap_size_i)
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700290
A R Karthickd44cea12016-07-20 12:16:41 -0700291class OnosCord(Container):
292 """Use this when running the cord tester agent on the onos compute node"""
293 onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord')
294 onos_config_dir_guest = '/root/onos/config'
295 onos_config_dir = os.path.join(onos_cord_dir, 'config')
296 docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml')
297
A R Karthickbd9b8a32016-07-21 09:56:45 -0700298 def __init__(self, onos_ip, conf, boot_delay = 60):
299 self.onos_ip = onos_ip
A R Karthickd44cea12016-07-20 12:16:41 -0700300 self.cord_conf_dir = conf
A R Karthickbd9b8a32016-07-21 09:56:45 -0700301 self.boot_delay = boot_delay
A R Karthickd44cea12016-07-20 12:16:41 -0700302 if os.access(self.cord_conf_dir, os.F_OK) and not os.access(self.onos_cord_dir, os.F_OK):
303 os.mkdir(self.onos_cord_dir)
304 os.mkdir(self.onos_config_dir)
305 ##copy the config file from cord-tester-config
306 cmd = 'cp {}/* {}'.format(self.cord_conf_dir, self.onos_cord_dir)
307 os.system(cmd)
308
309 ##update the docker yaml with the config volume
310 with open(self.docker_yaml, 'r') as f:
311 yaml_config = yaml.load(f)
312 image = yaml_config['services'].keys()[0]
313 name = 'cordtestercord_{}_1'.format(image)
314 volumes = yaml_config['services'][image]['volumes']
315 config_volumes = filter(lambda e: e.find(self.onos_config_dir_guest) >= 0, volumes)
316 if not config_volumes:
317 config_volume = '{}:{}'.format(self.onos_config_dir, self.onos_config_dir_guest)
318 volumes.append(config_volume)
319 docker_yaml_changed = '{}-changed'.format(self.docker_yaml)
320 with open(docker_yaml_changed, 'w') as wf:
321 yaml.dump(yaml_config, wf)
322
323 os.rename(docker_yaml_changed, self.docker_yaml)
324 self.volumes = volumes
325
326 super(OnosCord, self).__init__(name, image, tag = '')
327 cord_conf_dir_basename = os.path.basename(self.cord_conf_dir.replace('-', ''))
328 self.xos_onos_name = '{}_{}_1'.format(cord_conf_dir_basename, image)
329 ##Create an container instance of xos onos
330 self.xos_onos = Container(self.xos_onos_name, image, tag = '')
331
332 def start(self, restart = False, network_cfg = None):
333 if restart is True:
334 if self.exists():
335 ##Kill the existing instance
336 print('Killing container %s' %self.name)
337 self.kill()
338 if self.xos_onos.exists():
339 print('Killing container %s' %self.xos_onos.name)
340 self.xos_onos.kill()
341
342 if network_cfg is not None:
343 json_data = json.dumps(network_cfg, indent=4)
344 with open('{}/network-cfg.json'.format(self.onos_config_dir), 'w') as f:
345 f.write(json_data)
346
347 #start the container using docker-compose
348 cmd = 'cd {} && docker-compose up -d'.format(self.onos_cord_dir)
349 os.system(cmd)
A R Karthickbd9b8a32016-07-21 09:56:45 -0700350 #Delay to make sure ONOS fully boots
351 time.sleep(self.boot_delay)
352 Onos.install_cord_apps(onos_ip = self.onos_ip)
A R Karthickd44cea12016-07-20 12:16:41 -0700353
354 def build_image(self):
355 build_cmd = 'cd {} && docker-compose build'.format(self.onos_cord_dir)
356 os.system(build_cmd)
357
A.R Karthick1700e0e2016-10-06 18:16:57 -0700358class OnosCordStopWrapper(Container):
359 onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord')
360 docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml')
361
362 def __init__(self):
363 if os.access(self.docker_yaml, os.F_OK):
364 with open(self.docker_yaml, 'r') as f:
365 yaml_config = yaml.load(f)
366 image = yaml_config['services'].keys()[0]
367 name = 'cordtestercord_{}_1'.format(image)
368 super(OnosCordStopWrapper, self).__init__(name, image, tag = '')
369 if self.exists():
370 print('Killing container %s' %self.name)
371 self.kill()
372
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700373class Onos(Container):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800374 QUAGGA_CONFIG = [ { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, ]
A R Karthicka2492c12016-12-16 10:31:51 -0800375 MAX_INSTANCES = 3
A R Karthickc69d73e2017-01-20 11:44:34 -0800376 JVM_HEAP_SIZE = None
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700377 SYSTEM_MEMORY = (get_mem(),) * 2
A R Karthicka2492c12016-12-16 10:31:51 -0800378 INSTANCE_MEMORY = (get_mem(instances=MAX_INSTANCES),) * 2
A R Karthickc69d73e2017-01-20 11:44:34 -0800379 JAVA_OPTS_FORMAT = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'
380 JAVA_OPTS_DEFAULT = JAVA_OPTS_FORMAT.format(*SYSTEM_MEMORY) #-XX:+PrintGCDetails -XX:+PrintGCTimeStamps'
381 JAVA_OPTS_CLUSTER_DEFAULT = JAVA_OPTS_FORMAT.format(*INSTANCE_MEMORY)
382 env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,vrouter', 'JAVA_OPTS' : JAVA_OPTS_DEFAULT }
A.R Karthickdfeadb02016-11-30 17:55:51 -0800383 onos_cord_apps = ( ('cord-config', '1.1-SNAPSHOT'),
384 ('aaa', '1.1-SNAPSHOT'),
385 ('igmp', '1.1-SNAPSHOT'),
386 #('vtn', '1.1-SNAPSHOT'),
A.R Karthick95d044e2016-06-10 18:44:36 -0700387 )
A.R Karthickc4e474d2016-12-12 15:24:57 -0800388 ports = [] #[ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ]
A R Karthickf2f4ca62016-08-17 10:34:08 -0700389 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
390 host_config_dir = os.path.join(setup_dir, 'onos-config')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700391 guest_config_dir = '/root/onos/config'
A R Karthickec2db322016-11-17 15:06:01 -0800392 guest_data_dir = '/root/onos/apache-karaf-3.0.5/data'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700393 onos_gen_partitions = os.path.join(setup_dir, 'onos-gen-partitions')
A R Karthick2b93d6a2016-09-06 15:19:09 -0700394 onos_form_cluster = os.path.join(setup_dir, 'onos-form-cluster')
A.R Karthick95d044e2016-06-10 18:44:36 -0700395 cord_apps_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'apps')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700396 host_guest_map = ( (host_config_dir, guest_config_dir), )
A R Karthick2b93d6a2016-09-06 15:19:09 -0700397 cluster_cfg = os.path.join(host_config_dir, 'cluster.json')
398 cluster_mode = False
399 cluster_instances = []
Chetan Gaonker503032a2016-05-12 12:06:29 -0700400 NAME = 'cord-onos'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700401 ##the ip of ONOS in default cluster.json in setup/onos-config
402 CLUSTER_CFG_IP = '172.17.0.2'
A R Karthick07608ef2016-08-23 16:51:19 -0700403 IMAGE = 'onosproject/onos'
404 TAG = 'latest'
405 PREFIX = ''
A R Karthickf2f4ca62016-08-17 10:34:08 -0700406
407 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700408 def generate_cluster_cfg(cls, ip):
409 if type(ip) in [ list, tuple ]:
410 ips = ' '.join(ip)
411 else:
412 ips = ip
A R Karthickf2f4ca62016-08-17 10:34:08 -0700413 try:
A R Karthick2b93d6a2016-09-06 15:19:09 -0700414 cmd = '{} {} {}'.format(cls.onos_gen_partitions, cls.cluster_cfg, ips)
415 os.system(cmd)
416 except: pass
417
418 @classmethod
419 def form_cluster(cls, ips):
420 nodes = ' '.join(ips)
421 try:
422 cmd = '{} {}'.format(cls.onos_form_cluster, nodes)
A R Karthickf2f4ca62016-08-17 10:34:08 -0700423 os.system(cmd)
424 except: pass
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700425
A R Karthick9d48c652016-09-15 09:16:36 -0700426 @classmethod
427 def cleanup_runtime(cls):
428 '''Cleanup ONOS runtime generated files'''
429 files = ( Onos.cluster_cfg, os.path.join(Onos.host_config_dir, 'network-cfg.json') )
430 for f in files:
431 if os.access(f, os.F_OK):
432 try:
433 os.unlink(f)
434 except: pass
435
A R Karthickec2db322016-11-17 15:06:01 -0800436 @classmethod
437 def get_data_map(cls, host_volume, guest_volume_dir):
438 host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume))
439 if not os.path.exists(host_volume_dir):
440 os.mkdir(host_volume_dir)
441 return ( (host_volume_dir, guest_volume_dir), )
442
443 @classmethod
444 def remove_data_map(cls, host_volume, guest_volume_dir):
445 host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume))
446 if os.path.exists(host_volume_dir):
447 rmtree(host_volume_dir)
448
449 def remove_data_volume(self):
450 if self.data_map is not None:
451 self.remove_data_map(*self.data_map)
452
A.R Karthick1700e0e2016-10-06 18:16:57 -0700453 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthickec2db322016-11-17 15:06:01 -0800454 boot_delay = 20, restart = False, network_cfg = None,
A R Karthick85eb1862017-01-23 16:10:57 -0800455 cluster = False, data_volume = None, async = False, quagga_config = None,
456 network = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700457 if restart is True:
458 ##Find the right image to restart
459 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
460 if running_image:
461 image_name = running_image[0]['Image']
462 try:
463 image = image_name.split(':')[0]
464 tag = image_name.split(':')[1]
465 except: pass
466
A R Karthickaa54a1c2016-12-15 11:42:08 -0800467 if quagga_config is None:
468 quagga_config = Onos.QUAGGA_CONFIG
469 super(Onos, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = quagga_config)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700470 self.boot_delay = boot_delay
A R Karthickec2db322016-11-17 15:06:01 -0800471 self.data_map = None
A R Karthickc69d73e2017-01-20 11:44:34 -0800472 instance_memory = (get_mem(jvm_heap_size = Onos.JVM_HEAP_SIZE, instances = Onos.MAX_INSTANCES),) * 2
473 self.env['JAVA_OPTS'] = self.JAVA_OPTS_FORMAT.format(*instance_memory)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700474 if cluster is True:
475 self.ports = []
A R Karthickec2db322016-11-17 15:06:01 -0800476 if data_volume is not None:
477 self.data_map = self.get_data_map(data_volume, self.guest_data_dir)
478 self.host_guest_map = self.host_guest_map + self.data_map
A R Karthick2b93d6a2016-09-06 15:19:09 -0700479 if os.access(self.cluster_cfg, os.F_OK):
480 try:
481 os.unlink(self.cluster_cfg)
482 except: pass
483
484 self.host_config = self.create_host_config(port_list = self.ports,
485 host_guest_map = self.host_guest_map)
486 self.volumes = []
487 for _,g in self.host_guest_map:
488 self.volumes.append(g)
489
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700490 if restart is True and self.exists():
491 self.kill()
A R Karthick2b93d6a2016-09-06 15:19:09 -0700492
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700493 if not self.exists():
494 self.remove_container(name, force=True)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700495 host_config = self.create_host_config(port_list = self.ports,
496 host_guest_map = self.host_guest_map)
497 volumes = []
498 for _,g in self.host_guest_map:
499 volumes.append(g)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700500 if network_cfg is not None:
A R Karthick81acbff2016-06-17 14:45:16 -0700501 json_data = json.dumps(network_cfg, indent=4)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700502 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
503 f.write(json_data)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800504 if cluster is False or async is False:
505 print('Starting ONOS container %s' %self.name)
506 self.start(ports = self.ports, environment = self.env,
507 host_config = self.host_config, volumes = self.volumes, tty = True)
508 if not restart:
509 ##wait a bit before fetching IP to regenerate cluster cfg
510 time.sleep(5)
511 ip = self.ip()
512 ##Just a quick hack/check to ensure we don't regenerate in the common case.
513 ##As ONOS is usually the first test container that is started
514 if cluster is False:
515 if ip != self.CLUSTER_CFG_IP or not os.access(self.cluster_cfg, os.F_OK):
516 print('Regenerating ONOS cluster cfg for ip %s' %ip)
517 self.generate_cluster_cfg(ip)
518 self.kill()
519 self.remove_container(self.name, force=True)
520 print('Restarting ONOS container %s' %self.name)
521 self.start(ports = self.ports, environment = self.env,
522 host_config = self.host_config, volumes = self.volumes, tty = True)
523 print('Waiting for ONOS to boot')
524 time.sleep(boot_delay)
525 self.wait_for_onos_start(self.ip())
526 self.running = True
527 else:
528 self.running = False
529 else:
530 self.running = True
531 if self.running:
532 self.ipaddr = self.ip()
533 if cluster is False:
534 self.install_cord_apps(self.ipaddr)
A R Karthick19aaf5c2016-11-09 17:47:57 -0800535
A.R Karthickc4e474d2016-12-12 15:24:57 -0800536 @classmethod
537 def get_quagga_config(cls, instance = 0):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800538 quagga_config = copy.deepcopy(cls.QUAGGA_CONFIG)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800539 if instance == 0:
540 return quagga_config
541 ip = quagga_config[0]['ip']
542 octets = ip.split('.')
A R Karthickaa54a1c2016-12-15 11:42:08 -0800543 octets[3] = str((int(octets[3]) + instance) & 255)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800544 ip = '.'.join(octets)
545 quagga_config[0]['ip'] = ip
546 return quagga_config
547
548 @classmethod
549 def start_cluster_async(cls, onos_instances):
550 instances = filter(lambda o: o.running == False, onos_instances)
551 if not instances:
552 return
553 tpool = ThreadPool(len(instances), queue_size = 1, wait_timeout = 1)
554 for onos in instances:
555 tpool.addTask(onos.start_async)
556 tpool.cleanUpThreads()
557
558 def start_async(self):
559 print('Starting ONOS container %s' %self.name)
560 self.start(ports = self.ports, environment = self.env,
561 host_config = self.host_config, volumes = self.volumes, tty = True)
562 time.sleep(3)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700563 self.ipaddr = self.ip()
A.R Karthickc4e474d2016-12-12 15:24:57 -0800564 print('Waiting for ONOS container %s to start' %self.name)
565 self.wait_for_onos_start(self.ipaddr)
566 self.running = True
567 print('ONOS container %s started' %self.name)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700568
A R Karthick2b93d6a2016-09-06 15:19:09 -0700569 @classmethod
A R Karthick19aaf5c2016-11-09 17:47:57 -0800570 def wait_for_onos_start(cls, ip, tries = 30):
571 onos_log = OnosLog(host = ip)
572 num_tries = 0
573 started = None
574 while not started and num_tries < tries:
575 time.sleep(3)
576 started = onos_log.search_log_pattern('ApplicationManager .* Started')
577 num_tries += 1
578
A R Karthick19aaf5c2016-11-09 17:47:57 -0800579 if not started:
580 print('ONOS did not start')
581 else:
582 print('ONOS started')
583 return started
584
585 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700586 def setup_cluster_deprecated(cls, onos_instances, image_name = None):
587 if not onos_instances or len(onos_instances) < 2:
588 return
589 ips = []
590 if image_name is not None:
591 ips = Container.ips(image_name)
592 else:
593 for onos in onos_instances:
594 ips.append(onos.ipaddr)
595 Onos.cluster_instances = onos_instances
596 Onos.cluster_mode = True
597 ##regenerate the cluster json with the 3 instance ips before restarting them back
598 print('Generating cluster cfg for ONOS instances with ips %s' %ips)
599 Onos.generate_cluster_cfg(ips)
600 for onos in onos_instances:
601 onos.kill()
602 onos.remove_container(onos.name, force=True)
603 print('Restarting ONOS container %s for forming cluster' %onos.name)
604 onos.start(ports = onos.ports, environment = onos.env,
605 host_config = onos.host_config, volumes = onos.volumes, tty = True)
606 print('Waiting %d seconds for ONOS %s to boot' %(onos.boot_delay, onos.name))
607 time.sleep(onos.boot_delay)
608 onos.ipaddr = onos.ip()
609 onos.install_cord_apps(onos.ipaddr)
610
611 @classmethod
612 def setup_cluster(cls, onos_instances, image_name = None):
613 if not onos_instances or len(onos_instances) < 2:
614 return
615 ips = []
616 if image_name is not None:
617 ips = Container.ips(image_name)
618 else:
619 for onos in onos_instances:
620 ips.append(onos.ipaddr)
621 Onos.cluster_instances = onos_instances
622 Onos.cluster_mode = True
623 ##regenerate the cluster json with the 3 instance ips before restarting them back
624 print('Forming cluster for ONOS instances with ips %s' %ips)
625 Onos.form_cluster(ips)
626 ##wait for the cluster to be formed
627 print('Waiting for the cluster to be formed')
628 time.sleep(60)
629 for onos in onos_instances:
630 onos.install_cord_apps(onos.ipaddr)
631
632 @classmethod
A R Karthicke2c24bd2016-10-07 14:51:38 -0700633 def add_cluster(cls, count = 1, network_cfg = None):
634 if not cls.cluster_instances or Onos.cluster_mode is False:
635 return
636 for i in range(count):
637 name = '{}-{}'.format(Onos.NAME, len(cls.cluster_instances)+1)
638 onos = cls(name = name, image = Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX,
639 cluster = True, network_cfg = network_cfg)
640 cls.cluster_instances.append(onos)
641
642 cls.setup_cluster(cls.cluster_instances)
643
644 @classmethod
A.R Karthick2560f042016-11-30 14:38:52 -0800645 def restart_cluster(cls, network_cfg = None, timeout = 10, setup = False):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700646 if cls.cluster_mode is False:
647 return
648 if not cls.cluster_instances:
649 return
650
651 if network_cfg is not None:
652 json_data = json.dumps(network_cfg, indent=4)
653 with open('{}/network-cfg.json'.format(cls.host_config_dir), 'w') as f:
654 f.write(json_data)
655
A.R Karthick2560f042016-11-30 14:38:52 -0800656 cls.cleanup_cluster()
657 if timeout > 0:
658 time.sleep(timeout)
659
A R Karthickaa54a1c2016-12-15 11:42:08 -0800660 #start the instances asynchronously
661 cls.start_cluster_async(cls.cluster_instances)
662 time.sleep(5)
A.R Karthick2560f042016-11-30 14:38:52 -0800663 ##form the cluster as appropriate
664 if setup is True:
665 cls.setup_cluster(cls.cluster_instances)
A R Karthickaa54a1c2016-12-15 11:42:08 -0800666 else:
667 for onos in cls.cluster_instances:
668 onos.install_cord_apps(onos.ipaddr)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700669
670 @classmethod
671 def cluster_ips(cls):
672 if cls.cluster_mode is False:
673 return []
674 if not cls.cluster_instances:
675 return []
676 ips = [ onos.ipaddr for onos in cls.cluster_instances ]
677 return ips
678
679 @classmethod
680 def cleanup_cluster(cls):
681 if cls.cluster_mode is False:
682 return
683 if not cls.cluster_instances:
684 return
685 for onos in cls.cluster_instances:
686 if onos.exists():
687 onos.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800688 onos.running = False
A R Karthick2b93d6a2016-09-06 15:19:09 -0700689 onos.remove_container(onos.name, force=True)
A R Karthickd44cea12016-07-20 12:16:41 -0700690
A.R Karthick95d044e2016-06-10 18:44:36 -0700691 @classmethod
A R Karthickde6b9dc2016-11-29 17:46:16 -0800692 def restart_node(cls, node = None, network_cfg = None, timeout = 10):
A R Karthick889d9652016-10-03 14:13:45 -0700693 if node is None:
694 cls(restart = True, network_cfg = network_cfg, image = cls.IMAGE, tag = cls.TAG)
695 else:
696 #Restarts a node in the cluster
697 valid_node = filter(lambda onos: node in [ onos.ipaddr, onos.name ], cls.cluster_instances)
698 if valid_node:
699 onos = valid_node.pop()
700 if onos.exists():
701 onos.kill()
702 onos.remove_container(onos.name, force=True)
A R Karthickde6b9dc2016-11-29 17:46:16 -0800703 if timeout > 0:
704 time.sleep(timeout)
A R Karthick889d9652016-10-03 14:13:45 -0700705 print('Restarting ONOS container %s' %onos.name)
706 onos.start(ports = onos.ports, environment = onos.env,
707 host_config = onos.host_config, volumes = onos.volumes, tty = True)
A R Karthick889d9652016-10-03 14:13:45 -0700708 onos.ipaddr = onos.ip()
A.R Karthick2560f042016-11-30 14:38:52 -0800709 onos.wait_for_onos_start(onos.ipaddr)
710 onos.install_cord_apps(onos.ipaddr)
A R Karthick889d9652016-10-03 14:13:45 -0700711
712 @classmethod
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700713 def install_cord_apps(cls, onos_ip = None):
A.R Karthick95d044e2016-06-10 18:44:36 -0700714 for app, version in cls.onos_cord_apps:
715 app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version)
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700716 ok, code = OnosCtrl.install_app(app_file, onos_ip = onos_ip)
A.R Karthick95d044e2016-06-10 18:44:36 -0700717 ##app already installed (conflicts)
718 if code in [ 409 ]:
719 ok = True
720 print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install'))
721 time.sleep(2)
722
A.R Karthick1700e0e2016-10-06 18:16:57 -0700723class OnosStopWrapper(Container):
724 def __init__(self, name):
725 super(OnosStopWrapper, self).__init__(name, Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX)
726 if self.exists():
727 self.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800728 self.running = False
A.R Karthick1700e0e2016-10-06 18:16:57 -0700729 else:
730 if Onos.cluster_mode is True:
731 valid_node = filter(lambda onos: name in [ onos.ipaddr, onos.name ], Onos.cluster_instances)
732 if valid_node:
733 onos = valid_node.pop()
734 if onos.exists():
735 onos.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800736 onos.running = False
A.R Karthick1700e0e2016-10-06 18:16:57 -0700737
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700738class Radius(Container):
739 ports = [ 1812, 1813 ]
A R Karthick41adfce2016-06-10 09:51:25 -0700740 env = {'TIMEZONE':'America/Los_Angeles',
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700741 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
742 }
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700743 host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700744 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700745 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700746 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700747 start_command = os.path.join(guest_config_dir, 'start-radius.py')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700748 host_guest_map = ( (host_db_dir, guest_db_dir),
749 (host_config_dir, guest_config_dir)
750 )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700751 IMAGE = 'cord-test/radius'
752 NAME = 'cord-radius'
753
A R Karthick07608ef2016-08-23 16:51:19 -0700754 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
A R Karthick85eb1862017-01-23 16:10:57 -0800755 boot_delay = 10, restart = False, update = False, network = None):
A R Karthick07608ef2016-08-23 16:51:19 -0700756 super(Radius, self).__init__(name, image, prefix = prefix, tag = tag, command = self.start_command)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700757 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700758 self.build_image(self.image_name)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700759 if restart is True and self.exists():
760 self.kill()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700761 if not self.exists():
762 self.remove_container(name, force=True)
763 host_config = self.create_host_config(port_list = self.ports,
764 host_guest_map = self.host_guest_map)
765 volumes = []
766 for _,g in self.host_guest_map:
767 volumes.append(g)
A R Karthick41adfce2016-06-10 09:51:25 -0700768 self.start(ports = self.ports, environment = self.env,
769 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700770 host_config = host_config, tty = True)
A R Karthick85eb1862017-01-23 16:10:57 -0800771 if network is not None:
772 Container.connect_to_network(self.name, network)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700773 time.sleep(boot_delay)
774
775 @classmethod
776 def build_image(cls, image):
777 print('Building Radius image %s' %image)
778 dockerfile = '''
779FROM hbouvier/docker-radius
780MAINTAINER chetan@ciena.com
781LABEL RUN docker pull hbouvier/docker-radius
782LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius
A R Karthickc762df42016-05-25 10:09:21 -0700783RUN apt-get update && \
784 apt-get -y install python python-pexpect strace
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700785WORKDIR /root
786CMD ["/etc/freeradius/start-radius.py"]
787'''
788 super(Radius, cls).build_image(dockerfile, image)
789 print('Done building image %s' %image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700790
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700791class Quagga(Container):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800792 QUAGGA_CONFIG = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 },
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700793 { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 },
794 )
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700795 ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ]
796 host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config')
797 guest_quagga_config = '/root/config'
798 quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
799 host_guest_map = ( (host_quagga_config, guest_quagga_config), )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700800 IMAGE = 'cord-test/quagga'
801 NAME = 'cord-quagga'
802
A R Karthick07608ef2016-08-23 16:51:19 -0700803 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
A R Karthick85eb1862017-01-23 16:10:57 -0800804 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False,
805 network = None):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800806 super(Quagga, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.QUAGGA_CONFIG)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700807 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700808 self.build_image(self.image_name)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700809 if restart is True and self.exists():
810 self.kill()
811 if not self.exists():
812 self.remove_container(name, force=True)
A R Karthick41adfce2016-06-10 09:51:25 -0700813 host_config = self.create_host_config(port_list = self.ports,
814 host_guest_map = self.host_guest_map,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700815 privileged = True)
816 volumes = []
817 for _,g in self.host_guest_map:
818 volumes.append(g)
819 self.start(ports = self.ports,
A R Karthick41adfce2016-06-10 09:51:25 -0700820 host_config = host_config,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700821 volumes = volumes, tty = True)
A R Karthick85eb1862017-01-23 16:10:57 -0800822 if network is not None:
823 Container.connect_to_network(self.name, network)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700824 print('Starting Quagga on container %s' %self.name)
825 self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
826 time.sleep(boot_delay)
827
828 @classmethod
829 def build_image(cls, image):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800830 onos_quagga_ip = Onos.QUAGGA_CONFIG[0]['ip']
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700831 print('Building Quagga image %s' %image)
832 dockerfile = '''
A R Karthick41adfce2016-06-10 09:51:25 -0700833FROM ubuntu:14.04
834MAINTAINER chetan@ciena.com
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700835WORKDIR /root
836RUN useradd -M quagga
837RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga
838RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga
A R Karthick973ea692016-10-17 12:23:02 -0700839RUN 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 -0700840RUN git clone git://git.savannah.nongnu.org/quagga.git quagga && \
A R Karthick8f69c2c2016-10-21 11:43:26 -0700841(cd quagga && git checkout quagga-1.0.20160315 && ./bootstrap.sh && \
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700842sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \
843./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install)
844RUN ldconfig
845'''.format(onos_quagga_ip)
846 super(Quagga, cls).build_image(dockerfile, image)
847 print('Done building image %s' %image)
A R Karthick81acbff2016-06-17 14:45:16 -0700848
A.R Karthick1700e0e2016-10-06 18:16:57 -0700849class QuaggaStopWrapper(Container):
850 def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'candidate'):
851 super(QuaggaStopWrapper, self).__init__(name, image, prefix = Container.IMAGE_PREFIX, tag = tag)
852 if self.exists():
853 self.kill()
854
855
A R Karthick81acbff2016-06-17 14:45:16 -0700856def reinitContainerClients():
857 docker_netns.dckr = Client()
858 Container.dckr = Client()
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700859
860class Xos(Container):
861 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
862 TAG = 'latest'
863 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700864 host_guest_map = None
865 env = None
866 ports = None
867 volumes = None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700868
A R Karthick6e80afd2016-10-10 16:03:12 -0700869 @classmethod
870 def get_cmd(cls, img_name):
871 cmd = cls.dckr.inspect_image(img_name)['Config']['Cmd']
872 return ' '.join(cmd)
873
A R Karthicke3bde962016-09-27 15:06:35 -0700874 def __init__(self, name, image, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700875 boot_delay = 20, restart = False, network_cfg = None, update = False):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700876 if restart is True:
877 ##Find the right image to restart
878 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
879 if running_image:
880 image_name = running_image[0]['Image']
881 try:
882 image = image_name.split(':')[0]
883 tag = image_name.split(':')[1]
884 except: pass
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700885 super(Xos, self).__init__(name, image, prefix = prefix, tag = tag)
886 if update is True or not self.img_exists():
887 self.build_image(self.image_name)
A R Karthick6e80afd2016-10-10 16:03:12 -0700888 self.command = self.get_cmd(self.image_name).strip() or None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700889 if restart is True and self.exists():
890 self.kill()
891 if not self.exists():
892 self.remove_container(name, force=True)
A R Karthicke3bde962016-09-27 15:06:35 -0700893 host_config = self.create_host_config(port_list = self.ports,
894 host_guest_map = self.host_guest_map,
895 privileged = True)
896 print('Starting XOS container %s' %self.name)
897 self.start(ports = self.ports, environment = self.env, host_config = host_config,
898 volumes = self.volumes, tty = True)
899 print('Waiting %d seconds for XOS Base Container to boot' %(boot_delay))
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700900 time.sleep(boot_delay)
901
902 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700903 def build_image(cls, image, dockerfile_path, image_target = 'build'):
904 cmd = 'cd {} && make {}'.format(dockerfile_path, image_target)
905 print('Building XOS %s' %image)
906 res = os.system(cmd)
907 print('Done building image %s. Image build %s' %(image, 'successful' if res == 0 else 'failed'))
908 return res
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700909
A R Karthicke3bde962016-09-27 15:06:35 -0700910class XosServer(Xos):
911 ports = [8000,9998,9999]
912 NAME = 'xos-server'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700913 IMAGE = 'xosproject/xos'
A R Karthicke3bde962016-09-27 15:06:35 -0700914 BASE_IMAGE = 'xosproject/xos-base'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700915 TAG = 'latest'
916 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700917 dockerfile_path = os.path.join(Xos.setup_dir, 'xos')
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700918
A R Karthicke3bde962016-09-27 15:06:35 -0700919 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700920 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700921 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700922
923 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700924 def build_image(cls, image = IMAGE):
925 ##build the base image and then build the server image
926 Xos.build_image(cls.BASE_IMAGE, cls.dockerfile_path, image_target = 'base')
927 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700928
A R Karthicke3bde962016-09-27 15:06:35 -0700929class XosSynchronizerOpenstack(Xos):
930 ports = [2375,]
931 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer')
932 NAME = 'xos-synchronizer'
933 IMAGE = 'xosproject/xos-synchronizer-openstack'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700934 TAG = 'latest'
935 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700936 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700937
A R Karthicke3bde962016-09-27 15:06:35 -0700938 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700939 tag = TAG, boot_delay = 20, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700940 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700941
942 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700943 def build_image(cls, image = IMAGE):
944 XosServer.build_image()
945 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700946
A R Karthicke3bde962016-09-27 15:06:35 -0700947class XosSynchronizerOnboarding(Xos):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700948 NAME = 'xos-synchronizer-onboarding'
949 IMAGE = 'xosproject/xos-synchronizer-onboarding'
950 TAG = 'latest'
951 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700952 dockerfile_path = os.path.join(Xos.setup_dir, 'onboarding_synchronizer')
953 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700954
A R Karthicke3bde962016-09-27 15:06:35 -0700955 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700956 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700957 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700958
959 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700960 def build_image(cls, image = IMAGE):
961 XosSynchronizerOpenstack.build_image()
962 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700963
A R Karthicke3bde962016-09-27 15:06:35 -0700964class XosSynchronizerOpenvpn(Xos):
965 NAME = 'xos-synchronizer-openvpn'
966 IMAGE = 'xosproject/xos-openvpn'
967 TAG = 'latest'
968 PREFIX = ''
969 dockerfile_path = os.path.join(Xos.setup_dir, 'openvpn')
970 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700971
A R Karthicke3bde962016-09-27 15:06:35 -0700972 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700973 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700974 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
975
976 @classmethod
977 def build_image(cls, image = IMAGE):
978 XosSynchronizerOpenstack.build_image()
979 Xos.build_image(image, cls.dockerfile_path)
980
981class XosPostgresql(Xos):
982 ports = [5432,]
983 NAME = 'xos-db-postgres'
984 IMAGE = 'xosproject/xos-postgres'
985 TAG = 'latest'
986 PREFIX = ''
987 volumes = ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
988 dockerfile_path = os.path.join(Xos.setup_dir, 'postgresql')
989
990 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700991 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700992 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
993
994 @classmethod
995 def build_image(cls, image = IMAGE):
996 Xos.build_image(image, cls.dockerfile_path)
997
998class XosSyndicateMs(Xos):
999 ports = [8080,]
1000 env = None
1001 NAME = 'xos-syndicate-ms'
1002 IMAGE = 'xosproject/syndicate-ms'
1003 TAG = 'latest'
1004 PREFIX = ''
1005 dockerfile_path = os.path.join(Xos.setup_dir, 'syndicate-ms')
1006
1007 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001008 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001009 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1010
1011 @classmethod
1012 def build_image(cls, image = IMAGE):
1013 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001014
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001015class XosSyncVtn(Xos):
1016 ports = [8080,]
1017 env = None
1018 NAME = 'xos-synchronizer-vtn'
1019 IMAGE = 'xosproject/xos-synchronizer-vtn'
1020 TAG = 'latest'
1021 PREFIX = ''
1022 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtn')
1023
1024 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001025 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001026 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1027
1028 @classmethod
1029 def build_image(cls, image = IMAGE):
1030 Xos.build_image(image, cls.dockerfile_path)
1031
1032class XosSyncVtr(Xos):
1033 ports = [8080,]
1034 env = None
1035 NAME = 'xos-synchronizer-vtr'
1036 IMAGE = 'xosproject/xos-synchronizer-vtr'
1037 TAG = 'latest'
1038 PREFIX = ''
1039 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtr')
1040
1041 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001042 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001043 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1044
1045 @classmethod
1046 def build_image(cls, image = IMAGE):
1047 Xos.build_image(image, cls.dockerfile_path)
1048
1049class XosSyncVsg(Xos):
1050 ports = [8080,]
1051 env = None
1052 NAME = 'xos-synchronizer-vsg'
1053 IMAGE = 'xosproject/xos-synchronizer-vsg'
1054 TAG = 'latest'
1055 PREFIX = ''
1056 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vsg')
1057
1058 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001059 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001060 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1061
1062 @classmethod
1063 def build_image(cls, image = IMAGE):
1064 Xos.build_image(image, cls.dockerfile_path)
1065
1066
1067class XosSyncOnos(Xos):
1068 ports = [8080,]
1069 env = None
1070 NAME = 'xos-synchronizer-onos'
1071 IMAGE = 'xosproject/xos-synchronizer-onos'
1072 TAG = 'latest'
1073 PREFIX = ''
1074 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-onos')
1075
1076 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001077 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001078 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1079
1080 @classmethod
1081 def build_image(cls, image = IMAGE):
1082 Xos.build_image(image, cls.dockerfile_path)
1083
1084class XosSyncFabric(Xos):
1085 ports = [8080,]
1086 env = None
1087 NAME = 'xos-synchronizer-fabric'
1088 IMAGE = 'xosproject/xos-synchronizer-fabric'
1089 TAG = 'latest'
1090 PREFIX = ''
1091 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-fabric')
1092
1093 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001094 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001095 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1096
1097 @classmethod
1098 def build_image(cls, image = IMAGE):
1099 Xos.build_image(image, cls.dockerfile_path)
A R Karthick19aaf5c2016-11-09 17:47:57 -08001100
1101if __name__ == '__main__':
1102 onos = Onos(boot_delay = 10, restart = True)