python fabric prompted for password everytime i execute a sudo command
Question: Below is a small code I've been working on to install packages on the remote
server. The code is working fine and the packages are being installed, but
once the installation is complete I get prompted for entering the password.
Any idea what is causing this?

    
    
    import re
    import os
    import sys
    from datetime import datetime
    
    import git
    
    from fabric.api import run, local, cd, env
    from fabric.contrib.files import exists, append
    from fabric.operations import put, sudo
    
    
    class WebAppConf():
        APPLICATION_DIR = '/home/xxx/webapps'
        APPLICATION_USER = '' # mazban
        APPLICATION_ROOT = ''
        APPLICATION_TEMPLATE_SETTINGS = 'com_work'  # see config/settings/templates
    
    BASE_DIR = os.path.abspath(os.path.dirname(__file__))
    
    ###########################################################################
    # CONFIGURATION FOR DEVELOPMENT, STAGING, TESTING AND PRODUCTION SETTINGS - START     #
    ###########################################################################
    
    def test():
        env.hosts = env.hosts or ['192.168.3.139', ]
        env.user = 'xxx'
        env.password = "xxx"
        env.warn_only = True
        env.no_keys = True
        # App variables 
        WebAppConf.APPLICATION_DIR = '/home/xxx/webapps' # application physical location
        WebAppConf.APPLICATION_USER = env.user
        WebAppConf.APPLICATION_ROOT = env.user
        WebAppConf.APPLICATION_TEMPLATE_SETTINGS = 'testing'
    
    
    def staging():
        env.hosts = env.hosts or ['xxx-staging.com', ]
        env.user = 'xxx'
        env.password = "xx"
        env.warn_only = True
        env.no_keys = True
    
    def production():
        """
        Production will deploay on one or many frontend's
            for peroduction deployment no password will be used and only ssh keys
        """
        env.hosts = env.hosts or ['xxx.com', ]
        env.user = APPLICATION_USER
        env.password = "xx"
        env.warn_only = True
        env.no_keys = True
    
    ##########################################################################
    # CONFIGURATION FOR DEVELOPMENT, STAGING, TESTING AND PRODUCTION SETTINGS - END      #
    ##########################################################################
    
    
    def firstrun():
        ## if no env is selected it will fail
        if not env.hosts:
            print 'Error: must use environment (e.g fab prod %s)' % 'firstrun'
            exit()
    
        # validate the OS distribution and version
        LINUX_DISTRIBUTION = ''
        if run('cat /etc/*-release|grep DISTRIB_ID').upper().__contains__('UBUNTU'):
            LINUX_DISTRIBUTION = 'UBUNTU'
            print 'Ubuntu Linux'
        elif run('cat /etc/*-release|grep DISTRIB_ID').upper().__contains__('DEBIAN'):
            LINUX_DISTRIBUTION = 'DEBIAN'
            print 'Debian Linux'
        else:
            print 'Exiting (Cannot recognize linux distribution).'
            exit()
    
        # Linux version
        LINUX_VERSION = run('cat /etc/*-release|grep DISTRIB_RELEASE')
        print 'Linux version %s' % LINUX_VERSION.replace('DISTRIB_RELEASE=', '')
    
        # GET PYTHON VERSIONS
        version = run("python2.6 -V").split()[1]
        if version[0]=='2' and version[2]=='6' and version[4]=='6':
            # version installed matches the requirements
            print 'Python version is compatible with this application (2.6.6).'
        else:
            print 'Python version is not compatible with this application %s' % version[0]+'.'+version[2]+'.'+version[4]
            _sudo('apt-get install python2.6')
            version = run("python2.6 -V").split()[1]
            if version[0]=='2' and version[2]=='6' and version[4]=='6':
                print 'python 2.6.6 installed successfully.'
            else:
                print 'cannot install python 2.6.6'
                exit()
    
        if not exists('/usr/bin/pip'):
            print "installing python-pip package"
            _sudo('apt-get install python-pip')
    
        if not exists('/usr/local/bin/virtualenv'):
            print 'installing virtualenv'
            _sudo('pip install -E /usr/bin/python2.6 virtualenv')
    
    
        # application directory check
        MAKE_APPLICATION_DIR = False
        if not exists(WebAppConf.APPLICATION_DIR):
            MAKE_APPLICATION_DIR = True
            print '%s does not exist.' % WebAppConf.APPLICATION_DIR
            run('mkdir -p %s' % WebAppConf.APPLICATION_DIR)
            print '%s application directory is created successfully.' % WebAppConf.APPLICATION_DIR
    
        # change to application root directory and start preparing for applications
        #with cd(WebAppConf.APPLICATION_DIR):
        return
    

