]> git.llucax.com Git - software/pymin.git/commitdiff
Move Device and Address to services.util and add ppp peer IPs.
authorLeandro Lucarella <llucarella@integratech.com.ar>
Mon, 5 Nov 2007 14:30:16 +0000 (11:30 -0300)
committerLeandro Lucarella <llucarella@integratech.com.ar>
Mon, 5 Nov 2007 14:33:14 +0000 (11:33 -0300)
pymin/services/ip/__init__.py
pymin/services/ip/templates/ip_add
pymin/services/util.py

index a6a0e676debb17b3aecb149e236181c2792daf72..881f6958acc98564a4c2b23c3f547e9f30da2dd2 100644 (file)
@@ -8,7 +8,7 @@ from pymin.dispatcher import handler, HandlerError, Handler
 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
                                 TransactionalHandler, SubHandler, call, \
                                 get_network_devices, ListComposedSubHandler, \
-                                DictComposedSubHandler
+                                DictComposedSubHandler, Device, Address, ExecutionError
 
 __ALL__ = ('IpHandler',)
 
@@ -105,13 +105,6 @@ class RouteHandler(ListComposedSubHandler):
     _comp_subhandler_attr = 'routes'
     _comp_subhandler_class = Route
 
-class Address(Sequence):
-    def __init__(self, ip, netmask, broadcast=None):
-        self.ip = ip
-        self.netmask = netmask
-        self.broadcast = broadcast
-    def as_tuple(self):
-        return (self.ip, self.netmask, self.broadcast)
 
 class AddressHandler(DictComposedSubHandler):
     handler_help = u"Manage IP addresses"
@@ -119,14 +112,6 @@ class AddressHandler(DictComposedSubHandler):
     _comp_subhandler_attr = 'addrs'
     _comp_subhandler_class = Address
 
-class Device(Sequence):
-    def __init__(self, name, mac):
-        self.name = name
-        self.mac = mac
-        self.addrs = dict()
-        self.routes = list()
-    def as_tuple(self):
-        return (self.name, self.mac)
 
 class DeviceHandler(SubHandler):
 
@@ -169,8 +154,7 @@ class IpHandler(Restorable, ConfigWriter, TransactionalHandler):
     _persistent_attrs = ('devices','hops')
 
     _restorable_defaults = dict(
-                            devices=dict((dev, Device(dev, mac))
-                                for (dev, mac) in get_network_devices().items()),
+                            devices=get_network_devices(),
                             hops = list()
                             )
 
@@ -203,6 +187,7 @@ class IpHandler(Restorable, ConfigWriter, TransactionalHandler):
                         dev = device.name,
                         addr = address.ip,
                         netmask = address.netmask,
+                        peer = address.peer,
                         broadcast = broadcast,
                     )
                 ), shell=True)
@@ -232,7 +217,7 @@ class IpHandler(Restorable, ConfigWriter, TransactionalHandler):
         #add not registered devices
         for k,v in devices.items():
             if k not in self.devices:
-                self.devices[k] = Device(k,v)
+                self.devices[k] = v
         #delete dead devices
         for k in self.devices.keys():
             if k not in devices:
index d6307b31b6cc90b62085b6dade97d8d93ff606f0..8f5cc15bd062e6b6db07c33f90d5e4dc1ed62bbb 100644 (file)
@@ -1 +1,5 @@
-ip addr add dev ${dev} ${addr}/${netmask} broadcast ${broadcast}
\ No newline at end of file
+%if peer is None:
+ip addr add dev ${dev} ${addr}/${netmask} broadcast ${broadcast}
+%else:
+ip addr add dev ${dev} ${addr} peer ${peer}
+%endif
index 18b7fefc84c4db3511f75f85b3d88a58c1a4c149..91d28c3213830bd5064d5c4705bd2a449088e831 100644 (file)
@@ -11,6 +11,7 @@ except ImportError:
 
 from pymin.dispatcher import Handler, handler, HandlerError, \
                                 CommandNotFoundError
