2011-07-02 81 views
2

下面是我一直致力於在遠程服務器上安裝軟件包的小代碼。代碼工作正常,軟件包正在安裝,但一旦安裝完成,系統會提示您輸入密碼。任何想法是什麼造成這個?蟒蛇織物提示輸入密碼,每次我執行一個sudo命令

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 

輸出執行時功能

[email protected]:~/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: 
+0

plz添加整個腳本,或者至少包括所有定義 – ashwoods

+0

包括進口 – ashwoods

+0

我剛添加完整的腳本,我更新了現有的帖子 –

回答

1

我是從有限的經驗來說,但我相信,因爲你是「命令」安裝,你需要有「sudo」命令來運行它py2.6 ,或者至少在正確的組合中(是否是「輪子」或類似於Ubuntu的東西?)。

我想一個簡單的測試將是,改變這種:

_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] 

&看,如果你仍然提示輸入密碼。

+0

正如Auston說的。注意run()和sudo()是相同的。如果您希望在另一端有提示,請使用sudo()。 (所以相反,如果我在sudoers中將用戶的命令配置爲NOPASSWD,那麼當我運行sudo命令時,我不會使用sudo(),而是使用run('sudo blahcommand')。) –