cs


Namecs JSON
Version 3.2.0 PyPI version JSON
download
home_pagehttps://github.com/exoscale/cs
SummaryA simple yet powerful CloudStack API client for Python and the command-line.
upload_time2023-06-22 07:16:39
maintainer
docs_urlNone
authorBruno Renié
requires_python
licenseBSD
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            CS
==

.. image:: https://img.shields.io/pypi/l/cs.svg
   :alt: License
   :target: https://pypi.org/project/cs/

.. image:: https://img.shields.io/pypi/pyversions/cs.svg
   :alt: Python versions
   :target: https://pypi.org/project/cs/

A simple, yet powerful CloudStack API client for python and the command-line.

* Async support.
* All present and future CloudStack API calls and parameters are supported.
* Syntax highlight in the command-line client if Pygments is installed.
* BSD license.

Installation
------------

::

    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]

Usage
-----

In Python:

.. code-block:: 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')

From the command-line, this requires some configuration:

.. code-block:: console

    cat $HOME/.cloudstack.ini

.. code-block:: 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

Then:

.. code-block:: console

    $ cs listVirtualMachines

.. code-block:: json

    {
      "count": 1,
      "virtualmachine": [
        {
          "account": "...",
          ...
        }
      ]
    }

.. code-block:: console

    $ cs authorizeSecurityGroupIngress \
        cidrlist="0.0.0.0/0" endport=443 startport=443 \
        securitygroupname="blah blah" protocol=tcp

The command-line client polls when async results are returned. To disable
polling, use the ``--async`` flag.

To find the list CloudStack API calls go to
http://cloudstack.apache.org/api.html

Configuration
-------------

Configuration is read from several locations, in the following order:

* The ``CLOUDSTACK_ENDPOINT``, ``CLOUDSTACK_KEY``, ``CLOUDSTACK_SECRET`` and
  ``CLOUDSTACK_METHOD`` environment variables,
* A ``CLOUDSTACK_CONFIG`` environment variable pointing to an ``.ini`` file,
* A ``CLOUDSTACK_VERIFY`` (optional) environment variable pointing to a CA authority cert file,
* A ``CLOUDSTACK_CERT`` (optional) environment variable pointing to a client PEM cert file,
* A ``CLOUDSTACK_CERT_KEY`` (optional) environment variable pointing to a client PEM certificate key file,
* A ``cloudstack.ini`` file in the current working directory,
* A ``.cloudstack.ini`` file in the home directory.

To use that configuration scheme from your Python code:

.. code-block:: python

    from cs import CloudStack, read_config

    cs = CloudStack(**read_config())

Note that ``read_config()`` can raise ``SystemExit`` if no configuration is
found.

``CLOUDSTACK_METHOD`` or the ``method`` entry in the configuration file can be
used to change the HTTP verb used to make CloudStack requests. By default,
requests are made with the GET method but CloudStack supports POST requests.
POST can be useful to overcome some length limits in the CloudStack API.

``CLOUDSTACK_TIMEOUT`` or the ``timeout`` entry in the configuration file can
be used to change the HTTP timeout when making CloudStack requests (in
seconds). The default value is 10.

``CLOUDSTACK_RETRY`` or the ``retry`` entry in the configuration file
(integer) can be used to retry ``list`` and ``queryAsync`` requests on
failure. The default value is 0, meaning no retry.

