Revision 80d647a9

View differences:

examples/cluster.py
125 125
        if server == 'localhost':
126 126
            server = None
127 127
        self.server = server
128
        if not serverIP:
129
                serverIP = self.findServerIP( server )
130
        self.serverIP = serverIP
131
        if not user:
132
            user = quietRun( 'echo $SUDO_USER' )
133
        self.user = user
128
        self.serverIP = serverIP if serverIP else self.findServerIP( server )
129
        self.user = user if user else self.findUser()
134 130
        if self.user and self.server:
135 131
            self.dest = '%s@%s' % ( self.user, self.serverIP )
136 132
        else:
......
146 142
        self.splitInit = splitInit
147 143
        super( RemoteMixin, self ).__init__( name, **kwargs )
148 144

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

  
149 159
    # Determine IP address of local host
150 160
    _ipMatchRegex = re.compile( r'\d+\.\d+\.\d+\.\d+' )
151 161

  
......
593 603
        if not self.serverIP:
594 604
            self.serverIP = { server: RemoteMixin.findServerIP( server )
595 605
                              for server in self.servers }
596
        self.user = params.pop( 'user', None )
597
        if self.servers and not self.user:
598
            self.user = quietRun( 'echo $SUDO_USER' )
606
        self.user = params.pop( 'user', RemoteMixin.findUser() )
599 607
        if params.pop( 'precheck' ):
600 608
            self.precheck()
601 609
        self.connections = {}

Also available in: Unified diff