+from pymin.seqtools import Sequence
 
 DEBUG = False
 #DEBUG = True
@@ -21,7 +22,7 @@ __ALL__ = ('Error', 'ReturnNot0Error', 'ExecutionError', 'ItemError',
             'Persistent', 'Restorable', 'ConfigWriter', 'ServiceHandler',
             'RestartHandler', 'ReloadHandler', 'InitdHandler', 'SubHandler',
             'DictSubHandler', 'ListSubHandler', 'ComposedSubHandler',
-            'ListComposedSubHandler', 'DictComposedSubHandler')
+            'ListComposedSubHandler', 'DictComposedSubHandler', 'Device','Address')
 
 class Error(HandlerError):
     r"""
@@ -152,6 +153,30 @@ class ContainerNotFoundError(ContainerError):
         r"Initialize the object. See class documentation for more info."
         self.message = u'Container not found: "%s"' % key
 
+class Address(Sequence):
+    def __init__(self, ip, netmask, broadcast=None, peer=None):
+        self.ip = ip
+        self.netmask = netmask
+        self.broadcast = broadcast
+        self.peer = peer
+    def update(self, netmask=None, broadcast=None):
+        if netmask is not None: self.netmask = netmask
+        if broadcast is not None: self.broadcast = broadcast
+    def as_tuple(self):
+        return (self.ip, self.netmask, self.broadcast, self.peer)
+
+
+class Device(Sequence):
+    def __init__(self, name, mac, ppp):
+        self.name = name
+        self.mac = mac
+        self.ppp = ppp
+        self.addrs = dict()
+        self.routes = list()
+    def as_tuple(self):
+        return (self.name, self.mac, self.addrs)
+
+
 
 def get_network_devices():
     p = subprocess.Popen(('ip', '-o', 'link'), stdout=subprocess.PIPE,
@@ -165,16 +190,38 @@ def get_network_devices():
         if dev.find('link/ether') != -1:
             i = dev.find('link/ether')
             mac = dev[i+11 : i+11+17]
-            i = dev.find(':',2)
-            name = dev[3: i]
-            d[name] = mac
+            i = dev.find(':')
+            j = dev.find(':', i+1)
+            name = dev[i+2: j]
+            d[name] = Device(name,mac,False)
         elif dev.find('link/ppp') != -1:
             i = dev.find('link/ppp')
             mac =  '00:00:00:00:00:00'
-            i = dev.find(':',2)
-            name = dev[3 : i]
-            d[name] = mac
+            i = dev.find(':')
+            j = dev.find(':', i+1)
+            name = dev[i+2 : j]
+            d[name] = Device(name,mac,True)
+            #since the device is ppp, get the address and peer
+            try:
+                p = subprocess.Popen(('ip', '-o', 'addr', 'show', name), stdout=subprocess.PIPE,
+                                                        close_fds=True, stderr=subprocess.PIPE)
+                string = p.stdout.read()
+                p.wait()
+                addrs = string.splitlines()
+                inet = addrs[1].find('inet')
+                peer = addrs[1].find('peer')
+                bar = addrs[1].find('/')
+                from_addr = addrs[1][inet+5 : peer-1]
+                to_addr = addrs[1][peer+5 : bar]
+                d[name].addrs[from_addr] = Address(from_addr,24, peer=to_addr)
+            except IndexError:
+                pass
+            
     return d
+       
+def get_peers():
+    p = subprocess.Popen(('ip', '-o', 'addr'), stdout=subprocess.PIPE,
+                                                    close_fds=True)
 
 def call(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
             stderr=subprocess.PIPE, close_fds=True, universal_newlines=True,
@@ -1049,6 +1096,8 @@ class DictComposedSubHandler(ComposedSubHandler):
 
 if __name__ == '__main__':
 
+    import sys
+
     # Execution tests
     class STestHandler1(ServiceHandler):
         _service_start = ('service', 'start')
@@ -1151,3 +1200,5 @@ if __name__ == '__main__':
     os.rmdir('templates')
     print
 
+    print get_network_devices()
+