]> git.llucax.com Git - software/pymin.git/blob - pymin/services/ip/__init__.py
Remove unused error classes.
[software/pymin.git] / pymin / services / ip / __init__.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from subprocess import Popen, PIPE
4 from os import path
5
6 from pymin.seqtools import Sequence
7 from pymin.dispatcher import handler, HandlerError, Handler
8 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
9                                 TransactionalHandler, SubHandler, call, \
10                                 get_network_devices, ListComposedSubHandler, \
11                                 DictComposedSubHandler
12
13 __ALL__ = ('IpHandler',)
14
15 class Route(Sequence):
16     def __init__(self, net_addr, prefix, gateway):
17         self.net_addr = net_addr
18         self.prefix = prefix
19         self.gateway = gateway
20     def update(self, net_addr=None, prefix=None, gateway=None):
21         if net_addr is not None: self.net_addr = net_addr
22         if prefix is not None: self.prefix = prefix
23         if gateway is not None: self.gateway = gateway
24     def as_tuple(self):
25         return(self.net_addr, self.prefix, self.gateway)
26
27 class RouteHandler(ListComposedSubHandler):
28     handler_help = u"Manage IP routes"
29     _comp_subhandler_cont = 'devices'
30     _comp_subhandler_attr = 'routes'
31     _comp_subhandler_class = Route
32
33 class Address(Sequence):
34     def __init__(self, ip, netmask, broadcast=None):
35         self.ip = ip
36         self.netmask = netmask
37         self.broadcast = broadcast
38     def update(self, netmask=None, broadcast=None):
39         if netmask is not None: self.netmask = netmask
40         if broadcast is not None: self.broadcast = broadcast
41     def as_tuple(self):
42         return (self.ip, self.netmask, self.broadcast)
43
44 class AddressHandler(DictComposedSubHandler):
45     handler_help = u"Manage IP addresses"
46     _comp_subhandler_cont = 'devices'
47     _comp_subhandler_attr = 'addrs'
48     _comp_subhandler_class = Address
49
50 class Device(Sequence):
51     def __init__(self, name, mac):
52         self.name = name
53         self.mac = mac
54         self.addrs = dict()
55         self.routes = list()
56     def as_tuple(self):
57         return (self.name, self.mac)
58
59 class DeviceHandler(SubHandler):
60
61     handler_help = u"Manage network devices"
62
63     def __init__(self, parent):
64         # FIXME remove templates to execute commands
65         from mako.template import Template
66         self.parent = parent
67         template_dir = path.join(path.dirname(__file__), 'templates')
68         dev_fn = path.join(template_dir, 'device')
69         self.device_template = Template(filename=dev_fn)
70
71     @handler(u'Bring the device up')
72     def up(self, name):
73         if name in self.parent.devices:
74             call(self.device_template.render(dev=name, action='up'), shell=True)
75         else:
76             raise DeviceNotFoundError(name)
77
78     @handler(u'Bring the device down')
79     def down(self, name):
80         if name in self.parent.devices:
81             call(self.device_template.render(dev=name, action='down'), shell=True)
82         else:
83             raise DeviceNotFoundError(name)
84
85     @handler(u'List all devices')
86     def list(self):
87         return self.parent.devices.keys()
88
89     @handler(u'Get information about a device')
90     def show(self):
91         return self.parent.devices.items()
92
93 class IpHandler(Restorable, ConfigWriter, TransactionalHandler):
94
95     handler_help = u"Manage IP devices, addresses and routes"
96
97     _persistent_attrs = 'devices'
98
99     _restorable_defaults = dict(devices=dict((dev, Device(dev, mac))
100                             for (dev, mac) in get_network_devices().items()))
101
102     _config_writer_files = ('device', 'ip_add', 'ip_del', 'ip_flush',
103                             'route_add', 'route_del', 'route_flush')
104     _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
105
106     def __init__(self, pickle_dir='.', config_dir='.'):
107         r"Initialize DhcpHandler object, see class documentation for details."
108         self._persistent_dir = pickle_dir
109         self._config_writer_cfg_dir = config_dir
110         self._config_build_templates()
111         self._restore()
112         self.addr = AddressHandler(self)
113         self.route = RouteHandler(self)
114         self.dev = DeviceHandler(self)
115
116     def _write_config(self):
117         r"_write_config() -> None :: Execute all commands."
118         for device in self.devices.values():
119             call(self._render_config('route_flush', dict(dev=device.name)), shell=True)
120             call(self._render_config('ip_flush', dict(dev=device.name)), shell=True)
121             for address in device.addrs.values():
122                 call(self._render_config('ip_add', dict(
123                         dev = device.name,
124                         addr = address.ip,
125                         netmask = address.netmask,
126                         broadcast = address.broadcast,
127                     )
128                 ), shell=True)
129             for route in device.routes:
130                 call(self._render_config('route_add', dict(
131                         dev = device.name,
132                         net_addr = route.net_addr,
133                         prefix = route.prefix,
134                         gateway = route.gateway,
135                     )
136                 ), shell=True)
137
138
139 if __name__ == '__main__':
140
141     ip = IpHandler()
142     print '----------------------'
143     ip.dev.up('eth0')
144     ip.addr.add('eth0','192.168.0.23','24','192.168.255.255')
145     ip.addr.add('eth0','192.168.0.26','24')
146     ip.commit()
147     ip.route.add('eth0','192.168.0.0','24','192.168.0.1')
148     ip.route.add('eth0','192.168.0.5','24','192.168.0.1')
149     ip.commit()
150     ip.route.clear('eth0')
151     ip.commit()
152     ip.addr.delete('eth0','192.168.0.23')
153     ip.commit()
154
155
156
157