getting_started.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. Getting Started
  2. ===============
  3. This getting started guide will provide a quick tour of some CloudBridge
  4. features. For more details on individual features, see the
  5. `Using CloudBridge <topics/overview.html>`_ section or the
  6. `API reference <api_docs/ref.html>`_.
  7. Installation
  8. ------------
  9. CloudBridge is available on PyPI so to install the latest available version,
  10. run::
  11. pip install --upgrade cloudbridge
  12. For common issues during setup, check the following section:
  13. `Common Setup Issues <topics/troubleshooting.html>`
  14. Create a provider
  15. -----------------
  16. To start, you will need to create a reference to a provider object. The
  17. provider object identifies the cloud you want to work with and supplies your
  18. credentials. The following two code snippets setup a necessary provider object,
  19. for AWS and OpenStack. For the details on other providers, take a look at the
  20. `Setup page <topics/setup.html>`_. The remainder of the code is the same for
  21. either provider.
  22. AWS:
  23. .. code-block:: python
  24. from cloudbridge.cloud.factory import CloudProviderFactory, ProviderList
  25. config = {'aws_access_key': 'AKIAJW2XCYO4AF55XFEQ',
  26. 'aws_secret_key': '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'}
  27. provider = CloudProviderFactory().create_provider(ProviderList.AWS, config)
  28. image_id = 'ami-aa2ea6d0' # Ubuntu 16.04 (HVM)
  29. OpenStack (with Keystone authentication v2):
  30. .. code-block:: python
  31. from cloudbridge.cloud.factory import CloudProviderFactory, ProviderList
  32. config = {'os_username': 'username',
  33. 'os_password': 'password',
  34. 'os_auth_url': 'authentication URL',
  35. 'os_region_name': 'region name',
  36. 'os_project_name': 'project name'}
  37. provider = CloudProviderFactory().create_provider(ProviderList.OPENSTACK,
  38. config)
  39. image_id = 'c1f4b7bc-a563-4feb-b439-a2e071d861aa' # Ubuntu 14.04 @ NeCTAR
  40. OpenStack (with Keystone authentication v3):
  41. .. code-block:: python
  42. from cloudbridge.cloud.factory import CloudProviderFactory, ProviderList
  43. config = {'os_username': 'username',
  44. 'os_password': 'password',
  45. 'os_auth_url': 'authentication URL',
  46. 'os_project_name': 'project name',
  47. 'os_project_domain_name': 'project domain name',
  48. 'os_user_domain_name': 'domain name'}
  49. provider = CloudProviderFactory().create_provider(ProviderList.OPENSTACK,
  50. config)
  51. image_id = '470d2fba-d20b-47b0-a89a-ab725cd09f8b' # Ubuntu 18.04@Jetstream
  52. Azure:
  53. .. code-block:: python
  54. from cloudbridge.cloud.factory import CloudProviderFactory, ProviderList
  55. config = {'azure_subscription_id': 'REPLACE WITH ACTUAL VALUE',
  56. 'azure_client_id': 'REPLACE WITH ACTUAL VALUE',
  57. 'azure_secret': 'REPLACE WITH ACTUAL VALUE',
  58. 'azure_tenant': ' REPLACE WITH ACTUAL VALUE'}
  59. provider = CloudProviderFactory().create_provider(ProviderList.AZURE, config)
  60. image_id = 'Canonical:UbuntuServer:16.04.0-LTS:latest' # Ubuntu 16.04
  61. Google Compute Cloud:
  62. .. code-block:: python
  63. from cloudbridge.cloud.factory import CloudProviderFactory, ProviderList
  64. config = {'gcp_project_name': 'project name',
  65. 'gcp_service_creds_file': 'service_file.json',
  66. 'gcp_region_name': 'us-east1', # Use desired value
  67. 'gcp_zone_name': 'us-east1-b'} # Use desired value
  68. provider = CloudProviderFactory().create_provider(ProviderList.GCP, config)
  69. image_id = 'https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1804-bionic-v20181222'
  70. List some resources
  71. -------------------
  72. Once you have a reference to a provider, explore the cloud platform:
  73. .. code-block:: python
  74. provider.security.vm_firewalls.list()
  75. provider.compute.vm_types.list()
  76. provider.storage.snapshots.list()
  77. provider.storage.buckets.list()
  78. This will demonstrate the fact that the library was properly installed and your
  79. provider object is setup correctly. By itself, those commands are not very
  80. interesting so let's create a new instance we can ssh into using a key pair.
  81. Create a key pair
  82. -----------------
  83. We'll create a new key pair and save the private portion of the key to a file
  84. on disk as a read-only file.
  85. .. code-block:: python
  86. import os
  87. kp = provider.security.key_pairs.create('cb-keypair')
  88. with open('cloudbridge_intro.pem', 'wb') as f:
  89. f.write(kp.material)
  90. os.chmod('cloudbridge_intro.pem', 0o400)
  91. Create a network
  92. ----------------
  93. A cloudbridge instance should be launched into a private subnet. We'll create
  94. a private network and subnet, and make sure it has internet connectivity, by
  95. attaching an internet gateway to the subnet via a router.
  96. .. code-block:: python
  97. net = provider.networking.networks.create(cidr_block='10.0.0.0/16',
  98. label='cb-network')
  99. zone = provider.compute.regions.get(provider.region_name).zones[0]
  100. sn = net.subnets.create(
  101. cidr_block='10.0.0.0/28', label='cb-subnet', zone=zone)
  102. router = provider.networking.routers.create(network=net, label='cb-router')
  103. router.attach_subnet(sn)
  104. gateway = net.gateways.get_or_create()
  105. router.attach_gateway(gateway)
  106. Create a VM firewall
  107. --------------------
  108. Next, we need to create a VM firewall (also commonly known as a security group)
  109. and add a rule to allow ssh access. A VM firewall needs to be associated with
  110. a private network.
  111. .. code-block:: python
  112. from cloudbridge.cloud.interfaces.resources import TrafficDirection
  113. fw = provider.security.vm_firewalls.create(
  114. label='cb-firewall', description='A VM firewall used by
  115. CloudBridge', network=net)
  116. fw.rules.create(TrafficDirection.INBOUND, 'tcp', 22, 22, '0.0.0.0/0')
  117. Launch an instance
  118. ------------------
  119. We can now launch an instance using the created key pair and security group.
  120. We will launch an instance type that has at least 2 CPUs and 4GB RAM. We will
  121. also add the network interface as a launch argument.
  122. .. code-block:: python
  123. img = provider.compute.images.get(image_id)
  124. vm_type = sorted([t for t in provider.compute.vm_types
  125. if t.vcpus >= 2 and t.ram >= 4],
  126. key=lambda x: x.vcpus*x.ram)[0]
  127. inst = provider.compute.instances.create(
  128. image=img, vm_type=vm_type, label='cb-instance',
  129. subnet=sn, zone=zone, key_pair=kp, vm_firewalls=[fw])
  130. # Wait until ready
  131. inst.wait_till_ready() # This is a blocking call
  132. # Show instance state
  133. inst.state
  134. # 'running'
  135. .. note ::
  136. Note that we iterated through provider.compute.vm_types directly
  137. instead of calling provider.compute.vm_types.list(). This is
  138. because we need to iterate through all records in this case. The list()
  139. method may not always return all records, depending on the global limit
  140. for records, necessitating that additional records be paged in. See
  141. :doc:`topics/paging_and_iteration`.
  142. Assign a public IP address
  143. --------------------------
  144. To access the instance, let's assign a public IP address to the instance. For
  145. this step, we'll first need to allocate a floating IP address for our account
  146. and then associate it with the instance. Note that floating IPs are associated
  147. with an Internet Gateway so we allocate the IP under the gateway we dealt with
  148. earlier.
  149. .. code-block:: python
  150. if not inst.public_ips:
  151. fip = gateway.floating_ips.create()
  152. inst.add_floating_ip(fip)
  153. inst.refresh()
  154. inst.public_ips
  155. # [u'54.166.125.219']
  156. From the command prompt, you can now ssh into the instance
  157. ``ssh -i cloudbridge_intro.pem ubuntu@54.166.125.219``.
  158. Get a resource
  159. --------------
  160. When a resource already exists, a reference to it can be retrieved using either
  161. its ID, name, or label. It is important to note that while IDs and names are
  162. unique, multiple resources of the same type could use the same label, thus the
  163. `find` method always returns a list, while the `get` method returns a single
  164. object. While the methods are similar across resources, they are explicitly
  165. listed in order to help map each resource with the service that handles it.
  166. Note that labeled resources allow to find by label, while unlabeled
  167. resources find by name or their special properties (eg: public_ip for
  168. floating IPs). For more detailed information on the types of resources and
  169. their provider mappings, see :doc:`topics/resource_types_and_mapping`.
  170. .. code-block:: python
  171. # Key Pair
  172. kp = provider.security.key_pairs.get('keypair ID')
  173. kp = provider.security.key_pairs.find(name='cb-keypair')[0]
  174. # Floating IPs
  175. fip = gateway.floating_ips.get('FloatingIP ID')
  176. # Find using public IP address
  177. fip_list = gateway.floating_ips.find(public_ip='IP address')
  178. # Find using name (the behavior of the `name` property can be
  179. # cloud-dependent). More details can be found `here <topics/resource_types_and_mapping.html>`
  180. fip_list = gateway.floating_ips.find(name='cb-fip')[0]
  181. # Network
  182. net = provider.networking.networks.get('network ID')
  183. net_list = provider.networking.networks.find(label='my-network')
  184. net = net_list[0]
  185. # Subnet
  186. sn = provider.networking.subnets.get('subnet ID')
  187. # Unknown network
  188. sn_list = provider.networking.subnets.find(label='cb-subnet')
  189. # Known network
  190. sn_list = provider.networking.subnets.find(network=net.id,
  191. label='cb-subnet')
  192. sn = sn_list(0)
  193. # Router
  194. router = provider.networking.routers.get('router ID')
  195. router_list = provider.networking.routers.find(label='cb-router')
  196. router = router_list[0]
  197. # Gateway
  198. gateway = net.gateways.get_or_create()
  199. # Firewall
  200. fw = provider.security.vm_firewalls.get('firewall ID')
  201. fw_list = provider.security.vm_firewalls.find(label='cb-firewall')
  202. fw = fw_list[0]
  203. # Instance
  204. inst = provider.compute.instances.get('instance ID')
  205. inst_list = provider.compute.instances.list(label='cb-instance')
  206. inst = inst_list[0]
  207. Cleanup
  208. -------
  209. To wrap things up, let's clean up all the resources we have created
  210. .. code-block:: python
  211. from cloudbridge.cloud.interfaces import InstanceState
  212. inst.delete()
  213. inst.wait_for([InstanceState.DELETED, InstanceState.UNKNOWN],
  214. terminal_states=[InstanceState.ERROR]) # Blocking call
  215. fip.delete()
  216. fw.delete()
  217. kp.delete()
  218. os.remove('cloudbridge_intro.pem')
  219. router.detach_gateway(gateway)
  220. router.detach_subnet(sn)
  221. gateway.delete()
  222. router.delete()
  223. sn.delete()
  224. net.delete()
  225. And that's it - a full circle in a few lines of code. You can now try
  226. the same with a different provider. All you will need to change is the
  227. cloud-specific data, namely the provider setup and the image ID.