Viper提供了一个简单的API(Python和CLI),可以轻松管理大型基础设施。
项目描述
Viper提供了一个简单的API(Python和CLI),可以轻松管理大型基础设施。
入门
安装
pip install -U viper-infra-commander
# Or install with batteries included
pip install -U "viper-infra-commander[batteries]"
初始化
# (Optional) enable tab completion
eval "$(viper autocomplete $(basename $SHELL))"
# See the help menu
viper -h
# Initialize SQLite DB
viper init -f
Viper实战(基本模式)
在 hosts.csv 中定义一组主机(也支持json和yml格式)
cat > hosts.csv << EOF
ip,hostname,login_name,identity_file
192.168.0.11,host11,root,/root/.ssh/id_rsa.pub
192.168.0.12,host12,root,/root/.ssh/id_rsa.pub
192.168.0.13,host13,root,/root/.ssh/id_rsa.pub
192.168.0.14,host14,root,/root/.ssh/id_rsa.pub
192.168.0.15,host15,root,/root/.ssh/id_rsa.pub
EOF
在 task.py 中定义一个任务
cat > task.py << EOF
from viper import Task
def ping_command(host):
return "ping", "-c", "1", host.ip
def ping():
return Task(
name="Ping once",
command_factory=ping_command
)
EOF
执行以下操作
使用5个工作线程并行在主机集上运行任务
仅筛选出任务失败的结果
对它们重新运行任务
将结果存储在数据库中
viper hosts:from-file hosts.csv \
| viper hosts:run-task task.ping --max-worker 5 \
| viper results:where returncode IS_NOT 0 \
| viper results:re-run --indent 4
查看来自数据库的最终结果的stdout
viper results \
| viper results:final \
| viper results:format "{host.hostname}: {stdout}"
将结果导出到csv文件
viper results --final \
| viper results:to-file results.csv --indent 4
使用Python API(CLI和Python API几乎相同)定义一个作业
cat > job.py << EOF
from viper import WhereConditions
from task import ping
def ping_and_export(hosts):
return (
hosts.task(ping())
.run(max_workers=5)
.final()
.to_file("results.csv")
)
EOF
使用CLI运行作业
viper hosts:from-file hosts.csv \
| viper run job.ping_and_export \
| viper results:format "{host.hostname}: {stdout}"
Viperfile实战(高级模式)
在viperfile中定义一个具有自定义子命令的项目
cat > viperfile.py << EOF
from viper import Hosts, Task
from viper.project import Project, arg
foo = Project(prefix="foo")
@foo.hostgroup(args=[arg("-f", "--file", default="hosts.csv")])
def allhosts(args):
return Hosts.from_file(args.file)
def remote_exec_command(host, command):
return (
"ssh",
"-i",
host.identity_file,
"-l",
host.login_name,
"-p",
str(host.port),
"-o",
"StrictHostKeyChecking=no",
"-o",
"PubkeyAuthentication=yes",
host.ip,
command,
)
@foo.job(
args=[
arg("command", help="command to execute"),
arg("-w", "--workers", type=int, default=1),
]
)
def remote_exec(hosts, args):
return (
hosts.task(
Task(
name="Remote execute command",
command_factory=remote_exec_command,
timeout=5,
),
args.command,
)
.run(max_workers=args.workers)
.final()
)
EOF
查看自动生成的自定义子命令
viper --help
运行作业
viper @foo:allhosts \
| viper @foo:remote_exec "uname -a" --workers 5 \
| viper results:to-file results.csv \
| viper results:format "{task.name} [{host.hostname}]: {returncode}: {stdout}"
进一步阅读
带有示例的API文档 ☞ https://viper-infra-commander.netlify.com
为Viper做出贡献
贡献指南 ☞ https://github.com/sayanarijit/viper/blob/master/CONTRIBUTING.md
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
viper-infra-commander-0.28.3.tar.gz (29.3 kB 查看哈希值)
构建分布
关闭
viper-infra-commander-0.28.3.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0d814ad64e6d7b0621e72a35fe048ed1a8d122d627dbbd7066983214335b15d1 |
|
MD5 | cf99d573a1982115b4ed5959cce3c226 |
|
BLAKE2b-256 | 967c0ead9d4325b22ad586f168ad35db7926730c36a071969eda7a8c72c5768c |
关闭
viper_infra_commander-0.28.3-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 27516e5134411c80a61fc408ccb7a6090819ca98d2d135e7a88629562ecb7659 |
|
MD5 | 7c031cd4a8f6d9069d6e298758a7668d |
|
BLAKE2b-256 | d32800177562414cdd12dc1fb6807b59049ec01766241136d76404d0ef6118c2 |