blob: 8ad4c2ebed42b7c4d71d50b3afc2cacea0997ae9 [file] [log] [blame]
Chetan Gaonkercfcce782016-05-10 10:10:42 -07001#
2# 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
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#
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
24
25class docker_netns(object):
26
27 dckr = Client()
28 def __init__(self, name):
29 pid = int(self.dckr.inspect_container(name)['State']['Pid'])
30 if pid == 0:
31 raise Exception('no container named {0}'.format(name))
32 self.pid = pid
33
34 def __enter__(self):
35 pid = self.pid
36 if not os.path.exists('/var/run/netns'):
37 os.mkdir('/var/run/netns')
38 os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid))
39 return str(pid)
40
41 def __exit__(self, type, value, traceback):
42 pid = self.pid
43 os.unlink('/var/run/netns/{0}'.format(pid))
44
45flatten = lambda l: chain.from_iterable(l)
46
47class Container(object):
48 dckr = Client()
49 def __init__(self, name, image, tag = 'latest', command = 'bash', quagga_config = None):
50 self.name = name
51 self.image = image
52 self.tag = tag
53 self.image_name = image + ':' + tag
54 self.id = None
55 self.command = command
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -070056 self.quagga_config = quagga_config
Chetan Gaonker3533faa2016-04-25 17:50:14 -070057
58 @classmethod
59 def build_image(cls, dockerfile, tag, force=True, nocache=False):
60 f = io.BytesIO(dockerfile.encode('utf-8'))
61 if force or not cls.image_exists(tag):
62 print('Build {0}...'.format(tag))
63 for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache):
64 if 'stream' in line:
65 print(line['stream'].strip())
66
67 @classmethod
68 def image_exists(cls, name):
69 return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()]
70
71 @classmethod
72 def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False):
73 port_bindings = None
74 binds = None
75 if port_list:
76 port_bindings = {}
77 for p in port_list:
78 port_bindings[str(p)] = str(p)
79
80 if host_guest_map:
81 binds = []
82 for h, g in host_guest_map:
83 binds.append('{0}:{1}'.format(h, g))
84
85 return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged)
86
87 @classmethod
88 def cleanup(cls, image):
A R Karthick09b1f4e2016-05-12 14:31:50 -070089 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers(all=True))
Chetan Gaonker3533faa2016-04-25 17:50:14 -070090 for cnt in cnt_list:
91 print('Cleaning container %s' %cnt['Id'])
A R Karthick09b1f4e2016-05-12 14:31:50 -070092 if cnt['State'] == 'running':
93 cls.dckr.kill(cnt['Id'])
Chetan Gaonker3533faa2016-04-25 17:50:14 -070094 cls.dckr.remove_container(cnt['Id'], force=True)
95
96 @classmethod
97 def remove_container(cls, name, force=True):
98 try:
99 cls.dckr.remove_container(name, force = force)
100 except: pass
101
102 def exists(self):
103 return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers()))
104
105 def img_exists(self):
106 return self.image_name in [ctn['RepoTags'][0] for ctn in self.dckr.images()]
107
108 def ip(self):
109 cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
110 cnt_settings = cnt_list.pop()
111 return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress']
112
113 def kill(self, remove = True):
114 self.dckr.kill(self.name)
115 self.dckr.remove_container(self.name, force=True)
116
117 def start(self, rm = True, ports = None, volumes = None, host_config = None,
118 environment = None, tty = False, stdin_open = True):
119
120 if rm and self.exists():
121 print('Removing container:', self.name)
122 self.dckr.remove_container(self.name, force=True)
123
124 ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command,
125 detach=True, name=self.name,
126 environment = environment,
127 volumes = volumes,
128 host_config = host_config, stdin_open=stdin_open, tty = tty)
129 self.dckr.start(container=self.name)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700130 if self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700131 self.connect_to_br()
132 self.id = ctn['Id']
133 return ctn
134
135 def connect_to_br(self):
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700136 index = 0
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700137 with docker_netns(self.name) as pid:
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700138 for quagga_config in self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700139 ip = IPRoute()
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700140 br = ip.link_lookup(ifname=quagga_config['bridge'])
141 if len(br) == 0:
Chetan Gaonker5a0fda32016-05-10 14:09:07 -0700142 ip.link_create(ifname=quagga_config['bridge'], kind='bridge')
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700143 br = ip.link_lookup(ifname=quagga_config['bridge'])
144 br = br[0]
145 ip.link('set', index=br, state='up')
146 ifname = '{0}-{1}'.format(self.name, index)
147 ifs = ip.link_lookup(ifname=ifname)
148 if len(ifs) > 0:
149 ip.link_remove(ifs[0])
150 peer_ifname = '{0}-{1}'.format(pid, index)
Chetan Gaonker5a0fda32016-05-10 14:09:07 -0700151 ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700152 host = ip.link_lookup(ifname=ifname)[0]
153 ip.link('set', index=host, master=br)
154 ip.link('set', index=host, state='up')
155 guest = ip.link_lookup(ifname=peer_ifname)[0]
156 ip.link('set', index=guest, net_ns_fd=pid)
157 with Namespace(pid, 'net'):
158 ip = IPRoute()
159 ip.link('set', index=guest, ifname='eth{}'.format(index+1))
160 ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask'])
161 ip.link('set', index=guest, state='up')
162 index += 1
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700163
164 def execute(self, cmd, tty = True, stream = False, shell = False):
165 res = 0
166 if type(cmd) == str:
167 cmds = (cmd,)
168 else:
169 cmds = cmd
170 if shell:
171 for c in cmds:
172 res += os.system('docker exec {0} {1}'.format(self.name, c))
173 return res
174 for c in cmds:
175 i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True)
176 self.dckr.exec_start(i['Id'], stream = stream, detach=True)
177 result = self.dckr.exec_inspect(i['Id'])
178 res += 0 if result['ExitCode'] == None else result['ExitCode']
179 return res
180
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700181def get_mem():
182 with open('/proc/meminfo', 'r') as fd:
183 meminfo = fd.readlines()
184 mem = 0
185 for m in meminfo:
186 if m.startswith('MemTotal:') or m.startswith('SwapTotal:'):
187 mem += int(m.split(':')[1].strip().split()[0])
188
Chetan Gaonkerc0421e82016-05-04 17:23:08 -0700189 mem = max(mem/1024/1024/2, 1)
Chetan Gaonker6d0a7b02016-05-03 16:57:28 -0700190 mem = min(mem, 16)
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700191 return str(mem) + 'G'
192
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700193class Onos(Container):
194
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700195 quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, )
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700196 SYSTEM_MEMORY = (get_mem(),) * 2
197 JAVA_OPTS = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'.format(*SYSTEM_MEMORY)#-XX:+PrintGCDetails -XX:+PrintGCTimeStamps'
Chetan Gaonker68d95172016-05-03 11:16:59 -0700198 env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,aaa,igmp,vrouter', 'JAVA_OPTS' : JAVA_OPTS }
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700199 ports = [ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ]
200 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/onos-config')
201 guest_config_dir = '/root/onos/config'
202 host_guest_map = ( (host_config_dir, guest_config_dir), )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700203 NAME = 'cord-onos'
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700204
Chetan Gaonker503032a2016-05-12 12:06:29 -0700205 def __init__(self, name = NAME, image = 'onosproject/onos', tag = 'latest',
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700206 boot_delay = 60, restart = False, network_cfg = None):
207 if restart is True:
208 ##Find the right image to restart
209 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
210 if running_image:
211 image_name = running_image[0]['Image']
212 try:
213 image = image_name.split(':')[0]
214 tag = image_name.split(':')[1]
215 except: pass
216
217 super(Onos, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config)
218 if restart is True and self.exists():
219 self.kill()
220 if not self.exists():
221 self.remove_container(name, force=True)
222 host_config = self.create_host_config(port_list = self.ports,
223 host_guest_map = self.host_guest_map)
224 volumes = []
225 for _,g in self.host_guest_map:
226 volumes.append(g)
227 if network_cfg is not None:
228 json_data = json.dumps(network_cfg)
229 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
230 f.write(json_data)
231 print('Starting ONOS container %s' %self.name)
232 self.start(ports = self.ports, environment = self.env,
233 host_config = host_config, volumes = volumes, tty = True)
234 print('Waiting %d seconds for ONOS to boot' %(boot_delay))
235 time.sleep(boot_delay)
236
237class Radius(Container):
238 ports = [ 1812, 1813 ]
239 env = {'TIMEZONE':'America/Los_Angeles',
240 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
241 }
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700242 host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700243 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700244 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700245 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700246 start_command = os.path.join(guest_config_dir, 'start-radius.py')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700247 host_guest_map = ( (host_db_dir, guest_db_dir),
248 (host_config_dir, guest_config_dir)
249 )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700250 IMAGE = 'cord-test/radius'
251 NAME = 'cord-radius'
252
253 def __init__(self, name = NAME, image = IMAGE, tag = 'latest',
254 boot_delay = 10, restart = False, update = False):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700255 super(Radius, self).__init__(name, image, tag = tag, command = self.start_command)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700256 if update is True or not self.img_exists():
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700257 self.build_image(image)
258 if restart is True and self.exists():
259 self.kill()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700260 if not self.exists():
261 self.remove_container(name, force=True)
262 host_config = self.create_host_config(port_list = self.ports,
263 host_guest_map = self.host_guest_map)
264 volumes = []
265 for _,g in self.host_guest_map:
266 volumes.append(g)
267 self.start(ports = self.ports, environment = self.env,
268 volumes = volumes,
269 host_config = host_config, tty = True)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700270 time.sleep(boot_delay)
271
272 @classmethod
273 def build_image(cls, image):
274 print('Building Radius image %s' %image)
275 dockerfile = '''
276FROM hbouvier/docker-radius
277MAINTAINER chetan@ciena.com
278LABEL RUN docker pull hbouvier/docker-radius
279LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius
280RUN apt-get update
281RUN apt-get -y install python python-pexpect strace
282WORKDIR /root
283CMD ["/etc/freeradius/start-radius.py"]
284'''
285 super(Radius, cls).build_image(dockerfile, image)
286 print('Done building image %s' %image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700287
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700288class Quagga(Container):
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700289 quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 },
290 { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 },
291 )
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700292 ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ]
293 host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config')
294 guest_quagga_config = '/root/config'
295 quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
296 host_guest_map = ( (host_quagga_config, guest_quagga_config), )
Chetan Gaonker503032a2016-05-12 12:06:29 -0700297 IMAGE = 'cord-test/quagga'
298 NAME = 'cord-quagga'
299
300 def __init__(self, name = NAME, image = IMAGE, tag = 'latest',
301 boot_delay = 15, restart = False, config_file = quagga_config_file, update = False):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700302 super(Quagga, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config)
Chetan Gaonker503032a2016-05-12 12:06:29 -0700303 if update is True or not self.img_exists():
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700304 self.build_image(image)
305 if restart is True and self.exists():
306 self.kill()
307 if not self.exists():
308 self.remove_container(name, force=True)
309 host_config = self.create_host_config(port_list = self.ports,
310 host_guest_map = self.host_guest_map,
311 privileged = True)
312 volumes = []
313 for _,g in self.host_guest_map:
314 volumes.append(g)
315 self.start(ports = self.ports,
316 host_config = host_config,
317 volumes = volumes, tty = True)
318 print('Starting Quagga on container %s' %self.name)
319 self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
320 time.sleep(boot_delay)
321
322 @classmethod
323 def build_image(cls, image):
Chetan Gaonker2a6601b2016-05-02 17:28:26 -0700324 onos_quagga_ip = Onos.quagga_config[0]['ip']
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700325 print('Building Quagga image %s' %image)
326 dockerfile = '''
327FROM ubuntu:latest
328WORKDIR /root
329RUN useradd -M quagga
330RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga
331RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga
332RUN apt-get update && apt-get install -qy git autoconf libtool gawk make telnet libreadline6-dev
333RUN git clone git://git.sv.gnu.org/quagga.git quagga && \
334(cd quagga && git checkout HEAD && ./bootstrap.sh && \
335sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \
336./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install)
337RUN ldconfig
338'''.format(onos_quagga_ip)
339 super(Quagga, cls).build_image(dockerfile, image)
340 print('Done building image %s' %image)
341