``CLOUDSTACK_JOB_TIMEOUT`` or the `job_timeout`` entry in the configuration file
(float) can be used to set how long an async call is retried assuming ``fetch_result`` is set to true). The default value is ``None``, it waits forever.

``CLOUDSTACK_POLL_INTERVAL`` or the ``poll_interval`` entry in the configuration file (number of seconds, float) can be used to set how frequently polling an async job result is done. The default value is 2.

``CLOUDSTACK_EXPIRATION`` or the ``expiration`` entry in the configuration file
(integer) can be used to set how long a signature is valid. By default, it picks
10 minutes but may be deactivated using any negative value, e.g. -1.

``CLOUDSTACK_DANGEROUS_NO_TLS_VERIFY`` or the ``dangerous_no_tls_verify`` entry
in the configuration file (boolean) can be used to deactivate the TLS verification
made when using the HTTPS protocol.

Multiple credentials can be set in ``.cloudstack.ini``. This allows selecting
the credentials or endpoint to use with a command-line flag.

.. code-block:: 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

Usage::

    $ cs listVirtualMachines --region=exoscale

Optionally ``CLOUDSTACK_REGION`` can be used to overwrite the default region ``cloudstack``.

For the power users that don't want to put any secrets on disk,
``CLOUDSTACK_OVERRIDES`` let you pick which key will be set from the
environment even if present in the ini file.


Pagination
----------

CloudStack paginates requests. ``cs`` is able to abstract away the pagination
logic to allow fetching large result sets in one go. This is done with the
``fetch_list`` parameter::

    $ cs listVirtualMachines fetch_list=true

Or in Python::

    cs.listVirtualMachines(fetch_list=True)

Tracing HTTP requests
---------------------

Once in a while, it could be useful to understand, see what HTTP calls are made
under the hood. The ``trace`` flag (or ``CLOUDSTACK_TRACE``) does just that::

   $ cs --trace listVirtualMachines

   $ cs -t listZones

Async client
------------

``cs`` provides the ``AIOCloudStack`` class for async/await calls in Python
3.5+.

.. code-block:: python

    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())

Async deployment of multiple VMs
________________________________

.. code-block:: python

    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())

Release Procedure
-----------------

.. code-block:: shell-session

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

Links
-----

* CloudStack API: http://cloudstack.apache.org/api.html
* Example of use: `Get Started with the exoscale API client <https://www.exoscale.com/syslog/2016/02/23/get-started-with-the-exoscale-api-client/>`_

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/exoscale/cs",
    "name": "cs",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Bruno Reni\u00e9",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/68/09/f2ed131a6b154b371ca59c0cce2368be3020ef577980663b739aac90424f/cs-3.2.0.tar.gz",
    "platform": null,
    "description": "CS\n==\n\n.. image:: https://img.shields.io/pypi/l/cs.svg\n   :alt: License\n   :target: https://pypi.org/project/cs/\n\n.. image:: https://img.shields.io/pypi/pyversions/cs.svg\n   :alt: Python versions\n   :target: https://pypi.org/project/cs/\n\nA simple, yet powerful CloudStack API client for python and the command-line.\n\n* Async support.\n* All present and future CloudStack API calls and parameters are supported.\n* Syntax highlight in the command-line client if Pygments is installed.\n* BSD license.\n\nInstallation\n------------\n\n::\n\n    pip install cs\n\n    # with the colored output\n    pip install cs[highlight]\n\n    # with the async support\n    pip install cs[async]\n\n    # with both\n    pip install cs[async,highlight]\n\nUsage\n-----\n\nIn Python:\n\n.. code-block:: python\n\n    from cs import CloudStack\n\n    cs = CloudStack(endpoint='https://api.exoscale.ch/v1',\n                    key='cloudstack api key',\n                    secret='cloudstack api secret')\n\n    vms = cs.listVirtualMachines()\n\n    cs.createSecurityGroup(name='web', description='HTTP traffic')\n\nFrom the command-line, this requires some configuration:\n\n.. code-block:: console\n\n    cat $HOME/.cloudstack.ini\n\n.. code-block:: ini\n\n    [cloudstack]\n    endpoint = https://api.exoscale.ch/v1\n    key = cloudstack api key\n    secret = cloudstack api secret\n    # Optional ca authority certificate\n    verify = /path/to/certs/exoscale_ca.crt\n    # Optional client PEM certificate\n    cert = /path/to/client_exoscale.pem\n    # If you need to pass the certificate and key as separate files\n    cert_key = /path/to/client_key.pem\n\nThen:\n\n.. code-block:: console\n\n    $ cs listVirtualMachines\n\n.. code-block:: json\n\n    {\n      \"count\": 1,\n      \"virtualmachine\": [\n        {\n          \"account\": \"...\",\n          ...\n        }\n      ]\n    }\n\n.. code-block:: console\n\n    $ cs authorizeSecurityGroupIngress \\\n        cidrlist=\"0.0.0.0/0\" endport=443 startport=443 \\\n        securitygroupname=\"blah blah\" protocol=tcp\n\nThe command-line client polls when async results are returned. To disable\npolling, use the ``--async`` flag.\n\nTo find the list CloudStack API calls go to\nhttp://cloudstack.apache.org/api.html\n\nConfiguration\n-------------\n\nConfiguration is read from several locations, in the following order:\n\n* The ``CLOUDSTACK_ENDPOINT``, ``CLOUDSTACK_KEY``, ``CLOUDSTACK_SECRET`` and\n  ``CLOUDSTACK_METHOD`` environment variables,\n* A ``CLOUDSTACK_CONFIG`` environment variable pointing to an ``.ini`` file,\n* A ``CLOUDSTACK_VERIFY`` (optional) environment variable pointing to a CA authority cert file,\n* A ``CLOUDSTACK_CERT`` (optional) environment variable pointing to a client PEM cert file,\n* A ``CLOUDSTACK_CERT_KEY`` (optional) environment variable pointing to a client PEM certificate key file,\n* A ``cloudstack.ini`` file in the current working directory,\n* A ``.cloudstack.ini`` file in the home directory.\n\nTo use that configuration scheme from your Python code:\n\n.. code-block:: python\n\n    from cs import CloudStack, read_config\n\n    cs = CloudStack(**read_config())\n\nNote that ``read_config()`` can raise ``SystemExit`` if no configuration is\nfound.\n\n``CLOUDSTACK_METHOD`` or the ``method`` entry in the configuration file can be\nused to change the HTTP verb used to make CloudStack requests. By default,\nrequests are made with the GET method but CloudStack supports POST requests.\nPOST can be useful to overcome some length limits in the CloudStack API.\n\n``CLOUDSTACK_TIMEOUT`` or the ``timeout`` entry in the configuration file can\nbe used to change the HTTP timeout when making CloudStack requests (in\nseconds). The default value is 10.\n\n``CLOUDSTACK_RETRY`` or the ``retry`` entry in the configuration file\n(integer) can be used to retry ``list`` and ``queryAsync`` requests on\nfailure. The default value is 0, meaning no retry.\n\n``CLOUDSTACK_JOB_TIMEOUT`` or the `job_timeout`` entry in the configuration file\n(float) can be used to set how long an async call is retried assuming ``fetch_result`` is set to true). The default value is ``None``, it waits forever.\n\n``CLOUDSTACK_POLL_INTERVAL`` or the ``poll_interval`` entry in the configuration file (number of seconds, float) can be used to set how frequently polling an async job result is done. The default value is 2.\n\n``CLOUDSTACK_EXPIRATION`` or the ``expiration`` entry in the configuration file\n(integer) can be used to set how long a signature is valid. By default, it picks\n10 minutes but may be deactivated using any negative value, e.g. -1.\n\n``CLOUDSTACK_DANGEROUS_NO_TLS_VERIFY`` or the ``dangerous_no_tls_verify`` entry\nin the configuration file (boolean) can be used to deactivate the TLS verification\nmade when using the HTTPS protocol.\n\nMultiple credentials can be set in ``.cloudstack.ini``. This allows selecting\nthe credentials or endpoint to use with a command-line flag.\n\n.. code-block:: ini\n\n    [cloudstack]\n    endpoint = https://some-host/api/v1\n    key = api key\n    secret = api secret\n\n    [exoscale]\n    endpoint = https://api.exoscale.ch/v1\n    key = api key\n    secret = api secret\n\nUsage::\n\n    $ cs listVirtualMachines --region=exoscale\n\nOptionally ``CLOUDSTACK_REGION`` can be used to overwrite the default region ``cloudstack``.\n\nFor the power users that don't want to put any secrets on disk,\n``CLOUDSTACK_OVERRIDES`` let you pick which key will be set from the\nenvironment even if present in the ini file.\n\n\nPagination\n----------\n\nCloudStack paginates requests. ``cs`` is able to abstract away the pagination\nlogic to allow fetching large result sets in one go. This is done with the\n``fetch_list`` parameter::\n\n    $ cs listVirtualMachines fetch_list=true\n\nOr in Python::\n\n    cs.listVirtualMachines(fetch_list=True)\n\nTracing HTTP requests\n---------------------\n\nOnce in a while, it could be useful to understand, see what HTTP calls are made\nunder the hood. The ``trace`` flag (or ``CLOUDSTACK_TRACE``) does just that::\n\n   $ cs --trace listVirtualMachines\n\n   $ cs -t listZones\n\nAsync client\n------------\n\n``cs`` provides the ``AIOCloudStack`` class for async/await calls in Python\n3.5+.\n\n.. code-block:: python\n\n    import asyncio\n    from cs import AIOCloudStack, read_config\n\n    cs = AIOCloudStack(**read_config())\n\n    async def main():\n       vms = await cs.listVirtualMachines(fetch_list=True)\n       print(vms)\n\n    asyncio.run(main())\n\nAsync deployment of multiple VMs\n________________________________\n\n.. code-block:: python\n\n    import asyncio\n    from cs import AIOCloudStack, read_config\n\n    cs = AIOCloudStack(**read_config())\n\n    machine = {\"zoneid\": ..., \"serviceofferingid\": ..., \"templateid\": ...}\n\n    async def main():\n       tasks = asyncio.gather(*(cs.deployVirtualMachine(name=f\"vm-{i}\",\n                                                        **machine,\n                                                        fetch_result=True)\n                                for i in range(5)))\n\n       results = await tasks\n\n       # Destroy all of them, but skip waiting on the job results\n       await asyncio.gather(*(cs.destroyVirtualMachine(id=result['virtualmachine']['id'])\n                              for result in results))\n\n    asyncio.run(main())\n\nRelease Procedure\n-----------------\n\n.. code-block:: shell-session\n\n    mktmpenv -p /usr/bin/python3\n    pip install -U twine wheel build\n    cd exoscale/cs\n    rm -rf build dist\n    python -m build\n    twine upload dist/*\n\nLinks\n-----\n\n* CloudStack API: http://cloudstack.apache.org/api.html\n* Example of use: `Get Started with the exoscale API client <https://www.exoscale.com/syslog/2016/02/23/get-started-with-the-exoscale-api-client/>`_\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "A simple yet powerful CloudStack API client for Python and the command-line.",
    "version": "3.2.0",
    "project_urls": {
        "Homepage": "https://github.com/exoscale/cs"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9e73ac396cd9431d9aec223387540312f045caba29244dfd05d20392a5cff382",
                "md5": "a5212f9b8e07cd69322752c875abd9fd",
                "sha256": "2c5a7813c592056d7220b30baebea376aeae8d6ef09de347b309b6f659181a39"
            },
            "downloads": -1,
            "filename": "cs-3.2.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a5212f9b8e07cd69322752c875abd9fd",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 13651,
            "upload_time": "2023-06-22T07:16:37",
            "upload_time_iso_8601": "2023-06-22T07:16:37.189692Z",
            "url": "https://files.pythonhosted.org/packages/9e/73/ac396cd9431d9aec223387540312f045caba29244dfd05d20392a5cff382/cs-3.2.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6809f2ed131a6b154b371ca59c0cce2368be3020ef577980663b739aac90424f",
                "md5": "f5e9d10bc9ed8da95d820f8bf2878147",
                "sha256": "c2da85d7dff114a867feb56fbeb9e7bdeaf1ee6883535ce231988ad200a3a66e"
            },
            "downloads": -1,
            "filename": "cs-3.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "f5e9d10bc9ed8da95d820f8bf2878147",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 15112,
            "upload_time": "2023-06-22T07:16:39",
            "upload_time_iso_8601": "2023-06-22T07:16:39.339732Z",
            "url": "https://files.pythonhosted.org/packages/68/09/f2ed131a6b154b371ca59c0cce2368be3020ef577980663b739aac90424f/cs-3.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-22 07:16:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "exoscale",
    "github_project": "cs",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "cs"
}
        
Elapsed time: 0.16485s