HomeHome ArchiveArchive

manually initializing the pycloudsigma module

How to manually initialize the pycloudsigma module - possibly from libcloud data.

pycloudsigma expects its configuration in ~/.cloudsigma.conf.

However if you are - as an example - working with both libcloud and its Cloudsigma driver then you allready had to configure those things for the libcloud driver and optimaly would not want to do the initialization twice.

Here is a solution to initialize the pycloudsigma module from the libcloud configuration. It should be easily adaptable to initializing it with plain strings withouth needing to fetch the configuration items from libcloud.

def init_cloudsigma_from_libcloud_driver(libcloud_driver):
    # let the cloudsigma module load an empty configuration file
    os.putenv('CLOUDSIGMA_CONFIG','/dev/null')

    # now import (and initialize) the cloudsigma module
    import cloudsigma

    # Set all pycloudsigma config parameters manually.
    #
    # These are the parameters that are usually configured
    # from ~/.cloudsigma.conf. See 
    # https://github.com/cloudsigma/pycloudsigma/blob/master/README.md
    # for further documentation.
    #
    # Below we get the required config parameters from
    # libcloud's cloudsigma driver, however these could
    # also be set explicitly via plaintext strings.
    #
    cloudsigma.conf.config.__setitem__('api_endpoint', ("https://%s.cloudsigma.com/api/2.0/"       %
                                                       libcloud_driver.region))
    cloudsigma.conf.config.__setitem__('ws_endpoint',  ("wss://direct.%s.cloudsigma.com/websocket" %
                                                       libcloud_driver.region))
    cloudsigma.conf.config.__setitem__('username',     libcloud_driver.connection.user_id)
    cloudsigma.conf.config.__setitem__('password',     libcloud_driver.connection.key)