Output when executing function

    
    
    mo@ubuntu:~/Projects/mazban/mazban$ fab test firstrun
    [192.168.3.139] Executing task 'firstrun'
    [192.168.3.139] run: cat /etc/*-release|grep DISTRIB_ID
    [192.168.3.139] out: DISTRIB_ID=Ubuntu
    [192.168.3.139] out: 
    Ubuntu Linux
    [192.168.3.139] run: cat /etc/*-release|grep DISTRIB_RELEASE
    [192.168.3.139] out: DISTRIB_RELEASE=11.04
    [192.168.3.139] out: 
    Linux version 11.04
    [192.168.3.139] run: python2.6 -V
    [192.168.3.139] out: /bin/bash: python2.6: command not found
    [192.168.3.139] out: 
    
    Warning: run() encountered an error (return code 127) while executing 'python2.6 -V'
    
    Python version is not compatible with this application p.t.o
    [192.168.3.139] sudo: apt-get install python2.6
    [192.168.3.139] out: sudo password:
    [192.168.3.139] out: Reading package lists... Done
    [192.168.3.139] out: Building dependency tree       
    [192.168.3.139] out: Reading state information... Done
    [192.168.3.139] out: The following extra packages will be installed:
    [192.168.3.139] out:   python2.6-minimal
    [192.168.3.139] out: Suggested packages:
    [192.168.3.139] out:   python2.6-doc python2.6-profiler
    [192.168.3.139] out: The following NEW packages will be installed:
    [192.168.3.139] out:   python2.6 python2.6-minimal
    [192.168.3.139] out: 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
    [192.168.3.139] out: Need to get 3,743 kB of archives.
    [192.168.3.139] out: After this operation, 14.3 MB of additional disk space will be used.
    [192.168.3.139] out: Do you want to continue [Y/n]? Y
    [192.168.3.139] out: Get:1 http://us.archive.ubuntu.com/ubuntu/ natty/main python2.6-minimal i386 2.6.6-6ubuntu7 [1,386 kB]
    [192.168.3.139] out: Get:2 http://us.archive.ubuntu.com/ubuntu/ natty/main python2.6 i386 2.6.6-6ubuntu7 [2,357 kB]
    [192.168.3.139] out: 53% [2 python2.6 602 kB/2,357 kB 25%]                                                                                                                        [192.168.3.139] out: 58% [2 python2.6 797 kB/2,357 kB 33%]                                                                                                                        [192.168.3.139] out: 63% [2 python2.6 1,001 kB/2,357 kB 42%]                                                                                                                      [192.168.3.139] out: 69% [2 python2.6 1,208 kB/2,357 kB 51%]                                                                                                                      [192.168.3.139] out: 75% [2 python2.6 1,425 kB/2,357 kB 60%]                                                                                                                      [192.168.3.139] out: 81% [2 python2.6 1,651 kB/2,357 kB 70%]                                                                                                                      [192.168.3.139] out: 87% [2 python2.6 1,890 kB/2,357 kB 80%]                                                                                                                      [192.168.3.139] out: 94% [2 python2.6 2,141 kB/2,357 kB 90%]                                                                                                                      [192.168.3.139] out: 100% [Working]                                                                                                                                               [192.168.3.139] out:                                                                                                                                                              [192.168.3.139] out: Fetched 3,743 kB in 10s (371 kB/s)
    [192.168.3.139] out: Selecting previously deselected package python2.6-minimal.
    [192.168.3.139] out: (Reading database ... 131472 files and directories currently installed.)
    [192.168.3.139] out: Unpacking python2.6-minimal (from .../python2.6-minimal_2.6.6-6ubuntu7_i386.deb) ...
    [192.168.3.139] out: Selecting previously deselected package python2.6.
    [192.168.3.139] out: Unpacking python2.6 (from .../python2.6_2.6.6-6ubuntu7_i386.deb) ...
    [192.168.3.139] out: Processing triggers for man-db ...
    [192.168.3.139] out: Processing triggers for bamfdaemon ...
    [192.168.3.139] out: Rebuilding /usr/share/applications/bamf.index...
    [192.168.3.139] out: Processing triggers for desktop-file-utils ...
    [192.168.3.139] out: Processing triggers for python-gmenu ...
    [192.168.3.139] out: Rebuilding /usr/share/applications/desktop.en_US.utf8.cache...
    [192.168.3.139] out: Processing triggers for python-support ...
    [192.168.3.139] out: Setting up python2.6-minimal (2.6.6-6ubuntu7) ...
    [192.168.3.139] out: Linking and byte-compiling packages for runtime python2.6...
    [192.168.3.139] out: Setting up python2.6 (2.6.6-6ubuntu7) ...
    [192.168.3.139] out: 
    [192.168.3.139] run: python2.6 -V
    [192.168.3.139] Login password: 
    


Answer: I'm speaking from limited experience, but I believe that since you're "sudo"
installing py2.6 you need be "sudo" to run it, or at least in the right group
(is it "wheel" or something like that on Ubuntu?).

I suppose an easy test would be, change this:

    
    
     _sudo('apt-get install python2.6')
     version = run("python2.6 -V").split()[1]
    

to this:

    
    
     _sudo('apt-get install python2.6')
     version = _sudo("python2.6 -V").split()[1]
    

& see if you're still prompted for the password.

