apache-libcloud<\/tt> package.<\/p>\n\n\n\npip install apache-libcloud<\/pre>\n\n\n\nIf you already had a version of Libcloud installed, you can upgrade it with the next command.<\/p>\n\n\n\n
pip install --upgrade apache-libcloud<\/pre>\n\n\n\nTo get the most up-to-date packages, you will need to use the development version. The Libcloud driver for UpCloud will initially be only available on the development\u00a0channel until it gets included in the stable release.<\/p>\n\n\n\n
Install the Libcloud development version with the command underneath.<\/p>\n\n\n\n
pip install -e git+https:\/\/git-wip-us.apache.org\/repos\/asf\/libcloud.git@trunk#egg=apache-libcloud<\/pre>\n\n\n\nWith the libraries installed, you are then ready to start programming. Continue below with examples of how to create and manage nodes on UpCloud using the Libcloud API.<\/p>\n\n\n\n
Instantiating a new driver<\/h2>\n\n\n\n
To issue commands to the UpCloud services, you first need to instantiate a driver with your credentials. Begin by creating a file called libctest.py<\/tt> with the following content.<\/p>\n\n\n\n#!\/usr\/bin\/env python\nimport os\nimport libcloud\nfrom pprint import pprint\n\ncls = libcloud.get_driver(libcloud.DriverType.COMPUTE,libcloud.DriverType.COMPUTE.UPCLOUD)\ndriver = cls(os.environ['UpCloud_API_username'], os.environ['UpCloud_API_password'])\n\npprint(driver.list_locations())<\/pre>\n\n\n\nNext, assign\u00a0your credentials to environmental variables named\u00a0UpCloud_API_username<\/tt>\u00a0and UpCloud_API_password<\/tt>. For example on Linux use the following commands.<\/p>\n\n\n\nexport UpCloud_API_username='username'\nexport UpCloud_API_password='password'\n<\/tt><\/tt><\/pre>\n\n\n\nThese can also be stored permanently in your .bashrc<\/tt> or .zshrc<\/tt> profiles avoiding having to rerun the export commands manually on every new terminal.<\/p>\n\n\n\n
Set the test program as executable with the next command.<\/p>\n\n\n\n
chmod +x libctest.py<\/pre>\n\n\n\nThen run the short program on the terminal to test that it connects to the UpCloud API correctly using the command below.<\/p>\n\n\n\n
.\/libctest<\/tt>.py<\/pre>\n\n\n\n[<NodeLocation: id=de-fra1, name=Frankfurt #1, country=DE, driver=Upcloud>,\n <NodeLocation: id=fi-hel1, name=Helsinki #1, country=FI, driver=Upcloud>,\n <NodeLocation: id=fi-hel2, name=Helsinki #2, country=FI, driver=Upcloud>,\n <NodeLocation: id=nl-ams1, name=Amsterdam #1, country=NL, driver=Upcloud>,\n <NodeLocation: id=sg-sin1, name=Singapore #1, country=SG, driver=Upcloud>,\n <NodeLocation: id=uk-lon1, name=London #1, country=UK, driver=Upcloud>,\n <NodeLocation: id=us-chi1, name=Chicago #1, country=US, driver=Upcloud>]<\/pre>\n\n\n\nIf you see an output like the above, your driver is working!<\/p>\n\n\n\n
Creating a new server<\/h2>\n\n\n\n
The basic process of creating a new server requires the following minimum parameters: system image, configuration size, data centre location, and a name for the server. Below are some examples of how to select the parameters for the deployment call.<\/p>\n\n\n\n
# Get the Ubuntu 16.04 public template\nfor image in driver.list_images():\n if image.name.startswith('Ubuntu') \n and image.name.count('16.04') \n and image.id.endswith('0'):\n break\n\n# Select the node size from the preconfigured instances\nfor size in driver.list_sizes():\n if size.name == '1xCPU-1GB':\n break\n\n# Pick the zone by the city name\nfor location in driver.list_locations():\n if location.name.startswith('London'):\n break\n\nnode = driver.create_node(\n image=image,\n size=size,\n location=location,\n name='Libcloud Example',\n ex_hostname='libcloud.example.com'\n )\n\npprint(node.state)<\/pre>\n\n\n\nSimply add the above code to the end of your libctest.py<\/tt> test file. Then run the program as before to deploy the first server.<\/p>\n\n\n\nRebooting a server<\/h2>\n\n\n\n
Issuing a reboot command requires you to first get the right node object. Afterwards, you have the following two options to restart the node.<\/p>\n\n\n\n
Asking the driver to reboot the node.<\/p>\n\n\n\n
node = [node for node in driver.list_nodes() if node.name == 'Libcloud Example'][0]\ndriver.reboot_node(node)<\/pre>\n\n\n\nAlternatively, you can command the node object directly.<\/p>\n\n\n\n
node = [node for node in driver.list_nodes() if node.name == 'Libcloud Example'][0]\nnode.reboot()<\/pre>\n\n\n\nThe functions will return True<\/tt> once the server has been rebooted successfully. If something went wrong, you will get False<\/tt> instead.<\/p>\n\n\n\nDestroying a server<\/h2>\n\n\n\n
Similarly to the reboot commands, a node can be destroyed using either of the two methods below. The process will shut down the target node and remove the server without deleting the storage device.<\/p>\n\n\n\n
Select the node you wish to destroy and ask the driver to remove it.<\/p>\n\n\n\n
node = [node for node in driver.list_nodes() if node.name == 'Libcloud Example'][0]\ndriver.destroy_node(node)<\/pre>\n\n\n\nOr tell the node to destroy itself.<\/p>\n\n\n\n
node = [node for node in driver.list_nodes() if node.name == 'Libcloud Example'][0]\nnode.destroy()<\/pre>\n\n\n\nThe functions will return True<\/tt> once the server has been successfully deleted. In case the server could not be removed, you will get False<\/tt> as a response.<\/p>\n\n\n\n
Note that after destroying a server, the state of the node object will no longer be up to date. You should always refresh the list of nodes after creating or destroying nodes.<\/p>\n\n\n\n
Including SSH keys at deployment<\/h2>\n\n\n\n
SSH keys are a safe and convenient way to authenticate when logging in to your cloud servers. You can include the public half of your SSH key pair when deploying a new node by reading it from the key file into the auth<\/tt> parameter.<\/p>\n\n\n\n
By default, the public key is added to the root<\/tt> user account. If you wish to define a custom username, you can add it to the\u00a0ex_username<\/tt> parameter. The new username is created at deployment and given sudo<\/tt> privileges.<\/p>\n\n\n\nfrom libcloud.compute.base import NodeAuthSSHKey\n\nwith open('\/path\/to\/public_ssh_key', 'r') as public:\n public_ssh_key = public.read().replace('n', '')\n\nnode = driver.create_node(\n image=image,\n size=size,\n location=location,\n name='Libcloud Example',\n ex_hostname='libcloud.example.com',\n auth=NodeAuthSSHKey(public_ssh_key),\n ex_username='username'\n )<\/pre>\n\n\n\nDefining a username already at deployment allows you to skip the hassle of setting up sudo<\/tt> access manually.<\/p>\n\n\n\nUsing init scripts at deployment<\/h2>\n\n\n\n
Libcloud also supports running custom scripts at server deployment. The scripts can contain any normal Linux commands and tasks but will only work with Linux system templates.<\/p>\n\n\n\n
Using an init script will require a private SSH key that corresponds to your public SSH key. The Libcloud driver needs to connect to the new server over SSH to run the script. Include the file path to your private SSH key in the ssh_key<\/tt> parameter as shown in the example below.<\/p>\n\n\n\n
You also have the option to run the init script under a specific user account by defining the ssh_username<\/tt> parameter.<\/p>\n\n\n\n
Note that the init script and deployment time SSH details are only available in the deploy_node()<\/tt> function.<\/p>\n\n\n\nfrom libcloud.compute.deployment import ScriptDeployment\nfrom libcloud.compute.base import NodeAuthSSHKey\n\n# Create an init script that is run during deployment\ninitscript = ScriptDeployment('echo \"Deployed with Libcloud\" > ~\/libcloud.out')\n\n# Include SSH keys that are used to authenticate on the server\nprivate_ssh_key_file = '\/path\/to\/private_ssh_key'\nwith open('\/path\/to\/public_ssh_key', 'r') as public:\n public_ssh_key = public.read().replace('n', '')\n\nnode = driver.deploy_node(\n ssh_key=private_ssh_key_file,\n ssh_username='root',\n deploy=initscript,\n image=image,\n size=size,\n location=location,\n name='Libcloud Example',\n ex_hostname='libcloud.example.com',\n auth=NodeAuthSSHKey(public_ssh_key)\n )<\/pre>\n\n\n\nOnce deployed, the process will attempt to connect to the server and run the init script.<\/p>\n\n\n\n
In case the SSH connection fails, the function will raise an HTTP exception.<\/p>\n","protected":false},"featured_media":27277,"comment_status":"open","ping_status":"closed","template":"","community-category":[113,117],"class_list":["post-24434","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry","community-category-integrations","community-category-automation"],"acf":[],"_links":{"self":[{"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/tutorial\/24434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/types\/tutorial"}],"replies":[{"embeddable":true,"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/comments?post=24434"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/media\/27277"}],"wp:attachment":[{"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/media?parent=24434"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/community-category?post=24434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}