2013-02-15 44 views

回答

10

將此從這superuser commentstackoverflow answer加在一起。 (注:我運行爲root而不是使用sudo):

def package_installed(pkg_name): 
    """ref: http:superuser.com/questions/427318/#comment490784_427339""" 
    cmd_f = 'dpkg-query -l "%s" | grep -q ^.i' 
    cmd = cmd_f % (pkg_name) 
    with settings(warn_only=True): 
     result = run(cmd) 
    return result.succeeded 

def yes_install(pkg_name): 
    """ref: https://stackoverflow.com/a/10439058/1093087""" 
    run('apt-get --force-yes --yes install %s' % (pkg_name)) 

def make_sure_memcached_is_installed_and_running(): 
    if not package_installed('memcached'): 
     yes_install('memcached') 
    with settings(warn_only=True): 
     run('/etc/init.d/memcached restart', pty=False) 
+0

非常好,這正是我想要做的。你有GitHub上的任何Fabfile或任何我可以從中學習?提前歡呼。 :) – tmaster 2013-09-02 12:01:25

0

至於檢查是否安裝包(運行在本地測試目的)

import re 

def is_package_installed(pkgname): 
    output = local('dpkg -s {}'.format(pkgname), capture=True) 
    match = re.search(r'Status: (\w+.)*', output) 
    if match and 'installed' in match.group(0).lower(): 
     return True 
    return False 
1

Fabtools是一個非常有用的我添加到所有Fabric項目中的Python模塊。

它有一個方法deb.is_installed,檢查是否安裝了Debian軟件包。在我的所有項目中使用這種標準方法是很好的,Fabtools還提供了一些您可能會喜歡的其他有用的軟件包管理助手。

相關問題