getting_started.rst 6.9 KB

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