Upload
mike-muzurakis
View
269
Download
1
Embed Size (px)
DESCRIPTION
Apache Libcloud is a Python library which hides differences between different cloud provider APIs and allows you to manage different cloud resources through a unified and easy to use API. In this presentation we will go through the process of adding a new driver for a Cloud provider and submitting the changes but to the libcloud project
Citation preview
How to create a
libcloud driver from scratchthe NephoScale case
Libcloudone library to rule them all
Supports many providers
What if your provider is not supported?
● Clone● Branch● Write● Test● Merge back
Time to contribute
TasklistBackend actions
● list_nodes: list all nodes, including ip addresses, node state,
metadata, etc
● list_images: list all the images the provider supports
● list_sizes: list different plans, providing data such as CPU, ram and
disk size
● list_locations: List all available provider zones when applicable
TasklistNode actions
● create_node: create a node specifying it’s size, image, name and
location
● deploy_node: create a node and deploy a public ssh key and
optionally run a deploy script
● reboot_node/shut down_node
● start_node/stop_node: start a stopped node, or stop a started one
● destroy_node: Delete the node
Getting readyAPIs and drivers
● Familiarize yourself with the provider API
● Choose a recent libcloud driver to base your code on
● Don’t break conventions. Do it the libcloud way
libcloud repoClone and extend
● Clone libcloud from https://github.com/apache/libcloud
● Libcloud drivers live in libcloud/compute/drivers
● They all inherit from the base driver at
libcloud/compute/base.py
● Create your git branch
● Add your driver on libcloud/compute/providers.py
● Add your Provider class on libcloud/compute/types.py
Create driver fileCreate libcloud/compute/drivers/nephoscale.py
Specify the cloud provider API endpoint
API_HOST = 'api.nephoscale.com'
and the possible node states
NODE_STATE_MAP = {'on': NodeState.RUNNING,'off': NodeState.UNKNOWN,'unknown': NodeState.UNKNOWN,
}
Writing the driverWe need to create a NodeDriver based driver
class NephoscaleNodeDriver(NodeDriver): """Nephoscale node driver class"""
type = Provider.NEPHOSCALE api_name = 'nephoscale' name = 'NephoScale' website = 'http://www.nephoscale.com' connectionCls = NephoscaleConnection features = {'create_node': ['ssh_key']}
def list_locations(self): ...
Writing the Connection classand write a Connection class
class NephoscaleConnection(ConnectionUserAndKey):
host = API_HOST responseCls = NephoscaleResponse
def add_default_headers(self, headers): user_b64 = base64.b64encode(b('%s:%s' % (self.user_id, self.key))) headers['Authorization'] = 'Basic %s' % (user_b64.decode('utf-8')) return headers
Writing the Response classResponse classes derive from JsonResponse or XMLRPCResponse
class NephoscaleResponse(JsonResponse): """Nephoscale API Response"""
def parse_error(self): if self.status == httplib.UNAUTHORIZED: raise InvalidCredsError('Authorization Failed') if self.status == httplib.NOT_FOUND: raise Exception("The resource you are looking for is not found.")
return self.body
Keep consistencyKeep consistency, hardcode things when necessary
● Most of the times you don’t need to override deploy_node
● Libcloud standardizes things for providers (nodes/sizes/images/locations)
● When a provider does non-standard stuff, override
● Some providers don’t return enough details
● Hardcode only when necessary
Testing the driverTest your driver often to make sure it works
user@user:~/dev/libcloud$ python>>> from libcloud.compute.types import Provider>>> from libcloud.compute.providers import get_driver>>> driver = get_driver(Provider.NEPHOSCALE)>>> conn = driver('user','correct_password')>>> conn.list_nodes()[<Node: uuid=e20bdbf7ef6890645f5b217e0bd2b5912b969cc1, name=nepho-7, state=0, public_ips=['198.89.109.116'], provider=NephoScale ...>]
Contributing to libcloud
Some ground rules and contribution workflow:
● Open a ticket on the tracker to notify any interested parties
● Write unit tests
● Follow code style (follow PEP8, use spaces for tabs, etc)
● Commit and open a pull request
● Wait for a review and make any changes pointed
● Create a patch with your changes and add it to your ticket
Thank you!To check the driver in action, visit https://mist.io
More resources:
● https://libcloud.readthedocs.org/en/latest/development.html
● https://github.com/apache/libcloud
● https://libcloud.readthedocs.org/en/latest/developer_information.html
Special thanks to Tomaz, maintainer of libcloud and a very helpful guy :)