跳转到主要内容

Python和命令行的一个简单而强大的CloudStack API客户端。

项目描述

License Python versions

Python和命令行的简单而强大的CloudStack API客户端。

  • 异步支持。

  • 支持所有现有的和未来的CloudStack API调用和参数。

  • 如果安装了Pygments,则命令行客户端支持语法高亮。

  • BSD许可证。

安装

pip install cs

# with the colored output
pip install cs[highlight]

# with the async support
pip install cs[async]

# with both
pip install cs[async,highlight]

使用

在Python中

from cs import CloudStack

cs = CloudStack(endpoint='https://api.exoscale.ch/v1',
                key='cloudstack api key',
                secret='cloudstack api secret')

vms = cs.listVirtualMachines()

cs.createSecurityGroup(name='web', description='HTTP traffic')

从命令行,这需要一些配置

cat $HOME/.cloudstack.ini
[cloudstack]
endpoint = https://api.exoscale.ch/v1
key = cloudstack api key
secret = cloudstack api secret
# Optional ca authority certificate
verify = /path/to/certs/exoscale_ca.crt
# Optional client PEM certificate
cert = /path/to/client_exoscale.pem
# If you need to pass the certificate and key as separate files
cert_key = /path/to/client_key.pem

然后

$ cs listVirtualMachines
{
  "count": 1,
  "virtualmachine": [
    {
      "account": "...",
      ...
    }
  ]
}
$ cs authorizeSecurityGroupIngress \
    cidrlist="0.0.0.0/0" endport=443 startport=443 \
    securitygroupname="blah blah" protocol=tcp

命令行客户端在异步结果返回时轮询。要禁用轮询,请使用--async标志。

要查找CloudStack API调用列表,请访问http://cloudstack.apache.org/api.html

配置

配置从几个位置读取,顺序如下

  • CLOUDSTACK_ENDPOINTCLOUDSTACK_KEYCLOUDSTACK_SECRETCLOUDSTACK_METHOD 环境变量,

  • 指向一个.ini文件的CLOUDSTACK_CONFIG环境变量,

  • 指向一个CA授权证书文件的CLOUDSTACK_VERIFY(可选)环境变量,

  • 指向一个客户端PEM证书文件的CLOUDSTACK_CERT(可选)环境变量,

  • 指向一个客户端PEM证书密钥文件的CLOUDSTACK_CERT_KEY(可选)环境变量,

  • 当前工作目录中的cloudstack.ini文件,

  • 在用户主目录下有一个 .cloudstack.ini 配置文件。

要从Python代码中使用该配置方案

from cs import CloudStack, read_config

cs = CloudStack(**read_config())

注意,如果在配置文件中找不到配置,read_config() 函数可能会引发 SystemExit

CLOUDSTACK_METHOD 或配置文件中的 method 条目可以用来改变用于CloudStack请求的HTTP方法。默认情况下,使用GET方法进行请求,但CloudStack支持POST请求。POST请求可以用来克服CloudStack API中的一些长度限制。

CLOUDSTACK_TIMEOUT 或配置文件中的 timeout 条目可以用来改变在执行CloudStack请求时的HTTP超时时间(以秒为单位)。默认值为10。

CLOUDSTACK_RETRY 或配置文件中的 retry 条目(整数值)可以用来在请求失败时重试 listqueryAsync 请求。默认值为0,表示不重试。

CLOUDSTACK_JOB_TIMEOUT 或配置文件中的 job_timeout 条目(浮点数)可以用来设置异步调用重试的时间长度,假设 fetch_result 已设置为true。默认值为 None,表示无限期等待。

CLOUDSTACK_POLL_INTERVAL 或配置文件中的 poll_interval 条目(秒数,浮点数)可以用来设置异步作业结果轮询的频率。默认值为2。

CLOUDSTACK_EXPIRATION 或配置文件中的 expiration 条目(整数值)可以用来设置签名有效的时间长度。默认值为10分钟,但可以使用任何负值来禁用,例如-1。

CLOUDSTACK_DANGEROUS_NO_TLS_VERIFY 或配置文件中的 dangerous_no_tls_verify 条目(布尔值)可以用来禁用使用HTTPS协议时进行的TLS验证。

可以在 .cloudstack.ini 中设置多个凭证。这允许使用命令行标志选择要使用的凭证或端点。

[cloudstack]
endpoint = https://some-host/api/v1
key = api key
secret = api secret

[exoscale]
endpoint = https://api.exoscale.ch/v1
key = api key
secret = api secret

使用

$ cs listVirtualMachines --region=exoscale

可选地,可以使用 CLOUDSTACK_REGION 来覆盖默认区域 cloudstack

对于不希望在磁盘上存储任何秘密的高级用户,CLOUDSTACK_OVERRIDES 允许您选择即使存在于ini文件中,也将从环境变量中设置哪个密钥。

分页

CloudStack对请求进行分页。 cs 能够抽象分页逻辑,允许一次获取大量结果集。这是通过 fetch_list 参数完成的。

$ cs listVirtualMachines fetch_list=true

或者,在Python中

cs.listVirtualMachines(fetch_list=True)

跟踪HTTP请求

偶尔,了解底层的HTTP调用可能会有所帮助。 trace 标志(或 CLOUDSTACK_TRACE)正是为此而设计。

$ cs --trace listVirtualMachines

$ cs -t listZones

异步客户端

cs 为Python 3.5+提供了 AIOCloudStack 类,用于异步/等待调用。

import asyncio
from cs import AIOCloudStack, read_config

cs = AIOCloudStack(**read_config())

async def main():
   vms = await cs.listVirtualMachines(fetch_list=True)
   print(vms)

asyncio.run(main())

多虚拟机异步部署

import asyncio
from cs import AIOCloudStack, read_config

cs = AIOCloudStack(**read_config())

machine = {"zoneid": ..., "serviceofferingid": ..., "templateid": ...}

async def main():
   tasks = asyncio.gather(*(cs.deployVirtualMachine(name=f"vm-{i}",
                                                    **machine,
                                                    fetch_result=True)
                            for i in range(5)))

   results = await tasks

   # Destroy all of them, but skip waiting on the job results
   await asyncio.gather(*(cs.destroyVirtualMachine(id=result['virtualmachine']['id'])
                          for result in results))

asyncio.run(main())

发布程序

mktmpenv -p /usr/bin/python3
pip install -U twine wheel build
cd exoscale/cs
rm -rf build dist
python -m build
twine upload dist/*

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

cs-3.2.0.tar.gz (15.1 kB 查看哈希值)

上传时间 源代码

构建分发

cs-3.2.0-py2.py3-none-any.whl (13.7 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面