1 # vim: set encoding=utf-8 et sw=4 sts=4 :
3 from subprocess import Popen, PIPE
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
13 __ALL__ = ('IpHandler',)
15 class Route(Sequence):
16 def __init__(self, net_addr, prefix, gateway):
17 self.net_addr = net_addr
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
25 return(self.net_addr, self.prefix, self.gateway)
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
33 class Address(Sequence):
34 def __init__(self, ip, netmask, broadcast=None):
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
42 return (self.ip, self.netmask, self.broadcast)
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
50 class Device(Sequence):
51 def __init__(self, name, mac):
57 return (self.name, self.mac)
59 class DeviceHandler(SubHandler):
61 handler_help = u"Manage network devices"
63 def __init__(self, parent):
64 # FIXME remove templates to execute commands
65 from mako.template import Template
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)
71 @handler(u'Bring the device up')
73 if name in self.parent.devices:
74 call(self.device_template.render(dev=name, action='up'), shell=True)
76 raise DeviceNotFoundError(name)
78 @handler(u'Bring the device down')
80 if name in self.parent.devices:
81 call(self.device_template.render(dev=name, action='down'), shell=True)
83 raise DeviceNotFoundError(name)
85 @handler(u'List all devices')
87 return self.parent.devices.keys()
89 @handler(u'Get information about a device')
91 return self.parent.devices.items()
93 class IpHandler(Restorable, ConfigWriter, TransactionalHandler):
95 handler_help = u"Manage IP devices, addresses and routes"
97 _persistent_attrs = 'devices'
99 _restorable_defaults = dict(devices=dict((dev, Device(dev, mac))
100 for (dev, mac) in get_network_devices().items()))
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')
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()
112 self.addr = AddressHandler(self)
113 self.route = RouteHandler(self)
114 self.dev = DeviceHandler(self)
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(
125 netmask = address.netmask,
126 broadcast = address.broadcast,
129 for route in device.routes:
130 call(self._render_config('route_add', dict(
132 net_addr = route.net_addr,
133 prefix = route.prefix,
134 gateway = route.gateway,
139 if __name__ == '__main__':
142 print '----------------------'
144 ip.addr.add('eth0','192.168.0.23','24','192.168.255.255')
145 ip.addr.add('eth0','192.168.0.26','24')
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')
150 ip.route.clear('eth0')
152 ip.addr.delete('eth0','192.168.0.23')