【问题标题】:In Fabric, how can I check if a Debian or Ubuntu package exists and install it if it does not?在 Fabric 中,如何检查 Debian 或 Ubuntu 软件包是否存在,如果不存在则安装它?
【发布时间】:2013-02-15 22:38:13
【问题描述】:

我需要它来快速安装 memcached,作为设置测试服务器的 Fabric 脚本的一部分。我想我会把它记录在这里以供将来参考。

【问题讨论】:

    标签: python ubuntu debian fabric


    【解决方案1】:

    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)
    

    【讨论】:

    • 非常好,这正是我想要做的。你有 github 上的 Fabfile 或者我可以学习的任何东西吗?提前喝彩。 :)
    【解决方案2】:

    Fabtools 是一个非常有用的 Python 模块,我将它添加到我所有的 Fabric 项目中。

    它有一个方法deb.is_installed 来检查是否安装了Debian 软件包。很高兴在我的所有项目中都使用这种标准方法,而且 Fabtools 还提供了一些其他有用的包管理助手,您可能会喜欢。

    【讨论】:

      【解决方案3】:

      至于检查是否安装了包(在本地运行以进行测试)

      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
      

      【讨论】:

        猜你喜欢
        • 2011-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-06
        • 1970-01-01
        • 2016-06-10
        • 2017-11-22
        相关资源
        最近更新 更多