blob: 2c5782394cb01664792dbee8dbf4fe68f9dc2477 [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 Karthickec2db322016-11-17 15:06:01 -080027from shutil import rmtree
A.R Karthick95d044e2016-06-10 18:44:36 -070028from OnosCtrl import OnosCtrl
A R Karthick19aaf5c2016-11-09 17:47:57 -080029from OnosLog import OnosLog
A.R Karthickc4e474d2016-12-12 15:24:57 -080030from threadPool import ThreadPool
A R Karthickaa54a1c2016-12-15 11:42:08 -080031from threading import Lock
Chetan Gaonker3533faa2016-04-25 17:50:14 -070032
33class docker_netns(object):
34
35 dckr = Client()
36 def __init__(self, name):
37 pid = int(self.dckr.inspect_container(name)['State']['Pid'])
38 if pid == 0:
39 raise Exception('no container named {0}'.format(name))
40 self.pid = pid
41
42 def __enter__(self):
43 pid = self.pid
44 if not os.path.exists('/var/run/netns'):
45 os.mkdir('/var/run/netns')
46 os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid))
47 return str(pid)
48
49 def __exit__(self, type, value, traceback):
50 pid = self.pid
51 os.unlink('/var/run/netns/{0}'.format(pid))
52
53flatten = lambda l: chain.from_iterable(l)
54
55class Container(object):
56 dckr = Client()
A R Karthick07608ef2016-08-23 16:51:19 -070057 IMAGE_PREFIX = '' ##for saving global prefix for all test classes
A R Karthickaa54a1c2016-12-15 11:42:08 -080058 CONFIG_LOCK = Lock()
A R Karthick07608ef2016-08-23 16:51:19 -070059
60 def __init__(self, name, image, prefix='', tag = 'candidate', command = 'bash', quagga_config = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -070061 self.name = name
A R Karthick07608ef2016-08-23 16:51:19 -070062 self.prefix = prefix
63 if prefix:
64 self.prefix += '/'
65 image = '{}{}'.format(self.prefix, image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -070066 self.image = image
67 self.tag = tag
A R Karthickd44cea12016-07-20 12:16:41 -070068 if tag:
69 self.image_name = image + ':' + tag
70 else:
71 self.image_name = image
Chetan Gaonker3533faa2016-04-25 17:50:14 -070072 self.id = None
73 self.command = command
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -070074 self.quagga_config = quagga_config
Chetan Gaonker3533faa2016-04-25 17:50:14 -070075
76 @classmethod
77 def build_image(cls, dockerfile, tag, force=True, nocache=False):
78 f = io.BytesIO(dockerfile.encode('utf-8'))
79 if force or not cls.image_exists(tag):
80 print('Build {0}...'.format(tag))
81 for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache):
82 if 'stream' in line:
83 print(line['stream'].strip())
84
85 @classmethod
86 def image_exists(cls, name):
87 return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()]
88
89 @classmethod
90 def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False):
91 port_bindings = None
92 binds = None
93 if port_list:
94 port_bindings = {}
95 for p in port_list:
96 port_bindings[str(p)] = str(p)
97
98 if host_guest_map:
99 binds = []
100 for h, g in host_guest_map:
101 binds.append('{0}:{1}'.format(h, g))
102
103 return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged)
104
105 @classmethod
106 def cleanup(cls, image):
A R Karthick09b1f4e2016-05-12 14:31:50 -0700107 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700108 for cnt in cnt_list:
109 print('Cleaning container %s' %cnt['Id'])
A.R Karthick95d044e2016-06-10 18:44:36 -0700110 if cnt.has_key('State') and cnt['State'] == 'running':
A R Karthick09b1f4e2016-05-12 14:31:50 -0700111 cls.dckr.kill(cnt['Id'])
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700112 cls.dckr.remove_container(cnt['Id'], force=True)
113
114 @classmethod
115 def remove_container(cls, name, force=True):
116 try:
117 cls.dckr.remove_container(name, force = force)
118 except: pass
119
120 def exists(self):
121 return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers()))
122
123 def img_exists(self):
A R Karthick6d98a592016-08-24 15:16:46 -0700124 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 -0700125
126 def ip(self):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700127 cnt_list = filter(lambda c: c['Names'][0] == '/{}'.format(self.name), self.dckr.containers())
128 #if not cnt_list:
129 # cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700130 cnt_settings = cnt_list.pop()
131 return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress']
132
A R Karthick2b93d6a2016-09-06 15:19:09 -0700133 @classmethod
134 def ips(cls, image_name):
135 cnt_list = filter(lambda c: c['Image'] == image_name, cls.dckr.containers())
136 ips = [ cnt['NetworkSettings']['Networks']['bridge']['IPAddress'] for cnt in cnt_list ]
137 return ips
138
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700139 def kill(self, remove = True):
140 self.dckr.kill(self.name)
141 self.dckr.remove_container(self.name, force=True)
142
A R Karthick41adfce2016-06-10 09:51:25 -0700143 def start(self, rm = True, ports = None, volumes = None, host_config = None,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700144 environment = None, tty = False, stdin_open = True):
145
146 if rm and self.exists():
147 print('Removing container:', self.name)
148 self.dckr.remove_container(self.name, force=True)
149
A R Karthick41adfce2016-06-10 09:51:25 -0700150 ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700151 detach=True, name=self.name,
A R Karthick41adfce2016-06-10 09:51:25 -0700152 environment = environment,
153 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700154 host_config = host_config, stdin_open=stdin_open, tty = tty)
155 self.dckr.start(container=self.name)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700156 if self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700157 self.connect_to_br()
158 self.id = ctn['Id']
159 return ctn
160
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000161 @classmethod
162 def pause_container(cls, image, delay):
163 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
164 for cnt in cnt_list:
165 print('Pause the container %s' %cnt['Id'])
166 if cnt.has_key('State') and cnt['State'] == 'running':
167 cls.dckr.pause(cnt['Id'])
168 if delay != 0:
169 time.sleep(delay)
170 for cnt in cnt_list:
171 print('Unpause the container %s' %cnt['Id'])
172 cls.dckr.unpause(cnt['Id'])
173 else:
174 print('Infinity time pause the container %s' %cnt['Id'])
175 return 'success'
176
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700177 def connect_to_br(self):
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700178 index = 0
A R Karthickaa54a1c2016-12-15 11:42:08 -0800179 self.CONFIG_LOCK.acquire()
180 try:
181 with docker_netns(self.name) as pid:
182 for quagga_config in self.quagga_config:
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700183 ip = IPRoute()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800184 br = ip.link_lookup(ifname=quagga_config['bridge'])
185 if len(br) == 0:
186 try:
187 ip.link_create(ifname=quagga_config['bridge'], kind='bridge')
188 except NetlinkError as e:
189 err, _ = e.args
190 if err == errno.EEXIST:
191 pass
192 else:
193 raise NetlinkError(*e.args)
194 br = ip.link_lookup(ifname=quagga_config['bridge'])
195 br = br[0]
196 ip.link('set', index=br, state='up')
197 ifname = '{0}-{1}'.format(self.name, index)
198 ifs = ip.link_lookup(ifname=ifname)
199 if len(ifs) > 0:
200 ip.link_remove(ifs[0])
201 peer_ifname = '{0}-{1}'.format(pid, index)
202 ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname)
203 host = ip.link_lookup(ifname=ifname)[0]
204 ip.link('set', index=host, master=br)
205 ip.link('set', index=host, state='up')
206 guest = ip.link_lookup(ifname=peer_ifname)[0]
207 ip.link('set', index=guest, net_ns_fd=pid)
208 with Namespace(pid, 'net'):
209 ip = IPRoute()
210 ip.link('set', index=guest, ifname='eth{}'.format(index+1))
211 ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask'])
212 ip.link('set', index=guest, state='up')
213 index += 1
214 finally:
215 self.CONFIG_LOCK.release()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700216
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000217 def execute(self, cmd, tty = True, stream = False, shell = False, detach = True):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700218 res = 0
219 if type(cmd) == str:
220 cmds = (cmd,)
221 else:
222 cmds = cmd
223 if shell:
224 for c in cmds:
225 res += os.system('docker exec {0} {1}'.format(self.name, c))
226 return res
227 for c in cmds:
228 i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True)
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000229 self.dckr.exec_start(i['Id'], stream = stream, detach=detach)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700230 result = self.dckr.exec_inspect(i['Id'])
231 res += 0 if result['ExitCode'] == None else result['ExitCode']
232 return res
233
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700234 def restart(self, timeout =10):
235 return self.dckr.restart(self.name, timeout)
236
A R Karthickc69d73e2017-01-20 11:44:34 -0800237def get_mem(jvm_heap_size = None, instances = 1):
A R Karthick1f908202016-11-16 17:32:20 -0800238 if instances <= 0:
239 instances = 1
A R Karthickc69d73e2017-01-20 11:44:34 -0800240 heap_size = jvm_heap_size
241 heap_size_i = 0
242 #sanitize the heap size config
243 if heap_size is not None:
244 if not heap_size.isdigit():
245 try:
246 heap_size_i = int(heap_size[:-1])
247 suffix = heap_size[-1]
248 if suffix == 'M':
249 heap_size_i /= 1024 #convert to gigs
250 except:
251 ##invalid suffix length probably. Fall back to default
252 heap_size = None
253 else:
254 heap_size_i = int(heap_size)
255
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700256 with open('/proc/meminfo', 'r') as fd:
257 meminfo = fd.readlines()
258 mem = 0
259 for m in meminfo:
260 if m.startswith('MemTotal:') or m.startswith('SwapTotal:'):
261 mem += int(m.split(':')[1].strip().split()[0])
262
A R Karthick1f908202016-11-16 17:32:20 -0800263 mem = max(mem/1024/1024/2/instances, 1)
Chetan Gaonker6d0a7b02016-05-03 16:57:28 -0700264 mem = min(mem, 16)
A R Karthickc69d73e2017-01-20 11:44:34 -0800265
266 if heap_size_i:
267 #we take the minimum of the provided heap size and max allowed heap size
268 heap_size_i = min(heap_size_i, mem)
269 else:
270 heap_size_i = mem
271
272 return '{}G'.format(heap_size_i)
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700273
A R Karthickd44cea12016-07-20 12:16:41 -0700274class OnosCord(Container):
275 """Use this when running the cord tester agent on the onos compute node"""
276 onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord')
277 onos_config_dir_guest = '/root/onos/config'
278 onos_config_dir = os.path.join(onos_cord_dir, 'config')
279 docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml')
280
A R Karthickbd9b8a32016-07-21 09:56:45 -0700281 def __init__(self, onos_ip, conf, boot_delay = 60):
282 self.onos_ip = onos_ip
A R Karthickd44cea12016-07-20 12:16:41 -0700283 self.cord_conf_dir = conf
A R Karthickbd9b8a32016-07-21 09:56:45 -0700284 self.boot_delay = boot_delay
A R Karthickd44cea12016-07-20 12:16:41 -0700285 if os.access(self.cord_conf_dir, os.F_OK) and not os.access(self.onos_cord_dir, os.F_OK):
286 os.mkdir(self.onos_cord_dir)
287 os.mkdir(self.onos_config_dir)
288 ##copy the config file from cord-tester-config
289 cmd = 'cp {}/* {}'.format(self.cord_conf_dir, self.onos_cord_dir)
290 os.system(cmd)
291
292 ##update the docker yaml with the config volume
293 with open(self.docker_yaml, 'r') as f:
294 yaml_config = yaml.load(f)
295 image = yaml_config['services'].keys()[0]
296 name = 'cordtestercord_{}_1'.format(image)
297 volumes = yaml_config['services'][image]['volumes']
298 config_volumes = filter(lambda e: e.find(self.onos_config_dir_guest) >= 0, volumes)
299 if not config_volumes:
300 config_volume = '{}:{}'.format(self.onos_config_dir, self.onos_config_dir_guest)
301 volumes.append(config_volume)
302 docker_yaml_changed = '{}-changed'.format(self.docker_yaml)
303 with open(docker_yaml_changed, 'w') as wf:
304 yaml.dump(yaml_config, wf)
305
306 os.rename(docker_yaml_changed, self.docker_yaml)
307 self.volumes = volumes
308
309 super(OnosCord, self).__init__(name, image, tag = '')
310 cord_conf_dir_basename = os.path.basename(self.cord_conf_dir.replace('-', ''))
311 self.xos_onos_name = '{}_{}_1'.format(cord_conf_dir_basename, image)
312 ##Create an container instance of xos onos
313 self.xos_onos = Container(self.xos_onos_name, image, tag = '')
314
315 def start(self, restart = False, network_cfg = None):
316 if restart is True:
317 if self.exists():
318 ##Kill the existing instance
319 print('Killing container %s' %self.name)
320 self.kill()
321 if self.xos_onos.exists():
322 print('Killing container %s' %self.xos_onos.name)
323 self.xos_onos.kill()
324
325 if network_cfg is not None:
326 json_data = json.dumps(network_cfg, indent=4)
327 with open('{}/network-cfg.json'.format(self.onos_config_dir), 'w') as f:
328 f.write(json_data)
329
330 #start the container using docker-compose
331 cmd = 'cd {} && docker-compose up -d'.format(self.onos_cord_dir)
332 os.system(cmd)
A R Karthickbd9b8a32016-07-21 09:56:45 -0700333 #Delay to make sure ONOS fully boots
334 time.sleep(self.boot_delay)
335 Onos.install_cord_apps(onos_ip = self.onos_ip)
A R Karthickd44cea12016-07-20 12:16:41 -0700336
337 def build_image(self):
338 build_cmd = 'cd {} && docker-compose build'.format(self.onos_cord_dir)
339 os.system(build_cmd)
340
A.R Karthick1700e0e2016-10-06 18:16:57 -0700341class OnosCordStopWrapper(Container):
342 onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord')
343 docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml')
344
345 def __init__(self):
346 if os.access(self.docker_yaml, os.F_OK):
347 with open(self.docker_yaml, 'r') as f:
348 yaml_config = yaml.load(f)
349 image = yaml_config['services'].keys()[0]
350 name = 'cordtestercord_{}_1'.format(image)
351 super(OnosCordStopWrapper, self).__init__(name, image, tag = '')
352 if self.exists():
353 print('Killing container %s' %self.name)
354 self.kill()
355
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700356class Onos(Container):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800357 QUAGGA_CONFIG = [ { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, ]
A R Karthicka2492c12016-12-16 10:31:51 -0800358 MAX_INSTANCES = 3
A R Karthickc69d73e2017-01-20 11:44:34 -0800359 JVM_HEAP_SIZE = None
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700360 SYSTEM_MEMORY = (get_mem(),) * 2
A R Karthicka2492c12016-12-16 10:31:51 -0800361 INSTANCE_MEMORY = (get_mem(instances=MAX_INSTANCES),) * 2
A R Karthickc69d73e2017-01-20 11:44:34 -0800362 JAVA_OPTS_FORMAT = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'
363 JAVA_OPTS_DEFAULT = JAVA_OPTS_FORMAT.format(*SYSTEM_MEMORY) #-XX:+PrintGCDetails -XX:+PrintGCTimeStamps'
364 JAVA_OPTS_CLUSTER_DEFAULT = JAVA_OPTS_FORMAT.format(*INSTANCE_MEMORY)
365 env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,vrouter', 'JAVA_OPTS' : JAVA_OPTS_DEFAULT }
A.R Karthickdfeadb02016-11-30 17:55:51 -0800366 onos_cord_apps = ( ('cord-config', '1.1-SNAPSHOT'),
367 ('aaa', '1.1-SNAPSHOT'),
368 ('igmp', '1.1-SNAPSHOT'),
369 #('vtn', '1.1-SNAPSHOT'),
A.R Karthick95d044e2016-06-10 18:44:36 -0700370 )
A.R Karthickc4e474d2016-12-12 15:24:57 -0800371 ports = [] #[ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ]
A R Karthickf2f4ca62016-08-17 10:34:08 -0700372 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
373 host_config_dir = os.path.join(setup_dir, 'onos-config')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700374 guest_config_dir = '/root/onos/config'
A R Karthickec2db322016-11-17 15:06:01 -0800375 guest_data_dir = '/root/onos/apache-karaf-3.0.5/data'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700376 onos_gen_partitions = os.path.join(setup_dir, 'onos-gen-partitions')
A R Karthick2b93d6a2016-09-06 15:19:09 -0700377 onos_form_cluster = os.path.join(setup_dir, 'onos-form-cluster')
A.R Karthick95d044e2016-06-10 18:44:36 -0700378 cord_apps_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'apps')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700379 host_guest_map = ( (host_config_dir, guest_config_dir), )
A R Karthick2b93d6a2016-09-06 15:19:09 -0700380 cluster_cfg = os.path.join(host_config_dir, 'cluster.json')
381 cluster_mode = False
382 cluster_instances = []
Chetan Gaonker503032a2016-05-12 12:06:29 -0700383 NAME = 'cord-onos'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700384 ##the ip of ONOS in default cluster.json in setup/onos-config
385 CLUSTER_CFG_IP = '172.17.0.2'
A R Karthick07608ef2016-08-23 16:51:19 -0700386 IMAGE = 'onosproject/onos'
387 TAG = 'latest'
388 PREFIX = ''
A R Karthickf2f4ca62016-08-17 10:34:08 -0700389
390 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700391 def generate_cluster_cfg(cls, ip):
392 if type(ip) in [ list, tuple ]:
393 ips = ' '.join(ip)
394 else:
395 ips = ip
A R Karthickf2f4ca62016-08-17 10:34:08 -0700396 try:
A R Karthick2b93d6a2016-09-06 15:19:09 -0700397 cmd = '{} {} {}'.format(cls.onos_gen_partitions, cls.cluster_cfg, ips)
398 os.system(cmd)
399 except: pass
400
401 @classmethod
402 def form_cluster(cls, ips):
403 nodes = ' '.join(ips)
404 try:
405 cmd = '{} {}'.format(cls.onos_form_cluster, nodes)
A R Karthickf2f4ca62016-08-17 10:34:08 -0700406 os.system(cmd)
407 except: pass
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700408
A R Karthick9d48c652016-09-15 09:16:36 -0700409 @classmethod
410 def cleanup_runtime(cls):
411 '''Cleanup ONOS runtime generated files'''
412 files = ( Onos.cluster_cfg, os.path.join(Onos.host_config_dir, 'network-cfg.json') )
413 for f in files:
414 if os.access(f, os.F_OK):
415 try:
416 os.unlink(f)
417 except: pass
418
A R Karthickec2db322016-11-17 15:06:01 -0800419 @classmethod
420 def get_data_map(cls, host_volume, guest_volume_dir):
421 host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume))
422 if not os.path.exists(host_volume_dir):
423 os.mkdir(host_volume_dir)
424 return ( (host_volume_dir, guest_volume_dir), )
425
426 @classmethod
427 def remove_data_map(cls, host_volume, guest_volume_dir):
428 host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume))
429 if os.path.exists(host_volume_dir):
430 rmtree(host_volume_dir)
431
432 def remove_data_volume(self):
433 if self.data_map is not None:
434 self.remove_data_map(*self.data_map)
435
A.R Karthick1700e0e2016-10-06 18:16:57 -0700436 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthickec2db322016-11-17 15:06:01 -0800437 boot_delay = 20, restart = False, network_cfg = None,
A R Karthicka2492c12016-12-16 10:31:51 -0800438 cluster = False, data_volume = None, async = False, quagga_config = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700439 if restart is True:
440 ##Find the right image to restart
441 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
442 if running_image:
443 image_name = running_image[0]['Image']
444 try:
445 image = image_name.split(':')[0]
446 tag = image_name.split(':')[1]
447 except: pass
448
A R Karthickaa54a1c2016-12-15 11:42:08 -0800449 if quagga_config is None:
450 quagga_config = Onos.QUAGGA_CONFIG
451 super(Onos, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = quagga_config)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700452 self.boot_delay = boot_delay
A R Karthickec2db322016-11-17 15:06:01 -0800453 self.data_map = None
A R Karthickc69d73e2017-01-20 11:44:34 -0800454 instance_memory = (get_mem(jvm_heap_size = Onos.JVM_HEAP_SIZE, instances = Onos.MAX_INSTANCES),) * 2
455 self.env['JAVA_OPTS'] = self.JAVA_OPTS_FORMAT.format(*instance_memory)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700456 if cluster is True:
457 self.ports = []
A R Karthickec2db322016-11-17 15:06:01 -0800458 if data_volume is not None:
459 self.data_map = self.get_data_map(data_volume, self.guest_data_dir)
460 self.host_guest_map = self.host_guest_map + self.data_map
A R Karthick2b93d6a2016-09-06 15:19:09 -0700461 if os.access(self.cluster_cfg, os.F_OK):
462 try:
463 os.unlink(self.cluster_cfg)
464 except: pass
465
466 self.host_config = self.create_host_config(port_list = self.ports,
467 host_guest_map = self.host_guest_map)
468 self.volumes = []
469 for _,g in self.host_guest_map:
470 self.volumes.append(g)
471
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700472 if restart is True and self.exists():
473 self.kill()
A R Karthick2b93d6a2016-09-06 15:19:09 -0700474
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700475 if not self.exists():
476 self.remove_container(name, force=True)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700477 host_config = self.create_host_config(port_list = self.ports,
478 host_guest_map = self.host_guest_map)
479 volumes = []
480 for _,g in self.host_guest_map:
481 volumes.append(g)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700482 if network_cfg is not None:
A R Karthick81acbff2016-06-17 14:45:16 -0700483 json_data = json.dumps(network_cfg, indent=4)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700484 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
485 f.write(json_data)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800486 if cluster is False or async is False:
487 print('Starting ONOS container %s' %self.name)
488 self.start(ports = self.ports, environment = self.env,
489 host_config = self.host_config, volumes = self.volumes, tty = True)
490 if not restart:
491 ##wait a bit before fetching IP to regenerate cluster cfg
492 time.sleep(5)
493 ip = self.ip()
494 ##Just a quick hack/check to ensure we don't regenerate in the common case.
495 ##As ONOS is usually the first test container that is started
496 if cluster is False:
497 if ip != self.CLUSTER_CFG_IP or not os.access(self.cluster_cfg, os.F_OK):
498 print('Regenerating ONOS cluster cfg for ip %s' %ip)
499 self.generate_cluster_cfg(ip)
500 self.kill()
501 self.remove_container(self.name, force=True)
502 print('Restarting ONOS container %s' %self.name)
503 self.start(ports = self.ports, environment = self.env,
504 host_config = self.host_config, volumes = self.volumes, tty = True)
505 print('Waiting for ONOS to boot')
506 time.sleep(boot_delay)
507 self.wait_for_onos_start(self.ip())
508 self.running = True
509 else:
510 self.running = False
511 else:
512 self.running = True
513 if self.running:
514 self.ipaddr = self.ip()
515 if cluster is False:
516 self.install_cord_apps(self.ipaddr)
A R Karthick19aaf5c2016-11-09 17:47:57 -0800517
A.R Karthickc4e474d2016-12-12 15:24:57 -0800518 @classmethod
519 def get_quagga_config(cls, instance = 0):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800520 quagga_config = copy.deepcopy(cls.QUAGGA_CONFIG)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800521 if instance == 0:
522 return quagga_config
523 ip = quagga_config[0]['ip']
524 octets = ip.split('.')
A R Karthickaa54a1c2016-12-15 11:42:08 -0800525 octets[3] = str((int(octets[3]) + instance) & 255)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800526 ip = '.'.join(octets)
527 quagga_config[0]['ip'] = ip
528 return quagga_config
529
530 @classmethod
531 def start_cluster_async(cls, onos_instances):
532 instances = filter(lambda o: o.running == False, onos_instances)
533 if not instances:
534 return
535 tpool = ThreadPool(len(instances), queue_size = 1, wait_timeout = 1)
536 for onos in instances:
537 tpool.addTask(onos.start_async)
538 tpool.cleanUpThreads()
539
540 def start_async(self):
541 print('Starting ONOS container %s' %self.name)
542 self.start(ports = self.ports, environment = self.env,
543 host_config = self.host_config, volumes = self.volumes, tty = True)
544 time.sleep(3)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700545 self.ipaddr = self.ip()
A.R Karthickc4e474d2016-12-12 15:24:57 -0800546 print('Waiting for ONOS container %s to start' %self.name)
547 self.wait_for_onos_start(self.ipaddr)
548 self.running = True
549 print('ONOS container %s started' %self.name)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700550
A R Karthick2b93d6a2016-09-06 15:19:09 -0700551 @classmethod
A R Karthick19aaf5c2016-11-09 17:47:57 -0800552 def wait_for_onos_start(cls, ip, tries = 30):
553 onos_log = OnosLog(host = ip)
554 num_tries = 0
555 started = None
556 while not started and num_tries < tries:
557 time.sleep(3)
558 started = onos_log.search_log_pattern('ApplicationManager .* Started')
559 num_tries += 1
560
A R Karthick19aaf5c2016-11-09 17:47:57 -0800561 if not started:
562 print('ONOS did not start')
563 else:
564 print('ONOS started')
565 return started
566
567 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700568 def setup_cluster_deprecated(cls, onos_instances, image_name = None):
569 if not onos_instances or len(onos_instances) < 2:
570 return
571 ips = []
572 if image_name is not None:
573 ips = Container.ips(image_name)
574 else:
575 for onos in onos_instances:
576 ips.append(onos.ipaddr)
577 Onos.cluster_instances = onos_instances
578 Onos.cluster_mode = True
579 ##regenerate the cluster json with the 3 instance ips before restarting them back
580 print('Generating cluster cfg for ONOS instances with ips %s' %ips)
581 Onos.generate_cluster_cfg(ips)
582 for onos in onos_instances:
583 onos.kill()
584 onos.remove_container(onos.name, force=True)
585 print('Restarting ONOS container %s for forming cluster' %onos.name)
586 onos.start(ports = onos.ports, environment = onos.env,
587 host_config = onos.host_config, volumes = onos.volumes, tty = True)
588 print('Waiting %d seconds for ONOS %s to boot' %(onos.boot_delay, onos.name))
589 time.sleep(onos.boot_delay)
590 onos.ipaddr = onos.ip()
591 onos.install_cord_apps(onos.ipaddr)
592
593 @classmethod
594 def setup_cluster(cls, onos_instances, image_name = None):
595 if not onos_instances or len(onos_instances) < 2:
596 return
597 ips = []
598 if image_name is not None:
599 ips = Container.ips(image_name)
600 else:
601 for onos in onos_instances:
602 ips.append(onos.ipaddr)
603 Onos.cluster_instances = onos_instances
604 Onos.cluster_mode = True
605 ##regenerate the cluster json with the 3 instance ips before restarting them back
606 print('Forming cluster for ONOS instances with ips %s' %ips)
607 Onos.form_cluster(ips)
608 ##wait for the cluster to be formed
609 print('Waiting for the cluster to be formed')
610 time.sleep(60)
611 for onos in onos_instances:
612 onos.install_cord_apps(onos.ipaddr)
613
614 @classmethod
A R Karthicke2c24bd2016-10-07 14:51:38 -0700615 def add_cluster(cls, count = 1, network_cfg = None):
616 if not cls.cluster_instances or Onos.cluster_mode is False:
617 return
618 for i in range(count):
619 name = '{}-{}'.format(Onos.NAME, len(cls.cluster_instances)+1)
620 onos = cls(name = name, image = Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX,
621 cluster = True, network_cfg = network_cfg)
622 cls.cluster_instances.append(onos)
623
624 cls.setup_cluster(cls.cluster_instances)
625
626 @classmethod
A.R Karthick2560f042016-11-30 14:38:52 -0800627 def restart_cluster(cls, network_cfg = None, timeout = 10, setup = False):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700628 if cls.cluster_mode is False:
629 return
630 if not cls.cluster_instances:
631 return
632
633 if network_cfg is not None:
634 json_data = json.dumps(network_cfg, indent=4)
635 with open('{}/network-cfg.json'.format(cls.host_config_dir), 'w') as f:
636 f.write(json_data)
637
A.R Karthick2560f042016-11-30 14:38:52 -0800638 cls.cleanup_cluster()
639 if timeout > 0:
640 time.sleep(timeout)
641
A R Karthickaa54a1c2016-12-15 11:42:08 -0800642 #start the instances asynchronously
643 cls.start_cluster_async(cls.cluster_instances)
644 time.sleep(5)
A.R Karthick2560f042016-11-30 14:38:52 -0800645 ##form the cluster as appropriate
646 if setup is True:
647 cls.setup_cluster(cls.cluster_instances)
A R Karthickaa54a1c2016-12-15 11:42:08 -0800648 else:
649 for onos in cls.cluster_instances:
650 onos.install_cord_apps(onos.ipaddr)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700651
652 @classmethod
653 def cluster_ips(cls):
654 if cls.cluster_mode is False:
655 return []
656 if not cls.cluster_instances:
657 return []
658 ips = [ onos.ipaddr for onos in cls.cluster_instances ]
659 return ips
660
661 @classmethod
662 def cleanup_cluster(cls):
663 if cls.cluster_mode is False:
664 return
665 if not cls.cluster_instances:
666 return
667 for onos in cls.cluster_instances:
668 if onos.exists():
669 onos.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800670 onos.running = False
A R Karthick2b93d6a2016-09-06 15:19:09 -0700671 onos.remove_container(onos.name, force=True)
A R Karthickd44cea12016-07-20 12:16:41 -0700672
A.R Karthick95d044e2016-06-10 18:44:36 -0700673 @classmethod
A R Karthickde6b9dc2016-11-29 17:46:16 -0800674 def restart_node(cls, node = None, network_cfg = None, timeout = 10):
A R Karthick889d9652016-10-03 14:13:45 -0700675 if node is None:
676 cls(restart = True, network_cfg = network_cfg, image = cls.IMAGE, tag = cls.TAG)
677 else:
678 #Restarts a node in the cluster
679 valid_node = filter(lambda onos: node in [ onos.ipaddr, onos.name ], cls.cluster_instances)
680 if valid_node:
681 onos = valid_node.pop()
682 if onos.exists():
683 onos.kill()
684 onos.remove_container(onos.name, force=True)
A R Karthickde6b9dc2016-11-29 17:46:16 -0800685 if timeout > 0:
686 time.sleep(timeout)
A R Karthick889d9652016-10-03 14:13:45 -0700687 print('Restarting ONOS container %s' %onos.name)
688 onos.start(ports = onos.ports, environment = onos.env,
689 host_config = onos.host_config, volumes = onos.volumes, tty = True)
A R Karthick889d9652016-10-03 14:13:45 -0700690 onos.ipaddr = onos.ip()
A.R Karthick2560f042016-11-30 14:38:52 -0800691 onos.wait_for_onos_start(onos.ipaddr)
692 onos.install_cord_apps(onos.ipaddr)
A R Karthick889d9652016-10-03 14:13:45 -0700693
694 @classmethod
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700695 def install_cord_apps(cls, onos_ip = None):
A.R Karthick95d044e2016-06-10 18:44:36 -0700696 for app, version in cls.onos_cord_apps:
697 app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version)
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700698 ok, code = OnosCtrl.install_app(app_file, onos_ip = onos_ip)
A.R Karthick95d044e2016-06-10 18:44:36 -0700699 ##app already installed (conflicts)
700 if code in [ 409 ]:
701 ok = True
702 print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install'))
703 time.sleep(2)
704
A.R Karthick1700e0e2016-10-06 18:16:57 -0700705class OnosStopWrapper(Container):
706 def __init__(self, name):
707 super(OnosStopWrapper, self).__init__(name, Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX)
708 if self.exists():
709 self.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800710 self.running = False
A.R Karthick1700e0e2016-10-06 18:16:57 -0700711 else:
712 if Onos.cluster_mode is True:
713 valid_node = filter(lambda onos: name in [ onos.ipaddr, onos.name ], Onos.cluster_instances)
714 if valid_node:
715 onos = valid_node.pop()
716 if onos.exists():
717 onos.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800718 onos.running = False
A.R Karthick1700e0e2016-10-06 18:16:57 -0700719
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700720class Radius(Container):
721 ports = [ 1812, 1813 ]
A R Karthick41adfce2016-06-10 09:51:25 -0700722 env = {'TIMEZONE':'America/Los_Angeles',
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700723 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
724 }
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700725 host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700726 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700727 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700728 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700729 start_command = os.path.join(guest_config_dir, 'start-radius.py')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700730 host_guest_map = ( (host_db_dir, guest_db_dir),
731 (host_config_dir, guest_config_dir)
732 )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700733 IMAGE = 'cord-test/radius'
734 NAME = 'cord-radius'
735
A R Karthick07608ef2016-08-23 16:51:19 -0700736 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
Chetan Gaonker503032a2016-05-12 12:06:29 -0700737 boot_delay = 10, restart = False, update = False):
A R Karthick07608ef2016-08-23 16:51:19 -0700738 super(Radius, self).__init__(name, image, prefix = prefix, tag = tag, command = self.start_command)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700739 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700740 self.build_image(self.image_name)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700741 if restart is True and self.exists():
742 self.kill()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700743 if not self.exists():
744 self.remove_container(name, force=True)
745 host_config = self.create_host_config(port_list = self.ports,
746 host_guest_map = self.host_guest_map)
747 volumes = []
748 for _,g in self.host_guest_map:
749 volumes.append(g)
A R Karthick41adfce2016-06-10 09:51:25 -0700750 self.start(ports = self.ports, environment = self.env,
751 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700752 host_config = host_config, tty = True)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700753 time.sleep(boot_delay)
754
755 @classmethod
756 def build_image(cls, image):
757 print('Building Radius image %s' %image)
758 dockerfile = '''
759FROM hbouvier/docker-radius
760MAINTAINER chetan@ciena.com
761LABEL RUN docker pull hbouvier/docker-radius
762LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius
A R Karthickc762df42016-05-25 10:09:21 -0700763RUN apt-get update && \
764 apt-get -y install python python-pexpect strace
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700765WORKDIR /root
766CMD ["/etc/freeradius/start-radius.py"]
767'''
768 super(Radius, cls).build_image(dockerfile, image)
769 print('Done building image %s' %image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700770
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700771class Quagga(Container):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800772 QUAGGA_CONFIG = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 },
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700773 { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 },
774 )
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700775 ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ]
776 host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config')
777 guest_quagga_config = '/root/config'
778 quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
779 host_guest_map = ( (host_quagga_config, guest_quagga_config), )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700780 IMAGE = 'cord-test/quagga'
781 NAME = 'cord-quagga'
782
A R Karthick07608ef2016-08-23 16:51:19 -0700783 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
Chetan Gaonker503032a2016-05-12 12:06:29 -0700784 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800785 super(Quagga, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.QUAGGA_CONFIG)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700786 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700787 self.build_image(self.image_name)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700788 if restart is True and self.exists():
789 self.kill()
790 if not self.exists():
791 self.remove_container(name, force=True)
A R Karthick41adfce2016-06-10 09:51:25 -0700792 host_config = self.create_host_config(port_list = self.ports,
793 host_guest_map = self.host_guest_map,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700794 privileged = True)
795 volumes = []
796 for _,g in self.host_guest_map:
797 volumes.append(g)
798 self.start(ports = self.ports,
A R Karthick41adfce2016-06-10 09:51:25 -0700799 host_config = host_config,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700800 volumes = volumes, tty = True)
801 print('Starting Quagga on container %s' %self.name)
802 self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
803 time.sleep(boot_delay)
804
805 @classmethod
806 def build_image(cls, image):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800807 onos_quagga_ip = Onos.QUAGGA_CONFIG[0]['ip']
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700808 print('Building Quagga image %s' %image)
809 dockerfile = '''
A R Karthick41adfce2016-06-10 09:51:25 -0700810FROM ubuntu:14.04
811MAINTAINER chetan@ciena.com
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700812WORKDIR /root
813RUN useradd -M quagga
814RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga
815RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga
A R Karthick973ea692016-10-17 12:23:02 -0700816RUN 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 -0700817RUN git clone git://git.savannah.nongnu.org/quagga.git quagga && \
A R Karthick8f69c2c2016-10-21 11:43:26 -0700818(cd quagga && git checkout quagga-1.0.20160315 && ./bootstrap.sh && \
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700819sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \
820./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install)
821RUN ldconfig
822'''.format(onos_quagga_ip)
823 super(Quagga, cls).build_image(dockerfile, image)
824 print('Done building image %s' %image)
A R Karthick81acbff2016-06-17 14:45:16 -0700825
A.R Karthick1700e0e2016-10-06 18:16:57 -0700826class QuaggaStopWrapper(Container):
827 def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'candidate'):
828 super(QuaggaStopWrapper, self).__init__(name, image, prefix = Container.IMAGE_PREFIX, tag = tag)
829 if self.exists():
830 self.kill()
831
832
A R Karthick81acbff2016-06-17 14:45:16 -0700833def reinitContainerClients():
834 docker_netns.dckr = Client()
835 Container.dckr = Client()
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700836
837class Xos(Container):
838 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
839 TAG = 'latest'
840 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700841 host_guest_map = None
842 env = None
843 ports = None
844 volumes = None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700845
A R Karthick6e80afd2016-10-10 16:03:12 -0700846 @classmethod
847 def get_cmd(cls, img_name):
848 cmd = cls.dckr.inspect_image(img_name)['Config']['Cmd']
849 return ' '.join(cmd)
850
A R Karthicke3bde962016-09-27 15:06:35 -0700851 def __init__(self, name, image, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700852 boot_delay = 20, restart = False, network_cfg = None, update = False):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700853 if restart is True:
854 ##Find the right image to restart
855 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
856 if running_image:
857 image_name = running_image[0]['Image']
858 try:
859 image = image_name.split(':')[0]
860 tag = image_name.split(':')[1]
861 except: pass
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700862 super(Xos, self).__init__(name, image, prefix = prefix, tag = tag)
863 if update is True or not self.img_exists():
864 self.build_image(self.image_name)
A R Karthick6e80afd2016-10-10 16:03:12 -0700865 self.command = self.get_cmd(self.image_name).strip() or None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700866 if restart is True and self.exists():
867 self.kill()
868 if not self.exists():
869 self.remove_container(name, force=True)
A R Karthicke3bde962016-09-27 15:06:35 -0700870 host_config = self.create_host_config(port_list = self.ports,
871 host_guest_map = self.host_guest_map,
872 privileged = True)
873 print('Starting XOS container %s' %self.name)
874 self.start(ports = self.ports, environment = self.env, host_config = host_config,
875 volumes = self.volumes, tty = True)
876 print('Waiting %d seconds for XOS Base Container to boot' %(boot_delay))
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700877 time.sleep(boot_delay)
878
879 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700880 def build_image(cls, image, dockerfile_path, image_target = 'build'):
881 cmd = 'cd {} && make {}'.format(dockerfile_path, image_target)
882 print('Building XOS %s' %image)
883 res = os.system(cmd)
884 print('Done building image %s. Image build %s' %(image, 'successful' if res == 0 else 'failed'))
885 return res
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700886
A R Karthicke3bde962016-09-27 15:06:35 -0700887class XosServer(Xos):
888 ports = [8000,9998,9999]
889 NAME = 'xos-server'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700890 IMAGE = 'xosproject/xos'
A R Karthicke3bde962016-09-27 15:06:35 -0700891 BASE_IMAGE = 'xosproject/xos-base'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700892 TAG = 'latest'
893 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700894 dockerfile_path = os.path.join(Xos.setup_dir, 'xos')
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700895
A R Karthicke3bde962016-09-27 15:06:35 -0700896 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700897 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700898 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700899
900 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700901 def build_image(cls, image = IMAGE):
902 ##build the base image and then build the server image
903 Xos.build_image(cls.BASE_IMAGE, cls.dockerfile_path, image_target = 'base')
904 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700905
A R Karthicke3bde962016-09-27 15:06:35 -0700906class XosSynchronizerOpenstack(Xos):
907 ports = [2375,]
908 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer')
909 NAME = 'xos-synchronizer'
910 IMAGE = 'xosproject/xos-synchronizer-openstack'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700911 TAG = 'latest'
912 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700913 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700914
A R Karthicke3bde962016-09-27 15:06:35 -0700915 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700916 tag = TAG, boot_delay = 20, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700917 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700918
919 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700920 def build_image(cls, image = IMAGE):
921 XosServer.build_image()
922 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700923
A R Karthicke3bde962016-09-27 15:06:35 -0700924class XosSynchronizerOnboarding(Xos):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700925 NAME = 'xos-synchronizer-onboarding'
926 IMAGE = 'xosproject/xos-synchronizer-onboarding'
927 TAG = 'latest'
928 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700929 dockerfile_path = os.path.join(Xos.setup_dir, 'onboarding_synchronizer')
930 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700931
A R Karthicke3bde962016-09-27 15:06:35 -0700932 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700933 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700934 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700935
936 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700937 def build_image(cls, image = IMAGE):
938 XosSynchronizerOpenstack.build_image()
939 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700940
A R Karthicke3bde962016-09-27 15:06:35 -0700941class XosSynchronizerOpenvpn(Xos):
942 NAME = 'xos-synchronizer-openvpn'
943 IMAGE = 'xosproject/xos-openvpn'
944 TAG = 'latest'
945 PREFIX = ''
946 dockerfile_path = os.path.join(Xos.setup_dir, 'openvpn')
947 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700948
A R Karthicke3bde962016-09-27 15:06:35 -0700949 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700950 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700951 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
952
953 @classmethod
954 def build_image(cls, image = IMAGE):
955 XosSynchronizerOpenstack.build_image()
956 Xos.build_image(image, cls.dockerfile_path)
957
958class XosPostgresql(Xos):
959 ports = [5432,]
960 NAME = 'xos-db-postgres'
961 IMAGE = 'xosproject/xos-postgres'
962 TAG = 'latest'
963 PREFIX = ''
964 volumes = ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
965 dockerfile_path = os.path.join(Xos.setup_dir, 'postgresql')
966
967 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700968 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700969 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
970
971 @classmethod
972 def build_image(cls, image = IMAGE):
973 Xos.build_image(image, cls.dockerfile_path)
974
975class XosSyndicateMs(Xos):
976 ports = [8080,]
977 env = None
978 NAME = 'xos-syndicate-ms'
979 IMAGE = 'xosproject/syndicate-ms'
980 TAG = 'latest'
981 PREFIX = ''
982 dockerfile_path = os.path.join(Xos.setup_dir, 'syndicate-ms')
983
984 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700985 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700986 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
987
988 @classmethod
989 def build_image(cls, image = IMAGE):
990 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700991
ChetanGaonkerc220e0d2016-10-05 05:06:25 -0700992class XosSyncVtn(Xos):
993 ports = [8080,]
994 env = None
995 NAME = 'xos-synchronizer-vtn'
996 IMAGE = 'xosproject/xos-synchronizer-vtn'
997 TAG = 'latest'
998 PREFIX = ''
999 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtn')
1000
1001 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001002 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001003 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1004
1005 @classmethod
1006 def build_image(cls, image = IMAGE):
1007 Xos.build_image(image, cls.dockerfile_path)
1008
1009class XosSyncVtr(Xos):
1010 ports = [8080,]
1011 env = None
1012 NAME = 'xos-synchronizer-vtr'
1013 IMAGE = 'xosproject/xos-synchronizer-vtr'
1014 TAG = 'latest'
1015 PREFIX = ''
1016 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtr')
1017
1018 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001019 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001020 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1021
1022 @classmethod
1023 def build_image(cls, image = IMAGE):
1024 Xos.build_image(image, cls.dockerfile_path)
1025
1026class XosSyncVsg(Xos):
1027 ports = [8080,]
1028 env = None
1029 NAME = 'xos-synchronizer-vsg'
1030 IMAGE = 'xosproject/xos-synchronizer-vsg'
1031 TAG = 'latest'
1032 PREFIX = ''
1033 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vsg')
1034
1035 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001036 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001037 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1038
1039 @classmethod
1040 def build_image(cls, image = IMAGE):
1041 Xos.build_image(image, cls.dockerfile_path)
1042
1043
1044class XosSyncOnos(Xos):
1045 ports = [8080,]
1046 env = None
1047 NAME = 'xos-synchronizer-onos'
1048 IMAGE = 'xosproject/xos-synchronizer-onos'
1049 TAG = 'latest'
1050 PREFIX = ''
1051 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-onos')
1052
1053 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001054 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001055 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1056
1057 @classmethod
1058 def build_image(cls, image = IMAGE):
1059 Xos.build_image(image, cls.dockerfile_path)
1060
1061class XosSyncFabric(Xos):
1062 ports = [8080,]
1063 env = None
1064 NAME = 'xos-synchronizer-fabric'
1065 IMAGE = 'xosproject/xos-synchronizer-fabric'
1066 TAG = 'latest'
1067 PREFIX = ''
1068 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-fabric')
1069
1070 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001071 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001072 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1073
1074 @classmethod
1075 def build_image(cls, image = IMAGE):
1076 Xos.build_image(image, cls.dockerfile_path)
A R Karthick19aaf5c2016-11-09 17:47:57 -08001077
1078if __name__ == '__main__':
1079 onos = Onos(boot_delay = 10, restart = True)