blob: f22dbfd2d6463fe58f286fcce4d30e646e329928 [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
19from pyroute2 import IPRoute
20from itertools import chain
21from nsenter import Namespace
22from docker import Client
23from shutil import copy
A.R Karthick95d044e2016-06-10 18:44:36 -070024from OnosCtrl import OnosCtrl
Chetan Gaonker3533faa2016-04-25 17:50:14 -070025
26class docker_netns(object):
27
28 dckr = Client()
29 def __init__(self, name):
30 pid = int(self.dckr.inspect_container(name)['State']['Pid'])
31 if pid == 0:
32 raise Exception('no container named {0}'.format(name))
33 self.pid = pid
34
35 def __enter__(self):
36 pid = self.pid
37 if not os.path.exists('/var/run/netns'):
38 os.mkdir('/var/run/netns')
39 os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid))
40 return str(pid)
41
42 def __exit__(self, type, value, traceback):
43 pid = self.pid
44 os.unlink('/var/run/netns/{0}'.format(pid))
45
46flatten = lambda l: chain.from_iterable(l)
47
48class Container(object):
49 dckr = Client()
50 def __init__(self, name, image, tag = 'latest', command = 'bash', quagga_config = None):
51 self.name = name
52 self.image = image
53 self.tag = tag
54 self.image_name = image + ':' + tag
55 self.id = None
56 self.command = command
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -070057 self.quagga_config = quagga_config
Chetan Gaonker3533faa2016-04-25 17:50:14 -070058
59 @classmethod
60 def build_image(cls, dockerfile, tag, force=True, nocache=False):
61 f = io.BytesIO(dockerfile.encode('utf-8'))
62 if force or not cls.image_exists(tag):
63 print('Build {0}...'.format(tag))
64 for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache):
65 if 'stream' in line:
66 print(line['stream'].strip())
67
68 @classmethod
69 def image_exists(cls, name):
70 return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()]
71
72 @classmethod
73 def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False):
74 port_bindings = None
75 binds = None
76 if port_list:
77 port_bindings = {}
78 for p in port_list:
79 port_bindings[str(p)] = str(p)
80
81 if host_guest_map:
82 binds = []
83 for h, g in host_guest_map:
84 binds.append('{0}:{1}'.format(h, g))
85
86 return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged)
87
88 @classmethod
89 def cleanup(cls, image):
A R Karthick09b1f4e2016-05-12 14:31:50 -070090 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
Chetan Gaonker3533faa2016-04-25 17:50:14 -070091 for cnt in cnt_list:
92 print('Cleaning container %s' %cnt['Id'])
A.R Karthick95d044e2016-06-10 18:44:36 -070093 if cnt.has_key('State') and cnt['State'] == 'running':
A R Karthick09b1f4e2016-05-12 14:31:50 -070094 cls.dckr.kill(cnt['Id'])
Chetan Gaonker3533faa2016-04-25 17:50:14 -070095 cls.dckr.remove_container(cnt['Id'], force=True)
96
97 @classmethod
98 def remove_container(cls, name, force=True):
99 try:
100 cls.dckr.remove_container(name, force = force)
101 except: pass
102
103 def exists(self):
104 return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers()))
105
106 def img_exists(self):
107 return self.image_name in [ctn['RepoTags'][0] for ctn in self.dckr.images()]
108
109 def ip(self):
110 cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
111 cnt_settings = cnt_list.pop()
112 return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress']
113
114 def kill(self, remove = True):
115 self.dckr.kill(self.name)
116 self.dckr.remove_container(self.name, force=True)
117
A R Karthick41adfce2016-06-10 09:51:25 -0700118 def start(self, rm = True, ports = None, volumes = None, host_config = None,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700119 environment = None, tty = False, stdin_open = True):
120
121 if rm and self.exists():
122 print('Removing container:', self.name)
123 self.dckr.remove_container(self.name, force=True)
124
A R Karthick41adfce2016-06-10 09:51:25 -0700125 ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700126 detach=True, name=self.name,
A R Karthick41adfce2016-06-10 09:51:25 -0700127 environment = environment,
128 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700129 host_config = host_config, stdin_open=stdin_open, tty = tty)
130 self.dckr.start(container=self.name)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700131 if self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700132 self.connect_to_br()
133 self.id = ctn['Id']
134 return ctn
135
136 def connect_to_br(self):
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700137 index = 0
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700138 with docker_netns(self.name) as pid:
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700139 for quagga_config in self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700140 ip = IPRoute()
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700141 br = ip.link_lookup(ifname=quagga_config['bridge'])
142 if len(br) == 0:
Chetan Gaonker5a0fda32016-05-10 14:09:07 -0700143 ip.link_create(ifname=quagga_config['bridge'], kind='bridge')
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700144 br = ip.link_lookup(ifname=quagga_config['bridge'])
145 br = br[0]
146 ip.link('set', index=br, state='up')
147 ifname = '{0}-{1}'.format(self.name, index)
148 ifs = ip.link_lookup(ifname=ifname)
149 if len(ifs) > 0:
150 ip.link_remove(ifs[0])
151 peer_ifname = '{0}-{1}'.format(pid, index)
Chetan Gaonker5a0fda32016-05-10 14:09:07 -0700152 ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700153 host = ip.link_lookup(ifname=ifname)[0]
154 ip.link('set', index=host, master=br)
155 ip.link('set', index=host, state='up')
156 guest = ip.link_lookup(ifname=peer_ifname)[0]
157 ip.link('set', index=guest, net_ns_fd=pid)
158 with Namespace(pid, 'net'):
159 ip = IPRoute()
160 ip.link('set', index=guest, ifname='eth{}'.format(index+1))
161 ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask'])
162 ip.link('set', index=guest, state='up')
163 index += 1
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700164
165 def execute(self, cmd, tty = True, stream = False, shell = False):
166 res = 0
167 if type(cmd) == str:
168 cmds = (cmd,)
169 else:
170 cmds = cmd
171 if shell:
172 for c in cmds:
173 res += os.system('docker exec {0} {1}'.format(self.name, c))
174 return res
175 for c in cmds:
176 i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True)
177 self.dckr.exec_start(i['Id'], stream = stream, detach=True)
178 result = self.dckr.exec_inspect(i['Id'])
179 res += 0 if result['ExitCode'] == None else result['ExitCode']
180 return res
181
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700182def get_mem():
183 with open('/proc/meminfo', 'r') as fd:
184 meminfo = fd.readlines()
185 mem = 0
186 for m in meminfo:
187 if m.startswith('MemTotal:') or m.startswith('SwapTotal:'):
188 mem += int(m.split(':')[1].strip().split()[0])
189
Chetan Gaonkerc0421e82016-05-04 17:23:08 -0700190 mem = max(mem/1024/1024/2, 1)
Chetan Gaonker6d0a7b02016-05-03 16:57:28 -0700191 mem = min(mem, 16)
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700192 return str(mem) + 'G'
193
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700194class Onos(Container):
195
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700196 quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, )
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700197 SYSTEM_MEMORY = (get_mem(),) * 2
198 JAVA_OPTS = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'.format(*SYSTEM_MEMORY)#-XX:+PrintGCDetails -XX:+PrintGCTimeStamps'
A.R Karthick95d044e2016-06-10 18:44:36 -0700199 env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,vrouter', 'JAVA_OPTS' : JAVA_OPTS }
200 onos_cord_apps = ( ('cord-config', '1.0-SNAPSHOT'),
201 ('aaa', '1.0-SNAPSHOT'),
202 ('igmp', '1.0-SNAPSHOT'),
203 )
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700204 ports = [ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ]
205 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/onos-config')
206 guest_config_dir = '/root/onos/config'
A.R Karthick95d044e2016-06-10 18:44:36 -0700207 cord_apps_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'apps')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700208 host_guest_map = ( (host_config_dir, guest_config_dir), )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700209 NAME = 'cord-onos'
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700210
A R Karthick41adfce2016-06-10 09:51:25 -0700211 def __init__(self, name = NAME, image = 'onosproject/onos', tag = 'latest',
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700212 boot_delay = 60, restart = False, network_cfg = None):
213 if restart is True:
214 ##Find the right image to restart
215 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
216 if running_image:
217 image_name = running_image[0]['Image']
218 try:
219 image = image_name.split(':')[0]
220 tag = image_name.split(':')[1]
221 except: pass
222
223 super(Onos, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config)
224 if restart is True and self.exists():
225 self.kill()
226 if not self.exists():
227 self.remove_container(name, force=True)
228 host_config = self.create_host_config(port_list = self.ports,
229 host_guest_map = self.host_guest_map)
230 volumes = []
231 for _,g in self.host_guest_map:
232 volumes.append(g)
233 if network_cfg is not None:
A R Karthick81acbff2016-06-17 14:45:16 -0700234 json_data = json.dumps(network_cfg, indent=4)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700235 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
236 f.write(json_data)
237 print('Starting ONOS container %s' %self.name)
A R Karthick41adfce2016-06-10 09:51:25 -0700238 self.start(ports = self.ports, environment = self.env,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700239 host_config = host_config, volumes = volumes, tty = True)
240 print('Waiting %d seconds for ONOS to boot' %(boot_delay))
241 time.sleep(boot_delay)
242
A.R Karthick95d044e2016-06-10 18:44:36 -0700243 self.install_cord_apps()
244
245 @classmethod
246 def install_cord_apps(cls):
247 for app, version in cls.onos_cord_apps:
248 app_file = '{}/{}-{}.oar'.format(cls.cord_apps_dir, app, version)
249 ok, code = OnosCtrl.install_app(app_file)
250 ##app already installed (conflicts)
251 if code in [ 409 ]:
252 ok = True
253 print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install'))
254 time.sleep(2)
255
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700256class Radius(Container):
257 ports = [ 1812, 1813 ]
A R Karthick41adfce2016-06-10 09:51:25 -0700258 env = {'TIMEZONE':'America/Los_Angeles',
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700259 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
260 }
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700261 host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700262 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700263 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700264 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700265 start_command = os.path.join(guest_config_dir, 'start-radius.py')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700266 host_guest_map = ( (host_db_dir, guest_db_dir),
267 (host_config_dir, guest_config_dir)
268 )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700269 IMAGE = 'cord-test/radius'
270 NAME = 'cord-radius'
271
272 def __init__(self, name = NAME, image = IMAGE, tag = 'latest',
273 boot_delay = 10, restart = False, update = False):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700274 super(Radius, self).__init__(name, image, tag = tag, command = self.start_command)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700275 if update is True or not self.img_exists():
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700276 self.build_image(image)
277 if restart is True and self.exists():
278 self.kill()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700279 if not self.exists():
280 self.remove_container(name, force=True)
281 host_config = self.create_host_config(port_list = self.ports,
282 host_guest_map = self.host_guest_map)
283 volumes = []
284 for _,g in self.host_guest_map:
285 volumes.append(g)
A R Karthick41adfce2016-06-10 09:51:25 -0700286 self.start(ports = self.ports, environment = self.env,
287 volumes = volumes,
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700288 host_config = host_config, tty = True)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700289 time.sleep(boot_delay)
290
291 @classmethod
292 def build_image(cls, image):
293 print('Building Radius image %s' %image)
294 dockerfile = '''
295FROM hbouvier/docker-radius
296MAINTAINER chetan@ciena.com
297LABEL RUN docker pull hbouvier/docker-radius
298LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius
A R Karthickc762df42016-05-25 10:09:21 -0700299RUN apt-get update && \
300 apt-get -y install python python-pexpect strace
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700301WORKDIR /root
302CMD ["/etc/freeradius/start-radius.py"]
303'''
304 super(Radius, cls).build_image(dockerfile, image)
305 print('Done building image %s' %image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700306
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700307class Quagga(Container):
A R Karthick41adfce2016-06-10 09:51:25 -0700308 quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 },
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700309 { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 },
310 )
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700311 ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ]
312 host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config')
313 guest_quagga_config = '/root/config'
314 quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
315 host_guest_map = ( (host_quagga_config, guest_quagga_config), )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700316 IMAGE = 'cord-test/quagga'
317 NAME = 'cord-quagga'
318
A R Karthick41adfce2016-06-10 09:51:25 -0700319 def __init__(self, name = NAME, image = IMAGE, tag = 'latest',
Chetan Gaonker503032a2016-05-12 12:06:29 -0700320 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700321 super(Quagga, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700322 if update is True or not self.img_exists():
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700323 self.build_image(image)
324 if restart is True and self.exists():
325 self.kill()
326 if not self.exists():
327 self.remove_container(name, force=True)
A R Karthick41adfce2016-06-10 09:51:25 -0700328 host_config = self.create_host_config(port_list = self.ports,
329 host_guest_map = self.host_guest_map,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700330 privileged = True)
331 volumes = []
332 for _,g in self.host_guest_map:
333 volumes.append(g)
334 self.start(ports = self.ports,
A R Karthick41adfce2016-06-10 09:51:25 -0700335 host_config = host_config,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700336 volumes = volumes, tty = True)
337 print('Starting Quagga on container %s' %self.name)
338 self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
339 time.sleep(boot_delay)
340
341 @classmethod
342 def build_image(cls, image):
Chetan Gaonker2a6601b2016-05-02 17:28:26 -0700343 onos_quagga_ip = Onos.quagga_config[0]['ip']
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700344 print('Building Quagga image %s' %image)
345 dockerfile = '''
A R Karthick41adfce2016-06-10 09:51:25 -0700346FROM ubuntu:14.04
347MAINTAINER chetan@ciena.com
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700348WORKDIR /root
349RUN useradd -M quagga
350RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga
351RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga
352RUN apt-get update && apt-get install -qy git autoconf libtool gawk make telnet libreadline6-dev
353RUN git clone git://git.sv.gnu.org/quagga.git quagga && \
354(cd quagga && git checkout HEAD && ./bootstrap.sh && \
355sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \
356./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install)
357RUN ldconfig
358'''.format(onos_quagga_ip)
359 super(Quagga, cls).build_image(dockerfile, image)
360 print('Done building image %s' %image)
A R Karthick81acbff2016-06-17 14:45:16 -0700361
362def reinitContainerClients():
363 docker_netns.dckr = Client()
364 Container.dckr = Client()