Revision 18aab5b7 examples/cluster.py

View differences:

examples/cluster.py
123 123
           **kwargs: see Node()"""
124 124
        # We connect to servers by IP address
125 125
        self.server = server if server else 'localhost'
126
        self.serverIP = serverIP if serverIP else self.findServerIP( self.server )
126
        self.serverIP = ( serverIP if serverIP
127
                          else self.findServerIP( self.server ) )
127 128
        self.user = user if user else self.findUser()
128 129
        if controlPath is True:
129 130
            # Set a default control path for shared SSH connections
......
143 144
            self.dest = None
144 145
            self.sshcmd = []
145 146
            self.isRemote = False
147
        # Satisfy pylint
148
        self.shell, self.pid, self.cmd = None, None, None
146 149
        super( RemoteMixin, self ).__init__( name, **kwargs )
147 150

  
148 151
    @staticmethod
149 152
    def findUser():
150 153
        "Try to return logged-in (usually non-root) user"
151
        try:
154
        return (
152 155
            # If we're running sudo
153
            return os.environ[ 'SUDO_USER' ]
154
        except:
155
            try:
156
                # Logged-in user (if we have a tty)
157
                return quietRun( 'who am i' ).split()[ 0 ]
158
            except:
159
                # Give up and return effective user
160
                return quietRun( 'whoami' )
156
            os.environ.get( 'SUDO_USER', False ) or
157
            # Logged-in user (if we have a tty)
158
            ( quietRun( 'who am i' ).split() or [ False ] )[ 0 ] or
159
            # Give up and return effective user
160
            quietRun( 'whoami' ) )
161 161

  
162 162
    # Determine IP address of local host
163 163
    _ipMatchRegex = re.compile( r'\d+\.\d+\.\d+\.\d+' )
......
187 187
            self.pid = int( self.cmd( 'echo $$' ) )
188 188

  
189 189
    def finishInit( self ):
190
        "Wait for split initialization to complete"
191
        assert self  # please pylint
190 192
        self.pid = int( self.waitOutput() )
191 193

  
192 194
    def rpopen( self, *cmd, **opts ):
......
282 284
        cls = type( self )
283 285
        if self.server not in cls.OVSVersions:
284 286
            vers = self.cmd( 'ovs-vsctl --version' )
285
            cls.OVSVersions[ self.server ] = re.findall( r'\d+\.\d+', vers )[ 0 ]
287
            cls.OVSVersions[ self.server ] = re.findall(
288
                r'\d+\.\d+', vers )[ 0 ]
286 289
        return ( StrictVersion( cls.OVSVersions[ self.server ] ) <
287 290
                StrictVersion( '1.10' ) )
288 291

  
......
301 304
        self.tunnel = None
302 305
        kwargs.setdefault( 'params1', {} )
303 306
        kwargs.setdefault( 'params2', {} )
307
        self.cmd = None  # satisfy pylint
304 308
        Link.__init__( self, node1, node2, **kwargs )
305 309

  
306 310
    def stop( self ):
......
324 328
        elif server1 == server2:
325 329
            # Remote link on same remote server
326 330
            return makeIntfPair( intfname1, intfname2, addr1, addr2,
327
                                 run=node1.rcmd )
331
                                 runCmd=node1.rcmd )
328 332
        # Otherwise, make a tunnel
329
        self.tunnel = self.makeTunnel( node1, node2, intfname1, intfname2, addr1, addr2 )
333
        self.tunnel = self.makeTunnel( node1, node2, intfname1, intfname2,
334
                                       addr1, addr2 )
330 335
        return self.tunnel
331 336

  
332 337
    @staticmethod
......
340 345
        cmd = 'ip link set %s netns %s' % ( intf, node.pid )
341 346
        node.rcmd( cmd )
342 347
        links = node.cmd( 'ip link show' )
343
        if not ( ' %s:' % intf ) in links:
348
        if not ' %s:' % intf in links:
344 349
            if printError:
345 350
                error( '*** Error: RemoteLink.moveIntf: ' + intf +
346 351
                      ' not successfully moved to ' + node.name + '\n' )
......
442 447

  
443 448
    def place( self, node ):
444 449
        "Return server for a given node"
450
        assert self, node  # satisfy pylint
445 451
        # Default placement: run locally
446
        return None
452
        return 'localhost'
447 453

  
448 454

  
449 455
class RandomPlacer( Placer ):
......
451 457
    def place( self, nodename ):
452 458
        """Random placement function
453 459
            nodename: node name"""
460
        assert nodename  # please pylint
454 461
        # This may be slow with lots of servers
455 462
        return self.servers[ randrange( 0, len( self.servers ) ) ]
456 463

  
......
467 474
    def place( self, nodename ):
468 475
        """Round-robin placement function
469 476
            nodename: node name"""
477
        assert nodename  # please pylint
470 478
        # This may be slow with lots of servers
471 479
        server = self.servers[ self.next ]
472 480
        self.next = ( self.next + 1 ) % len( self.servers )
......
626 634

  
627 635
    def popen( self, cmd ):
628 636
        "Popen() for server connections"
637
        assert self  # please pylint
629 638
        old = signal( SIGINT, SIG_IGN )
630 639
        conn = Popen( cmd, stdin=PIPE, stdout=PIPE, close_fds=True )
631 640
        signal( SIGINT, old )
......
649 658
            cmd = [ 'sudo', '-E', '-u', self.user ]
650 659
            cmd += self.sshcmd + [ '-n', dest, 'sudo true' ]
651 660
            debug( ' '.join( cmd ), '\n' )
652
            out, err, code = errRun( cmd )
661
            _out, _err, code = errRun( cmd )
653 662
            if code != 0:
654 663
                error( '\nstartConnection: server connection check failed '
655 664
                       'to %s using command:\n%s\n'
......
665 674

  
666 675
    def modifiedaddHost( self, *args, **kwargs ):
667 676
        "Slightly modify addHost"
677
        assert self  # please pylint
668 678
        kwargs[ 'splitInit' ] = True
669 679
        return Mininet.addHost( *args, **kwargs )
670 680

  

Also available in: Unified diff