blob: 4d713d7b260699a94e0e16f272ab69600ec5d8ad [file] [log] [blame]
Chetan Gaonker3533faa2016-04-25 17:50:14 -07001import os,time
2import io
3import json
4from pyroute2 import IPRoute
5from itertools import chain
6from nsenter import Namespace
7from docker import Client
8from shutil import copy
9
10class docker_netns(object):
11
12 dckr = Client()
13 def __init__(self, name):
14 pid = int(self.dckr.inspect_container(name)['State']['Pid'])
15 if pid == 0:
16 raise Exception('no container named {0}'.format(name))
17 self.pid = pid
18
19 def __enter__(self):
20 pid = self.pid
21 if not os.path.exists('/var/run/netns'):
22 os.mkdir('/var/run/netns')
23 os.symlink('/proc/{0}/ns/net'.format(pid), '/var/run/netns/{0}'.format(pid))
24 return str(pid)
25
26 def __exit__(self, type, value, traceback):
27 pid = self.pid
28 os.unlink('/var/run/netns/{0}'.format(pid))
29
30flatten = lambda l: chain.from_iterable(l)
31
32class Container(object):
33 dckr = Client()
34 def __init__(self, name, image, tag = 'latest', command = 'bash', quagga_config = None):
35 self.name = name
36 self.image = image
37 self.tag = tag
38 self.image_name = image + ':' + tag
39 self.id = None
40 self.command = command
41 if quagga_config is not None:
42 self.bridge = quagga_config['bridge']
43 self.ipaddress = quagga_config['ip']
44 self.mask = quagga_config['mask']
45 else:
46 self.bridge = None
47 self.ipaddress = None
48 self.mask = None
49
50 @classmethod
51 def build_image(cls, dockerfile, tag, force=True, nocache=False):
52 f = io.BytesIO(dockerfile.encode('utf-8'))
53 if force or not cls.image_exists(tag):
54 print('Build {0}...'.format(tag))
55 for line in cls.dckr.build(fileobj=f, rm=True, tag=tag, decode=True, nocache=nocache):
56 if 'stream' in line:
57 print(line['stream'].strip())
58
59 @classmethod
60 def image_exists(cls, name):
61 return name in [ctn['RepoTags'][0] for ctn in cls.dckr.images()]
62
63 @classmethod
64 def create_host_config(cls, port_list = None, host_guest_map = None, privileged = False):
65 port_bindings = None
66 binds = None
67 if port_list:
68 port_bindings = {}
69 for p in port_list:
70 port_bindings[str(p)] = str(p)
71
72 if host_guest_map:
73 binds = []
74 for h, g in host_guest_map:
75 binds.append('{0}:{1}'.format(h, g))
76
77 return cls.dckr.create_host_config(binds = binds, port_bindings = port_bindings, privileged = privileged)
78
79 @classmethod
80 def cleanup(cls, image):
81 cnt_list = filter(lambda c: c['Image'] == image, cls.dckr.containers())
82 for cnt in cnt_list:
83 print('Cleaning container %s' %cnt['Id'])
84 cls.dckr.kill(cnt['Id'])
85 cls.dckr.remove_container(cnt['Id'], force=True)
86
87 @classmethod
88 def remove_container(cls, name, force=True):
89 try:
90 cls.dckr.remove_container(name, force = force)
91 except: pass
92
93 def exists(self):
94 return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers()))
95
96 def img_exists(self):
97 return self.image_name in [ctn['RepoTags'][0] for ctn in self.dckr.images()]
98
99 def ip(self):
100 cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
101 cnt_settings = cnt_list.pop()
102 return cnt_settings['NetworkSettings']['Networks']['bridge']['IPAddress']
103
104 def kill(self, remove = True):
105 self.dckr.kill(self.name)
106 self.dckr.remove_container(self.name, force=True)
107
108 def start(self, rm = True, ports = None, volumes = None, host_config = None,
109 environment = None, tty = False, stdin_open = True):
110
111 if rm and self.exists():
112 print('Removing container:', self.name)
113 self.dckr.remove_container(self.name, force=True)
114
115 ctn = self.dckr.create_container(image=self.image_name, ports = ports, command=self.command,
116 detach=True, name=self.name,
117 environment = environment,
118 volumes = volumes,
119 host_config = host_config, stdin_open=stdin_open, tty = tty)
120 self.dckr.start(container=self.name)
121 if self.bridge:
122 self.connect_to_br()
123 self.id = ctn['Id']
124 return ctn
125
126 def connect_to_br(self):
127 with docker_netns(self.name) as pid:
128 ip = IPRoute()
129 br = ip.link_lookup(ifname=self.bridge)
130 if len(br) == 0:
131 ip.link_create(ifname=self.bridge, kind='bridge')
132 br = ip.link_lookup(ifname=self.bridge)
133 br = br[0]
134 ip.link('set', index=br, state='up')
135
136 ifs = ip.link_lookup(ifname=self.name)
137 if len(ifs) > 0:
138 ip.link_remove(ifs[0])
139
140 ip.link_create(ifname=self.name, kind='veth', peer=pid)
141 host = ip.link_lookup(ifname=self.name)[0]
142 ip.link('set', index=host, master=br)
143 ip.link('set', index=host, state='up')
144 guest = ip.link_lookup(ifname=pid)[0]
145 ip.link('set', index=guest, net_ns_fd=pid)
146 with Namespace(pid, 'net'):
147 ip = IPRoute()
148 ip.link('set', index=guest, ifname='eth1')
149 ip.addr('add', index=guest, address=self.ipaddress, mask=self.mask)
150 ip.link('set', index=guest, state='up')
151
152 def execute(self, cmd, tty = True, stream = False, shell = False):
153 res = 0
154 if type(cmd) == str:
155 cmds = (cmd,)
156 else:
157 cmds = cmd
158 if shell:
159 for c in cmds:
160 res += os.system('docker exec {0} {1}'.format(self.name, c))
161 return res
162 for c in cmds:
163 i = self.dckr.exec_create(container=self.name, cmd=c, tty = tty, privileged = True)
164 self.dckr.exec_start(i['Id'], stream = stream, detach=True)
165 result = self.dckr.exec_inspect(i['Id'])
166 res += 0 if result['ExitCode'] == None else result['ExitCode']
167 return res
168
169class Onos(Container):
170
171 quagga_config = { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }
172 env = { 'ONOS_APPS' : 'drivers,openflow,proxyarp,aaa,igmp,vrouter,fwd' }
173 ports = [ 8181, 8101, 9876, 6653, 6633, 2000, 2620 ]
174 host_config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'setup/onos-config')
175 guest_config_dir = '/root/onos/config'
176 host_guest_map = ( (host_config_dir, guest_config_dir), )
177
178 def __init__(self, name = 'cord-onos', image = 'onosproject/onos', tag = 'latest',
179 boot_delay = 60, restart = False, network_cfg = None):
180 if restart is True:
181 ##Find the right image to restart
182 running_image = filter(lambda c: c['Names'][0] == '/{}'.format(name), self.dckr.containers())
183 if running_image:
184 image_name = running_image[0]['Image']
185 try:
186 image = image_name.split(':')[0]
187 tag = image_name.split(':')[1]
188 except: pass
189
190 super(Onos, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config)
191 if restart is True and self.exists():
192 self.kill()
193 if not self.exists():
194 self.remove_container(name, force=True)
195 host_config = self.create_host_config(port_list = self.ports,
196 host_guest_map = self.host_guest_map)
197 volumes = []
198 for _,g in self.host_guest_map:
199 volumes.append(g)
200 if network_cfg is not None:
201 json_data = json.dumps(network_cfg)
202 with open('{}/network-cfg.json'.format(self.host_config_dir), 'w') as f:
203 f.write(json_data)
204 print('Starting ONOS container %s' %self.name)
205 self.start(ports = self.ports, environment = self.env,
206 host_config = host_config, volumes = volumes, tty = True)
207 print('Waiting %d seconds for ONOS to boot' %(boot_delay))
208 time.sleep(boot_delay)
209
210class Radius(Container):
211 ports = [ 1812, 1813 ]
212 env = {'TIMEZONE':'America/Los_Angeles',
213 'DEBUG': 'true', 'cert_password':'whatever', 'primary_shared_secret':'radius_password'
214 }
215 host_db_dir = os.path.join(os.getenv('HOME'), 'services', 'radius', 'data', 'db')
216 guest_db_dir = os.path.join(os.path.sep, 'opt', 'db')
217 host_config_dir = os.path.join(os.getenv('HOME'), 'services', 'radius', 'freeradius')
218 guest_config_dir = os.path.join(os.path.sep, 'etc', 'freeradius')
219 start_command = '/root/start-radius.py'
220 host_guest_map = ( (host_db_dir, guest_db_dir),
221 (host_config_dir, guest_config_dir)
222 )
223 def __init__(self, name = 'cord-radius', image = 'freeradius', tag = 'podd'):
224 super(Radius, self).__init__(name, image, tag = tag, command = self.start_command)
225 if not self.exists():
226 self.remove_container(name, force=True)
227 host_config = self.create_host_config(port_list = self.ports,
228 host_guest_map = self.host_guest_map)
229 volumes = []
230 for _,g in self.host_guest_map:
231 volumes.append(g)
232 self.start(ports = self.ports, environment = self.env,
233 volumes = volumes,
234 host_config = host_config, tty = True)
235