python fabric每次执行sudo命令都提示输入密码

2 投票
1 回答
2556 浏览
提问于 2025-04-16 20:42

下面是我正在编写的一段小代码,用来在远程服务器上安装软件包。代码运行得很好,软件包也安装成功了,但一旦安装完成,我就会被要求输入密码。你知道这是什么原因吗?

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

执行这个功能时的输出

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: 

1 个回答

1

我虽然经验不多,但我觉得既然你是用“sudo”来安装py2.6,那在运行的时候也需要用“sudo”,或者至少要在正确的用户组里(在Ubuntu上是不是叫“wheel”之类的?)。

我想一个简单的测试方法是,把这个:

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

改成这个:

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

然后看看你是否还需要输入密码。

撰写回答