Revision 736db20c

View differences:

examples/bind.py
35 35
"""
36 36

  
37 37
from mininet.net import Mininet
38
from mininet.node import Host, HostWithPrivateDirs
38
from mininet.node import Host
39 39
from mininet.cli import CLI
40 40
from mininet.topo import SingleSwitchTopo
41 41
from mininet.log import setLogLevel, info, debug
......
51 51
    privateDirs = [ ( '/var/log', '/tmp/%(name)s/var/log' ), 
52 52
                    ( '/var/run', '/tmp/%(name)s/var/run' ), 
53 53
                      '/var/mn' ]
54
    host = partial( HostWithPrivateDirs,
54
    host = partial( Host,
55 55
                    privateDirs=privateDirs )
56 56
    net = Mininet( topo=topo, host=host )
57 57
    net.start()
58
    directories = []
59
    for directory in privateDirs:
60
        directories.append( directory[ 0 ]
61
                            if isinstance( directory, tuple )
62
                            else directory )
58
    directories = [ directory[ 0 ] if isinstance( directory, tuple )
59
                    else directory for directory in privateDirs ]
63 60
    info( 'Private Directories:',  directories, '\n' )
64 61
    CLI( net )
65 62
    net.stop()
examples/miniedit.py
43 43
from mininet.util import custom, customConstructor
44 44
from mininet.term import makeTerm, cleanUpScreens
45 45
from mininet.node import Controller, RemoteController, NOX, OVSController
46
from mininet.node import CPULimitedHost, Host, Node, HostWithPrivateDirs
46
from mininet.node import CPULimitedHost, Host, Node
47 47
from mininet.node import OVSKernelSwitch, OVSSwitch, UserSwitch
48 48
from mininet.link import TCLink, Intf, Link
49 49
from mininet.cli import CLI
......
78 78
          'cfs': custom( CPULimitedHost, sched='cfs' ) }
79 79

  
80 80

  
81
class CPULimitedHostWithPrivateDirs( CPULimitedHost ):
82
    "Host with private directories"
83

  
84
    def __init__( self, name, sched='cfs', **kwargs ):
85
        "privateDirs: list of private directory strings or tuples"
86
        self.name = name
87
        self.privateDirs = kwargs.pop( 'privateDirs', [] )
88
        CPULimitedHost.__init__( self, name, sched, **kwargs )
89
        self.mountPrivateDirs()
90

  
91
    def mountPrivateDirs( self ):
92
        "mount private directories"
93
        for directory in self.privateDirs:
94
            if isinstance( directory, tuple ):
95
                # mount given private directory
96
                privateDir = directory[ 1 ] % self.__dict__
97
                mountPoint = directory[ 0 ]
98
                self.cmd( 'mkdir -p %s' % privateDir )
99
                self.cmd( 'mkdir -p %s' % mountPoint )
100
                self.cmd( 'mount --bind %s %s' %
101
                               ( privateDir, mountPoint ) )
102
            else:
103
                # mount temporary filesystem on directory
104
                self.cmd( 'mkdir -p %s' % directory )
105
                self.cmd( 'mount -n -t tmpfs tmpfs %s' % directory )
106

  
107 81
class InbandController( RemoteController ):
108 82

  
109 83
    def checkListening( self ):
......
2784 2758
                # Create the correct host class
2785 2759
                if 'cores' in opts or 'cpu' in opts:
2786 2760
                    if ('privateDirectory' in opts):
2787
                        hostCls = partial( CPULimitedHostWithPrivateDirs,
2761
                        hostCls = partial( CPULimitedHost,
2788 2762
                                           privateDirs=opts['privateDirectory'] )
2789 2763
                    else:
2790 2764
                        hostCls=CPULimitedHost
2791 2765
                else:
2792 2766
                    if ('privateDirectory' in opts):
2793
                        hostCls = partial( HostWithPrivateDirs,
2767
                        hostCls = partial( Host,
2794 2768
                                           privateDirs=opts['privateDirectory'] )
2795 2769
                    else:
2796 2770
                        hostCls=Host
2771
                print hostCls
2797 2772
                newHost = net.addHost( name,
2798 2773
                                       cls=hostCls,
2799 2774
                                       ip=ip,
mininet/node.py
75 75
    def __init__( self, name, inNamespace=True, **params ):
76 76
        """name: name of node
