blob: d98bbe008b0bb3106a7c25fa17ee3f278bff461a [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
Chetan Gaonker3533faa2016-04-25 17:50:14 -070021from pyroute2 import IPRoute
A.R Karthickc4e474d2016-12-12 15:24:57 -080022from pyroute2.netlink import NetlinkError
Chetan Gaonker3533faa2016-04-25 17:50:14 -070023from itertools import chain
24from nsenter import Namespace
25from docker import Client
A R Karthickec2db322016-11-17 15:06:01 -080026from shutil import rmtree
A.R Karthick95d044e2016-06-10 18:44:36 -070027from OnosCtrl import OnosCtrl
A R Karthick19aaf5c2016-11-09 17:47:57 -080028from OnosLog import OnosLog
A.R Karthickc4e474d2016-12-12 15:24:57 -080029from threadPool import ThreadPool
Chetan Gaonker3533faa2016-04-25 17:50:14 -070030
31class docker_netns(object):
32
33 dckr = Client()
34 def __init__(self, name):
35 pid = int(self.dckr.inspect_container(name)['State']['Pid'])
36 if pid == 0:
37 raise Exception('no container named {0}'.format(name))
38 self.pid = pid
39
40 def __enter__(self):
41 pid = self.pid
42 if not os.path.exists('/var/run/netns'):
43 os.mkdir('/var/run/netns')
44 os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid))
45 return str(pid)
46
47 def __exit__(self, type, value, traceback):
48 pid = self.pid
49 os.unlink('/var/run/netns/{0}'.format(pid))
50
51flatten = lambda l: chain.from_iterable(l)
52
53class Container(object):
54 dckr = Client()
A R Karthick07608ef2016-08-23 16:51:19 -070055 IMAGE_PREFIX = '' ##for saving global prefix for all test classes
56
57 def __init__(self, name, image, prefix='', tag = 'candidate', command = 'bash', quagga_config = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -070058 self.name = name
A R Karthick07608ef2016-08-23 16:51:19 -070059 self.prefix = prefix
60 if prefix:
61 self.prefix += '/'
62 image = '{}{}'.format(self.prefix, image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -070063 self.image = image
64 self.tag = tag
A R Karthickd44cea12016-07-20 12:16:41 -070065 if tag:
66 self.image_name = image + ':' + tag
67 else:
68 self.image_name = image
Chetan Gaonker3533faa2016-04-25 17:50:14 -070069 self.id = None
70 self.command = command
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -070071 self.quagga_config = quagga_config
Chetan Gaonker3533faa2016-04-25 17:50:14 -070072
73 @classmethod
74 def build_image(cls, dockerfile, tag, force=True, nocache=False):
75 f = io.BytesIO(dockerfile.encode('utf-8'))
76 if force or not cls.image_exists(tag):
77 print('Build {0}...'.format(tag))
78 for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache):
79 if 'stream' in line:
80 print(line['stream'].strip())
81
82 @classmethod
83 def image_exists(cls, name):
84 return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()]
85
86 @classmethod
87 def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False):
88 port_bindings = None
89 binds = None
90 if port_list:
91 port_bindings = {}
92 for p in port_list:
93 port_bindings[str(p)] = str(p)
94
95 if host_guest_map:
96 binds = []
97 for h, g in host_guest_map:
98 binds.append('{0}:{1}'.format(h, g))
99
100 return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged)
101
102 @classmethod
103 def cleanup(cls, image):
A R Karthick09b1f4e2016-05-12 14:31:50 -0700104 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700105 for cnt in cnt_list:
106 print('Cleaning container %s' %cnt['Id'])
A.R Karthick95d044e2016-06-10 18:44:36 -0700107 if cnt.has_key('State') and cnt['State'] == 'running':
A R Karthick09b1f4e2016-05-12 14:31:50 -0700108 cls.dckr.kill(cnt['Id'])
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700109 cls.dckr.remove_container(cnt['Id'], force=True)
110
111 @classmethod
112 def remove_container(cls, name, force=True):
113 try:
114 cls.dckr.remove_container(name, force = force)
115 except: pass
116
117 def exists(self):
118 return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers()))
119
120 def img_exists(self):
A R Karthick6d98a592016-08-24 15:16:46 -0700121 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 -0700122
123 def ip(self):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700124 cnt_list = filter(lambda c: c['Names'][0] == '/{}'.format(self.name), self.dckr.containers())
125 #if not cnt_list:
126 # cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700127 cnt_settings = cnt_list.pop()
128 return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress']
129
A R Karthick2b93d6a2016-09-06 15:19:09 -0700130 @classmethod
131 def ips(cls, image_name):
132 cnt_list = filter(lambda c: c['Image'] == image_name, cls.dckr.containers())
133 ips = [ cnt['NetworkSettings']['Networks']['bridge']['IPAddress'] for cnt in cnt_list ]
134 return ips
135
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700136 def kill(self, remove = True):
137 self.dckr.kill(self.name)
138 self.dckr.remove_container(self.name, force=True)
139
A R Karthick41adfce2016-06-10 09:51:25 -0700140 def start(self, rm = True, ports = None, volumes = None, host_config = None,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700141 environment = None, tty = False, stdin_open = True):
142
143 if rm and self.exists():
144 print('Removing container:', self.name)
145 self.dckr.remove_container(self.name, force=True)
146
A R Karthick41adfce2016-06-10 09:51:25 -0700147 ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700148 detach=True, name=self.name,
A R Karthick41adfce2016-06-10 09:51:25 -0700149 environment = environment,
150 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700151 host_config = host_config, stdin_open=stdin_open, tty = tty)
152 self.dckr.start(container=self.name)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700153 if self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700154 self.connect_to_br()
155 self.id = ctn['Id']
156 return ctn
157
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000158 @classmethod
159 def pause_container(cls, image, delay):
160 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
161 for cnt in cnt_list:
162 print('Pause the container %s' %cnt['Id'])
163 if cnt.has_key('State') and cnt['State'] == 'running':
164 cls.dckr.pause(cnt['Id'])
165 if delay != 0:
166 time.sleep(delay)
167 for cnt in cnt_list:
168 print('Unpause the container %s' %cnt['Id'])
169 cls.dckr.unpause(cnt['Id'])
170 else:
171 print('Infinity time pause the container %s' %cnt['Id'])
172 return 'success'
173
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700174 def connect_to_br(self):
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700175 index = 0
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700176 with docker_netns(self.name) as pid:
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700177 for quagga_config in self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700178 ip = IPRoute()
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700179 br = ip.link_lookup(ifname=quagga_config['bridge'])
180 if len(br) == 0:
A.R Karthickc4e474d2016-12-12 15:24:57 -0800181 try:
182 ip.link_create(ifname=quagga_config['bridge'], kind='bridge')
183 except NetlinkError as e:
184 err, _ = e.args
185 if err == errno.EEXIST:
186 pass
187 else:
188 raise NetlinkError(*e.args)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700189 br = ip.link_lookup(ifname=quagga_config['bridge'])
190 br = br[0]
191 ip.link('set', index=br, state='up')
192 ifname = '{0}-{1}'.format(self.name, index)
193 ifs = ip.link_lookup(ifname=ifname)
194 if len(ifs) > 0:
195 ip.link_remove(ifs[0])
196 peer_ifname = '{0}-{1}'.format(pid, index)
Chetan Gaonker5a0fda32016-05-10 14:09:07 -0700197 ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700198 host = ip.link_lookup(ifname=ifname)[0]
199 ip.link('set', index=host, master=br)
200 ip.link('set', index=host, state='up')
201 guest = ip.link_lookup(ifname=peer_ifname)[0]
202 ip.link('set', index=guest, net_ns_fd=pid)
203 with Namespace(pid, 'net'):
204 ip = IPRoute()
205 ip.link('set', index=guest, ifname='eth{}'.format(index+1))
206 ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask'])
207 ip.link('set', index=guest, state='up')
208 index += 1
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700209
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000210 def execute(self, cmd, tty = True, stream = False, shell = False, detach = True):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700211 res = 0
212 if type(cmd) == str:
213 cmds = (cmd,)
214 else:
215 cmds = cmd
216 if shell:
217 for c in cmds:
218 res += os.system('docker exec {0} {1}'.format(self.name, c))
219 return res
220 for c in cmds:
221 i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True)
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000222 self.dckr.exec_start(i['Id'], stream = stream, detach=detach)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700223 result = self.dckr.exec_inspect(i['Id'])
224 res += 0 if result['ExitCode'] == None else result['ExitCode']
225 return res
226
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700227 def restart(self, timeout =10):
228 return self.dckr.restart(self.name, timeout)
229
A R Karthick1f908202016-11-16 17:32:20 -0800230def get_mem(instances = 1):
231 if instances <= 0:
232 instances = 1
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700233 with open('/proc/meminfo', 'r') as fd:
234 meminfo = fd.readlines()
235 mem = 0
236 for m in meminfo:
237 if m.startswith('MemTotal:') or m.startswith('SwapTotal:'):
238 mem += int(m.split(':')[1].strip().split()[0])
239
A R Karthick1f908202016-11-16 17:32:20 -0800240 mem = max(mem/1024/1024/2/instances, 1)
Chetan Gaonker6d0a7b02016-05-03 16:57:28 -0700241 mem = min(mem, 16)
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700242 return str(mem) + 'G'
243
A R Karthickd44cea12016-07-20 12:16:41 -0700244class OnosCord(Container):
245 """Use this when running the cord tester agent on the onos compute node"""
246 onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord')
247 onos_config_dir_guest = '/root/onos/config'
248 onos_config_dir = os.path.join(onos_cord_dir, 'config')
249 docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml')
250
A R Karthickbd9b8a32016-07-21 09:56:45 -0700251 def __init__(self, onos_ip, conf, boot_delay = 60):
252 self.onos_ip = onos_ip
A R Karthickd44cea12016-07-20 12:16:41 -0700253 self.cord_conf_dir = conf
A R Karthickbd9b8a32016-07-21 09:56:45 -0700254 self.boot_delay = boot_delay
A R Karthickd44cea12016-07-20 12:16:41 -0700255 if os.access(self.cord_conf_dir, os.F_OK) and not os.access(self.onos_cord_dir, os.F_OK):
256 os.mkdir(self.onos_cord_dir)
257 os.mkdir(self.onos_config_dir)
258 ##copy the config file from cord-tester-config
259 cmd = 'cp {}/* {}'.format(self.cord_conf_dir, self.onos_cord_dir)
260 os.system(cmd)
261
262 ##update the docker yaml with the config volume
263 with open(self.docker_yaml, 'r') as f:
264 yaml_config = yaml.load(f)
265 image = yaml_config['services'].keys()[0]
266 name = 'cordtestercord_{}_1'.format(image)
267 volumes = yaml_config['services'][image]['volumes']
268 config_volumes = filter(lambda e: e.find(self.onos_config_dir_guest) >= 0, volumes)
269 if not config_volumes:
270 config_volume = '{}:{}'.format(self.onos_config_dir, self.onos_config_dir_guest)
271 volumes.append(config_volume)
272 docker_yaml_changed = '{}-changed'.format(self.docker_yaml)
273 with open(docker_yaml_changed, 'w') as wf:
274 yaml.dump(yaml_config, wf)
275
276 os.rename(docker_yaml_changed, self.docker_yaml)
277 self.volumes = volumes
278
279 super(OnosCord, self).__init__(name, image, tag = '')
280 cord_conf_dir_basename = os.path.basename(self.cord_conf_dir.replace('-', ''))
281 self.xos_onos_name = '{}_{}_1'.format(cord_conf_dir_basename, image)
282 ##Create an container instance of xos onos
283 self.xos_onos = Container(self.xos_onos_name, image, tag = '')
284
285 def start(self, restart = False, network_cfg = None):
286 if restart is True:
287 if self.exists():
288 ##Kill the existing instance
289 print('Killing container %s' %self.name)
290 self.kill()
291 if self.xos_onos.exists():
292 print('Killing container %s' %self.xos_onos.name)
293 self.xos_onos.kill()
294
295 if network_cfg is not None:
296 json_data = json.dumps(network_cfg, indent=4)
297 with open('{}/network-cfg.json'.format(self.onos_config_dir), 'w') as f:
298 f.write(json_data)
299
300 #start the container using docker-compose
301 cmd = 'cd {} && docker-compose up -d'.format(self.onos_cord_dir)
302 os.system(cmd)
A R Karthickbd9b8a32016-07-21 09:56:45 -0700303 #Delay to make sure ONOS fully boots
304 time.sleep(self.boot_delay)
305 Onos.install_cord_apps(onos_ip = self.onos_ip)
A R Karthickd44cea12016-07-20 12:16:41 -0700306
307 def build_image(self):
308 build_cmd = 'cd {} && docker-compose build'.format(self.onos_cord_dir)
309 os.system(build_cmd)
310
A.R Karthick1700e0e2016-10-06 18:16:57 -0700311class OnosCordStopWrapper(Container):
312 onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord')
313 docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml')
314
315 def __init__(self):
316 if os.access(self.docker_yaml, os.F_OK):
317 with open(self.docker_yaml, 'r') as f:
318 yaml_config = yaml.load(f)
319 image = yaml_config['services'].keys()[0]
320 name = 'cordtestercord_{}_1'.format(image)
321 super(OnosCordStopWrapper, self).__init__(name, image, tag = '')
322 if self.exists():
323 print('Killing container %s' %self.name)
324 self.kill()
325
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700326class Onos(Container):
327
A.R Karthickc4e474d2016-12-12 15:24:57 -0800328 quagga_config = [ { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, ]
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700329 SYSTEM_MEMORY = (get_mem(),) * 2
A R Karthick1f908202016-11-16 17:32:20 -0800330 INSTANCE_MEMORY = (get_mem(instances=3),) * 2
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700331 JAVA_OPTS = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'.format(*SYSTEM_MEMORY)#-XX:+PrintGCDetails -XX:+PrintGCTimeStamps'
A R Karthick1f908202016-11-16 17:32:20 -0800332 JAVA_OPTS_CLUSTER = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'.format(*INSTANCE_MEMORY)
A.R Karthick95d044e2016-06-10 18:44:36 -0700333 env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,vrouter', 'JAVA_OPTS' : JAVA_OPTS }
A.R Karthickdfeadb02016-11-30 17:55:51 -0800334 onos_cord_apps = ( ('cord-config', '1.1-SNAPSHOT'),
335 ('aaa', '1.1-SNAPSHOT'),
336 ('igmp', '1.1-SNAPSHOT'),
337 #('vtn', '1.1-SNAPSHOT'),
A.R Karthick95d044e2016-06-10 18:44:36 -0700338 )
A.R Karthickc4e474d2016-12-12 15:24:57 -0800339 ports = [] #[ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ]
A R Karthickf2f4ca62016-08-17 10:34:08 -0700340 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
341 host_config_dir = os.path.join(setup_dir, 'onos-config')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700342 guest_config_dir = '/root/onos/config'
A R Karthickec2db322016-11-17 15:06:01 -0800343 guest_data_dir = '/root/onos/apache-karaf-3.0.5/data'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700344 onos_gen_partitions = os.path.join(setup_dir, 'onos-gen-partitions')
A R Karthick2b93d6a2016-09-06 15:19:09 -0700345 onos_form_cluster = os.path.join(setup_dir, 'onos-form-cluster')
A.R Karthick95d044e2016-06-10 18:44:36 -0700346 cord_apps_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'apps')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700347 host_guest_map = ( (host_config_dir, guest_config_dir), )
A R Karthick2b93d6a2016-09-06 15:19:09 -0700348 cluster_cfg = os.path.join(host_config_dir, 'cluster.json')
349 cluster_mode = False
350 cluster_instances = []
Chetan Gaonker503032a2016-05-12 12:06:29 -0700351 NAME = 'cord-onos'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700352 ##the ip of ONOS in default cluster.json in setup/onos-config
353 CLUSTER_CFG_IP = '172.17.0.2'
A R Karthick07608ef2016-08-23 16:51:19 -0700354 IMAGE = 'onosproject/onos'
355 TAG = 'latest'
356 PREFIX = ''
A R Karthickf2f4ca62016-08-17 10:34:08 -0700357
358 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700359 def generate_cluster_cfg(cls, ip):
360 if type(ip) in [ list, tuple ]:
361 ips = ' '.join(ip)
362 else:
363 ips = ip
A R Karthickf2f4ca62016-08-17 10:34:08 -0700364 try:
A R Karthick2b93d6a2016-09-06 15:19:09 -0700365 cmd = '{} {} {}'.format(cls.onos_gen_partitions, cls.cluster_cfg, ips)
366 os.system(cmd)
367 except: pass
368
369 @classmethod
370 def form_cluster(cls, ips):
371 nodes = ' '.join(ips)
372 try:
373 cmd = '{} {}'.format(cls.onos_form_cluster, nodes)
A R Karthickf2f4ca62016-08-17 10:34:08 -0700374 os.system(cmd)
375 except: pass
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700376
A R Karthick9d48c652016-09-15 09:16:36 -0700377 @classmethod
378 def cleanup_runtime(cls):
379 '''Cleanup ONOS runtime generated files'''
380 files = ( Onos.cluster_cfg, os.path.join(Onos.host_config_dir, 'network-cfg.json') )
381 for f in files:
382 if os.access(f, os.F_OK):
383 try:
384 os.unlink(f)
385 except: pass
386
A R Karthickec2db322016-11-17 15:06:01 -0800387 @classmethod
388 def get_data_map(cls, host_volume, guest_volume_dir):
389 host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume))
390 if not os.path.exists(host_volume_dir):
391 os.mkdir(host_volume_dir)
392 return ( (host_volume_dir, guest_volume_dir), )
393
394 @classmethod
395 def remove_data_map(cls, host_volume, guest_volume_dir):
396 host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume))
397 if os.path.exists(host_volume_dir):
398 rmtree(host_volume_dir)
399
400 def remove_data_volume(self):
401 if self.data_map is not None:
402 self.remove_data_map(*self.data_map)
403
A.R Karthick1700e0e2016-10-06 18:16:57 -0700404 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthickec2db322016-11-17 15:06:01 -0800405 boot_delay = 20, restart = False, network_cfg = None,
A R Karthick3b811152016-12-15 10:24:24 -0800406 cluster = False, data_volume = None, async = False, quagga_config = None, max_instances=1):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700407 if restart is True:
408 ##Find the right image to restart
409 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
410 if running_image:
411 image_name = running_image[0]['Image']
412 try:
413 image = image_name.split(':')[0]
414 tag = image_name.split(':')[1]
415 except: pass
416
A.R Karthickc4e474d2016-12-12 15:24:57 -0800417 if quagga_config is not None:
418 self.quagga_config = quagga_config
A R Karthick07608ef2016-08-23 16:51:19 -0700419 super(Onos, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.quagga_config)
A R Karthick3b811152016-12-15 10:24:24 -0800420 self.max_instances = max_instances
A R Karthick2b93d6a2016-09-06 15:19:09 -0700421 self.boot_delay = boot_delay
A R Karthickec2db322016-11-17 15:06:01 -0800422 self.data_map = None
A R Karthick2b93d6a2016-09-06 15:19:09 -0700423 if cluster is True:
424 self.ports = []
A R Karthick3b811152016-12-15 10:24:24 -0800425 if self.max_instances <= 3:
426 java_opts = self.JAVA_OPTS_CLUSTER
427 else:
428 instance_memory = (get_mem(instances=self.max_instances),) * 2
429 java_opts = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'.format(*instance_memory)
430
431 self.env['JAVA_OPTS'] = java_opts
A R Karthickec2db322016-11-17 15:06:01 -0800432 if data_volume is not None:
433 self.data_map = self.get_data_map(data_volume, self.guest_data_dir)
434 self.host_guest_map = self.host_guest_map + self.data_map
A R Karthick2b93d6a2016-09-06 15:19:09 -0700435 if os.access(self.cluster_cfg, os.F_OK):
436 try:
437 os.unlink(self.cluster_cfg)
438 except: pass
439
440 self.host_config = self.create_host_config(port_list = self.ports,
441 host_guest_map = self.host_guest_map)
442 self.volumes = []
443 for _,g in self.host_guest_map:
444 self.volumes.append(g)
445
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700446 if restart is True and self.exists():
447 self.kill()
A R Karthick2b93d6a2016-09-06 15:19:09 -0700448
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700449 if not self.exists():
450 self.remove_container(name, force=True)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700451 host_config = self.create_host_config(port_list = self.ports,
452 host_guest_map = self.host_guest_map)
453 volumes = []
454 for _,g in self.host_guest_map:
455 volumes.append(g)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700456 if network_cfg is not None:
A R Karthick81acbff2016-06-17 14:45:16 -0700457 json_data = json.dumps(network_cfg, indent=4)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700458 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
459 f.write(json_data)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800460 if cluster is False or async is False:
461 print('Starting ONOS container %s' %self.name)
462 self.start(ports = self.ports, environment = self.env,
463 host_config = self.host_config, volumes = self.volumes, tty = True)
464 if not restart:
465 ##wait a bit before fetching IP to regenerate cluster cfg
466 time.sleep(5)
467 ip = self.ip()
468 ##Just a quick hack/check to ensure we don't regenerate in the common case.
469 ##As ONOS is usually the first test container that is started
470 if cluster is False:
471 if ip != self.CLUSTER_CFG_IP or not os.access(self.cluster_cfg, os.F_OK):
472 print('Regenerating ONOS cluster cfg for ip %s' %ip)
473 self.generate_cluster_cfg(ip)
474 self.kill()
475 self.remove_container(self.name, force=True)
476 print('Restarting ONOS container %s' %self.name)
477 self.start(ports = self.ports, environment = self.env,
478 host_config = self.host_config, volumes = self.volumes, tty = True)
479 print('Waiting for ONOS to boot')
480 time.sleep(boot_delay)
481 self.wait_for_onos_start(self.ip())
482 self.running = True
483 else:
484 self.running = False
485 else:
486 self.running = True
487 if self.running:
488 self.ipaddr = self.ip()
489 if cluster is False:
490 self.install_cord_apps(self.ipaddr)
A R Karthick19aaf5c2016-11-09 17:47:57 -0800491
A.R Karthickc4e474d2016-12-12 15:24:57 -0800492 @classmethod
493 def get_quagga_config(cls, instance = 0):
494 quagga_config = cls.quagga_config[:]
495 if instance == 0:
496 return quagga_config
497 ip = quagga_config[0]['ip']
498 octets = ip.split('.')
499 octets[3] = str((int(octets[3]) + 1) & 255)
500 ip = '.'.join(octets)
501 quagga_config[0]['ip'] = ip
502 return quagga_config
503
504 @classmethod
505 def start_cluster_async(cls, onos_instances):
506 instances = filter(lambda o: o.running == False, onos_instances)
507 if not instances:
508 return
509 tpool = ThreadPool(len(instances), queue_size = 1, wait_timeout = 1)
510 for onos in instances:
511 tpool.addTask(onos.start_async)
512 tpool.cleanUpThreads()
513
514 def start_async(self):
515 print('Starting ONOS container %s' %self.name)
516 self.start(ports = self.ports, environment = self.env,
517 host_config = self.host_config, volumes = self.volumes, tty = True)
518 time.sleep(3)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700519 self.ipaddr = self.ip()
A.R Karthickc4e474d2016-12-12 15:24:57 -0800520 print('Waiting for ONOS container %s to start' %self.name)
521 self.wait_for_onos_start(self.ipaddr)
522 self.running = True
523 print('ONOS container %s started' %self.name)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700524
A R Karthick2b93d6a2016-09-06 15:19:09 -0700525 @classmethod
A R Karthick19aaf5c2016-11-09 17:47:57 -0800526 def wait_for_onos_start(cls, ip, tries = 30):
527 onos_log = OnosLog(host = ip)
528 num_tries = 0
529 started = None
530 while not started and num_tries < tries:
531 time.sleep(3)
532 started = onos_log.search_log_pattern('ApplicationManager .* Started')
533 num_tries += 1
534
A R Karthick19aaf5c2016-11-09 17:47:57 -0800535 if not started:
536 print('ONOS did not start')
537 else:
538 print('ONOS started')
539 return started
540
541 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700542 def setup_cluster_deprecated(cls, onos_instances, image_name = None):
543 if not onos_instances or len(onos_instances) < 2:
544 return
545 ips = []
546 if image_name is not None:
547 ips = Container.ips(image_name)
548 else:
549 for onos in onos_instances:
550 ips.append(onos.ipaddr)
551 Onos.cluster_instances = onos_instances
552 Onos.cluster_mode = True
553 ##regenerate the cluster json with the 3 instance ips before restarting them back
554 print('Generating cluster cfg for ONOS instances with ips %s' %ips)
555 Onos.generate_cluster_cfg(ips)
556 for onos in onos_instances:
557 onos.kill()
558 onos.remove_container(onos.name, force=True)
559 print('Restarting ONOS container %s for forming cluster' %onos.name)
560 onos.start(ports = onos.ports, environment = onos.env,
561 host_config = onos.host_config, volumes = onos.volumes, tty = True)
562 print('Waiting %d seconds for ONOS %s to boot' %(onos.boot_delay, onos.name))
563 time.sleep(onos.boot_delay)
564 onos.ipaddr = onos.ip()
565 onos.install_cord_apps(onos.ipaddr)
566
567 @classmethod
568 def setup_cluster(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('Forming cluster for ONOS instances with ips %s' %ips)
581 Onos.form_cluster(ips)
582 ##wait for the cluster to be formed
583 print('Waiting for the cluster to be formed')
584 time.sleep(60)
585 for onos in onos_instances:
586 onos.install_cord_apps(onos.ipaddr)
587
588 @classmethod
A R Karthicke2c24bd2016-10-07 14:51:38 -0700589 def add_cluster(cls, count = 1, network_cfg = None):
590 if not cls.cluster_instances or Onos.cluster_mode is False:
591 return
592 for i in range(count):
593 name = '{}-{}'.format(Onos.NAME, len(cls.cluster_instances)+1)
594 onos = cls(name = name, image = Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX,
595 cluster = True, network_cfg = network_cfg)
596 cls.cluster_instances.append(onos)
597
598 cls.setup_cluster(cls.cluster_instances)
599
600 @classmethod
A.R Karthick2560f042016-11-30 14:38:52 -0800601 def restart_cluster(cls, network_cfg = None, timeout = 10, setup = False):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700602 if cls.cluster_mode is False:
603 return
604 if not cls.cluster_instances:
605 return
606
607 if network_cfg is not None:
608 json_data = json.dumps(network_cfg, indent=4)
609 with open('{}/network-cfg.json'.format(cls.host_config_dir), 'w') as f:
610 f.write(json_data)
611
A.R Karthick2560f042016-11-30 14:38:52 -0800612 cls.cleanup_cluster()
613 if timeout > 0:
614 time.sleep(timeout)
615
A R Karthick2b93d6a2016-09-06 15:19:09 -0700616 for onos in cls.cluster_instances:
A R Karthick2b93d6a2016-09-06 15:19:09 -0700617 print('Restarting ONOS container %s' %onos.name)
618 onos.start(ports = onos.ports, environment = onos.env,
619 host_config = onos.host_config, volumes = onos.volumes, tty = True)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700620 onos.ipaddr = onos.ip()
A.R Karthick2560f042016-11-30 14:38:52 -0800621 onos.wait_for_onos_start(onos.ipaddr)
622 onos.install_cord_apps(onos.ipaddr)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700623
A.R Karthick2560f042016-11-30 14:38:52 -0800624 ##form the cluster as appropriate
625 if setup is True:
626 cls.setup_cluster(cls.cluster_instances)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700627
628 @classmethod
629 def cluster_ips(cls):
630 if cls.cluster_mode is False:
631 return []
632 if not cls.cluster_instances:
633 return []
634 ips = [ onos.ipaddr for onos in cls.cluster_instances ]
635 return ips
636
637 @classmethod
638 def cleanup_cluster(cls):
639 if cls.cluster_mode is False:
640 return
641 if not cls.cluster_instances:
642 return
643 for onos in cls.cluster_instances:
644 if onos.exists():
645 onos.kill()
646 onos.remove_container(onos.name, force=True)
A R Karthickd44cea12016-07-20 12:16:41 -0700647
A.R Karthick95d044e2016-06-10 18:44:36 -0700648 @classmethod
A R Karthickde6b9dc2016-11-29 17:46:16 -0800649 def restart_node(cls, node = None, network_cfg = None, timeout = 10):
A R Karthick889d9652016-10-03 14:13:45 -0700650 if node is None:
651 cls(restart = True, network_cfg = network_cfg, image = cls.IMAGE, tag = cls.TAG)
652 else:
653 #Restarts a node in the cluster
654 valid_node = filter(lambda onos: node in [ onos.ipaddr, onos.name ], cls.cluster_instances)
655 if valid_node:
656 onos = valid_node.pop()
657 if onos.exists():
658 onos.kill()
659 onos.remove_container(onos.name, force=True)
A R Karthickde6b9dc2016-11-29 17:46:16 -0800660 if timeout > 0:
661 time.sleep(timeout)
A R Karthick889d9652016-10-03 14:13:45 -0700662 print('Restarting ONOS container %s' %onos.name)
663 onos.start(ports = onos.ports, environment = onos.env,
664 host_config = onos.host_config, volumes = onos.volumes, tty = True)
A R Karthick889d9652016-10-03 14:13:45 -0700665 onos.ipaddr = onos.ip()
A.R Karthick2560f042016-11-30 14:38:52 -0800666 onos.wait_for_onos_start(onos.ipaddr)
667 onos.install_cord_apps(onos.ipaddr)
A R Karthick889d9652016-10-03 14:13:45 -0700668
669 @classmethod
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700670 def install_cord_apps(cls, onos_ip = None):
A.R Karthick95d044e2016-06-10 18:44:36 -0700671 for app, version in cls.onos_cord_apps:
672 app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version)
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700673 ok, code = OnosCtrl.install_app(app_file, onos_ip = onos_ip)
A.R Karthick95d044e2016-06-10 18:44:36 -0700674 ##app already installed (conflicts)
675 if code in [ 409 ]:
676 ok = True
677 print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install'))
678 time.sleep(2)
679
A.R Karthick1700e0e2016-10-06 18:16:57 -0700680class OnosStopWrapper(Container):
681 def __init__(self, name):
682 super(OnosStopWrapper, self).__init__(name, Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX)
683 if self.exists():
684 self.kill()
685 else:
686 if Onos.cluster_mode is True:
687 valid_node = filter(lambda onos: name in [ onos.ipaddr, onos.name ], Onos.cluster_instances)
688 if valid_node:
689 onos = valid_node.pop()
690 if onos.exists():
691 onos.kill()
692
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700693class Radius(Container):
694 ports = [ 1812, 1813 ]
A R Karthick41adfce2016-06-10 09:51:25 -0700695 env = {'TIMEZONE':'America/Los_Angeles',
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700696 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
697 }
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700698 host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700699 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700700 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700701 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700702 start_command = os.path.join(guest_config_dir, 'start-radius.py')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700703 host_guest_map = ( (host_db_dir, guest_db_dir),
704 (host_config_dir, guest_config_dir)
705 )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700706 IMAGE = 'cord-test/radius'
707 NAME = 'cord-radius'
708
A R Karthick07608ef2016-08-23 16:51:19 -0700709 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
Chetan Gaonker503032a2016-05-12 12:06:29 -0700710 boot_delay = 10, restart = False, update = False):
A R Karthick07608ef2016-08-23 16:51:19 -0700711 super(Radius, self).__init__(name, image, prefix = prefix, tag = tag, command = self.start_command)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700712 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700713 self.build_image(self.image_name)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700714 if restart is True and self.exists():
715 self.kill()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700716 if not self.exists():
717 self.remove_container(name, force=True)
718 host_config = self.create_host_config(port_list = self.ports,
719 host_guest_map = self.host_guest_map)
720 volumes = []
721 for _,g in self.host_guest_map:
722 volumes.append(g)
A R Karthick41adfce2016-06-10 09:51:25 -0700723 self.start(ports = self.ports, environment = self.env,
724 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700725 host_config = host_config, tty = True)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700726 time.sleep(boot_delay)
727
728 @classmethod
729 def build_image(cls, image):
730 print('Building Radius image %s' %image)
731 dockerfile = '''
732FROM hbouvier/docker-radius
733MAINTAINER chetan@ciena.com
734LABEL RUN docker pull hbouvier/docker-radius
735LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius
A R Karthickc762df42016-05-25 10:09:21 -0700736RUN apt-get update && \
737 apt-get -y install python python-pexpect strace
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700738WORKDIR /root
739CMD ["/etc/freeradius/start-radius.py"]
740'''
741 super(Radius, cls).build_image(dockerfile, image)
742 print('Done building image %s' %image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700743
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700744class Quagga(Container):
A R Karthick41adfce2016-06-10 09:51:25 -0700745 quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 },
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700746 { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 },
747 )
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700748 ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ]
749 host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config')
750 guest_quagga_config = '/root/config'
751 quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
752 host_guest_map = ( (host_quagga_config, guest_quagga_config), )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700753 IMAGE = 'cord-test/quagga'
754 NAME = 'cord-quagga'
755
A R Karthick07608ef2016-08-23 16:51:19 -0700756 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
Chetan Gaonker503032a2016-05-12 12:06:29 -0700757 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False):
A R Karthick07608ef2016-08-23 16:51:19 -0700758 super(Quagga, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.quagga_config)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700759 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700760 self.build_image(self.image_name)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700761 if restart is True and self.exists():
762 self.kill()
763 if not self.exists():
764 self.remove_container(name, force=True)
A R Karthick41adfce2016-06-10 09:51:25 -0700765 host_config = self.create_host_config(port_list = self.ports,
766 host_guest_map = self.host_guest_map,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700767 privileged = True)
768 volumes = []
769 for _,g in self.host_guest_map:
770 volumes.append(g)
771 self.start(ports = self.ports,
A R Karthick41adfce2016-06-10 09:51:25 -0700772 host_config = host_config,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700773 volumes = volumes, tty = True)
774 print('Starting Quagga on container %s' %self.name)
775 self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
776 time.sleep(boot_delay)
777
778 @classmethod
779 def build_image(cls, image):
Chetan Gaonker2a6601b2016-05-02 17:28:26 -0700780 onos_quagga_ip = Onos.quagga_config[0]['ip']
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700781 print('Building Quagga image %s' %image)
782 dockerfile = '''
A R Karthick41adfce2016-06-10 09:51:25 -0700783FROM ubuntu:14.04
784MAINTAINER chetan@ciena.com
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700785WORKDIR /root
786RUN useradd -M quagga
787RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga
788RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga
A R Karthick973ea692016-10-17 12:23:02 -0700789RUN 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 -0700790RUN git clone git://git.savannah.nongnu.org/quagga.git quagga && \
A R Karthick8f69c2c2016-10-21 11:43:26 -0700791(cd quagga && git checkout quagga-1.0.20160315 && ./bootstrap.sh && \
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700792sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \
793./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install)
794RUN ldconfig
795'''.format(onos_quagga_ip)
796 super(Quagga, cls).build_image(dockerfile, image)
797 print('Done building image %s' %image)
A R Karthick81acbff2016-06-17 14:45:16 -0700798
A.R Karthick1700e0e2016-10-06 18:16:57 -0700799class QuaggaStopWrapper(Container):
800 def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'candidate'):
801 super(QuaggaStopWrapper, self).__init__(name, image, prefix = Container.IMAGE_PREFIX, tag = tag)
802 if self.exists():
803 self.kill()
804
805
A R Karthick81acbff2016-06-17 14:45:16 -0700806def reinitContainerClients():
807 docker_netns.dckr = Client()
808 Container.dckr = Client()
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700809
810class Xos(Container):
811 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
812 TAG = 'latest'
813 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700814 host_guest_map = None
815 env = None
816 ports = None
817 volumes = None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700818
A R Karthick6e80afd2016-10-10 16:03:12 -0700819 @classmethod
820 def get_cmd(cls, img_name):
821 cmd = cls.dckr.inspect_image(img_name)['Config']['Cmd']
822 return ' '.join(cmd)
823
A R Karthicke3bde962016-09-27 15:06:35 -0700824 def __init__(self, name, image, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700825 boot_delay = 20, restart = False, network_cfg = None, update = False):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700826 if restart is True:
827 ##Find the right image to restart
828 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
829 if running_image:
830 image_name = running_image[0]['Image']
831 try:
832 image = image_name.split(':')[0]
833 tag = image_name.split(':')[1]
834 except: pass
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700835 super(Xos, self).__init__(name, image, prefix = prefix, tag = tag)
836 if update is True or not self.img_exists():
837 self.build_image(self.image_name)
A R Karthick6e80afd2016-10-10 16:03:12 -0700838 self.command = self.get_cmd(self.image_name).strip() or None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700839 if restart is True and self.exists():
840 self.kill()
841 if not self.exists():
842 self.remove_container(name, force=True)
A R Karthicke3bde962016-09-27 15:06:35 -0700843 host_config = self.create_host_config(port_list = self.ports,
844 host_guest_map = self.host_guest_map,
845 privileged = True)
846 print('Starting XOS container %s' %self.name)
847 self.start(ports = self.ports, environment = self.env, host_config = host_config,
848 volumes = self.volumes, tty = True)
849 print('Waiting %d seconds for XOS Base Container to boot' %(boot_delay))
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700850 time.sleep(boot_delay)
851
852 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700853 def build_image(cls, image, dockerfile_path, image_target = 'build'):
854 cmd = 'cd {} && make {}'.format(dockerfile_path, image_target)
855 print('Building XOS %s' %image)
856 res = os.system(cmd)
857 print('Done building image %s. Image build %s' %(image, 'successful' if res == 0 else 'failed'))
858 return res
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700859
A R Karthicke3bde962016-09-27 15:06:35 -0700860class XosServer(Xos):
861 ports = [8000,9998,9999]
862 NAME = 'xos-server'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700863 IMAGE = 'xosproject/xos'
A R Karthicke3bde962016-09-27 15:06:35 -0700864 BASE_IMAGE = 'xosproject/xos-base'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700865 TAG = 'latest'
866 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700867 dockerfile_path = os.path.join(Xos.setup_dir, 'xos')
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700868
A R Karthicke3bde962016-09-27 15:06:35 -0700869 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700870 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700871 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700872
873 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700874 def build_image(cls, image = IMAGE):
875 ##build the base image and then build the server image
876 Xos.build_image(cls.BASE_IMAGE, cls.dockerfile_path, image_target = 'base')
877 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700878
A R Karthicke3bde962016-09-27 15:06:35 -0700879class XosSynchronizerOpenstack(Xos):
880 ports = [2375,]
881 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer')
882 NAME = 'xos-synchronizer'
883 IMAGE = 'xosproject/xos-synchronizer-openstack'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700884 TAG = 'latest'
885 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700886 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700887
A R Karthicke3bde962016-09-27 15:06:35 -0700888 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700889 tag = TAG, boot_delay = 20, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700890 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700891
892 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700893 def build_image(cls, image = IMAGE):
894 XosServer.build_image()
895 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700896
A R Karthicke3bde962016-09-27 15:06:35 -0700897class XosSynchronizerOnboarding(Xos):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700898 NAME = 'xos-synchronizer-onboarding'
899 IMAGE = 'xosproject/xos-synchronizer-onboarding'
900 TAG = 'latest'
901 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700902 dockerfile_path = os.path.join(Xos.setup_dir, 'onboarding_synchronizer')
903 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700904
A R Karthicke3bde962016-09-27 15:06:35 -0700905 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700906 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700907 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700908
909 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700910 def build_image(cls, image = IMAGE):
911 XosSynchronizerOpenstack.build_image()
912 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700913
A R Karthicke3bde962016-09-27 15:06:35 -0700914class XosSynchronizerOpenvpn(Xos):
915 NAME = 'xos-synchronizer-openvpn'
916 IMAGE = 'xosproject/xos-openvpn'
917 TAG = 'latest'
918 PREFIX = ''
919 dockerfile_path = os.path.join(Xos.setup_dir, 'openvpn')
920 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700921
A R Karthicke3bde962016-09-27 15:06:35 -0700922 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700923 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700924 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
925
926 @classmethod
927 def build_image(cls, image = IMAGE):
928 XosSynchronizerOpenstack.build_image()
929 Xos.build_image(image, cls.dockerfile_path)
930
931class XosPostgresql(Xos):
932 ports = [5432,]
933 NAME = 'xos-db-postgres'
934 IMAGE = 'xosproject/xos-postgres'
935 TAG = 'latest'
936 PREFIX = ''
937 volumes = ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
938 dockerfile_path = os.path.join(Xos.setup_dir, 'postgresql')
939
940 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700941 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700942 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
943
944 @classmethod
945 def build_image(cls, image = IMAGE):
946 Xos.build_image(image, cls.dockerfile_path)
947
948class XosSyndicateMs(Xos):
949 ports = [8080,]
950 env = None
951 NAME = 'xos-syndicate-ms'
952 IMAGE = 'xosproject/syndicate-ms'
953 TAG = 'latest'
954 PREFIX = ''
955 dockerfile_path = os.path.join(Xos.setup_dir, 'syndicate-ms')
956
957 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700958 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700959 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
960
961 @classmethod
962 def build_image(cls, image = IMAGE):
963 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700964
ChetanGaonkerc220e0d2016-10-05 05:06:25 -0700965class XosSyncVtn(Xos):
966 ports = [8080,]
967 env = None
968 NAME = 'xos-synchronizer-vtn'
969 IMAGE = 'xosproject/xos-synchronizer-vtn'
970 TAG = 'latest'
971 PREFIX = ''
972 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtn')
973
974 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700975 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -0700976 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
977
978 @classmethod
979 def build_image(cls, image = IMAGE):
980 Xos.build_image(image, cls.dockerfile_path)
981
982class XosSyncVtr(Xos):
983 ports = [8080,]
984 env = None
985 NAME = 'xos-synchronizer-vtr'
986 IMAGE = 'xosproject/xos-synchronizer-vtr'
987 TAG = 'latest'
988 PREFIX = ''
989 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtr')
990
991 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700992 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -0700993 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
994
995 @classmethod
996 def build_image(cls, image = IMAGE):
997 Xos.build_image(image, cls.dockerfile_path)
998
999class XosSyncVsg(Xos):
1000 ports = [8080,]
1001 env = None
1002 NAME = 'xos-synchronizer-vsg'
1003 IMAGE = 'xosproject/xos-synchronizer-vsg'
1004 TAG = 'latest'
1005 PREFIX = ''
1006 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vsg')
1007
1008 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001009 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001010 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1011
1012 @classmethod
1013 def build_image(cls, image = IMAGE):
1014 Xos.build_image(image, cls.dockerfile_path)
1015
1016
1017class XosSyncOnos(Xos):
1018 ports = [8080,]
1019 env = None
1020 NAME = 'xos-synchronizer-onos'
1021 IMAGE = 'xosproject/xos-synchronizer-onos'
1022 TAG = 'latest'
1023 PREFIX = ''
1024 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-onos')
1025
1026 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001027 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001028 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1029
1030 @classmethod
1031 def build_image(cls, image = IMAGE):
1032 Xos.build_image(image, cls.dockerfile_path)
1033
1034class XosSyncFabric(Xos):
1035 ports = [8080,]
1036 env = None
1037 NAME = 'xos-synchronizer-fabric'
1038 IMAGE = 'xosproject/xos-synchronizer-fabric'
1039 TAG = 'latest'
1040 PREFIX = ''
1041 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-fabric')
1042
1043 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001044 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001045 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1046
1047 @classmethod
1048 def build_image(cls, image = IMAGE):
1049 Xos.build_image(image, cls.dockerfile_path)
A R Karthick19aaf5c2016-11-09 17:47:57 -08001050
1051if __name__ == '__main__':
1052 onos = Onos(boot_delay = 10, restart = True)