How to create a libcloud driver from scratch
Click here to load reader
-
Upload
mistio -
Category
Technology
-
view
72 -
download
2
description
Transcript of How to create a libcloud driver from scratch
![Page 1: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/1.jpg)
How to create a
libcloud driver from scratch
the NephoScale case
![Page 2: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/2.jpg)
Libcloud
one library to rule them all
![Page 3: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/3.jpg)
Supports many providers
![Page 4: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/4.jpg)
What if your provider is not
supported?
● Clone
● Branch
● Write
● Test
● Merge back
Time to contribute
![Page 5: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/5.jpg)
Tasklist
Backend 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
![Page 6: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/6.jpg)
Tasklist
Node 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
![Page 7: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/7.jpg)
Getting ready
APIs 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
![Page 8: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/8.jpg)
libcloud repo
Clone 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
![Page 9: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/9.jpg)
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,
}
![Page 10: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/10.jpg)
Writing the driverWe need to create a NodeDriver based driver
class NephoscaleNodeDriver(NodeDriver):"""Nephoscale node driver class"""
type = Provider.NEPHOSCALEapi_name = 'nephoscale'name = 'NephoScale'website = 'http://www.nephoscale.com'connectionCls = NephoscaleConnectionfeatures = {'create_node': ['ssh_key']}
def list_locations(self):...
![Page 11: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/11.jpg)
Writing the Connection classand write a Connection class
class NephoscaleConnection(ConnectionUserAndKey):
host = API_HOSTresponseCls = 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
![Page 12: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/12.jpg)
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
![Page 13: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/13.jpg)
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
![Page 14: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/14.jpg)
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 ...>]
![Page 15: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/15.jpg)
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
![Page 16: How to create a libcloud driver from scratch](https://reader038.fdocuments.us/reader038/viewer/2022100420/559b63431a28ab1f258b45b9/html5/thumbnails/16.jpg)
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 :)