PoV使用的Fabric辅助工具
项目描述
Fabric辅助工具
这是一个我们在Fabric脚本中使用的辅助工具集合。它们主要用于管理Ubuntu服务器(12.04 LTS或14.04 LTS)。
辅助工具(也就是为什么我要用这个?)
APT软件包
ensure_apt_not_outdated() - 至多每天运行一次apt-get update
install_packages("vim screen build-essential")
install_missing_packages("vim screen build-essential")
如果没有package_installed('git'): ...
如果没有package_available('pov-admin-helpers'): ...
用户账户
ensure_user("myusername")
SSH
ensure_known_host("example.com ssh-rsa AAA....")
区域设置
ensure_locales("en", "lt")
文件和目录
ensure_directory("/srv/data", mode=0o700)
upload_file('crontab', '/etc/cron.d/mycrontab')
generate_file('crontab.in', '/etc/cron.d/mycrontab', context, use_jinja=True)
download_file('/home/user/.ssh/authorized_keys', 'https://example.com/ssh.pubkey')
GIT
git_clone("git@github.com:ProgrammersOfVilnius/project.git", "/opt/project")
git_update("/opt/project")
PostgreSQL
ensure_postgresql_user("username")
ensure_postgresql_db("dbname", "owner")
Apache
ensure_ssl_key(...)
install_apache_website('apache.conf.in', 'example.com', context, use_jinja=True, modules='ssl rewrite proxy_http')
Postfix
install_postfix_virtual_table('virtual', '/etc/postfix/virtual.example.com')
make_postfix_public()
在/root/Changelog中保留更新日志(需要/usr/sbin/new-changelog-entry来自pov-admin-tools)
changelog("# 安装内容")
changelog_append("# 更多内容")
changelog_banner("安装 内容")
run_and_changelog("apt-get install 内容")
许多其他辅助函数都有 changelog 和/或 changelog_append 参数,可以隐式调用这些功能。
实例管理API
我的所有fabfile都可以管理特定服务的多个 实例。从外部看,这看起来像
fab instance1 task1 task2 instance2 task3
这将在实例 instance1 上执行 Fabric 任务 task1 和 task2,然后在对 instance2 执行 task3。
一个实例定义了各种参数,例如
它托管的服务器
它在文件系统中的位置
使用的 Unix 用户 ID
为此实例使用的数据库
等等。
为了方便起见,pov_fabric 提供了三个功能
一个 Instance 类,应该被继承以提供自己的实例
from pov_fabric import Instance as BaseInstance class Instance(BaseInstance): def __init__(self, name, host, home='/opt/sentry', user='sentry', dbname='sentry'): super(Instance, self).Instance.__init__(name, host) self.home = home self.user = user self.dbname = dbname
由于这有点重复,所以有一个辅助函数
from pov_fabric import Instance as BaseInstance Instance = BaseInstance.with_params( home='/opt/sentry', user='sentry', dbname='sentry', )
这与原始的手动继承等效。
(顺便说一句,您也可以使用这种方式添加没有合理默认值的参数,例如 BaseInstance.with_params(user=BaseInstance.REQUIRED).)
一个 Instance.define() 类方法,用于定义新实例并创建选择它们的任务
Instance.define( name='testing', host='root@vagrantbox', ) Instance.define( name='production', host='server1.pov.lt', ) Instance.define( name='staging', host='server1.pov.lt', home='/opt/sentry-staging', user='sentry-staging', dbname='sentry-staging', )
(顺便说一句,您也可以使用 Instance.define_alias('prod', 'production') 定义别名。)
一个 get_instance() 函数,用于返回当前选定的实例(如果用户未选择实例,则中断并显示错误)
from pov_fabric import get_instance @task def look_around(): instance = get_instance() with settings(host_string=instance.host): run('hostname')
以前我使用了一种稍微不同的命令风格
fab task1:instance1 task2:instance1 task3:instance2
如果您的任务是这样编写的,则仍然可以支持这种风格
@task
def look_around(instance=None):
instance = get_instance(instance)
with settings(host_string=instance.host):
run('hostname')
请注意,如果您混合了风格,例如
fab instance1 task1 task2:instance2 task3
将在 instance1 上运行 task1 和 task3,它将在 instance2 上运行 task2。
使用
从 PyPI 获取最新版本
pip install pov-fabric-helpers
然后导入您的 fabfile.py 中需要的辅助函数
from fabric.api import ...
from pov_fabric import ...
作为git子模块使用
您可以将此存储库作为 git 子模块添加
cd ~/src/project
git submodule add https://github.com/ProgrammersOfVilnius/pov-fabric-helpers
然后在您的 fabfile.py 中添加
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pov-fabric-helpers'))
if not os.path.exists(os.path.join(sys.path[0], 'pov_fabric.py')):
sys.exit("Please run 'git submodule update --init'.")
from pov_fabric import ...
使用Vagrant测试Fabfiles
我不知道你们是否一样,但我不曾能够一次性就编写出一个工作的 fabfile.py。 Vagrant 对于在测试 fabfile 而不破坏真实服务器方面非常有用。下面是如何做
在某个地方创建一个 Vagrantfile,如下所示
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/precise64" # Ubuntu 12.04 config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "1024"] end end
运行 vagrant up
运行 vagrant ssh-config 并将片段复制到您的 ~/.ssh/config 中,但将名称改为 vagrantbox,例如
Host vagrantbox HostName 127.0.0.1 User vagrant Port 2222 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile ~/.vagrant.d/insecure_private_key IdentitiesOnly yes LogLevel FATAL
测试 ssh vagrantbox 是否正常工作
在您的 fabfile.py 中创建一个测试实例
Instance.define( name='testing', host='vagrant@vagrantbox', ... )
使用 fab testing install 等进行测试
更新日志
0.3 (2016-09-11)
register_host_key() 现在接受 fingerprints,因此您可以指定 MD5 和 SHA256 指纹。
使用 register_host_key(key, fingerprint=md5_fprint) 或 register_host_key(key, fingerprints=[md5_fprint, sha256_fprint])。
低级辅助函数 ssh_key_fingerprint() 现在接受 force_md5,因此您可以坚持使用 MD5 而不是 OpenSSH 默认提供的(对于现代 OpenSSH,默认是 SHA256)。
0.2 (2015-08-06)
新辅助函数
git_update(),register_host_key(),
ensure_locales(),
changelog_banner(),run_and_changelog(),has_new_changelog_message(),
install_missing_packages(),package_available(),
upload_file(),generate_file(),ensure_directory(),download_file(),
install_postfix_virtual_table(),
install_apache_website(),
ensure_ssl_key().
现有助手的新可选参数
git_clone()现在接受branch和changelog。
ensure_user()现在接受shell,home,create_home和changelog。
install_packages()现在接受changelog。
changelog()现在接受context。
changelog_append()现在接受context和optional。
changelog_banner()现在接受context和optional。
提高安全性
所有助手都会检查它们的参数是否包含不安全的shell元字符。
changelog()和相关函数会正确引用参数。
改进实例API
允许使用str.format(**instance)(通过使Instance成为dict的子类)。
允许通过Instance.define_alias(alias, name)静态方法定义实例别名。
错误修正
ensure_postgresql_db()现在在Ubuntu 14.04上工作正常。
run_as_root现在可以正确处理没有用户名的env.host_string。
新的低级助手,除非您正在编写自己的助手,否则可能不感兴趣
aslist(),assert_shell_safe(),
ssh_key_fingerprint(),
render_jinja2(),render_sinterp(),
parse_git_repo(),
generate_ssl_config(),generate_ssl_key(),generate_ssl_csr(),
get_postfix_setting(),parse_postfix_setting(),add_postfix_virtual_map(),add_postfix_setting(),
run_as_root().
0.1 (2014-11-19)
首次公开发布。
助手
ensure_apt_not_outdated(),package_installed(),install_packages(),
ensure_known_host(),ensure_user(),
git_clone(),
ensure_postgresql_user(),ensure_postgresql_db(),
changelog(),changelog_append()。
实例API
class Instance,Instance.with_params(),Instance.REQUIRED,Instance.define()。
instance._asdict().
get_instance().
您可能不感兴趣的底层助手,除非您正在编写自己的助手
asbool(),
postgresql_user_exists(),postgresql_db_exists()。
项目详情
下载文件
下载适合您平台的应用程序。如果您不确定选择哪个,请了解更多关于安装软件包的信息。
源分发
构建分发
pov-fabric-helpers-0.3.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 133a5c763ab0f2f34e3338578675f92a0c44f5eb074692dab7929751065fe7e8 |
|
MD5 | 71adee9f8d325b7c6a61b5905b1992c2 |
|
BLAKE2b-256 | b57911e01dcfc8f280106832565ef7bc8c99e08db9ae0a68cad183d569ad43e4 |
pov_fabric_helpers-0.3-py2-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 9b21ec409f7d718a68d5d75a968ecff981a513e4f9c0b3772a7a8944359af3ca |
|
MD5 | b274375b58b8f187eae9845b8633f6a3 |
|
BLAKE2b-256 | a085671478cf55f81c86ec0eb1c1f969c0b00e2b39e872ddbf2d719696921a77 |