getting_started.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 security group
  72. -----------------------
  73. Next, we need to create a security group and add a rule to allow ssh access.
  74. A security group needs to be associated with a private network, so we'll also
  75. need to fetch it.
  76. .. code-block:: python
  77. provider.network.list() # Find a desired network ID
  78. net = provider.network.get('desired network ID')
  79. sg = provider.security.security_groups.create(
  80. 'cloudbridge_intro', 'A security group used by CloudBridge', net.id)
  81. sg.add_rule('tcp', 22, 22, '0.0.0.0/0')
  82. Launch an instance
  83. ------------------
  84. We can now launch an instance using the created key pair and security group.
  85. We will launch an instance type that has at least 2 CPUs and 4GB RAM. We will
  86. also add the network interface as a launch argument.
  87. .. code-block:: python
  88. img = provider.compute.images.get(image_id)
  89. inst_type = sorted([t for t in provider.compute.instance_types.list()
  90. if t.vcpus >= 2 and t.ram >= 4],
  91. key=lambda x: x.vcpus*x.ram)[0]
  92. inst = provider.compute.instances.create(
  93. name='CloudBridge-intro', image=img, instance_type=inst_type,
  94. network=net, key_pair=kp, security_groups=[sg])
  95. # Wait until ready
  96. inst.wait_till_ready() # This is a blocking call
  97. # Show instance state
  98. inst.state
  99. # 'running'
  100. Assign a public IP address
  101. --------------------------
  102. To access the instance, let's assign a public IP address to the instance. For
  103. this step, we'll first need to allocate a floating IP address for our account
  104. and then associate it with the instance.
  105. .. code-block:: python
  106. fip = provider.network.create_floating_ip()
  107. inst.add_floating_ip(fip.public_ip)
  108. inst.refresh()
  109. inst.public_ips
  110. # [u'54.166.125.219']
  111. From the command prompt, you can now ssh into the instance
  112. ``ssh -i cloudbridge_intro.pem ubuntu@54.166.125.219``.
  113. Cleanup
  114. -------
  115. To wrap things up, let's clean up all the resources we have created
  116. .. code-block:: python
  117. inst.terminate()
  118. from cloudbridge.cloud.interfaces import InstanceState
  119. inst.wait_for([InstanceState.TERMINATED, InstanceState.UNKNOWN],
  120. terminal_states=[InstanceState.ERROR]) # Blocking call
  121. fip.delete()
  122. sg.delete()
  123. kp.delete()
  124. os.remove('cloudbridge_intro.pem')
  125. router.remove_route(sn.id)
  126. router.detach_network()
  127. router.delete()
  128. sn.delete()
  129. net.delete()
  130. And that's it - a full circle in a few lines of code. You can now try
  131. the same with a different provider. All you will need to change is the
  132. cloud-specific data, namely the provider setup and the image ID.