我需要這個快速安裝memcached作爲設置測試服務器的Fabric腳本的一部分。我想在這裏記錄下來供將來參考。在Fabric中,如何檢查Debian或Ubuntu軟件包是否存在,如果不存在則安裝它?
2
A
回答
10
將此從這superuser comment和stackoverflow 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
至於檢查是否安裝包(運行在本地測試目的)
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還提供了一些您可能會喜歡的其他有用的軟件包管理助手。
相關問題
- 1. 如何檢查表中是否存在值,如果存在則刪除它?
- 2. 如何在安裝之前檢查conda包是否存在?
- 3. 軟件包管理器命令,在Ubuntu/Debian中返回0或1(如果安裝了軟件包)
- 4. 檢查div是否存在,如果不存在則重定向
- 5. Python 3.5.1:如何檢查openpyxl軟件包是否存在,如果不存在如何下載並在腳本內「安裝」它?
- 6. 如何檢查包是否已安裝在Ubuntu?
- 7. 如何在安裝程序中安裝PyPi的Ubuntu軟件包?
- 8. 檢查是否安裝了軟件包
- 9. 如何檢查軟件是否安裝在C#中?
- 10. 如何安裝不在CRAN存儲庫中的軟件包?
- 11. 在debian上安裝實驗軟件包
- 12. 如何檢查數據庫表是否存在或不存在?
- 13. 如何在debian whitout sudo權限中安裝軟件包?
- 14. 如何讓Python檢查文件是否存在,如果不存在則創建它?
- 15. 檢查一個表是否存在,如果它不存在,創建它... iOS/SQLite
- 16. 如何在moodle安裝開始時檢查mcrypt是否存在
- 17. 在linux/ubuntu中安裝軟件包
- 18. 安裝前檢查Java是否存在
- 19. 在Ubuntu中安裝軟件包是否需要互聯網?
- 20. 如何使if語句檢查是否在R中安裝了R軟件包
- 21. 檢查是否存在++,如果不是,則獲得最近
- 22. 如何檢查文件是否存在或不在JavaScript?
- 23. 如何檢查是否存在警報,如果是,則接受它
- 24. 如何檢查R是否已安裝在Ubuntu中?
- 25. PYTHON/MySQL:檢查是否存在記錄。如果存在不要插入,如果不存在,則插入
- 26. 如何在sqlite中插入檢查項是否存在,如果存在則更新它,如果不存在,則插入
- 27. 安卓與Sqlite - 如何檢查表是否存在,如果空
- 28. 檢查已安裝的軟件包,如果未找到安裝
- 29. 如何檢查路徑是否存在或不在java中?
- 30. 如何檢查object.subobject.subsubobject是否存在或不在JavaScript中?
非常好,這正是我想要做的。你有GitHub上的任何Fabfile或任何我可以從中學習?提前歡呼。 :) – tmaster 2013-09-02 12:01:25