Skip to content

CLI 参考

weave-cli 二进制文件提供对 Weave 平台的命令行访问,用于脚本和自动化。

安装

从源码构建:

bash
make build
# 二进制文件:./bin/weave-cli

或从发布包复制到 $PATH 中的目录。

配置

weave-cli 从环境变量或命令行参数读取配置:

环境变量参数描述
WEAVE_URL--url服务器基础 URL(例如 https://weave.yun
WEAVE_TOKEN--tokenJWT 访问令牌或 API 密钥
WEAVE_TENANT_ID--tenant-id租户 UUID

也可以在当前目录的 .env 文件中设置。

认证

bash
# 登录并存储令牌
weave-cli auth login --email admin@example.com --password secret

# 显示当前令牌信息
weave-cli auth me

命令

设备

bash
# 列出设备
weave-cli devices list
weave-cli devices list --group <group-id>
weave-cli devices list --status online

# 获取设备详情
weave-cli devices get <device-id>

# 生成认领令牌
weave-cli devices claim-token --group <group-id> --ttl 24h

# 更新设备
weave-cli devices update <device-id> --name "new-name" --group <group-id>

设备组

bash
# 列出分组
weave-cli groups list

# 创建分组
weave-cli groups create --name "prod-fleet" --description "生产设备"

# 删除分组
weave-cli groups delete <group-id>

应用

bash
# 列出应用
weave-cli apps list

# 创建应用
weave-cli apps create --name nginx --type container

# 创建版本
weave-cli apps versions create <app-id> --version 1.0.0 --spec '{"image":"nginx:latest"}'

# 列出版本
weave-cli apps versions list <app-id>

发布

bash
# 列出发布
weave-cli rollouts list
weave-cli rollouts list --status running

# 获取发布详情
weave-cli rollouts get <rollout-id>

# 创建发布
weave-cli rollouts create \
  --name "firmware-v2" \
  --group <group-id> \
  --strategy rolling \
  --batch-size 10 \
  --failure-threshold 20 \
  --target-version '{"firmware":"v2.0.0"}'

# 执行发布
weave-cli rollouts execute <rollout-id>

# 暂停 / 恢复 / 取消
weave-cli rollouts pause <rollout-id>
weave-cli rollouts resume <rollout-id>
weave-cli rollouts cancel <rollout-id>

影子状态

bash
# 获取设备影子
weave-cli shadow get <device-id>

# 设置期望状态
weave-cli shadow set <device-id> '{"apps":{"nginx":{"version":"1.0.0"}}}'

用户

bash
# 列出租户中的用户
weave-cli users list

# 创建用户
weave-cli users create --email user@example.com --password secret --role operator

# 更改用户角色
weave-cli users set-role <user-id> --role tenant_admin

API 密钥

bash
# 列出 API 密钥
weave-cli apikeys list

# 生成 API 密钥
weave-cli apikeys generate --name ci-deploy --role operator --expires 2025-12-31

# 撤销 API 密钥
weave-cli apikeys revoke <key-id>

事件

bash
# 列出事件
weave-cli events list
weave-cli events list --device <device-id> --type device.online
weave-cli events list --from 2024-01-01 --to 2024-01-31

租户(超级管理员)

bash
# 列出所有租户
weave-cli tenants list

# 创建租户
weave-cli tenants create --name "Acme Corp"

# 获取租户详情
weave-cli tenants get <tenant-id>

证书管理

详见证书管理获取完整的 weave cert 命令参考。

输出格式

大多数列表命令支持 --output 参数:

bash
weave-cli devices list --output json    # JSON 数组
weave-cli devices list --output table   # ASCII 表格(默认)
weave-cli devices list --output yaml    # YAML

退出码

代码含义
0成功
1一般错误
2认证错误
3权限被拒绝
4资源未找到

示例

跨设备集群部署应用更新

bash
# 创建新应用版本
APP_ID=$(weave-cli apps list --output json | jq -r '.[] | select(.name=="nginx") | .id')
weave-cli apps versions create "$APP_ID" --version 1.1.0 --spec '{"image":"nginx:1.25"}'

# 创建并执行滚动发布
ROLLOUT_ID=$(weave-cli rollouts create \
  --name "nginx-1.1.0" \
  --group prod-fleet-group-id \
  --strategy rolling \
  --batch-size 5 \
  --failure-threshold 10 \
  --target-version "{\"apps\":{\"nginx\":{\"version\":\"1.1.0\"}}}" \
  --output json | jq -r '.id')

weave-cli rollouts execute "$ROLLOUT_ID"

# 监控进度
watch weave-cli rollouts get "$ROLLOUT_ID"

为新设备生成认领令牌

bash
TOKEN=$(weave-cli devices claim-token --group prod-fleet-id --ttl 1h --output json | jq -r '.token')
echo "在设备上运行:weave-agent --claim-token $TOKEN --server grpc.weave.yun:8443"

Weave — IoT Device Management Platform