blob: 0050eb0953896aab2ea2b82d8b9897da3fcf3f2a [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
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
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# 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
16
A R Karthick41adfce2016-06-10 09:51:25 -070017#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070018# Copyright 2016-present Ciena Corporation
19#
20# Licensed under the Apache License, Version 2.0 (the "License");
21# you may not use this file except in compliance with the License.
22# You may obtain a copy of the License at
A R Karthick41adfce2016-06-10 09:51:25 -070023#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070024# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick41adfce2016-06-10 09:51:25 -070025#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070026# Unless required by applicable law or agreed to in writing, software
27# distributed under the License is distributed on an "AS IS" BASIS,
28# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29# See the License for the specific language governing permissions and
30# limitations under the License.
31#
Chetan Gaonker3533faa2016-04-25 17:50:14 -070032import os,time
33import io
34import json
A R Karthickd44cea12016-07-20 12:16:41 -070035import yaml
A.R Karthickc4e474d2016-12-12 15:24:57 -080036import errno
A R Karthickaa54a1c2016-12-15 11:42:08 -080037import copy
Chetan Gaonker3533faa2016-04-25 17:50:14 -070038from pyroute2 import IPRoute
A.R Karthickc4e474d2016-12-12 15:24:57 -080039from pyroute2.netlink import NetlinkError
Chetan Gaonker3533faa2016-04-25 17:50:14 -070040from itertools import chain
41from nsenter import Namespace
A R Karthick6f2ac6f2017-07-26 12:55:24 -070042try:
43 from docker import APIClient as Client
44except:
45 from docker import Client
A R Karthick85eb1862017-01-23 16:10:57 -080046from docker import utils as dockerutils
A.R Karthickf184b342017-01-27 19:30:50 -080047import shutil
A.R Karthick95d044e2016-06-10 18:44:36 -070048from OnosCtrl import OnosCtrl
A R Karthick19aaf5c2016-11-09 17:47:57 -080049from OnosLog import OnosLog
A R Karthickefcf1ab2017-09-08 18:24:16 -070050from OltConfig import OltConfig
51from EapolAAA import radius_add_users, radius_restore_users
A R Karthick03bd2812017-03-03 17:49:17 -080052from onosclidriver import OnosCliDriver
A.R Karthickc4e474d2016-12-12 15:24:57 -080053from threadPool import ThreadPool
A R Karthickaa54a1c2016-12-15 11:42:08 -080054from threading import Lock
Chetan Gaonker3533faa2016-04-25 17:50:14 -070055
56class docker_netns(object):
57
58 dckr = Client()
59 def __init__(self, name):
60 pid = int(self.dckr.inspect_container(name)['State']['Pid'])
61 if pid == 0:
62 raise Exception('no container named {0}'.format(name))
63 self.pid = pid
64
65 def __enter__(self):
66 pid = self.pid
67 if not os.path.exists('/var/run/netns'):
68 os.mkdir('/var/run/netns')
69 os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid))
70 return str(pid)
71
72 def __exit__(self, type, value, traceback):
73 pid = self.pid
74 os.unlink('/var/run/netns/{0}'.format(pid))
75
76flatten = lambda l: chain.from_iterable(l)
77
78class Container(object):
79 dckr = Client()
A R Karthick07608ef2016-08-23 16:51:19 -070080 IMAGE_PREFIX = '' ##for saving global prefix for all test classes
A R Karthickaa54a1c2016-12-15 11:42:08 -080081 CONFIG_LOCK = Lock()
A R Karthick07608ef2016-08-23 16:51:19 -070082
83 def __init__(self, name, image, prefix='', tag = 'candidate', command = 'bash', quagga_config = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -070084 self.name = name
A R Karthick07608ef2016-08-23 16:51:19 -070085 self.prefix = prefix
86 if prefix:
87 self.prefix += '/'
88 image = '{}{}'.format(self.prefix, image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -070089 self.image = image
90 self.tag = tag
A R Karthickd44cea12016-07-20 12:16:41 -070091 if tag:
92 self.image_name = image + ':' + tag
93 else:
94 self.image_name = image
Chetan Gaonker3533faa2016-04-25 17:50:14 -070095 self.id = None
96 self.command = command
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -070097 self.quagga_config = quagga_config
Chetan Gaonker3533faa2016-04-25 17:50:14 -070098
99 @classmethod
100 def build_image(cls, dockerfile, tag, force=True, nocache=False):
101 f = io.BytesIO(dockerfile.encode('utf-8'))
102 if force or not cls.image_exists(tag):
103 print('Build {0}...'.format(tag))
104 for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache):
105 if 'stream' in line:
106 print(line['stream'].strip())
107
108 @classmethod
109 def image_exists(cls, name):
A R Karthicke07fc3a2017-02-27 10:49:29 -0800110 #return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()]
111 return name in list( flatten(ctn['RepoTags'] if ctn['RepoTags'] else '' for ctn in cls.dckr.images()) )
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700112
113 @classmethod
114 def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False):
115 port_bindings = None
116 binds = None
117 if port_list:
118 port_bindings = {}
119 for p in port_list:
A R Karthick184945a2017-07-25 17:23:57 -0700120 if type(p) is tuple:
121 port_bindings[str(p[0])] = str(p[1])
122 else:
123 port_bindings[str(p)] = str(p)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700124
125 if host_guest_map:
126 binds = []
127 for h, g in host_guest_map:
128 binds.append('{0}:{1}'.format(h, g))
129
130 return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged)
131
132 @classmethod
A R Karthick85eb1862017-01-23 16:10:57 -0800133 def connect_to_network(cls, name, network):
134 try:
135 cls.dckr.connect_container_to_network(name, network)
A R Karthick85eb1862017-01-23 16:10:57 -0800136 except:
A R Karthick1555c7c2017-09-07 14:59:41 -0700137 connect_cmd = 'docker network connect %s %s' %(network, name)
138 os.system(connect_cmd)
139 return True
A R Karthick85eb1862017-01-23 16:10:57 -0800140
141 @classmethod
142 def create_network(cls, network, subnet = None, gateway = None):
143 ipam_config = None
144 if subnet is not None and gateway is not None:
A R Karthick1555c7c2017-09-07 14:59:41 -0700145 try:
146 ipam_pool = dockerutils.create_ipam_pool(subnet = subnet, gateway = gateway)
147 ipam_config = dockerutils.create_ipam_config(pool_configs = [ipam_pool])
148 cls.dckr.create_network(network, driver='bridge', ipam = ipam_config)
149 except:
150 create_cmd = 'docker network create %s --subnet %s --gateway %s >/dev/null 2>&1' %(network, subnet, gateway)
151 os.system(create_cmd)
A R Karthick85eb1862017-01-23 16:10:57 -0800152
153 @classmethod
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700154 def cleanup(cls, image):
A R Karthick09b1f4e2016-05-12 14:31:50 -0700155 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700156 for cnt in cnt_list:
157 print('Cleaning container %s' %cnt['Id'])
A.R Karthick95d044e2016-06-10 18:44:36 -0700158 if cnt.has_key('State') and cnt['State'] == 'running':
A R Karthick09b1f4e2016-05-12 14:31:50 -0700159 cls.dckr.kill(cnt['Id'])
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700160 cls.dckr.remove_container(cnt['Id'], force=True)
161
162 @classmethod
163 def remove_container(cls, name, force=True):
164 try:
165 cls.dckr.remove_container(name, force = force)
166 except: pass
167
168 def exists(self):
169 return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers()))
170
171 def img_exists(self):
A R Karthicke07fc3a2017-02-27 10:49:29 -0800172 #return self.image_name in [ctn['RepoTags'][0] if ctn['RepoTags'] else '' for ctn in self.dckr.images()]
173 return self.image_name in list( flatten(ctn['RepoTags'] if ctn['RepoTags'] else '' for ctn in self.dckr.images()) )
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700174
A R Karthick75844572017-01-23 16:57:44 -0800175 def ip(self, network = None):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700176 cnt_list = filter(lambda c: c['Names'][0] == '/{}'.format(self.name), self.dckr.containers())
177 #if not cnt_list:
178 # cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700179 cnt_settings = cnt_list.pop()
A R Karthick75844572017-01-23 16:57:44 -0800180 if network is not None and cnt_settings['NetworkSettings']['Networks'].has_key(network):
181 return cnt_settings['NetworkSettings']['Networks'][network]['IPAddress']
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700182 return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress']
183
A R Karthick2b93d6a2016-09-06 15:19:09 -0700184 @classmethod
185 def ips(cls, image_name):
186 cnt_list = filter(lambda c: c['Image'] == image_name, cls.dckr.containers())
187 ips = [ cnt['NetworkSettings']['Networks']['bridge']['IPAddress'] for cnt in cnt_list ]
188 return ips
189
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700190 def kill(self, remove = True):
191 self.dckr.kill(self.name)
192 self.dckr.remove_container(self.name, force=True)
193
A R Karthick41adfce2016-06-10 09:51:25 -0700194 def start(self, rm = True, ports = None, volumes = None, host_config = None,
A R Karthick1555c7c2017-09-07 14:59:41 -0700195 environment = None, tty = False, stdin_open = True,
196 network_disabled = False, network = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700197
198 if rm and self.exists():
199 print('Removing container:', self.name)
200 self.dckr.remove_container(self.name, force=True)
201
A R Karthick41adfce2016-06-10 09:51:25 -0700202 ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700203 detach=True, name=self.name,
A R Karthick41adfce2016-06-10 09:51:25 -0700204 environment = environment,
205 volumes = volumes,
A R Karthick1555c7c2017-09-07 14:59:41 -0700206 host_config = host_config, stdin_open=stdin_open, tty = tty,
207 network_disabled = network_disabled)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700208 self.dckr.start(container=self.name)
A R Karthick1555c7c2017-09-07 14:59:41 -0700209 if network_disabled is False:
210 if network is not None:
211 self.connect_to_network(self.name, network)
212 if self.quagga_config:
213 self.connect_to_br(index = 1)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700214 self.id = ctn['Id']
215 return ctn
216
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000217 @classmethod
218 def pause_container(cls, image, delay):
219 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
220 for cnt in cnt_list:
221 print('Pause the container %s' %cnt['Id'])
222 if cnt.has_key('State') and cnt['State'] == 'running':
223 cls.dckr.pause(cnt['Id'])
224 if delay != 0:
225 time.sleep(delay)
226 for cnt in cnt_list:
227 print('Unpause the container %s' %cnt['Id'])
228 cls.dckr.unpause(cnt['Id'])
229 else:
230 print('Infinity time pause the container %s' %cnt['Id'])
231 return 'success'
232
A R Karthick52414732017-01-31 09:59:47 -0800233 def connect_to_br(self, index = 0):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800234 self.CONFIG_LOCK.acquire()
235 try:
236 with docker_netns(self.name) as pid:
237 for quagga_config in self.quagga_config:
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700238 ip = IPRoute()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800239 br = ip.link_lookup(ifname=quagga_config['bridge'])
240 if len(br) == 0:
241 try:
242 ip.link_create(ifname=quagga_config['bridge'], kind='bridge')
243 except NetlinkError as e:
244 err, _ = e.args
245 if err == errno.EEXIST:
246 pass
247 else:
248 raise NetlinkError(*e.args)
249 br = ip.link_lookup(ifname=quagga_config['bridge'])
250 br = br[0]
251 ip.link('set', index=br, state='up')
A R Karthick52414732017-01-31 09:59:47 -0800252 ifname = '{0}-{1}'.format(self.name[:12], index)
A R Karthickaa54a1c2016-12-15 11:42:08 -0800253 ifs = ip.link_lookup(ifname=ifname)
254 if len(ifs) > 0:
255 ip.link_remove(ifs[0])
256 peer_ifname = '{0}-{1}'.format(pid, index)
257 ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname)
258 host = ip.link_lookup(ifname=ifname)[0]
259 ip.link('set', index=host, master=br)
260 ip.link('set', index=host, state='up')
261 guest = ip.link_lookup(ifname=peer_ifname)[0]
262 ip.link('set', index=guest, net_ns_fd=pid)
263 with Namespace(pid, 'net'):
264 ip = IPRoute()
265 ip.link('set', index=guest, ifname='eth{}'.format(index+1))
266 ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask'])
267 ip.link('set', index=guest, state='up')
268 index += 1
269 finally:
270 self.CONFIG_LOCK.release()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700271
Thangavelu K Sef6f0a52016-12-14 19:57:05 +0000272 def execute(self, cmd, tty = True, stream = False, shell = False, detach = True):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700273 res = 0
274 if type(cmd) == str:
275 cmds = (cmd,)
276 else:
277 cmds = cmd
278 if shell:
279 for c in cmds:
280 res += os.system('docker exec {0} {1}'.format(self.name, c))
281 return res
282 for c in cmds:
283 i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True)
A R Karthickd6dd9b22017-02-24 15:17:22 -0800284 s = self.dckr.exec_start(i['Id'], stream = stream, detach=detach, socket=True)
285 try:
286 s.close()
287 except: pass
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700288 result = self.dckr.exec_inspect(i['Id'])
289 res += 0 if result['ExitCode'] == None else result['ExitCode']
290 return res
291
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700292 def restart(self, timeout =10):
293 return self.dckr.restart(self.name, timeout)
294
A R Karthickc69d73e2017-01-20 11:44:34 -0800295def get_mem(jvm_heap_size = None, instances = 1):
A R Karthick1f908202016-11-16 17:32:20 -0800296 if instances <= 0:
297 instances = 1
A R Karthickc69d73e2017-01-20 11:44:34 -0800298 heap_size = jvm_heap_size
299 heap_size_i = 0
300 #sanitize the heap size config
301 if heap_size is not None:
302 if not heap_size.isdigit():
303 try:
304 heap_size_i = int(heap_size[:-1])
305 suffix = heap_size[-1]
306 if suffix == 'M':
307 heap_size_i /= 1024 #convert to gigs
A.R Karthick99044822017-02-09 14:04:20 -0800308 #allow to specific minimum heap size
309 if heap_size_i == 0:
310 return heap_size
A R Karthickc69d73e2017-01-20 11:44:34 -0800311 except:
312 ##invalid suffix length probably. Fall back to default
313 heap_size = None
314 else:
315 heap_size_i = int(heap_size)
316
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700317 with open('/proc/meminfo', 'r') as fd:
318 meminfo = fd.readlines()
319 mem = 0
320 for m in meminfo:
321 if m.startswith('MemTotal:') or m.startswith('SwapTotal:'):
322 mem += int(m.split(':')[1].strip().split()[0])
323
A R Karthick1f908202016-11-16 17:32:20 -0800324 mem = max(mem/1024/1024/2/instances, 1)
Chetan Gaonker6d0a7b02016-05-03 16:57:28 -0700325 mem = min(mem, 16)
A R Karthickc69d73e2017-01-20 11:44:34 -0800326
327 if heap_size_i:
328 #we take the minimum of the provided heap size and max allowed heap size
329 heap_size_i = min(heap_size_i, mem)
330 else:
331 heap_size_i = mem
332
333 return '{}G'.format(heap_size_i)
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700334
A R Karthickd44cea12016-07-20 12:16:41 -0700335class OnosCord(Container):
336 """Use this when running the cord tester agent on the onos compute node"""
A R Karthickd44cea12016-07-20 12:16:41 -0700337 onos_config_dir_guest = '/root/onos/config'
A R Karthick03bd2812017-03-03 17:49:17 -0800338 synchronizer_map = { 'vtn' : { 'install':
A R Karthick6e3977f2017-11-17 11:05:36 -0800339 ('http://mavenrepo:8080/repository/org/opencord/cord-config/1.3.0-SNAPSHOT/cord-config-1.3.0-SNAPSHOT.oar',
340 'http://mavenrepo:8080/repository/org/opencord/cord-config/1.4.0-SNAPSHOT/cord-config-1.4.0-SNAPSHOT.oar',
A R Karthick4d4f9e62017-10-30 10:30:21 -0700341 'http://mavenrepo:8080/repository/org/opencord/vtn/1.3.0/vtn-1.3.0.oar',
A R Karthick36a75932017-10-25 14:40:42 -0700342 'http://mavenrepo:8080/repository/org/opencord/vtn/1.4.0-SNAPSHOT/vtn-1.4.0-SNAPSHOT.oar',),
A R Karthick03bd2812017-03-03 17:49:17 -0800343 'activate':
344 ('org.onosproject.ovsdb-base', 'org.onosproject.drivers.ovsdb',
345 'org.onosproject.dhcp', 'org.onosproject.optical-model',
346 'org.onosproject.openflow-base', 'org.onosproject.proxyarp',
347 'org.onosproject.hostprovider'),
348 },
349 'fabric' : { 'activate':
350 ('org.onosproject.hostprovider', 'org.onosproject.optical-model',
351 'org.onosproject.openflow-base', 'org.onosproject.vrouter',
352 'org.onosproject.netcfghostprovider', 'org.onosproject.netcfglinksprovider',
353 'org.onosproject.segmentrouting', 'org.onosproject.proxyarp'),
354 }
355 }
A R Karthick36a75932017-10-25 14:40:42 -0700356 tester_apps = ('http://mavenrepo:8080/repository/org/opencord/aaa/1.4.0-SNAPSHOT/aaa-1.4.0-SNAPSHOT.oar',
357 'http://mavenrepo:8080/repository/org/opencord/igmp/1.4.0-SNAPSHOT/igmp-1.4.0-SNAPSHOT.oar',)
A R Karthickd44cea12016-07-20 12:16:41 -0700358
A.R Karthickddf12772017-05-17 13:49:47 -0700359 old_service_profile = '/opt/cord/orchestration/service-profile/cord-pod'
A R Karthick49529c52017-05-19 09:43:01 -0700360 cord_profile = '/opt/cord_profile'
A.R Karthickddf12772017-05-17 13:49:47 -0700361
A R Karthick03bd2812017-03-03 17:49:17 -0800362 def __init__(self, onos_ip, conf, service_profile, synchronizer, start = True, boot_delay = 5):
A.R Karthickf184b342017-01-27 19:30:50 -0800363 if not os.access(conf, os.F_OK):
364 raise Exception('ONOS cord configuration location %s is invalid' %conf)
A.R Karthickddf12772017-05-17 13:49:47 -0700365 self.old_cord = False
366 if os.access(self.old_service_profile, os.F_OK):
367 self.old_cord = True
A R Karthickbd9b8a32016-07-21 09:56:45 -0700368 self.onos_ip = onos_ip
A.R Karthickf184b342017-01-27 19:30:50 -0800369 self.onos_cord_dir = conf
A R Karthickbd9b8a32016-07-21 09:56:45 -0700370 self.boot_delay = boot_delay
A.R Karthickf184b342017-01-27 19:30:50 -0800371 self.synchronizer = synchronizer
372 self.service_profile = service_profile
373 self.docker_yaml = os.path.join(conf, 'docker-compose.yml')
374 self.docker_yaml_saved = os.path.join(conf, 'docker-compose.yml.saved')
375 self.onos_config_dir = os.path.join(conf, 'config')
376 self.onos_cfg_save_loc = os.path.join(conf, 'network-cfg.json.saved')
377 instance_active = False
378 #if we have a wrapper onos instance already active, back out
379 if os.access(self.onos_config_dir, os.F_OK) or os.access(self.docker_yaml_saved, os.F_OK):
380 instance_active = True
381 else:
382 if start is True:
383 os.mkdir(self.onos_config_dir)
384 shutil.copy(self.docker_yaml, self.docker_yaml_saved)
A R Karthickd44cea12016-07-20 12:16:41 -0700385
A.R Karthickf184b342017-01-27 19:30:50 -0800386 self.start_wrapper = instance_active is False and start is True
A R Karthickd44cea12016-07-20 12:16:41 -0700387 ##update the docker yaml with the config volume
388 with open(self.docker_yaml, 'r') as f:
389 yaml_config = yaml.load(f)
390 image = yaml_config['services'].keys()[0]
A R Karthick8983cb02017-06-09 11:32:53 -0700391 cord_conf_dir_basename = os.path.basename(self.onos_cord_dir.replace('-', '').replace('_', ''))
A.R Karthickf184b342017-01-27 19:30:50 -0800392 xos_onos_name = '{}_{}_1'.format(cord_conf_dir_basename, image)
A R Karthick5778a792017-01-31 13:47:16 -0800393 if not yaml_config['services'][image].has_key('volumes'):
394 yaml_config['services'][image]['volumes'] = []
A R Karthickd44cea12016-07-20 12:16:41 -0700395 volumes = yaml_config['services'][image]['volumes']
396 config_volumes = filter(lambda e: e.find(self.onos_config_dir_guest) >= 0, volumes)
397 if not config_volumes:
398 config_volume = '{}:{}'.format(self.onos_config_dir, self.onos_config_dir_guest)
399 volumes.append(config_volume)
A.R Karthickf184b342017-01-27 19:30:50 -0800400 if self.start_wrapper:
401 docker_yaml_changed = '{}-changed'.format(self.docker_yaml)
402 with open(docker_yaml_changed, 'w') as wf:
403 yaml.dump(yaml_config, wf)
404 os.rename(docker_yaml_changed, self.docker_yaml)
A R Karthickd44cea12016-07-20 12:16:41 -0700405 self.volumes = volumes
406
A R Karthickd44cea12016-07-20 12:16:41 -0700407 ##Create an container instance of xos onos
A R Karthick52414732017-01-31 09:59:47 -0800408 super(OnosCord, self).__init__(xos_onos_name, image, tag = '', quagga_config = Onos.QUAGGA_CONFIG)
A.R Karthickf184b342017-01-27 19:30:50 -0800409 self.last_cfg = None
410 if self.start_wrapper:
411 #fetch the current config of onos cord instance and save it
412 try:
413 self.last_cfg = OnosCtrl.get_config(controller = onos_ip)
414 json_data = json.dumps(self.last_cfg, indent=4)
415 with open(self.onos_cfg_save_loc, 'w') as f:
416 f.write(json_data)
417 except:
418 pass
419 #start the container back with the shared onos config volume
420 self.start()
A R Karthickd44cea12016-07-20 12:16:41 -0700421
A R Karthick03bd2812017-03-03 17:49:17 -0800422 def cliEnter(self):
423 retries = 0
424 while retries < 30:
425 cli = OnosCliDriver(controller = self.onos_ip, connect = True)
426 if cli.handle:
427 return cli
428 else:
429 retries += 1
A R Karthick72fcbc52017-03-06 12:35:17 -0800430 time.sleep(3)
A R Karthick03bd2812017-03-03 17:49:17 -0800431
432 return None
433
434 def cliExit(self, cli):
435 if cli:
436 cli.disconnect()
437
A.R Karthickddf12772017-05-17 13:49:47 -0700438 def synchronize_fabric(self, cfg = None):
439 if self.old_cord is True:
440 cmds = [ 'cd {} && make {}'.format(self.old_service_profile, self.synchronizer),
441 'sleep 30'
442 ]
443 for cmd in cmds:
444 try:
445 os.system(cmd)
446 except:
447 pass
448
A R Karthick03bd2812017-03-03 17:49:17 -0800449 def synchronize_vtn(self, cfg = None):
A.R Karthickddf12772017-05-17 13:49:47 -0700450 if self.old_cord is True:
451 cmds = [ 'cd {} && make {}'.format(self.old_service_profile, self.synchronizer),
452 'sleep 30'
453 ]
454 for cmd in cmds:
455 try:
456 os.system(cmd)
457 except:
458 pass
459 return
A R Karthick03bd2812017-03-03 17:49:17 -0800460 if cfg is None:
461 return
462 if not cfg.has_key('apps'):
463 return
464 if not cfg['apps'].has_key('org.opencord.vtn'):
465 return
466 vtn_neutron_cfg = cfg['apps']['org.opencord.vtn']['cordvtn']['openstack']
467 password = vtn_neutron_cfg['password']
468 endpoint = vtn_neutron_cfg['endpoint']
469 user = vtn_neutron_cfg['user']
470 tenant = vtn_neutron_cfg['tenant']
471 vtn_host = cfg['apps']['org.opencord.vtn']['cordvtn']['nodes'][0]['hostname']
472 cli = self.cliEnter()
473 if cli is None:
474 return
475 cli.cordVtnSyncNeutronStates(endpoint, password, tenant = tenant, user = user)
476 time.sleep(2)
477 cli.cordVtnNodeInit(vtn_host)
478 self.cliExit(cli)
479
480 def synchronize(self, cfg_unlink = False):
A R Karthick03bd2812017-03-03 17:49:17 -0800481
482 if not self.synchronizer_map.has_key(self.synchronizer):
483 return
484
485 install_list = ()
486 if self.synchronizer_map[self.synchronizer].has_key('install'):
487 install_list = self.synchronizer_map[self.synchronizer]['install']
488
489 activate_list = ()
490 if self.synchronizer_map[self.synchronizer].has_key('activate'):
491 activate_list = self.synchronizer_map[self.synchronizer]['activate']
492
493 for app_url in install_list:
494 print('Installing app from url: %s' %app_url)
495 OnosCtrl.install_app_from_url(None, None, app_url = app_url, onos_ip = self.onos_ip)
496
497 for app in activate_list:
498 print('Activating app %s' %app)
499 OnosCtrl(app, controller = self.onos_ip).activate()
500 time.sleep(2)
501
502 for app_url in self.tester_apps:
503 print('Installing tester app from url: %s' %app_url)
504 OnosCtrl.install_app_from_url(None, None, app_url = app_url, onos_ip = self.onos_ip)
505
A R Karthick72fcbc52017-03-06 12:35:17 -0800506 cfg = None
507 #restore the saved config after applications are activated
508 if os.access(self.onos_cfg_save_loc, os.F_OK):
509 with open(self.onos_cfg_save_loc, 'r') as f:
510 cfg = json.load(f)
511 try:
512 OnosCtrl.config(cfg, controller = self.onos_ip)
513 if cfg_unlink is True:
514 os.unlink(self.onos_cfg_save_loc)
515 except:
516 pass
517
518 if hasattr(self, 'synchronize_{}'.format(self.synchronizer)):
519 getattr(self, 'synchronize_{}'.format(self.synchronizer))(cfg = cfg)
520
521 #now restart the xos synchronizer container
A R Karthick49529c52017-05-19 09:43:01 -0700522 cmd = None
523 if os.access('{}/onboarding-docker-compose/docker-compose.yml'.format(self.cord_profile), os.F_OK):
524 cmd = 'cd {}/onboarding-docker-compose && \
525 docker-compose -p {} restart xos_synchronizer_{}'.format(self.cord_profile,
526 self.service_profile,
527 self.synchronizer)
528 else:
529 if os.access('{}/docker-compose.yml'.format(self.cord_profile), os.F_OK):
530 cmd = 'cd {} && \
531 docker-compose -p {} restart {}-synchronizer'.format(self.cord_profile,
532 self.service_profile,
533 self.synchronizer)
534 if cmd is not None:
535 try:
536 print(cmd)
537 os.system(cmd)
538 except:
539 pass
A R Karthick03bd2812017-03-03 17:49:17 -0800540
A R Karthickd44cea12016-07-20 12:16:41 -0700541 def start(self, restart = False, network_cfg = None):
A R Karthick928ad622017-01-30 12:18:32 -0800542 if network_cfg is not None:
A R Karthickd44cea12016-07-20 12:16:41 -0700543 json_data = json.dumps(network_cfg, indent=4)
544 with open('{}/network-cfg.json'.format(self.onos_config_dir), 'w') as f:
545 f.write(json_data)
A R Karthick52414732017-01-31 09:59:47 -0800546
547 #we avoid using docker-compose restart for now.
548 #since we don't want to retain the metadata across restarts
A R Karthick03bd2812017-03-03 17:49:17 -0800549 #stop and start and synchronize the services before installing tester cord apps
550 cmds = [ 'cd {} && docker-compose down'.format(self.onos_cord_dir),
551 'cd {} && docker-compose up -d'.format(self.onos_cord_dir),
A R Karthickbc894372017-05-12 16:34:08 -0700552 'sleep 150',
A R Karthick03bd2812017-03-03 17:49:17 -0800553 ]
554 for cmd in cmds:
A.R Karthickf184b342017-01-27 19:30:50 -0800555 try:
A R Karthick03bd2812017-03-03 17:49:17 -0800556 print(cmd)
A.R Karthickf184b342017-01-27 19:30:50 -0800557 os.system(cmd)
A R Karthick03bd2812017-03-03 17:49:17 -0800558 except:pass
A R Karthick52414732017-01-31 09:59:47 -0800559
A R Karthick03bd2812017-03-03 17:49:17 -0800560 self.synchronize()
A R Karthick52414732017-01-31 09:59:47 -0800561 ##we could also connect container to default docker network but disabled for now
562 #Container.connect_to_network(self.name, 'bridge')
A R Karthick52414732017-01-31 09:59:47 -0800563 #connect container to the quagga bridge
564 self.connect_to_br(index = 0)
A.R Karthickf184b342017-01-27 19:30:50 -0800565 print('Waiting %d seconds for ONOS instance to start' %self.boot_delay)
A R Karthickbd9b8a32016-07-21 09:56:45 -0700566 time.sleep(self.boot_delay)
A R Karthickd44cea12016-07-20 12:16:41 -0700567
568 def build_image(self):
569 build_cmd = 'cd {} && docker-compose build'.format(self.onos_cord_dir)
570 os.system(build_cmd)
571
A.R Karthickf184b342017-01-27 19:30:50 -0800572 def restore(self, force = False):
573 restore = self.start_wrapper is True or force is True
574 if not restore:
A.R Karthick263d3fc2017-01-27 12:52:53 -0800575 return
A R Karthick394976f2017-01-31 14:25:16 -0800576 #nothing to restore
577 if not os.access(self.docker_yaml_saved, os.F_OK):
578 return
A R Karthick03bd2812017-03-03 17:49:17 -0800579
A.R Karthickf184b342017-01-27 19:30:50 -0800580 #restore the config files back. The synchronizer restore should bring the last config back
581 cmds = ['cd {} && docker-compose down'.format(self.onos_cord_dir),
582 'rm -rf {}'.format(self.onos_config_dir),
583 'mv {} {}'.format(self.docker_yaml_saved, self.docker_yaml),
584 'cd {} && docker-compose up -d'.format(self.onos_cord_dir),
A R Karthickbc894372017-05-12 16:34:08 -0700585 'sleep 150',
A.R Karthickf184b342017-01-27 19:30:50 -0800586 ]
587 for cmd in cmds:
A.R Karthickb17e2022017-01-27 11:29:26 -0800588 try:
A.R Karthickf184b342017-01-27 19:30:50 -0800589 print(cmd)
590 os.system(cmd)
A.R Karthickb17e2022017-01-27 11:29:26 -0800591 except: pass
592
A R Karthick03bd2812017-03-03 17:49:17 -0800593 self.synchronize(cfg_unlink = True)
A.R Karthickb17e2022017-01-27 11:29:26 -0800594
A.R Karthick1700e0e2016-10-06 18:16:57 -0700595class OnosCordStopWrapper(Container):
596 onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord')
597 docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml')
598
599 def __init__(self):
600 if os.access(self.docker_yaml, os.F_OK):
601 with open(self.docker_yaml, 'r') as f:
602 yaml_config = yaml.load(f)
603 image = yaml_config['services'].keys()[0]
604 name = 'cordtestercord_{}_1'.format(image)
605 super(OnosCordStopWrapper, self).__init__(name, image, tag = '')
606 if self.exists():
607 print('Killing container %s' %self.name)
608 self.kill()
609
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700610class Onos(Container):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800611 QUAGGA_CONFIG = [ { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, ]
A R Karthicka2492c12016-12-16 10:31:51 -0800612 MAX_INSTANCES = 3
A R Karthickc69d73e2017-01-20 11:44:34 -0800613 JVM_HEAP_SIZE = None
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700614 SYSTEM_MEMORY = (get_mem(),) * 2
A R Karthicka2492c12016-12-16 10:31:51 -0800615 INSTANCE_MEMORY = (get_mem(instances=MAX_INSTANCES),) * 2
A R Karthickc69d73e2017-01-20 11:44:34 -0800616 JAVA_OPTS_FORMAT = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'
617 JAVA_OPTS_DEFAULT = JAVA_OPTS_FORMAT.format(*SYSTEM_MEMORY) #-XX:+PrintGCDetails -XX:+PrintGCTimeStamps'
618 JAVA_OPTS_CLUSTER_DEFAULT = JAVA_OPTS_FORMAT.format(*INSTANCE_MEMORY)
A R Karthickcf1a5d32017-10-05 16:04:43 -0700619 env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,vrouter,hostprovider', 'JAVA_OPTS' : JAVA_OPTS_DEFAULT }
A R Karthick6e70e142017-07-28 15:25:38 -0700620 onos_cord_apps = ( ['cord-config', '1.2-SNAPSHOT', 'org.opencord.config'],
A R Karthick1555c7c2017-09-07 14:59:41 -0700621 ['sadis-app', '3.0-SNAPSHOT', 'org.opencord.sadis'],
622 ['olt-app', '1.2-SNAPSHOT', 'org.onosproject.olt'],
A R Karthick6e70e142017-07-28 15:25:38 -0700623 ['aaa', '1.2-SNAPSHOT', 'org.opencord.aaa'],
624 ['igmp', '1.2-SNAPSHOT', 'org.opencord.igmp'],
A.R Karthick95d044e2016-06-10 18:44:36 -0700625 )
A R Karthickb608d402017-06-02 11:48:41 -0700626 cord_apps_version_updated = False
A R Karthick184945a2017-07-25 17:23:57 -0700627 expose_port = False
628 expose_ports = [ 8181, 8101, 9876, 6653, 6633, 2000, 2620, 5005 ]
629 ports = []
A R Karthickf2f4ca62016-08-17 10:34:08 -0700630 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
631 host_config_dir = os.path.join(setup_dir, 'onos-config')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700632 guest_config_dir = '/root/onos/config'
A.R Karthickdda22062017-02-09 14:39:20 -0800633 guest_data_dir = '/root/onos/apache-karaf-3.0.8/data'
634 guest_log_file = '/root/onos/apache-karaf-3.0.8/data/log/karaf.log'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700635 onos_gen_partitions = os.path.join(setup_dir, 'onos-gen-partitions')
A R Karthick2b93d6a2016-09-06 15:19:09 -0700636 onos_form_cluster = os.path.join(setup_dir, 'onos-form-cluster')
A.R Karthick95d044e2016-06-10 18:44:36 -0700637 cord_apps_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'apps')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700638 host_guest_map = ( (host_config_dir, guest_config_dir), )
A R Karthickd52ca8a2017-07-24 17:38:55 -0700639 ssl_key = None
A R Karthick2b93d6a2016-09-06 15:19:09 -0700640 cluster_cfg = os.path.join(host_config_dir, 'cluster.json')
641 cluster_mode = False
642 cluster_instances = []
Chetan Gaonker503032a2016-05-12 12:06:29 -0700643 NAME = 'cord-onos'
A R Karthickf2f4ca62016-08-17 10:34:08 -0700644 ##the ip of ONOS in default cluster.json in setup/onos-config
645 CLUSTER_CFG_IP = '172.17.0.2'
A R Karthick07608ef2016-08-23 16:51:19 -0700646 IMAGE = 'onosproject/onos'
647 TAG = 'latest'
648 PREFIX = ''
A R Karthickf2f4ca62016-08-17 10:34:08 -0700649
650 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700651 def generate_cluster_cfg(cls, ip):
652 if type(ip) in [ list, tuple ]:
653 ips = ' '.join(ip)
654 else:
655 ips = ip
A R Karthickf2f4ca62016-08-17 10:34:08 -0700656 try:
A R Karthick2b93d6a2016-09-06 15:19:09 -0700657 cmd = '{} {} {}'.format(cls.onos_gen_partitions, cls.cluster_cfg, ips)
658 os.system(cmd)
659 except: pass
660
661 @classmethod
662 def form_cluster(cls, ips):
663 nodes = ' '.join(ips)
664 try:
665 cmd = '{} {}'.format(cls.onos_form_cluster, nodes)
A R Karthickf2f4ca62016-08-17 10:34:08 -0700666 os.system(cmd)
667 except: pass
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700668
A R Karthick9d48c652016-09-15 09:16:36 -0700669 @classmethod
670 def cleanup_runtime(cls):
671 '''Cleanup ONOS runtime generated files'''
672 files = ( Onos.cluster_cfg, os.path.join(Onos.host_config_dir, 'network-cfg.json') )
673 for f in files:
674 if os.access(f, os.F_OK):
675 try:
676 os.unlink(f)
677 except: pass
678
A R Karthickec2db322016-11-17 15:06:01 -0800679 @classmethod
680 def get_data_map(cls, host_volume, guest_volume_dir):
681 host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume))
682 if not os.path.exists(host_volume_dir):
683 os.mkdir(host_volume_dir)
684 return ( (host_volume_dir, guest_volume_dir), )
685
686 @classmethod
687 def remove_data_map(cls, host_volume, guest_volume_dir):
688 host_volume_dir = os.path.join(cls.setup_dir, os.path.basename(host_volume))
689 if os.path.exists(host_volume_dir):
A.R Karthickf184b342017-01-27 19:30:50 -0800690 shutil.rmtree(host_volume_dir)
A R Karthickec2db322016-11-17 15:06:01 -0800691
A R Karthick973010f2017-02-06 16:41:51 -0800692 @classmethod
693 def update_data_dir(cls, karaf):
694 Onos.guest_data_dir = '/root/onos/apache-karaf-{}/data'.format(karaf)
695 Onos.guest_log_file = '/root/onos/apache-karaf-{}/data/log/karaf.log'.format(karaf)
696
A R Karthickd52ca8a2017-07-24 17:38:55 -0700697 @classmethod
698 def update_ssl_key(cls, key):
699 if os.access(key, os.F_OK):
700 try:
701 shutil.copy(key, cls.host_config_dir)
702 cls.ssl_key = os.path.join(cls.host_config_dir, os.path.basename(key))
703 except:pass
704
A R Karthick184945a2017-07-25 17:23:57 -0700705 @classmethod
706 def set_expose_port(cls, flag):
707 cls.expose_port = flag
708
709 def get_port_map(self, instance=0):
710 if self.expose_port is False:
711 return self.ports
712 return map(lambda p: (p, p + instance), self.expose_ports)
713
A R Karthickec2db322016-11-17 15:06:01 -0800714 def remove_data_volume(self):
715 if self.data_map is not None:
716 self.remove_data_map(*self.data_map)
717
A.R Karthick1700e0e2016-10-06 18:16:57 -0700718 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthickec2db322016-11-17 15:06:01 -0800719 boot_delay = 20, restart = False, network_cfg = None,
A R Karthick85eb1862017-01-23 16:10:57 -0800720 cluster = False, data_volume = None, async = False, quagga_config = None,
A R Karthick184945a2017-07-25 17:23:57 -0700721 network = None, instance = 0):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700722 if restart is True:
723 ##Find the right image to restart
724 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
725 if running_image:
726 image_name = running_image[0]['Image']
727 try:
728 image = image_name.split(':')[0]
729 tag = image_name.split(':')[1]
730 except: pass
731
A R Karthickaa54a1c2016-12-15 11:42:08 -0800732 if quagga_config is None:
733 quagga_config = Onos.QUAGGA_CONFIG
734 super(Onos, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = quagga_config)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700735 self.boot_delay = boot_delay
A R Karthickec2db322016-11-17 15:06:01 -0800736 self.data_map = None
A R Karthickc69d73e2017-01-20 11:44:34 -0800737 instance_memory = (get_mem(jvm_heap_size = Onos.JVM_HEAP_SIZE, instances = Onos.MAX_INSTANCES),) * 2
738 self.env['JAVA_OPTS'] = self.JAVA_OPTS_FORMAT.format(*instance_memory)
A R Karthick184945a2017-07-25 17:23:57 -0700739 self.ports = self.get_port_map(instance = instance)
A R Karthickd52ca8a2017-07-24 17:38:55 -0700740 if self.ssl_key:
741 key_files = ( os.path.join(self.guest_config_dir, os.path.basename(self.ssl_key)), ) * 2
742 self.env['JAVA_OPTS'] += ' -DenableOFTLS=true -Djavax.net.ssl.keyStore={} -Djavax.net.ssl.keyStorePassword=222222 -Djavax.net.ssl.trustStore={} -Djavax.net.ssl.trustStorePassword=222222'.format(*key_files)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700743 if cluster is True:
A R Karthickec2db322016-11-17 15:06:01 -0800744 if data_volume is not None:
745 self.data_map = self.get_data_map(data_volume, self.guest_data_dir)
746 self.host_guest_map = self.host_guest_map + self.data_map
A R Karthick2b93d6a2016-09-06 15:19:09 -0700747 if os.access(self.cluster_cfg, os.F_OK):
748 try:
749 os.unlink(self.cluster_cfg)
750 except: pass
751
752 self.host_config = self.create_host_config(port_list = self.ports,
753 host_guest_map = self.host_guest_map)
754 self.volumes = []
755 for _,g in self.host_guest_map:
756 self.volumes.append(g)
757
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700758 if restart is True and self.exists():
759 self.kill()
A R Karthick2b93d6a2016-09-06 15:19:09 -0700760
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700761 if not self.exists():
762 self.remove_container(name, force=True)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -0700763 host_config = self.create_host_config(port_list = self.ports,
764 host_guest_map = self.host_guest_map)
765 volumes = []
766 for _,g in self.host_guest_map:
767 volumes.append(g)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700768 if network_cfg is not None:
A R Karthick81acbff2016-06-17 14:45:16 -0700769 json_data = json.dumps(network_cfg, indent=4)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700770 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
771 f.write(json_data)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800772 if cluster is False or async is False:
773 print('Starting ONOS container %s' %self.name)
774 self.start(ports = self.ports, environment = self.env,
A R Karthick1555c7c2017-09-07 14:59:41 -0700775 host_config = self.host_config, volumes = self.volumes, tty = True,
776 network = Radius.NETWORK)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800777 if not restart:
778 ##wait a bit before fetching IP to regenerate cluster cfg
779 time.sleep(5)
780 ip = self.ip()
781 ##Just a quick hack/check to ensure we don't regenerate in the common case.
782 ##As ONOS is usually the first test container that is started
783 if cluster is False:
784 if ip != self.CLUSTER_CFG_IP or not os.access(self.cluster_cfg, os.F_OK):
785 print('Regenerating ONOS cluster cfg for ip %s' %ip)
786 self.generate_cluster_cfg(ip)
787 self.kill()
788 self.remove_container(self.name, force=True)
789 print('Restarting ONOS container %s' %self.name)
790 self.start(ports = self.ports, environment = self.env,
A R Karthick1555c7c2017-09-07 14:59:41 -0700791 host_config = self.host_config, volumes = self.volumes, tty = True,
792 network = Radius.NETWORK)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800793 print('Waiting for ONOS to boot')
794 time.sleep(boot_delay)
795 self.wait_for_onos_start(self.ip())
796 self.running = True
797 else:
798 self.running = False
799 else:
800 self.running = True
801 if self.running:
802 self.ipaddr = self.ip()
803 if cluster is False:
804 self.install_cord_apps(self.ipaddr)
A R Karthick19aaf5c2016-11-09 17:47:57 -0800805
A.R Karthickc4e474d2016-12-12 15:24:57 -0800806 @classmethod
807 def get_quagga_config(cls, instance = 0):
A R Karthickaa54a1c2016-12-15 11:42:08 -0800808 quagga_config = copy.deepcopy(cls.QUAGGA_CONFIG)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800809 if instance == 0:
810 return quagga_config
811 ip = quagga_config[0]['ip']
812 octets = ip.split('.')
A R Karthickaa54a1c2016-12-15 11:42:08 -0800813 octets[3] = str((int(octets[3]) + instance) & 255)
A.R Karthickc4e474d2016-12-12 15:24:57 -0800814 ip = '.'.join(octets)
815 quagga_config[0]['ip'] = ip
816 return quagga_config
817
818 @classmethod
819 def start_cluster_async(cls, onos_instances):
820 instances = filter(lambda o: o.running == False, onos_instances)
821 if not instances:
822 return
823 tpool = ThreadPool(len(instances), queue_size = 1, wait_timeout = 1)
824 for onos in instances:
825 tpool.addTask(onos.start_async)
826 tpool.cleanUpThreads()
827
828 def start_async(self):
829 print('Starting ONOS container %s' %self.name)
830 self.start(ports = self.ports, environment = self.env,
831 host_config = self.host_config, volumes = self.volumes, tty = True)
832 time.sleep(3)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700833 self.ipaddr = self.ip()
A.R Karthickc4e474d2016-12-12 15:24:57 -0800834 print('Waiting for ONOS container %s to start' %self.name)
835 self.wait_for_onos_start(self.ipaddr)
836 self.running = True
837 print('ONOS container %s started' %self.name)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700838
A R Karthick2b93d6a2016-09-06 15:19:09 -0700839 @classmethod
A R Karthick19aaf5c2016-11-09 17:47:57 -0800840 def wait_for_onos_start(cls, ip, tries = 30):
A R Karthick973010f2017-02-06 16:41:51 -0800841 onos_log = OnosLog(host = ip, log_file = Onos.guest_log_file)
A R Karthick19aaf5c2016-11-09 17:47:57 -0800842 num_tries = 0
843 started = None
844 while not started and num_tries < tries:
845 time.sleep(3)
846 started = onos_log.search_log_pattern('ApplicationManager .* Started')
847 num_tries += 1
848
A R Karthick19aaf5c2016-11-09 17:47:57 -0800849 if not started:
850 print('ONOS did not start')
851 else:
852 print('ONOS started')
853 return started
854
855 @classmethod
A R Karthick2b93d6a2016-09-06 15:19:09 -0700856 def setup_cluster_deprecated(cls, onos_instances, image_name = None):
857 if not onos_instances or len(onos_instances) < 2:
858 return
859 ips = []
860 if image_name is not None:
861 ips = Container.ips(image_name)
862 else:
863 for onos in onos_instances:
864 ips.append(onos.ipaddr)
865 Onos.cluster_instances = onos_instances
866 Onos.cluster_mode = True
867 ##regenerate the cluster json with the 3 instance ips before restarting them back
868 print('Generating cluster cfg for ONOS instances with ips %s' %ips)
869 Onos.generate_cluster_cfg(ips)
870 for onos in onos_instances:
871 onos.kill()
872 onos.remove_container(onos.name, force=True)
873 print('Restarting ONOS container %s for forming cluster' %onos.name)
874 onos.start(ports = onos.ports, environment = onos.env,
875 host_config = onos.host_config, volumes = onos.volumes, tty = True)
876 print('Waiting %d seconds for ONOS %s to boot' %(onos.boot_delay, onos.name))
877 time.sleep(onos.boot_delay)
878 onos.ipaddr = onos.ip()
879 onos.install_cord_apps(onos.ipaddr)
880
881 @classmethod
882 def setup_cluster(cls, onos_instances, image_name = None):
883 if not onos_instances or len(onos_instances) < 2:
884 return
885 ips = []
886 if image_name is not None:
887 ips = Container.ips(image_name)
888 else:
889 for onos in onos_instances:
890 ips.append(onos.ipaddr)
891 Onos.cluster_instances = onos_instances
892 Onos.cluster_mode = True
893 ##regenerate the cluster json with the 3 instance ips before restarting them back
894 print('Forming cluster for ONOS instances with ips %s' %ips)
895 Onos.form_cluster(ips)
896 ##wait for the cluster to be formed
897 print('Waiting for the cluster to be formed')
898 time.sleep(60)
899 for onos in onos_instances:
900 onos.install_cord_apps(onos.ipaddr)
901
902 @classmethod
A R Karthicke2c24bd2016-10-07 14:51:38 -0700903 def add_cluster(cls, count = 1, network_cfg = None):
904 if not cls.cluster_instances or Onos.cluster_mode is False:
905 return
906 for i in range(count):
A R Karthick184945a2017-07-25 17:23:57 -0700907 instance = len(cls.cluster_instances)
908 name = '{}-{}'.format(Onos.NAME, instance+1)
A R Karthicke2c24bd2016-10-07 14:51:38 -0700909 onos = cls(name = name, image = Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX,
A R Karthick184945a2017-07-25 17:23:57 -0700910 cluster = True, network_cfg = network_cfg, instance = instance)
A R Karthicke2c24bd2016-10-07 14:51:38 -0700911 cls.cluster_instances.append(onos)
912
913 cls.setup_cluster(cls.cluster_instances)
914
915 @classmethod
A.R Karthick2560f042016-11-30 14:38:52 -0800916 def restart_cluster(cls, network_cfg = None, timeout = 10, setup = False):
A R Karthick2b93d6a2016-09-06 15:19:09 -0700917 if cls.cluster_mode is False:
918 return
919 if not cls.cluster_instances:
920 return
921
922 if network_cfg is not None:
923 json_data = json.dumps(network_cfg, indent=4)
924 with open('{}/network-cfg.json'.format(cls.host_config_dir), 'w') as f:
925 f.write(json_data)
926
A.R Karthick2560f042016-11-30 14:38:52 -0800927 cls.cleanup_cluster()
928 if timeout > 0:
929 time.sleep(timeout)
930
A R Karthickaa54a1c2016-12-15 11:42:08 -0800931 #start the instances asynchronously
932 cls.start_cluster_async(cls.cluster_instances)
933 time.sleep(5)
A.R Karthick2560f042016-11-30 14:38:52 -0800934 ##form the cluster as appropriate
935 if setup is True:
936 cls.setup_cluster(cls.cluster_instances)
A R Karthickaa54a1c2016-12-15 11:42:08 -0800937 else:
938 for onos in cls.cluster_instances:
939 onos.install_cord_apps(onos.ipaddr)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700940
941 @classmethod
942 def cluster_ips(cls):
943 if cls.cluster_mode is False:
944 return []
945 if not cls.cluster_instances:
946 return []
947 ips = [ onos.ipaddr for onos in cls.cluster_instances ]
948 return ips
949
950 @classmethod
951 def cleanup_cluster(cls):
952 if cls.cluster_mode is False:
953 return
954 if not cls.cluster_instances:
955 return
956 for onos in cls.cluster_instances:
957 if onos.exists():
958 onos.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -0800959 onos.running = False
A R Karthick2b93d6a2016-09-06 15:19:09 -0700960 onos.remove_container(onos.name, force=True)
A R Karthickd44cea12016-07-20 12:16:41 -0700961
A.R Karthick95d044e2016-06-10 18:44:36 -0700962 @classmethod
A R Karthickde6b9dc2016-11-29 17:46:16 -0800963 def restart_node(cls, node = None, network_cfg = None, timeout = 10):
A R Karthick889d9652016-10-03 14:13:45 -0700964 if node is None:
965 cls(restart = True, network_cfg = network_cfg, image = cls.IMAGE, tag = cls.TAG)
966 else:
967 #Restarts a node in the cluster
968 valid_node = filter(lambda onos: node in [ onos.ipaddr, onos.name ], cls.cluster_instances)
969 if valid_node:
970 onos = valid_node.pop()
971 if onos.exists():
972 onos.kill()
973 onos.remove_container(onos.name, force=True)
A R Karthickde6b9dc2016-11-29 17:46:16 -0800974 if timeout > 0:
975 time.sleep(timeout)
A R Karthick889d9652016-10-03 14:13:45 -0700976 print('Restarting ONOS container %s' %onos.name)
977 onos.start(ports = onos.ports, environment = onos.env,
A R Karthick1555c7c2017-09-07 14:59:41 -0700978 host_config = onos.host_config, volumes = onos.volumes, tty = True,
979 network = Radius.NETWORK)
A R Karthick889d9652016-10-03 14:13:45 -0700980 onos.ipaddr = onos.ip()
A.R Karthick2560f042016-11-30 14:38:52 -0800981 onos.wait_for_onos_start(onos.ipaddr)
982 onos.install_cord_apps(onos.ipaddr)
A R Karthick889d9652016-10-03 14:13:45 -0700983
984 @classmethod
A R Karthickb608d402017-06-02 11:48:41 -0700985 def cliEnter(cls, onos_ip = None):
986 retries = 0
987 while retries < 10:
988 cli = OnosCliDriver(controller = onos_ip, connect = True)
989 if cli.handle:
990 return cli
991 else:
992 retries += 1
993 time.sleep(3)
994
995 return None
996
997 @classmethod
998 def cliExit(cls, cli):
999 if cli:
1000 cli.disconnect()
1001
1002 @classmethod
1003 def getVersion(cls, onos_ip = None):
1004 cli = cls.cliEnter(onos_ip = onos_ip)
1005 try:
1006 summary = json.loads(cli.summary(jsonFormat = True))
1007 except:
1008 cls.cliExit(cli)
1009 return '1.8.0'
1010 cls.cliExit(cli)
1011 return summary['version']
1012
1013 @classmethod
1014 def update_cord_apps_version(cls, onos_ip = None):
1015 if cls.cord_apps_version_updated == True:
1016 return
1017 version = cls.getVersion(onos_ip = onos_ip)
1018 major = int(version.split('.')[0])
1019 minor = int(version.split('.')[1])
A R Karthick5b8310e2017-09-01 13:55:15 -07001020 try:
1021 patch = int(version.split('.')[2])
1022 except:
1023 patch = 0
A R Karthickb608d402017-06-02 11:48:41 -07001024 app_version = '1.2-SNAPSHOT'
1025 if major > 1:
A R Karthick1555c7c2017-09-07 14:59:41 -07001026 app_version = '3.0-SNAPSHOT'
A R Karthick5b8310e2017-09-01 13:55:15 -07001027 elif major == 1 and minor >= 10:
A R Karthick1555c7c2017-09-07 14:59:41 -07001028 app_version = '3.0-SNAPSHOT'
A R Karthick5b8310e2017-09-01 13:55:15 -07001029 if patch < 3:
1030 app_version = '1.2-SNAPSHOT'
A R Karthickb608d402017-06-02 11:48:41 -07001031 for apps in cls.onos_cord_apps:
1032 apps[1] = app_version
1033 cls.cord_apps_version_updated = True
1034
1035 @classmethod
A R Karthickeaf1c4e2016-07-19 12:22:35 -07001036 def install_cord_apps(cls, onos_ip = None):
A R Karthickb608d402017-06-02 11:48:41 -07001037 cls.update_cord_apps_version(onos_ip = onos_ip)
A R Karthick6e70e142017-07-28 15:25:38 -07001038 for app, version,_ in cls.onos_cord_apps:
A.R Karthick95d044e2016-06-10 18:44:36 -07001039 app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version)
A R Karthickeaf1c4e2016-07-19 12:22:35 -07001040 ok, code = OnosCtrl.install_app(app_file, onos_ip = onos_ip)
A.R Karthick95d044e2016-06-10 18:44:36 -07001041 ##app already installed (conflicts)
1042 if code in [ 409 ]:
1043 ok = True
1044 print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install'))
1045 time.sleep(2)
1046
A R Karthick21782982017-10-02 10:49:22 -07001047 OnosCtrl.config_olt_component(controller = onos_ip)
1048
A R Karthick6e70e142017-07-28 15:25:38 -07001049 @classmethod
1050 def activate_apps(cls, apps, onos_ip = None, deactivate = False):
1051 for app in apps:
1052 if deactivate is True:
1053 OnosCtrl(app, controller = onos_ip).deactivate()
1054 time.sleep(2)
1055 OnosCtrl(app, controller = onos_ip).activate()
1056
1057 time.sleep(5)
1058
1059 @classmethod
1060 def activate_cord_apps(cls, onos_ip = None, deactivate = True):
1061 cord_apps = map(lambda a: a[2], cls.onos_cord_apps)
1062 cls.activate_apps(cord_apps, onos_ip = onos_ip, deactivate = deactivate)
1063
A.R Karthick1700e0e2016-10-06 18:16:57 -07001064class OnosStopWrapper(Container):
1065 def __init__(self, name):
1066 super(OnosStopWrapper, self).__init__(name, Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX)
1067 if self.exists():
1068 self.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -08001069 self.running = False
A.R Karthick1700e0e2016-10-06 18:16:57 -07001070 else:
1071 if Onos.cluster_mode is True:
1072 valid_node = filter(lambda onos: name in [ onos.ipaddr, onos.name ], Onos.cluster_instances)
1073 if valid_node:
1074 onos = valid_node.pop()
1075 if onos.exists():
1076 onos.kill()
A R Karthickaa54a1c2016-12-15 11:42:08 -08001077 onos.running = False
A.R Karthick1700e0e2016-10-06 18:16:57 -07001078
Chetan Gaonker3533faa2016-04-25 17:50:14 -07001079class Radius(Container):
1080 ports = [ 1812, 1813 ]
A R Karthick41adfce2016-06-10 09:51:25 -07001081 env = {'TIMEZONE':'America/Los_Angeles',
Chetan Gaonker3533faa2016-04-25 17:50:14 -07001082 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
1083 }
Chetan Gaonker7f4bf742016-05-04 15:56:08 -07001084 host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db')
Chetan Gaonker3533faa2016-04-25 17:50:14 -07001085 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -07001086 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius')
Chetan Gaonker3533faa2016-04-25 17:50:14 -07001087 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -07001088 start_command = os.path.join(guest_config_dir, 'start-radius.py')
Chetan Gaonker3533faa2016-04-25 17:50:14 -07001089 host_guest_map = ( (host_db_dir, guest_db_dir),
1090 (host_config_dir, guest_config_dir)
1091 )
A R Karthickf7a613b2017-02-24 09:36:44 -08001092 IMAGE = 'cordtest/radius'
Chetan Gaonker503032a2016-05-12 12:06:29 -07001093 NAME = 'cord-radius'
A R Karthick1555c7c2017-09-07 14:59:41 -07001094 NETWORK = 'cord-radius-test'
A R Karthickefcf1ab2017-09-08 18:24:16 -07001095 SOCKET_SUBNET = '11.0.0.0/24'
1096 SOCKET_SUBNET_PREFIX = '11.0.0'
1097 SOCKET_GATEWAY = '11.0.0.1'
A R Karthick1555c7c2017-09-07 14:59:41 -07001098
1099 @classmethod
1100 def create_network(cls, name = NETWORK):
1101 try:
A R Karthickefcf1ab2017-09-08 18:24:16 -07001102 Container.create_network(name, subnet = cls.SOCKET_SUBNET, gateway = cls.SOCKET_GATEWAY)
A R Karthick1555c7c2017-09-07 14:59:41 -07001103 except:
1104 pass
Chetan Gaonker503032a2016-05-12 12:06:29 -07001105
A R Karthick07608ef2016-08-23 16:51:19 -07001106 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
A R Karthickefcf1ab2017-09-08 18:24:16 -07001107 boot_delay = 10, restart = False, update = False, network = None,
1108 network_disabled = False, olt_config = ''):
A R Karthick07608ef2016-08-23 16:51:19 -07001109 super(Radius, self).__init__(name, image, prefix = prefix, tag = tag, command = self.start_command)
Chetan Gaonker503032a2016-05-12 12:06:29 -07001110 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -07001111 self.build_image(self.image_name)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -07001112 if restart is True and self.exists():
1113 self.kill()
A R Karthickefcf1ab2017-09-08 18:24:16 -07001114 else:
1115 subscribers = 10
1116 if olt_config:
1117 port_map, _ = OltConfig(olt_config).olt_port_map()
1118 if port_map:
1119 subscribers = port_map['num_ports'] * len(port_map['switch_port_list'])
1120 radius_restore_users()
1121 radius_add_users(subscribers)
Chetan Gaonker3533faa2016-04-25 17:50:14 -07001122 if not self.exists():
1123 self.remove_container(name, force=True)
1124 host_config = self.create_host_config(port_list = self.ports,
A R Karthickefcf1ab2017-09-08 18:24:16 -07001125 host_guest_map = self.host_guest_map,
1126 privileged = True)
Chetan Gaonker3533faa2016-04-25 17:50:14 -07001127 volumes = []
1128 for _,g in self.host_guest_map:
1129 volumes.append(g)
A R Karthick41adfce2016-06-10 09:51:25 -07001130 self.start(ports = self.ports, environment = self.env,
1131 volumes = volumes,
A R Karthick1555c7c2017-09-07 14:59:41 -07001132 host_config = host_config, tty = True, network_disabled = network_disabled)
1133 if network_disabled is False:
1134 Container.connect_to_network(self.name, self.NETWORK)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -07001135 time.sleep(boot_delay)
1136
1137 @classmethod
1138 def build_image(cls, image):
1139 print('Building Radius image %s' %image)
1140 dockerfile = '''
1141FROM hbouvier/docker-radius
1142MAINTAINER chetan@ciena.com
1143LABEL RUN docker pull hbouvier/docker-radius
1144LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius
A R Karthickc762df42016-05-25 10:09:21 -07001145RUN apt-get update && \
1146 apt-get -y install python python-pexpect strace
Chetan Gaonker7f4bf742016-05-04 15:56:08 -07001147WORKDIR /root
1148CMD ["/etc/freeradius/start-radius.py"]
1149'''
1150 super(Radius, cls).build_image(dockerfile, image)
1151 print('Done building image %s' %image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -07001152
Chetan Gaonker6cf6e472016-04-26 14:41:51 -07001153class Quagga(Container):
A R Karthickaa54a1c2016-12-15 11:42:08 -08001154 QUAGGA_CONFIG = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 },
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -07001155 { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 },
1156 )
Chetan Gaonker6cf6e472016-04-26 14:41:51 -07001157 ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ]
1158 host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config')
1159 guest_quagga_config = '/root/config'
1160 quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
1161 host_guest_map = ( (host_quagga_config, guest_quagga_config), )
A R Karthickf7a613b2017-02-24 09:36:44 -08001162 IMAGE = 'cordtest/quagga'
Chetan Gaonker503032a2016-05-12 12:06:29 -07001163 NAME = 'cord-quagga'
1164
A R Karthick07608ef2016-08-23 16:51:19 -07001165 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
A R Karthick85eb1862017-01-23 16:10:57 -08001166 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False,
1167 network = None):
A R Karthickaa54a1c2016-12-15 11:42:08 -08001168 super(Quagga, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.QUAGGA_CONFIG)
Chetan Gaonker503032a2016-05-12 12:06:29 -07001169 if update is True or not self.img_exists():
A R Karthick07608ef2016-08-23 16:51:19 -07001170 self.build_image(self.image_name)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -07001171 if restart is True and self.exists():
1172 self.kill()
1173 if not self.exists():
1174 self.remove_container(name, force=True)
A R Karthick41adfce2016-06-10 09:51:25 -07001175 host_config = self.create_host_config(port_list = self.ports,
1176 host_guest_map = self.host_guest_map,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -07001177 privileged = True)
1178 volumes = []
1179 for _,g in self.host_guest_map:
1180 volumes.append(g)
1181 self.start(ports = self.ports,
A R Karthick41adfce2016-06-10 09:51:25 -07001182 host_config = host_config,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -07001183 volumes = volumes, tty = True)
A R Karthick85eb1862017-01-23 16:10:57 -08001184 if network is not None:
1185 Container.connect_to_network(self.name, network)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -07001186 print('Starting Quagga on container %s' %self.name)
1187 self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
1188 time.sleep(boot_delay)
1189
1190 @classmethod
1191 def build_image(cls, image):
A R Karthickaa54a1c2016-12-15 11:42:08 -08001192 onos_quagga_ip = Onos.QUAGGA_CONFIG[0]['ip']
Chetan Gaonker6cf6e472016-04-26 14:41:51 -07001193 print('Building Quagga image %s' %image)
1194 dockerfile = '''
A R Karthick41adfce2016-06-10 09:51:25 -07001195FROM ubuntu:14.04
1196MAINTAINER chetan@ciena.com
Chetan Gaonker6cf6e472016-04-26 14:41:51 -07001197WORKDIR /root
1198RUN useradd -M quagga
1199RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga
1200RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga
A R Karthick973ea692016-10-17 12:23:02 -07001201RUN 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 -07001202RUN git clone git://git.savannah.nongnu.org/quagga.git quagga && \
A R Karthick8f69c2c2016-10-21 11:43:26 -07001203(cd quagga && git checkout quagga-1.0.20160315 && ./bootstrap.sh && \
Chetan Gaonker6cf6e472016-04-26 14:41:51 -07001204sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \
1205./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install)
1206RUN ldconfig
1207'''.format(onos_quagga_ip)
1208 super(Quagga, cls).build_image(dockerfile, image)
1209 print('Done building image %s' %image)
A R Karthick81acbff2016-06-17 14:45:16 -07001210
A.R Karthick1700e0e2016-10-06 18:16:57 -07001211class QuaggaStopWrapper(Container):
1212 def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'candidate'):
1213 super(QuaggaStopWrapper, self).__init__(name, image, prefix = Container.IMAGE_PREFIX, tag = tag)
1214 if self.exists():
1215 self.kill()
1216
1217
A R Karthick81acbff2016-06-17 14:45:16 -07001218def reinitContainerClients():
1219 docker_netns.dckr = Client()
1220 Container.dckr = Client()
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001221
1222class Xos(Container):
1223 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup')
1224 TAG = 'latest'
1225 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -07001226 host_guest_map = None
1227 env = None
1228 ports = None
1229 volumes = None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001230
A R Karthick6e80afd2016-10-10 16:03:12 -07001231 @classmethod
1232 def get_cmd(cls, img_name):
1233 cmd = cls.dckr.inspect_image(img_name)['Config']['Cmd']
1234 return ' '.join(cmd)
1235
A R Karthicke3bde962016-09-27 15:06:35 -07001236 def __init__(self, name, image, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001237 boot_delay = 20, restart = False, network_cfg = None, update = False):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001238 if restart is True:
1239 ##Find the right image to restart
1240 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
1241 if running_image:
1242 image_name = running_image[0]['Image']
1243 try:
1244 image = image_name.split(':')[0]
1245 tag = image_name.split(':')[1]
1246 except: pass
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001247 super(Xos, self).__init__(name, image, prefix = prefix, tag = tag)
1248 if update is True or not self.img_exists():
1249 self.build_image(self.image_name)
A R Karthick6e80afd2016-10-10 16:03:12 -07001250 self.command = self.get_cmd(self.image_name).strip() or None
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001251 if restart is True and self.exists():
1252 self.kill()
1253 if not self.exists():
1254 self.remove_container(name, force=True)
A R Karthicke3bde962016-09-27 15:06:35 -07001255 host_config = self.create_host_config(port_list = self.ports,
1256 host_guest_map = self.host_guest_map,
1257 privileged = True)
1258 print('Starting XOS container %s' %self.name)
1259 self.start(ports = self.ports, environment = self.env, host_config = host_config,
1260 volumes = self.volumes, tty = True)
1261 print('Waiting %d seconds for XOS Base Container to boot' %(boot_delay))
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001262 time.sleep(boot_delay)
1263
1264 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -07001265 def build_image(cls, image, dockerfile_path, image_target = 'build'):
1266 cmd = 'cd {} && make {}'.format(dockerfile_path, image_target)
1267 print('Building XOS %s' %image)
1268 res = os.system(cmd)
1269 print('Done building image %s. Image build %s' %(image, 'successful' if res == 0 else 'failed'))
1270 return res
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001271
A R Karthicke3bde962016-09-27 15:06:35 -07001272class XosServer(Xos):
1273 ports = [8000,9998,9999]
1274 NAME = 'xos-server'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001275 IMAGE = 'xosproject/xos'
A R Karthicke3bde962016-09-27 15:06:35 -07001276 BASE_IMAGE = 'xosproject/xos-base'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001277 TAG = 'latest'
1278 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -07001279 dockerfile_path = os.path.join(Xos.setup_dir, 'xos')
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001280
A R Karthicke3bde962016-09-27 15:06:35 -07001281 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001282 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001283 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001284
1285 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -07001286 def build_image(cls, image = IMAGE):
1287 ##build the base image and then build the server image
1288 Xos.build_image(cls.BASE_IMAGE, cls.dockerfile_path, image_target = 'base')
1289 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001290
A R Karthicke3bde962016-09-27 15:06:35 -07001291class XosSynchronizerOpenstack(Xos):
1292 ports = [2375,]
1293 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer')
1294 NAME = 'xos-synchronizer'
1295 IMAGE = 'xosproject/xos-synchronizer-openstack'
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001296 TAG = 'latest'
1297 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -07001298 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001299
A R Karthicke3bde962016-09-27 15:06:35 -07001300 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -07001301 tag = TAG, boot_delay = 20, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001302 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001303
1304 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -07001305 def build_image(cls, image = IMAGE):
1306 XosServer.build_image()
1307 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001308
A R Karthicke3bde962016-09-27 15:06:35 -07001309class XosSynchronizerOnboarding(Xos):
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001310 NAME = 'xos-synchronizer-onboarding'
1311 IMAGE = 'xosproject/xos-synchronizer-onboarding'
1312 TAG = 'latest'
1313 PREFIX = ''
A R Karthicke3bde962016-09-27 15:06:35 -07001314 dockerfile_path = os.path.join(Xos.setup_dir, 'onboarding_synchronizer')
1315 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001316
A R Karthicke3bde962016-09-27 15:06:35 -07001317 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -07001318 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001319 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001320
1321 @classmethod
A R Karthicke3bde962016-09-27 15:06:35 -07001322 def build_image(cls, image = IMAGE):
1323 XosSynchronizerOpenstack.build_image()
1324 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001325
A R Karthicke3bde962016-09-27 15:06:35 -07001326class XosSynchronizerOpenvpn(Xos):
1327 NAME = 'xos-synchronizer-openvpn'
1328 IMAGE = 'xosproject/xos-openvpn'
1329 TAG = 'latest'
1330 PREFIX = ''
1331 dockerfile_path = os.path.join(Xos.setup_dir, 'openvpn')
1332 host_guest_map = ( ('/usr/local/share/ca-certificates', '/usr/local/share/ca-certificates'),)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001333
A R Karthicke3bde962016-09-27 15:06:35 -07001334 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -07001335 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001336 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1337
1338 @classmethod
1339 def build_image(cls, image = IMAGE):
1340 XosSynchronizerOpenstack.build_image()
1341 Xos.build_image(image, cls.dockerfile_path)
1342
1343class XosPostgresql(Xos):
1344 ports = [5432,]
1345 NAME = 'xos-db-postgres'
1346 IMAGE = 'xosproject/xos-postgres'
1347 TAG = 'latest'
1348 PREFIX = ''
1349 volumes = ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
1350 dockerfile_path = os.path.join(Xos.setup_dir, 'postgresql')
1351
1352 def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX,
A R Karthick6e80afd2016-10-10 16:03:12 -07001353 tag = TAG, boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001354 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1355
1356 @classmethod
1357 def build_image(cls, image = IMAGE):
1358 Xos.build_image(image, cls.dockerfile_path)
1359
1360class XosSyndicateMs(Xos):
1361 ports = [8080,]
1362 env = None
1363 NAME = 'xos-syndicate-ms'
1364 IMAGE = 'xosproject/syndicate-ms'
1365 TAG = 'latest'
1366 PREFIX = ''
1367 dockerfile_path = os.path.join(Xos.setup_dir, 'syndicate-ms')
1368
1369 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001370 boot_delay = 10, restart = False, network_cfg = None, update = False):
A R Karthicke3bde962016-09-27 15:06:35 -07001371 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1372
1373 @classmethod
1374 def build_image(cls, image = IMAGE):
1375 Xos.build_image(image, cls.dockerfile_path)
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001376
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001377class XosSyncVtn(Xos):
1378 ports = [8080,]
1379 env = None
1380 NAME = 'xos-synchronizer-vtn'
1381 IMAGE = 'xosproject/xos-synchronizer-vtn'
1382 TAG = 'latest'
1383 PREFIX = ''
1384 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtn')
1385
1386 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001387 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001388 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1389
1390 @classmethod
1391 def build_image(cls, image = IMAGE):
1392 Xos.build_image(image, cls.dockerfile_path)
1393
1394class XosSyncVtr(Xos):
1395 ports = [8080,]
1396 env = None
1397 NAME = 'xos-synchronizer-vtr'
1398 IMAGE = 'xosproject/xos-synchronizer-vtr'
1399 TAG = 'latest'
1400 PREFIX = ''
1401 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vtr')
1402
1403 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001404 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001405 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1406
1407 @classmethod
1408 def build_image(cls, image = IMAGE):
1409 Xos.build_image(image, cls.dockerfile_path)
1410
1411class XosSyncVsg(Xos):
1412 ports = [8080,]
1413 env = None
1414 NAME = 'xos-synchronizer-vsg'
1415 IMAGE = 'xosproject/xos-synchronizer-vsg'
1416 TAG = 'latest'
1417 PREFIX = ''
1418 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-vsg')
1419
1420 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001421 boot_delay = 10, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001422 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1423
1424 @classmethod
1425 def build_image(cls, image = IMAGE):
1426 Xos.build_image(image, cls.dockerfile_path)
1427
1428
1429class XosSyncOnos(Xos):
1430 ports = [8080,]
1431 env = None
1432 NAME = 'xos-synchronizer-onos'
1433 IMAGE = 'xosproject/xos-synchronizer-onos'
1434 TAG = 'latest'
1435 PREFIX = ''
1436 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-onos')
1437
1438 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001439 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001440 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1441
1442 @classmethod
1443 def build_image(cls, image = IMAGE):
1444 Xos.build_image(image, cls.dockerfile_path)
1445
1446class XosSyncFabric(Xos):
1447 ports = [8080,]
1448 env = None
1449 NAME = 'xos-synchronizer-fabric'
1450 IMAGE = 'xosproject/xos-synchronizer-fabric'
1451 TAG = 'latest'
1452 PREFIX = ''
1453 dockerfile_path = os.path.join(Xos.setup_dir, 'synchronizer-fabric')
1454
1455 def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = TAG,
A R Karthick6e80afd2016-10-10 16:03:12 -07001456 boot_delay = 30, restart = False, network_cfg = None, update = False):
ChetanGaonkerc220e0d2016-10-05 05:06:25 -07001457 Xos.__init__(self, name, image, prefix, tag, boot_delay, restart, network_cfg, update)
1458
1459 @classmethod
1460 def build_image(cls, image = IMAGE):
1461 Xos.build_image(image, cls.dockerfile_path)
A R Karthick19aaf5c2016-11-09 17:47:57 -08001462
1463if __name__ == '__main__':
1464 onos = Onos(boot_delay = 10, restart = True)