getting_started.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 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. In this code snippet, we will be using AWS. For the details on
  17. other providers, take a look at the `Setup page <topics/setup.html>`_.
  18. .. code-block:: python
  19. from cloudbridge.cloud.factory import CloudProviderFactory, ProviderList
  20. config = {'aws_access_key': 'AKIAJW2XCYO4AF55XFEQ',
  21. 'aws_secret_key': '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'}
  22. provider = CloudProviderFactory().create_provider(ProviderList.AWS, config)
  23. List some resources
  24. -------------------
  25. Once you have a reference to a provider, explore the cloud platform:
  26. .. code-block:: python
  27. provider.compute.images.list()
  28. provider.security.security_groups.list()
  29. provider.block_store.snapshots.list()
  30. provider.object_store.list()
  31. This will demonstrate the fact that the library was properly installed and your
  32. provider object is setup correctly but it is not very interesting. Therefore,
  33. let's create a new instance we can ssh into using a key pair.
  34. Create a key pair
  35. -----------------
  36. We'll create a new key pair and save the private portion of the key to a file
  37. on disk as a read-only file.
  38. .. code-block:: python
  39. kp = provider.security.key_pairs.create('cloudbridge_intro')
  40. with open('cloudbridge_intro.pem', 'w') as f:
  41. f.write(kp.material)
  42. import os
  43. os.chmod('cloudbridge_intro.pem', 0400)
  44. Create a security group
  45. -----------------------
  46. Next, we need to create a security group and add a rule to allow ssh access.
  47. .. code-block:: python
  48. sg = provider.security.security_groups.create(
  49. 'cloudbridge_intro', 'A security group used by Cloudbridge')
  50. sg.add_rule('tcp', 22, 22, '0.0.0.0/0')
  51. Launch an instance
  52. ------------------
  53. Before we can launch an instance, we need to decide what image to use so let's
  54. get a base Ubuntu image ``ami-d85e75b0`` and launch an instance.
  55. .. code-block:: python
  56. img = provider.compute.images.get('ami-d85e75b0')
  57. inst_type = provider.compute.instance_types.find(name='m1.small')
  58. inst = provider.compute.instances.create(
  59. name='Cloudbridge-intro', image=img, instance_type=inst_type,
  60. keypair=kp, security_groups=[sg])
  61. # Refresh the state
  62. inst.refresh()
  63. inst.state
  64. # 'running'
  65. inst.public_ips
  66. # [u'54.166.125.219']
  67. From the command prompt, you can now ssh into the instance
  68. ``ssh -i cloudbridge_intro.pem ubuntu@54.166.125.219``.
  69. Cleanup
  70. -------
  71. To wrap things up, let's clean up all the resources we have created
  72. .. code-block:: python
  73. inst.terminate()
  74. sg.delete()
  75. kp.delete()
  76. And that's it - a full circle in a few lines of code. You can now try
  77. the same with a different provider. All you will need to change is the
  78. cloud-specific data, namely the provider setup and the image ID.