getting_started.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. Common Setup Issues
  13. -------------------
  14. **macOS Issues**
  15. * If you are getting an error message like so: ``Authentication with cloud provider failed: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)``
  16. then this indicates that you are probably using a newer version of Python on
  17. macOS. Starting with Python 3.6, the Python installer includes its own version
  18. of OpenSSL and it no longer uses the system trusted certificate keychains.
  19. Python 3.6 includes a script that can install a bundle of root certificates
  20. from ``certifi``. To install this bundle execute the following:
  21. .. code-block:: bash
  22. cd /Applications/Python\ 3.6/
  23. sudo ./Install\ Certificates.command
  24. For more information see `this StackOverflow
  25. answer <https://stackoverflow.com/a/42583411/1419499>`_ and the `Python 3.6
  26. Release Notes <https://www.python.org/downloads/release/python-360/>`_.
  27. Create a provider
  28. -----------------
  29. To start, you will need to create a reference to a provider object. The
  30. provider object identifies the cloud you want to work with and supplies your
  31. credentials. The following two code snippets setup a necessary provider object,
  32. for AWS and OpenStack. For the details on other providers, take a look at the
  33. `Setup page <topics/setup.html>`_. The remainder of the code is the same for
  34. either provider.
  35. AWS:
  36. .. code-block:: python
  37. from cloudbridge.cloud.factory import CloudProviderFactory, ProviderList
  38. config = {'aws_access_key': 'AKIAJW2XCYO4AF55XFEQ',
  39. 'aws_secret_key': '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'}
  40. provider = CloudProviderFactory().create_provider(ProviderList.AWS, config)
  41. image_id = 'ami-2d39803a' # Ubuntu 14.04 (HVM)
  42. OpenStack (with Keystone authentication v2):
  43. .. code-block:: python
  44. from cloudbridge.cloud.factory import CloudProviderFactory, ProviderList
  45. config = {'os_username': 'username',
  46. 'os_password': 'password',
  47. 'os_auth_url': 'authentication URL',
  48. 'os_region_name': 'region name',
  49. 'os_project_name': 'project name'}
  50. provider = CloudProviderFactory().create_provider(ProviderList.OPENSTACK,
  51. config)
  52. image_id = 'c1f4b7bc-a563-4feb-b439-a2e071d861aa' # Ubuntu 14.04 @ NeCTAR
  53. OpenStack (with Keystone authentication v3):
  54. .. code-block:: python
  55. from cloudbridge.cloud.factory import CloudProviderFactory, ProviderList
  56. config = {'os_username': 'username',
  57. 'os_password': 'password',
  58. 'os_auth_url': 'authentication URL',
  59. 'os_project_name': 'project name',
  60. 'os_project_domain_name': 'project domain name',
  61. 'os_user_domain_name': 'domain name'}
  62. provider = CloudProviderFactory().create_provider(ProviderList.OPENSTACK,
  63. config)
  64. image_id = '97755049-ee4f-4515-b92f-ca00991ee99a' # Ubuntu 14.04 @ Jetstream
  65. List some resources
  66. -------------------
  67. Once you have a reference to a provider, explore the cloud platform:
  68. .. code-block:: python
  69. provider.compute.images.list()
  70. provider.security.security_groups.list()
  71. provider.block_store.snapshots.list()
  72. provider.object_store.list()
  73. This will demonstrate the fact that the library was properly installed and your
  74. provider object is setup correctly but it is not very interesting. Therefore,
  75. let's create a new instance we can ssh into using a key pair.
  76. Create a key pair
  77. -----------------
  78. We'll create a new key pair and save the private portion of the key to a file
  79. on disk as a read-only file.
  80. .. code-block:: python
  81. kp = provider.security.key_pairs.create('cloudbridge_intro')
  82. with open('cloudbridge_intro.pem', 'w') as f:
  83. f.write(kp.material)
  84. import os
  85. os.chmod('cloudbridge_intro.pem', 0400)
  86. Create a network
  87. ----------------
  88. A cloudbridge instance should be launched into a private subnet. We'll create
  89. a private network and subnet, and make sure it has internet connectivity, by
  90. attaching an internet gateway to the subnet via a router.
  91. .. code-block:: python
  92. net = self.provider.networking.networks.create(
  93. name='my-network', cidr_block='10.0.0.0/16')
  94. sn = net.create_subnet(name='my-subnet', cidr_block='10.0.0.0/28')
  95. router = self.provider.networking.routers.create(network=net, name='my-router')
  96. router.attach_subnet(sn)
  97. gateway = self.provider.networking.gateways.get_or_create_inet_gateway(name)
  98. router.attach_gateway(gateway)
  99. Create a security group
  100. -----------------------
  101. Next, we need to create a security group and add a rule to allow ssh access.
  102. A security group needs to be associated with a private network.
  103. .. code-block:: python
  104. net = provider.networking.networks.get('desired network ID')
  105. sg = provider.security.security_groups.create(
  106. 'cloudbridge_intro', 'A security group used by CloudBridge', net.id)
  107. sg.add_rule('tcp', 22, 22, '0.0.0.0/0')
  108. Launch an instance
  109. ------------------
  110. We can now launch an instance using the created key pair and security group.
  111. We will launch an instance type that has at least 2 CPUs and 4GB RAM. We will
  112. also add the network interface as a launch argument.
  113. .. code-block:: python
  114. img = provider.compute.images.get(image_id)
  115. inst_type = sorted([t for t in provider.compute.instance_types
  116. if t.vcpus >= 2 and t.ram >= 4],
  117. key=lambda x: x.vcpus*x.ram)[0]
  118. inst = provider.compute.instances.create(
  119. name='CloudBridge-intro', image=img, instance_type=inst_type,
  120. subnet=subnet, key_pair=kp, security_groups=[sg])
  121. # Wait until ready
  122. inst.wait_till_ready() # This is a blocking call
  123. # Show instance state
  124. inst.state
  125. # 'running'
  126. .. note ::
  127. Note that we iterated through provider.compute.instance_types directly
  128. instead of calling provider.compute.instance_types.list(). This is
  129. because we need to iterate through all records in this case. The list()
  130. method may not always return all records, depending on the global limit
  131. for records, necessitating that additional records be paged in. See
  132. :doc:`topics/paging_and_iteration`.
  133. Assign a public IP address
  134. --------------------------
  135. To access the instance, let's assign a public IP address to the instance. For
  136. this step, we'll first need to allocate a floating IP address for our account
  137. and then associate it with the instance.
  138. .. code-block:: python
  139. fip = provider.networking.networks.create_floating_ip()
  140. inst.add_floating_ip(fip.public_ip)
  141. inst.refresh()
  142. inst.public_ips
  143. # [u'54.166.125.219']
  144. From the command prompt, you can now ssh into the instance
  145. ``ssh -i cloudbridge_intro.pem ubuntu@54.166.125.219``.
  146. Cleanup
  147. -------
  148. To wrap things up, let's clean up all the resources we have created
  149. .. code-block:: python
  150. inst.terminate()
  151. from cloudbridge.cloud.interfaces import InstanceState
  152. inst.wait_for([InstanceState.TERMINATED, InstanceState.UNKNOWN],
  153. terminal_states=[InstanceState.ERROR]) # Blocking call
  154. fip.delete()
  155. sg.delete()
  156. kp.delete()
  157. os.remove('cloudbridge_intro.pem')
  158. router.detach_gateway(gateway)
  159. router.detach_subnet(subnet)
  160. gateway.delete()
  161. router.delete()
  162. sn.delete()
  163. net.delete()
  164. And that's it - a full circle in a few lines of code. You can now try
  165. the same with a different provider. All you will need to change is the
  166. cloud-specific data, namely the provider setup and the image ID.