Skip to main content

远程部署与状态管理工具 - Remote deployment and state management tool based on SSH/SFTP

Project description

HostWeaver

PyPI version Python version License

远程部署与状态管理工具 - 基于 SSH/SFTP 的自动化部署引擎

Remote deployment and state management tool based on SSH/SFTP.

✨ 功能特性

  • 🔐 SSH/SFTP 封装 - 支持密码和私钥认证,连接池管理
  • 📦 文件传输 - 可靠的文件上传/下载,自动赋予执行权限(chmod +x
  • ⚙️ Systemd 管理 - 动态生成 .service 配置文件,完整生命周期管理(start/stop/restart/status
  • 🔑 License 生成 - 基于机器指纹(/etc/machine-id)的授权密钥生成
  • 🚀 一键部署 - 完整的部署工作流,支持进度显示
  • 🖥️ 多机并发 - 批量部署支持,失败隔离,汇总报告
  • 🎨 优雅 CLI - 基于 typerrich 的终端体验

📦 安装

# 使用 pip 安装
pip install hostweaver

# 使用 uv 安装
uv pip install hostweaver

# 从源码安装(开发模式)
git clone https://github.com/zsuroy/hostweaver.git
cd hostweaver
uv pip install -e ".[dev]"

🚀 快速开始

命令行使用

# 部署单个二进制文件
hostweaver deploy \
    --host 192.168.1.100 \
    --username root \
    --key-file ~/.ssh/id_rsa \
    --source ./dist/app \
    --install-dir /opt/app \
    --service myapp \
    --env APP_ENV=production \
    --env APP_PORT=8000

# 部署整个项目目录
hostweaver deploy \
    --host 192.168.1.100 \
    --username root \
    --key-file ~/.ssh/id_rsa \
    --source ./project \
    --entrypoint main.py \
    --install-dir /opt/app \
    --service myapp

# 查询服务状态
hostweaver status \
    --host 192.168.1.100 \
    --username root \
    --service myapp

# 查看服务日志
hostweaver logs \
    --host 192.168.1.100 \
    --username root \
    --service myapp \
    --lines 50

# 重启服务
hostweaver restart \
    --host 192.168.1.100 \
    --username root \
    --service myapp

Python API 使用

单机部署

from hostweaver.business.generic_deployer import GenericDeployer
from hostweaver.workflow.single_deploy import SingleDeployWorkflow

# 创建部署器
deployer = GenericDeployer(
    host="192.168.1.100",
    username="root",
    pkey_path="~/.ssh/id_rsa",
    source_path="./dist/app",
    install_dir="/opt/app",
    service_name="myapp",
    env_vars={"APP_ENV": "production"},
    secret_key="your-secret-key",  # 用于生成 License
)

# 执行部署
workflow = SingleDeployWorkflow(deployer)
result = workflow.execute()

# 检查结果
if result.success:
    print(f"部署成功!机器指纹:{result.machine_id}")
else:
    print(f"部署失败:{result.message}")

多机并发部署

from hostweaver.business.generic_deployer import GenericDeployer
from hostweaver.concurrent.pool import DeployPool, HostConfig
from hostweaver.concurrent.reporter import DeploymentReporter

# 创建并发池
pool = DeployPool(max_workers=5)

# 添加主机
hosts = [
    HostConfig(host="192.168.1.100", username="root"),
    HostConfig(host="192.168.1.101", username="root"),
    HostConfig(host="192.168.1.102", username="root", env_vars={"NODE_ID": "3"}),
]

for config in hosts:
    pool.add_task(
        config=config,
        deployer_class=GenericDeployer,
        source_path="./dist/app",
        install_dir="/opt/app",
        service_name="myapp",
        secret_key="your-secret-key",
    )

# 执行部署
results = pool.execute()

# 生成报告
reporter = DeploymentReporter(results)
reporter.print_report(verbose=True)

# 获取汇总
summary = reporter.get_summary()
print(f"成功率:{summary['success_rate']:.1%}")

📖 核心概念

部署流程

HostWeaver 定义了一个标准的部署生命周期:

connect → pre_deploy() → run() → post_deploy() → disconnect

  1. connect: 建立 SSH 连接,初始化 SFTP 和 Systemd 管理器
  2. pre_deploy(): 部署前准备(获取机器指纹、停止旧服务、生成 License)
  3. run(): 执行部署(文件上传、配置渲染、systemd 重载)
  4. post_deploy(): 部署后处理(启动服务、验证状态、输出日志)
  5. disconnect: 断开 SSH 连接

📚 更多文档

🤝 贡献

欢迎提交 Issue 和 Pull Request!

  1. Fork 本仓库
  2. 创建特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 开启 Pull Request

📄 License

MIT License - 详见 LICENSE 文件

🙏 致谢

感谢以下开源项目:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hostweaver-0.1.0.tar.gz (44.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hostweaver-0.1.0-py3-none-any.whl (34.9 kB view details)

Uploaded Python 3

File details

Details for the file hostweaver-0.1.0.tar.gz.

File metadata

  • Download URL: hostweaver-0.1.0.tar.gz
  • Upload date:
  • Size: 44.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hostweaver-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7fb915e4fd524a1d567503022c9fdf9f3a11823b3ac20c2fc24cd45cfdd7b44c
MD5 eb85edd8b3e360d26ca6f22e8e81ab8c
BLAKE2b-256 7a59e481f0f589101853bdb9f87f32acaeb6ae4e5bee2337cd5506d4f75582cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for hostweaver-0.1.0.tar.gz:

Publisher: workflow.yml on zsuroy/hostweaver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hostweaver-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hostweaver-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 34.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hostweaver-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 862af39dd1d0e2f9e76548217101c867d76d22609b1dccdc3ee5b8c9a2d0dfed
MD5 68192bcd6f15c15b39c322bb4014dc62
BLAKE2b-256 2e36250fce00177431cec1e6db783045e508be95c28ee1f204a6ee63a6b24e8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for hostweaver-0.1.0-py3-none-any.whl:

Publisher: workflow.yml on zsuroy/hostweaver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page