getting_started.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_tenant_name': 'tenant name',
  33. 'os_auth_url': 'authentication URL',
  34. 'os_region_name': 'region 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_user_domain_name': 'domain name',
  45. 'os_project_domain_name': 'project domain name',
  46. 'os_project_name': 'project 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. Configure a private network
  72. ---------------------------
  73. We want to provision our instance into a private network to give us flexibility
  74. in the future. Also, providers these days are increasingly requiring use of
  75. private networks. Setting up a private network requires several steps:
  76. (1) create a network; (2) create a subnet within the network; (3) create a
  77. router; (4) attach the router to an external network; and (5) add a route to
  78. the router that links with with a subnet.
  79. .. code-block:: python
  80. net = provider.network.create('cloudbridge_intro')
  81. sn = net.create_subnet('10.0.0.1/28', 'cloudbridge-intro')
  82. router = provider.network.create_router('cloudbridge-intro')
  83. router.attach_network(net.id)
  84. router.add_route(sn.id)
  85. Create a security group
  86. -----------------------
  87. Next, we need to create a security group and add a rule to allow ssh access.
  88. .. code-block:: python
  89. sg = provider.security.security_groups.create(
  90. 'cloudbridge_intro', 'A security group used by CloudBridge', net.id)
  91. sg.add_rule('tcp', 22, 22, '0.0.0.0/0')
  92. Launch an instance
  93. ------------------
  94. We can now launch an instance using the created key pair and security group.
  95. We will launch an instance type that has at least 2 CPUs and 4GB RAM. We will
  96. also add the network interface as a launch argument.
  97. .. code-block:: python
  98. img = provider.compute.images.get(image_id)
  99. inst_type = sorted([t for t in provider.compute.instance_types.list()
  100. if t.vcpus >= 2 and t.ram >= 4],
  101. key=lambda x: x.vcpus*x.ram)[0]
  102. lc = provider.compute.instances.create_launch_config()
  103. lc.add_network_interface(net.id)
  104. inst = provider.compute.instances.create(
  105. name='CloudBridge-intro', image=img, instance_type=inst_type,
  106. key_pair=kp, security_groups=[sg], launch_config=lc)
  107. # Wait until ready
  108. inst.wait_till_ready() # This is a blocking call
  109. # Show instance state
  110. inst.state
  111. # 'running'
  112. Assign a public IP address
  113. --------------------------
  114. To access the instance, let's assign a public IP address to the instance. For
  115. this step, we'll first need to allocate a floating IP address for our account
  116. and then associate it with the instance.
  117. fip = provider.network.create_floating_ip()
  118. inst.add_floating_ip(fip.public_ip)
  119. inst.refresh()
  120. inst.public_ips
  121. # [u'54.166.125.219']
  122. From the command prompt, you can now ssh into the instance
  123. ``ssh -i cloudbridge_intro.pem ubuntu@54.166.125.219``.
  124. Cleanup
  125. -------
  126. To wrap things up, let's clean up all the resources we have created
  127. .. code-block:: python
  128. inst.terminate()
  129. from cloudbridge.cloud.interfaces import InstanceState
  130. inst.wait_for([InstanceState.TERMINATED, InstanceState.UNKNOWN],
  131. terminal_states=[InstanceState.ERROR]) # Blocking call
  132. fip.delete()
  133. sg.delete()
  134. kp.delete()
  135. os.remove('cloudbridge_intro.pem')
  136. router.remove_route(sn.id)
  137. router.detach_network()
  138. router.delete()
  139. sn.delete()
  140. net.delete()
  141. And that's it - a full circle in a few lines of code. You can now try
  142. the same with a different provider. All you will need to change is the
  143. cloud-specific data, namely the provider setup and the image ID.