2
0

getting_started.rst 11 KB

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