77 77
           inNamespace: in network namespace?
78
           privateDirs: list of private directory strings or tuples
78 79
           params: Node parameters (see config() for details)"""
79 80

  
80 81
        # Make sure class actually works
81 82
        self.checkSetup()
82 83

  
83 84
        self.name = params.get( 'name', name )
85
        self.privateDirs = params.get( 'privateDirs', [] )
84 86
        self.inNamespace = params.get( 'inNamespace', inNamespace )
85 87

  
86 88
        # Stash configuration parameters for future reference
......
100 102

  
101 103
        # Start command interpreter shell
102 104
        self.startShell()
105
        self.mountPrivateDirs()
103 106

  
104 107
    # File descriptor to node mapping support
105 108
    # Class variables and methods
......
161 164
        self.cmd( 'stty -echo' )
162 165
        self.cmd( 'set +m' )
163 166

  
167
    def mountPrivateDirs( self ):
168
        "mount private directories"
169
        for directory in self.privateDirs:
170
            if isinstance( directory, tuple ):
171
                # mount given private directory
172
                privateDir = directory[ 1 ] % self.__dict__ 
173
                mountPoint = directory[ 0 ]
174
                self.cmd( 'mkdir -p %s' % privateDir )
175
                self.cmd( 'mkdir -p %s' % mountPoint )
176
                self.cmd( 'mount --bind %s %s' %
177
                               ( privateDir, mountPoint ) )
178
            else:
179
                # mount temporary filesystem on directory
180
                self.cmd( 'mkdir -p %s' % directory ) 
181
                self.cmd( 'mount -n -t tmpfs tmpfs %s' % directory )
182

  
164 183
    def _popen( self, cmd, **params ):
165 184
        """Internal method: spawn and return a process
166 185
            cmd: command to run (list)
......
590 609
        "Make sure our class dependencies are available"
591 610
        pathCheck( 'mnexec', 'ifconfig', moduleName='Mininet')
592 611

  
593

  
594 612
class Host( Node ):
595 613
    "A host is simply a Node"
596 614
    pass
597 615

  
598

  
599 616
class CPULimitedHost( Host ):
600 617

  
601 618
    "CPU limited host"
......
762 779
        mountCgroups()
763 780
        cls.inited = True
764 781

  
765
class HostWithPrivateDirs( Host ):
766
    "Host with private directories"
767

  
768
    def __init__( self, name, *args, **kwargs ):
769
        "privateDirs: list of private directory strings or tuples"
770
        self.name = name
771
        self.privateDirs = kwargs.pop( 'privateDirs', [] )
772
        Host.__init__( self, name, *args, **kwargs )
773
        self.mountPrivateDirs()
774

  
775
    def mountPrivateDirs( self ):
776
        "mount private directories"
777
        for directory in self.privateDirs:
778
            if isinstance( directory, tuple ):
779
                # mount given private directory
780
                privateDir = directory[ 1 ] % self.__dict__ 
781
                mountPoint = directory[ 0 ]
782
                self.cmd( 'mkdir -p %s' % privateDir )
783
                self.cmd( 'mkdir -p %s' % mountPoint )
784
                self.cmd( 'mount --bind %s %s' %
785
                               ( privateDir, mountPoint ) )
786
            else:
787
                # mount temporary filesystem on directory
788
                self.cmd( 'mkdir -p %s' % directory ) 
789
                self.cmd( 'mount -n -t tmpfs tmpfs %s' % directory )
790

  
791

  
792 782

  
793 783
# Some important things to note:
794 784
#

Also available in: Unified diff