blob: c04f219b0ab7f912819a06561738fae081444046 [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):
89 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers())
90 for cnt in cnt_list:
91 print('Cleaning container %s' %cnt['Id'])
92 cls.dckr.kill(cnt['Id'])
93 cls.dckr.remove_container(cnt['Id'], force=True)
94
95 @classmethod
96 def remove_container(cls, name, force=True):
97 try:
98 cls.dckr.remove_container(name, force = force)
99 except: pass
100
101 def exists(self):
102 return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers()))
103
104 def img_exists(self):
105 return self.image_name in [ctn['RepoTags'][0] for ctn in self.dckr.images()]
106
107 def ip(self):
108 cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
109 cnt_settings = cnt_list.pop()
110 return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress']
111
112 def kill(self, remove = True):
113 self.dckr.kill(self.name)
114 self.dckr.remove_container(self.name, force=True)
115
116 def start(self, rm = True, ports = None, volumes = None, host_config = None,
117 environment = None, tty = False, stdin_open = True):
118
119 if rm and self.exists():
120 print('Removing container:', self.name)
121 self.dckr.remove_container(self.name, force=True)
122
123 ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command,
124 detach=True, name=self.name,
125 environment = environment,
126 volumes = volumes,
127 host_config = host_config, stdin_open=stdin_open, tty = tty)
128 self.dckr.start(container=self.name)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700129 if self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700130 self.connect_to_br()
131 self.id = ctn['Id']
132 return ctn
133
134 def connect_to_br(self):
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700135 index = 0
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700136 with docker_netns(self.name) as pid:
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700137 for quagga_config in self.quagga_config:
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700138 ip = IPRoute()
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700139 br = ip.link_lookup(ifname=quagga_config['bridge'])
140 if len(br) == 0:
Chetan Gaonker5a0fda32016-05-10 14:09:07 -0700141 ip.link_create(ifname=quagga_config['bridge'], kind='bridge')
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700142 br = ip.link_lookup(ifname=quagga_config['bridge'])
143 br = br[0]
144 ip.link('set', index=br, state='up')
145 ifname = '{0}-{1}'.format(self.name, index)
146 ifs = ip.link_lookup(ifname=ifname)
147 if len(ifs) > 0:
148 ip.link_remove(ifs[0])
149 peer_ifname = '{0}-{1}'.format(pid, index)
Chetan Gaonker5a0fda32016-05-10 14:09:07 -0700150 ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname)
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700151 host = ip.link_lookup(ifname=ifname)[0]
152 ip.link('set', index=host, master=br)
153 ip.link('set', index=host, state='up')
154 guest = ip.link_lookup(ifname=peer_ifname)[0]
155 ip.link('set', index=guest, net_ns_fd=pid)
156 with Namespace(pid, 'net'):
157 ip = IPRoute()
158 ip.link('set', index=guest, ifname='eth{}'.format(index+1))
159 ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask'])
160 ip.link('set', index=guest, state='up')
161 index += 1
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700162
163 def execute(self, cmd, tty = True, stream = False, shell = False):
164 res = 0
165 if type(cmd) == str:
166 cmds = (cmd,)
167 else:
168 cmds = cmd
169 if shell:
170 for c in cmds:
171 res += os.system('docker exec {0} {1}'.format(self.name, c))
172 return res
173 for c in cmds:
174 i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True)
175 self.dckr.exec_start(i['Id'], stream = stream, detach=True)
176 result = self.dckr.exec_inspect(i['Id'])
177 res += 0 if result['ExitCode'] == None else result['ExitCode']
178 return res
179
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700180def get_mem():
181 with open('/proc/meminfo', 'r') as fd:
182 meminfo = fd.readlines()
183 mem = 0
184 for m in meminfo:
185 if m.startswith('MemTotal:') or m.startswith('SwapTotal:'):
186 mem += int(m.split(':')[1].strip().split()[0])
187
Chetan Gaonkerc0421e82016-05-04 17:23:08 -0700188 mem = max(mem/1024/1024/2, 1)
Chetan Gaonker6d0a7b02016-05-03 16:57:28 -0700189 mem = min(mem, 16)
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700190 return str(mem) + 'G'
191
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700192class Onos(Container):
193
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700194 quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, )
Chetan Gaonker462d9fa2016-05-03 16:39:10 -0700195 SYSTEM_MEMORY = (get_mem(),) * 2
196 JAVA_OPTS = '-Xms{} -Xmx{} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode'.format(*SYSTEM_MEMORY)#-XX:+PrintGCDetails -XX:+PrintGCTimeStamps'
Chetan Gaonker68d95172016-05-03 11:16:59 -0700197 env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,aaa,igmp,vrouter', 'JAVA_OPTS' : JAVA_OPTS }
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700198 ports = [ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ]
199 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/onos-config')
200 guest_config_dir = '/root/onos/config'
201 host_guest_map = ( (host_config_dir, guest_config_dir), )
202
203 def __init__(self, name = 'cord-onos', image = 'onosproject/onos', tag = 'latest',
204 boot_delay = 60, restart = False, network_cfg = None):
205 if restart is True:
206 ##Find the right image to restart
207 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
208 if running_image:
209 image_name = running_image[0]['Image']
210 try:
211 image = image_name.split(':')[0]
212 tag = image_name.split(':')[1]
213 except: pass
214
215 super(Onos, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config)
216 if restart is True and self.exists():
217 self.kill()
218 if not self.exists():
219 self.remove_container(name, force=True)
220 host_config = self.create_host_config(port_list = self.ports,
221 host_guest_map = self.host_guest_map)
222 volumes = []
223 for _,g in self.host_guest_map:
224 volumes.append(g)
225 if network_cfg is not None:
226 json_data = json.dumps(network_cfg)
227 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
228 f.write(json_data)
229 print('Starting ONOS container %s' %self.name)
230 self.start(ports = self.ports, environment = self.env,
231 host_config = host_config, volumes = volumes, tty = True)
232 print('Waiting %d seconds for ONOS to boot' %(boot_delay))
233 time.sleep(boot_delay)
234
235class Radius(Container):
236 ports = [ 1812, 1813 ]
237 env = {'TIMEZONE':'America/Los_Angeles',
238 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
239 }
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700240 host_db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/db')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700241 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700242 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/radius-config/freeradius')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700243 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700244 start_command = os.path.join(guest_config_dir, 'start-radius.py')
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700245 host_guest_map = ( (host_db_dir, guest_db_dir),
246 (host_config_dir, guest_config_dir)
247 )
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700248 def __init__(self, name = 'cord-radius', image = 'cord-test/radius', tag = 'latest',
249 boot_delay = 10, restart = False):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700250 super(Radius, self).__init__(name, image, tag = tag, command = self.start_command)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700251 if not self.img_exists():
252 self.build_image(image)
253 if restart is True and self.exists():
254 self.kill()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700255 if not self.exists():
256 self.remove_container(name, force=True)
257 host_config = self.create_host_config(port_list = self.ports,
258 host_guest_map = self.host_guest_map)
259 volumes = []
260 for _,g in self.host_guest_map:
261 volumes.append(g)
262 self.start(ports = self.ports, environment = self.env,
263 volumes = volumes,
264 host_config = host_config, tty = True)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700265 time.sleep(boot_delay)
266
267 @classmethod
268 def build_image(cls, image):
269 print('Building Radius image %s' %image)
270 dockerfile = '''
271FROM hbouvier/docker-radius
272MAINTAINER chetan@ciena.com
273LABEL RUN docker pull hbouvier/docker-radius
274LABEL RUN docker run -it --name cord-radius hbouvier/docker-radius
275RUN apt-get update
276RUN apt-get -y install python python-pexpect strace
277WORKDIR /root
278CMD ["/etc/freeradius/start-radius.py"]
279'''
280 super(Radius, cls).build_image(dockerfile, image)
281 print('Done building image %s' %image)
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700282
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700283class Quagga(Container):
Chetan Gaonker8e25e1b2016-05-02 13:42:21 -0700284 quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.3', 'mask' : 16 },
285 { 'bridge' : 'quagga-br', 'ip': '192.168.10.3', 'mask': 16 },
286 )
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700287 ports = [ 179, 2601, 2602, 2603, 2604, 2605, 2606 ]
288 host_quagga_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/quagga-config')
289 guest_quagga_config = '/root/config'
290 quagga_config_file = os.path.join(guest_quagga_config, 'testrib.conf')
291 host_guest_map = ( (host_quagga_config, guest_quagga_config), )
292
293 def __init__(self, name = 'cord-quagga', image = 'cord-test/quagga', tag = 'latest',
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -0700294 boot_delay = 15, restart = False, config_file = quagga_config_file):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700295 super(Quagga, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config)
296 if not self.img_exists():
297 self.build_image(image)
298 if restart is True and self.exists():
299 self.kill()
300 if not self.exists():
301 self.remove_container(name, force=True)
302 host_config = self.create_host_config(port_list = self.ports,
303 host_guest_map = self.host_guest_map,
304 privileged = True)
305 volumes = []
306 for _,g in self.host_guest_map:
307 volumes.append(g)
308 self.start(ports = self.ports,
309 host_config = host_config,
310 volumes = volumes, tty = True)
311 print('Starting Quagga on container %s' %self.name)
312 self.execute('{0}/start.sh {1}'.format(self.guest_quagga_config, config_file))
313 time.sleep(boot_delay)
314
315 @classmethod
316 def build_image(cls, image):
Chetan Gaonker2a6601b2016-05-02 17:28:26 -0700317 onos_quagga_ip = Onos.quagga_config[0]['ip']
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700318 print('Building Quagga image %s' %image)
319 dockerfile = '''
320FROM ubuntu:latest
321WORKDIR /root
322RUN useradd -M quagga
323RUN mkdir /var/log/quagga && chown quagga:quagga /var/log/quagga
324RUN mkdir /var/run/quagga && chown quagga:quagga /var/run/quagga
325RUN apt-get update && apt-get install -qy git autoconf libtool gawk make telnet libreadline6-dev
326RUN git clone git://git.sv.gnu.org/quagga.git quagga && \
327(cd quagga && git checkout HEAD && ./bootstrap.sh && \
328sed -i -r 's,htonl.*?\(INADDR_LOOPBACK\),inet_addr\("{0}"\),g' zebra/zebra_fpm.c && \
329./configure --enable-fpm --disable-doc --localstatedir=/var/run/quagga && make && make install)
330RUN ldconfig
331'''.format(onos_quagga_ip)
332 super(Quagga, cls).build_image(dockerfile, image)
333 print('Done building image %s' %image)
334