getting_started.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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-2d39803a' # Ubuntu 14.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 = '97755049-ee4f-4515-b92f-ca00991ee99a' # Ubuntu 14.04 @ Jetstream
  52. List some resources
  53. -------------------
  54. Once you have a reference to a provider, explore the cloud platform:
  55. .. code-block:: python
  56. provider.security.security_groups.list()
  57. provider.compute.vm_types.list()
  58. provider.storage.snapshots.list()
  59. provider.storage.buckets.list()
  60. This will demonstrate the fact that the library was properly installed and your
  61. provider object is setup correctly but it is not very interesting. Therefore,
  62. let's create a new instance we can ssh into using a key pair.
  63. Create a key pair
  64. -----------------
  65. We'll create a new key pair and save the private portion of the key to a file
  66. on disk as a read-only file.
  67. .. code-block:: python
  68. kp = provider.security.key_pairs.create('cloudbridge_intro')
  69. with open('cloudbridge_intro.pem', 'w') as f:
  70. f.write(kp.material)
  71. import os
  72. os.chmod('cloudbridge_intro.pem', 0400)
  73. Create a network
  74. ----------------
  75. A cloudbridge instance should be launched into a private subnet. We'll create
  76. a private network and subnet, and make sure it has internet connectivity, by
  77. attaching an internet gateway to the subnet via a router.
  78. .. code-block:: python
  79. net = self.provider.networking.networks.create(
  80. name='my-network', cidr_block='10.0.0.0/16')
  81. sn = net.create_subnet(name='my-subnet', cidr_block='10.0.0.0/28')
  82. router = self.provider.networking.routers.create(network=net, name='my-router')
  83. router.attach_subnet(sn)
  84. gateway = self.provider.networking.gateways.get_or_create_inet_gateway(name)
  85. router.attach_gateway(gateway)
  86. Create a VM firewall
  87. -----------------------
  88. Next, we need to create a VM firewall (also commonly known as a security group)
  89. and add a rule to allow ssh access. A VM firewall needs to be associated with
  90. a private network.
  91. .. code-block:: python
  92. net = provider.networking.networks.get('desired network ID')
  93. fw = provider.security.vm_firewalls.create(
  94. 'cloudbridge-intro', 'A VM firewall used by CloudBridge', net.id)
  95. fw.rules.create(TrafficDirection.INBOUND, 'tcp', 22, 22, '0.0.0.0/0')
  96. Launch an instance
  97. ------------------
  98. We can now launch an instance using the created key pair and security group.
  99. We will launch an instance type that has at least 2 CPUs and 4GB RAM. We will
  100. also add the network interface as a launch argument.
  101. .. code-block:: python
  102. img = provider.compute.images.get(image_id)
  103. vm_type = sorted([t for t in provider.compute.vm_types
  104. if t.vcpus >= 2 and t.ram >= 4],
  105. key=lambda x: x.vcpus*x.ram)[0]
  106. inst = provider.compute.instances.create(
  107. name='cloudbridge-intro', image=img, vm_type=vm_type,
  108. subnet=subnet, key_pair=kp, vm_firewalls=[fw])
  109. # Wait until ready
  110. inst.wait_till_ready() # This is a blocking call
  111. # Show instance state
  112. inst.state
  113. # 'running'
  114. .. note ::
  115. Note that we iterated through provider.compute.vm_types directly
  116. instead of calling provider.compute.vm_types.list(). This is
  117. because we need to iterate through all records in this case. The list()
  118. method may not always return all records, depending on the global limit
  119. for records, necessitating that additional records be paged in. See
  120. :doc:`topics/paging_and_iteration`.
  121. Assign a public IP address
  122. --------------------------
  123. To access the instance, let's assign a public IP address to the instance. For
  124. this step, we'll first need to allocate a floating IP address for our account
  125. and then associate it with the instance.
  126. .. code-block:: python
  127. fip = provider.networking.floating_ips.create()
  128. inst.add_floating_ip(fip)
  129. inst.refresh()
  130. inst.public_ips
  131. # [u'54.166.125.219']
  132. From the command prompt, you can now ssh into the instance
  133. ``ssh -i cloudbridge_intro.pem ubuntu@54.166.125.219``.
  134. Cleanup
  135. -------
  136. To wrap things up, let's clean up all the resources we have created
  137. .. code-block:: python
  138. inst.terminate()
  139. from cloudbridge.cloud.interfaces import InstanceState
  140. inst.wait_for([InstanceState.DELETED, InstanceState.UNKNOWN],
  141. terminal_states=[InstanceState.ERROR]) # Blocking call
  142. fip.delete()
  143. fw.delete()
  144. kp.delete()
  145. os.remove('cloudbridge_intro.pem')
  146. router.detach_gateway(gateway)
  147. router.detach_subnet(subnet)
  148. gateway.delete()
  149. router.delete()
  150. sn.delete()
  151. net.delete()
  152. And that's it - a full circle in a few lines of code. You can now try
  153. the same with a different provider. All you will need to change is the
  154. cloud-specific data, namely the provider setup and the image ID.