blob: 3b62f68106e8003c4f56911da53c735097cf37cb [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 Karthickc4e474d2016-12-12 15:24:57 -0800406 cluster = False, data_volume = None, async = False, quagga_config = None):
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 Karthick2b93d6a2016-09-06 15:19:09 -0700420 self.boot_delay = boot_delay
A R Karthickec2db322016-11-17 15:06:01 -0800421 self.data_map = None
A R Karthick2b93d6a2016-09-06 15:19:09 -0700422 if cluster is True:
423 self.ports = []
A R Karthick1f908202016-11-16 17:32:20 -0800424 self.env['JAVA_OPTS'] = self.JAVA_OPTS_CLUSTER
A R Karthickec2db322016-11-17 15:06:01 -0800425 if data_volume is not None:
426 self.data_map = self.get_data_map(data_volume, self.guest_data_dir)
427 self.host_guest_map = self.host_guest_map + self.data_map
A R Karthick2b93d6a2016-09-06 15:19:09 -0700428 if os.access(self.cluster_cfg, os.F_OK):
429 try:
430 os.unlink(self.cluster_cfg)
431 except: pass
432
433 self.host_config = self.create_host_config(port_list = self.ports,
434 host_guest_map = self.host_guest_map)
435 self.volumes = []
436 for _,g in self.host_guest_map:
437 self.volumes.append(g)
438
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700439 if restart is True and self.exists():
440 self.kill()
A R Karthick2b93d6a2016-09-06 15:19:09 -0700441
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700442 if not self.exists():
443 self.remove_container(name, force=True)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700444 host_config = self.create_host_config(port_list = self.ports,
445 host_guest_map = self.host_guest_map)
446 volumes = []
447 for _,g in self.host_guest_map:
448 volumes.append(g)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700449 if network_cfg is not None:
A R Karthick81acbff2016-06-17 14:45:16 -0700450 json_data = json.dumps(network_cfg, indent=4)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700451 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
452 f.write(json_data)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800453 if cluster is False or async is False:
454 print('Starting ONOS container %s' %self.name)
455 self.start(ports = self.ports, environment = self.env,
456 host_config = self.host_config, volumes = self.volumes, tty = True)
457 if not restart:
458 ##wait a bit before fetching IP to regenerate cluster cfg
459 time.sleep(5)
460 ip = self.ip()
461 ##Just a quick hack/check to ensure we don't regenerate in the common case.
462 ##As ONOS is usually the first test container that is started
463 if cluster is False:
464 if ip != self.CLUSTER_CFG_IP or not os.access(self.cluster_cfg, os.F_OK):
465 print('Regenerating ONOS cluster cfg for ip %s' %ip)
466 self.generate_cluster_cfg(ip)
467 self.kill()
468 self.remove_container(self.name, force=True)
469 print('Restarting ONOS container %s' %self.name)
470 self.start(ports = self.ports, environment = self.env,
471 host_config = self.host_config, volumes = self.volumes, tty = True)
472 print('Waiting for ONOS to boot')
473 time.sleep(boot_delay)
474 self.wait_for_onos_start(self.ip())
475 self.running = True
476 else:
477 self.running = False
478 else:
479 self.running = True
480 if self.running:
481 self.ipaddr = self.ip()
482 if cluster is False:
483 self.install_cord_apps(self.ipaddr)
A R Karthick19aaf5c2016-11-09 17:47:57 -0800484
A.R Karthickc4e474d2016-12-12 15:24:57 -0800485 @classmethod
486 def get_quagga_config(cls, instance = 0):
487 quagga_config = cls.quagga_config[:]
488 if instance == 0:
489 return quagga_config
490 ip = quagga_config[0]['ip']
491 octets = ip.split('.')
492 octets[3] = str((int(octets[3]) + 1) & 255)
493 ip = '.'.join(octets)
494 quagga_config[0]['ip'] = ip
495 return quagga_config
496
497 @classmethod
498 def start_cluster_async(cls, onos_instances):
499 instances = filter(lambda o: o.running == False, onos_instances)
500 if not instances:
501 return
502 tpool = ThreadPool(len(instances), queue_size = 1, wait_timeout = 1)
503 for onos in instances:
504 tpool.addTask(onos.start_async)
505 tpool.cleanUpThreads()
506
507 def start_async(self):
508 print('Starting ONOS container %s' %self.name)
509 self.start(ports = self.ports, environment = self.env,
510 host_config = self.host_config, volumes = self.volumes, tty = True)
511 time.sleep(3)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700512 self.ipaddr = self.ip()
A.R Karthickc4e474d2016-12-12 15:24:57 -0800513 print('Waiting for ONOS container %s to start' %self.name)
514 self.wait_for_onos_start(self.ipaddr)
515 self.running = True
516 print('ONOS container %s started' %self.name)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700517
A R Karthick2b93d6a2016-09-06 15:19:09 -0700518 @classmethod
A R Karthick19aaf5c2016-11-09 17:47:57 -0800519 def wait_for_onos_start(cls, ip, tries = 30):
520 onos_log = OnosLog(host = ip)
521 num_tries = 0
522 started = None
523 while not started and num_tries < tries:
524 time.sleep(3)
525 started = onos_log.search_log_pattern('ApplicationManager .* Started')
526 num_tries += 1
527
A R Karthick19aaf5c2016-11-09 17:47:57 -0800528 if not started:
529 print('ONOS did not start')
530 else:
531 print('ONOS started')
532 return started
533
534 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700535 def setup_cluster_deprecated(cls, onos_instances, image_name = None):
536 if not onos_instances or len(onos_instances) < 2:
537 return
538 ips = []
539 if image_name is not None:
540 ips = Container.ips(image_name)
541 else:
542 for onos in onos_instances:
543 ips.append(onos.ipaddr)
544 Onos.cluster_instances = onos_instances
545 Onos.cluster_mode = True
546 ##regenerate the cluster json with the 3 instance ips before restarting them back
547 print('Generating cluster cfg for ONOS instances with ips %s' %ips)
548 Onos.generate_cluster_cfg(ips)
549 for onos in onos_instances:
550 onos.kill()
551 onos.remove_container(onos.name, force=True)
552 print('Restarting ONOS container %s for forming cluster' %onos.name)
553 onos.start(ports = onos.ports, environment = onos.env,
554 host_config = onos.host_config, volumes = onos.volumes, tty = True)
555 print('Waiting %d seconds for ONOS %s to boot' %(onos.boot_delay, onos.name))
556 time.sleep(onos.boot_delay)
557 onos.ipaddr = onos.ip()
558 onos.install_cord_apps(onos.ipaddr)
559
560 @classmethod
561 def setup_cluster(cls, onos_instances, image_name = None):
562 if not onos_instances or len(onos_instances) < 2:
563 return
564 ips = []
565 if image_name is not None:
566 ips = Container.ips(image_name)
567 else:
568 for onos in onos_instances:
569 ips.append(onos.ipaddr)
570 Onos.cluster_instances = onos_instances
571 Onos.cluster_mode = True
572 ##regenerate the cluster json with the 3 instance ips before restarting them back
573 print('Forming cluster for ONOS instances with ips %s' %ips)
574 Onos.form_cluster(ips)
575 ##wait for the cluster to be formed
576 print('Waiting for the cluster to be formed')
577 time.sleep(60)
578 for onos in onos_instances:
579 onos.install_cord_apps(onos.ipaddr)
580
581 @classmethod
A R Karthicke2c24bd2016-10-07 14:51:38 -0700582 def add_cluster(cls, count = 1, network_cfg = None):
583 if not cls.cluster_instances or Onos.cluster_mode is False:
584 return
585 for i in range(count):
586 name = '{}-{}'.format(Onos.NAME, len(cls.cluster_instances)+1)
587 onos = cls(name = name, image = Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX,
588 cluster = True, network_cfg = network_cfg)
589 cls.cluster_instances.append(onos)
590
591 cls.setup_cluster(cls.cluster_instances)
592
593 @classmethod
A.R Karthick2560f042016-11-30 14:38:52 -0800594 def restart_cluster(cls, network_cfg = None, timeout = 10, setup = False):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700595 if cls.cluster_mode is False:
596 return
597 if not cls.cluster_instances:
598 return
599
600 if network_cfg is not None:
601 json_data = json.dumps(network_cfg, indent=4)
602 with open('{}/network-cfg.json'.format(cls.host_config_dir), 'w') as f:
603 f.write(json_data)
604
A.R Karthick2560f042016-11-30 14:38:52 -0800605 cls.cleanup_cluster()
606 if timeout > 0:
607 time.sleep(timeout)
608
A R Karthick2b93d6a2016-09-06 15:19:09 -0700609 for onos in cls.cluster_instances:
A R Karthick2b93d6a2016-09-06 15:19:09 -0700610 print('Restarting ONOS container %s' %onos.name)
611 onos.start(ports = onos.ports, environment = onos.env,
612 host_config = onos.host_config, volumes = onos.volumes, tty = True)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700613 onos.ipaddr = onos.ip()
A.R Karthick2560f042016-11-30 14:38:52 -0800614 onos.wait_for_onos_start(onos.ipaddr)
615 onos.install_cord_apps(onos.ipaddr)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700616
A.R Karthick2560f042016-11-30 14:38:52 -0800617 ##form the cluster as appropriate
618 if setup is True:
619 cls.setup_cluster(cls.cluster_instances)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700620
621 @classmethod
622 def cluster_ips(cls):
623 if cls.cluster_mode is False:
624 return []
625 if not cls.cluster_instances:
626 return []
627 ips = [ onos.ipaddr for onos in cls.cluster_instances ]
628 return ips
629
630 @classmethod
631 def cleanup_cluster(cls):
632 if cls.cluster_mode is False:
633 return
634 if not cls.cluster_instances:
635 return
636 for onos in cls.cluster_instances:
637 if onos.exists():
638 onos.kill()
639 onos.remove_container(onos.name, force=True)
A R Karthickd44cea12016-07-20 12:16:41 -0700640
A.R Karthick95d044e2016-06-10 18:44:36 -0700641 @classmethod
A R Karthickde6b9dc2016-11-29 17:46:16 -0800642 def restart_node(cls, node = None, network_cfg = None, timeout = 10):
A R Karthick889d9652016-10-03 14:13:45 -0700643 if node is None:
644 cls(restart = True, network_cfg = network_cfg, image = cls.IMAGE, tag = cls.TAG)
645 else:
646 #Restarts a node in the cluster
647 valid_node = filter(lambda onos: node in [ onos.ipaddr, onos.name ], cls.cluster_instances)
648 if valid_node:
649 onos = valid_node.pop()
650 if onos.exists():
651 onos.kill()
652 onos.remove_container(onos.name, force=True)
A R Karthickde6b9dc2016-11-29 17:46:16 -0800653 if timeout > 0:
654 time.sleep(timeout)
A R Karthick889d9652016-10-03 14:13:45 -0700655 print('Restarting ONOS container %s' %onos.name)
656 onos.start(ports = onos.ports, environment = onos.env,
657 host_config = onos.host_config, volumes = onos.volumes, tty = True)
A R Karthick889d9652016-10-03 14:13:45 -0700658 onos.ipaddr = onos.ip()
A.R Karthick2560f042016-11-30 14:38:52 -0800659 onos.wait_for_onos_start(onos.ipaddr)
660 onos.install_cord_apps(onos.ipaddr)
A R Karthick889d9652016-10-03 14:13:45 -0700661
662 @classmethod
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700663 def install_cord_apps(cls, onos_ip = None):
A.R Karthick95d044e2016-06-10 18:44:36 -0700664 for app, version in cls.onos_cord_apps:
665 app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version)
A R Karthickeaf1c4e2016-07-19 12:22:35 -0700666 ok, code = OnosCtrl.install_app(app_file, onos_ip = onos_ip)
A.R Karthick95d044e2016-06-10 18:44:36 -0700667 ##app already installed (conflicts)
668 if code in [ 409 ]:
669 ok = True
670 print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install'))
671 time.sleep(2)
672
A.R Karthick1700e0e2016-10-06 18:16:57 -0700673class OnosStopWrapper(Container):
674 def __init__(self, name):
675 super(OnosStopWrapper, self).__init__(name, Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX)
676 if self.exists():
677 self.kill()
678 else:
679 if Onos.cluster_mode is True:
680 valid_node = filter(lambda onos: name in [ onos.ipaddr, onos.name ], Onos.cluster_instances)
681 if valid_node:
682 onos = valid_node.pop()
683 if onos.exists():
684 onos.kill()
685
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700686class Radius(Container):
687 ports = [ 1812, 1813 ]
A R Karthick41adfce2016-06-10 09:51:25 -0700688 env = {'TIMEZONE':'America/Los_Angeles',
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700689 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
690 }
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700691 host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700692 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700693 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700694 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700695 start_command = os.path.join(guest_config_dir, 'start-radius.py')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700696 host_guest_map = ( (host_db_dir, guest_db_dir),
697 (host_config_dir, guest_config_dir)
698 )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700699 IMAGE = 'cord-test/radius'
700 NAME = 'cord-radius'
701
A R Karthick07608ef2016-08-23 16:51:19 -0700702 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
Chetan Gaonker503032a2016-05-12 12:06:29 -0700703 boot_delay = 10, restart = False, update = False):
A R Karthick07608ef2016-08-23 16:51:19 -0700704 super(Radius, self).__init__(name, image, prefix = prefix, tag = tag, command = self.start_command)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700705 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700706 self.build_image(self.image_name)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700707 if restart is True and self.exists():
708 self.kill()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700709 if not self.exists():
710 self.remove_container(name, force=True)
711 host_config = self.create_host_config(port_list = self.ports,
712 host_guest_map = self.host_guest_map)
713 volumes = []
714 for _,g in self.host_guest_map:
715 volumes.append(g)
A R Karthick41adfce2016-06-10 09:51:25 -0700716 self.start(ports = self.ports, environment = self.env,
717 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700718 host_config = host_config, tty = True)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700719 time.sleep(boot_delay)
720
721 @classmethod
722 def build_image(cls, image):
723 print('Building Radius image %s' %image)
724 dockerfile = '''
725FROM hbouvier/docker-radius
726MAINTAINER chetan@ciena.com
727LABEL RUN docker pull hbouvier/docker-radius
728LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius
A R Karthickc762df42016-05-25 10:09:21 -0700729RUN apt-get update && \
730 apt-get -y install python python-pexpect strace
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700731WORKDIR /root
732CMD ["/etc/freeradius/start-radius.py"]
733'''
734 super(Radius, cls).build_image(dockerfile, image)
735 print('Done building image %s' %image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700736
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700737class Quagga(Container):
A R Karthick41adfce2016-06-10 09:51:25 -0700738 quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 },
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700739 { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 },
740 )
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700741 ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ]
742 host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config')
743 guest_quagga_config = '/root/config'
744 quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
745 host_guest_map = ( (host_quagga_config, guest_quagga_config), )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700746 IMAGE = 'cord-test/quagga'
747 NAME = 'cord-quagga'
748
A R Karthick07608ef2016-08-23 16:51:19 -0700749 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
Chetan Gaonker503032a2016-05-12 12:06:29 -0700750 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False):
A R Karthick07608ef2016-08-23 16:51:19 -0700751 super(Quagga, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.quagga_config)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700752 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -0700753 self.build_image(self.image_name)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700754 if restart is True and self.exists():
755 self.kill()
756 if not self.exists():
757 self.remove_container(name, force=True)
A R Karthick41adfce2016-06-10 09:51:25 -0700758 host_config = self.create_host_config(port_list = self.ports,
759 host_guest_map = self.host_guest_map,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700760 privileged = True)
761 volumes = []
762 for _,g in self.host_guest_map:
763 volumes.append(g)
764 self.start(ports = self.ports,
A R Karthick41adfce2016-06-10 09:51:25 -0700765 host_config = host_config,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700766 volumes = volumes, tty = True)
767 print('Starting Quagga on container %s' %self.name)
768 self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
769 time.sleep(boot_delay)
770
771 @classmethod
772 def build_image(cls, image):
Chetan Gaonker2a6601b2016-05-02 17:28:26 -0700773 onos_quagga_ip = Onos.quagga_config[0]['ip']
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700774 print('Building Quagga image %s' %image)
775 dockerfile = '''
A R Karthick41adfce2016-06-10 09:51:25 -0700776FROM ubuntu:14.04
777MAINTAINER chetan@ciena.com
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700778WORKDIR /root
779RUN useradd -M quagga
780RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga
781RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga
A R Karthick973ea692016-10-17 12:23:02 -0700782RUN 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 -0700783RUN git clone git://git.savannah.nongnu.org/quagga.git quagga && \
A R Karthick8f69c2c2016-10-21 11:43:26 -0700784(cd quagga && git checkout quagga-1.0.20160315 && ./bootstrap.sh && \
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700785sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \
786./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install)
787RUN ldconfig
788'''.format(onos_quagga_ip)
789 super(Quagga, cls).build_image(dockerfile, image)
790 print('Done building image %s' %image)
A R Karthick81acbff2016-06-17 14:45:16 -0700791
A.R Karthick1700e0e2016-10-06 18:16:57 -0700792class QuaggaStopWrapper(Container):
793 def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'candidate'):
794 super(QuaggaStopWrapper, self).__init__(name, image, prefix = Container.IMAGE_PREFIX, tag = tag)
795 if self.exists():
796 self.kill()
797
798
A R Karthick81acbff2016-06-17 14:45:16 -0700799def reinitContainerClients():
800 docker_netns.dckr = Client()
801 Container.dckr = Client()
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700802
803class Xos(Container):
804 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
805 TAG = 'latest'
806 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700807 host_guest_map = None
808 env = None
809 ports = None
810 volumes = None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700811
A R Karthick6e80afd2016-10-10 16:03:12 -0700812 @classmethod
813 def get_cmd(cls, img_name):
814 cmd = cls.dckr.inspect_image(img_name)['Config']['Cmd']
815 return ' '.join(cmd)
816
A R Karthicke3bde962016-09-27 15:06:35 -0700817 def __init__(self, name, image, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700818 boot_delay = 20, restart = False, network_cfg = None, update = False):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700819 if restart is True:
820 ##Find the right image to restart
821 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
822 if running_image:
823 image_name = running_image[0]['Image']
824 try:
825 image = image_name.split(':')[0]
826 tag = image_name.split(':')[1]
827 except: pass
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700828 super(Xos, self).__init__(name, image, prefix = prefix, tag = tag)
829 if update is True or not self.img_exists():
830 self.build_image(self.image_name)
A R Karthick6e80afd2016-10-10 16:03:12 -0700831 self.command = self.get_cmd(self.image_name).strip() or None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700832 if restart is True and self.exists():
833 self.kill()
834 if not self.exists():
835 self.remove_container(name, force=True)
A R Karthicke3bde962016-09-27 15:06:35 -0700836 host_config = self.create_host_config(port_list = self.ports,
837 host_guest_map = self.host_guest_map,
838 privileged = True)
839 print('Starting XOS container %s' %self.name)
840 self.start(ports = self.ports, environment = self.env, host_config = host_config,
841 volumes = self.volumes, tty = True)
842 print('Waiting %d seconds for XOS Base Container to boot' %(boot_delay))
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700843 time.sleep(boot_delay)
844
845 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700846 def build_image(cls, image, dockerfile_path, image_target = 'build'):
847 cmd = 'cd {} && make {}'.format(dockerfile_path, image_target)
848 print('Building XOS %s' %image)
849 res = os.system(cmd)
850 print('Done building image %s. Image build %s' %(image, 'successful' if res == 0 else 'failed'))
851 return res
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700852
A R Karthicke3bde962016-09-27 15:06:35 -0700853class XosServer(Xos):
854 ports = [8000,9998,9999]
855 NAME = 'xos-server'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700856 IMAGE = 'xosproject/xos'
A R Karthicke3bde962016-09-27 15:06:35 -0700857 BASE_IMAGE = 'xosproject/xos-base'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700858 TAG = 'latest'
859 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700860 dockerfile_path = os.path.join(Xos.setup_dir, 'xos')
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700861
A R Karthicke3bde962016-09-27 15:06:35 -0700862 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700863 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700864 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700865
866 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700867 def build_image(cls, image = IMAGE):
868 ##build the base image and then build the server image
869 Xos.build_image(cls.BASE_IMAGE, cls.dockerfile_path, image_target = 'base')
870 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700871
A R Karthicke3bde962016-09-27 15:06:35 -0700872class XosSynchronizerOpenstack(Xos):
873 ports = [2375,]
874 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer')
875 NAME = 'xos-synchronizer'
876 IMAGE = 'xosproject/xos-synchronizer-openstack'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700877 TAG = 'latest'
878 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700879 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700880
A R Karthicke3bde962016-09-27 15:06:35 -0700881 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700882 tag = TAG, boot_delay = 20, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700883 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700884
885 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700886 def build_image(cls, image = IMAGE):
887 XosServer.build_image()
888 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700889
A R Karthicke3bde962016-09-27 15:06:35 -0700890class XosSynchronizerOnboarding(Xos):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700891 NAME = 'xos-synchronizer-onboarding'
892 IMAGE = 'xosproject/xos-synchronizer-onboarding'
893 TAG = 'latest'
894 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -0700895 dockerfile_path = os.path.join(Xos.setup_dir, 'onboarding_synchronizer')
896 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700897
A R Karthicke3bde962016-09-27 15:06:35 -0700898 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700899 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700900 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700901
902 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -0700903 def build_image(cls, image = IMAGE):
904 XosSynchronizerOpenstack.build_image()
905 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700906
A R Karthicke3bde962016-09-27 15:06:35 -0700907class XosSynchronizerOpenvpn(Xos):
908 NAME = 'xos-synchronizer-openvpn'
909 IMAGE = 'xosproject/xos-openvpn'
910 TAG = 'latest'
911 PREFIX = ''
912 dockerfile_path = os.path.join(Xos.setup_dir, 'openvpn')
913 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 = 10, 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)
918
919 @classmethod
920 def build_image(cls, image = IMAGE):
921 XosSynchronizerOpenstack.build_image()
922 Xos.build_image(image, cls.dockerfile_path)
923
924class XosPostgresql(Xos):
925 ports = [5432,]
926 NAME = 'xos-db-postgres'
927 IMAGE = 'xosproject/xos-postgres'
928 TAG = 'latest'
929 PREFIX = ''
930 volumes = ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
931 dockerfile_path = os.path.join(Xos.setup_dir, 'postgresql')
932
933 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -0700934 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700935 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
936
937 @classmethod
938 def build_image(cls, image = IMAGE):
939 Xos.build_image(image, cls.dockerfile_path)
940
941class XosSyndicateMs(Xos):
942 ports = [8080,]
943 env = None
944 NAME = 'xos-syndicate-ms'
945 IMAGE = 'xosproject/syndicate-ms'
946 TAG = 'latest'
947 PREFIX = ''
948 dockerfile_path = os.path.join(Xos.setup_dir, 'syndicate-ms')
949
950 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700951 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -0700952 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
953
954 @classmethod
955 def build_image(cls, image = IMAGE):
956 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700957
ChetanGaonkerc220e0d2016-10-05 05:06:25 -0700958class XosSyncVtn(Xos):
959 ports = [8080,]
960 env = None
961 NAME = 'xos-synchronizer-vtn'
962 IMAGE = 'xosproject/xos-synchronizer-vtn'
963 TAG = 'latest'
964 PREFIX = ''
965 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtn')
966
967 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -0700968 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -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 XosSyncVtr(Xos):
976 ports = [8080,]
977 env = None
978 NAME = 'xos-synchronizer-vtr'
979 IMAGE = 'xosproject/xos-synchronizer-vtr'
980 TAG = 'latest'
981 PREFIX = ''
982 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtr')
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):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -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)
991
992class XosSyncVsg(Xos):
993 ports = [8080,]
994 env = None
995 NAME = 'xos-synchronizer-vsg'
996 IMAGE = 'xosproject/xos-synchronizer-vsg'
997 TAG = 'latest'
998 PREFIX = ''
999 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vsg')
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
1009
1010class XosSyncOnos(Xos):
1011 ports = [8080,]
1012 env = None
1013 NAME = 'xos-synchronizer-onos'
1014 IMAGE = 'xosproject/xos-synchronizer-onos'
1015 TAG = 'latest'
1016 PREFIX = ''
1017 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-onos')
1018
1019 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001020 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001021 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1022
1023 @classmethod
1024 def build_image(cls, image = IMAGE):
1025 Xos.build_image(image, cls.dockerfile_path)
1026
1027class XosSyncFabric(Xos):
1028 ports = [8080,]
1029 env = None
1030 NAME = 'xos-synchronizer-fabric'
1031 IMAGE = 'xosproject/xos-synchronizer-fabric'
1032 TAG = 'latest'
1033 PREFIX = ''
1034 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-fabric')
1035
1036 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001037 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001038 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1039
1040 @classmethod
1041 def build_image(cls, image = IMAGE):
1042 Xos.build_image(image, cls.dockerfile_path)
A R Karthick19aaf5c2016-11-09 17:47:57 -08001043
1044if __name__ == '__main__':
1045 onos = Onos(boot_delay = 10, restart = True)