launch.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. Launching instances
  2. ===================
  3. Before being able to run below commands, you will need a ``provider`` object
  4. (see `this page <setup.html>`_).
  5. Common launch data
  6. ------------------
  7. Before launching an instance, you need to decide what image to launch
  8. as well as what type of instance. We will create those objects here. The
  9. specified image ID is a base Ubuntu image on AWS so feel free to change it as
  10. desired. For instance type, we're going to let CloudBridge figure out what's
  11. the appropriate name on a given provider for an instance with at least 2 CPUs
  12. and 4 GB RAM.
  13. .. code-block:: python
  14. img = provider.compute.images.get('ami-f4cc1de2') # Ubuntu 16.04 on AWS
  15. inst_type = sorted([t for t in provider.compute.instance_types.list()
  16. if t.vcpus >= 2 and t.ram >= 4],
  17. key=lambda x: x.vcpus*x.ram)[0]
  18. When launching an instance, you can also specify several optional arguments
  19. such as the security group, a key pair, or instance user data. To allow you to
  20. connect to the launched instances, we will also supply those parameters (note
  21. that we're making an assumption here these resources exist; if you don't have
  22. those resources under your account, take a look at the
  23. `Getting Started <../getting_started.html>`_ guide).
  24. .. code-block:: python
  25. kp = provider.security.key_pairs.find(name='cloudbridge_intro')[0]
  26. sg = provider.security.security_groups.list()[0]
  27. Launch an instance
  28. ------------------
  29. Once we have all the desired pieces, we'll use them to launch an instance:
  30. .. code-block:: python
  31. inst = provider.compute.instances.create(
  32. name='CloudBridge-VPC', image=img, instance_type=inst_type,
  33. key_pair=kp, security_groups=[sg])
  34. Private networking
  35. ~~~~~~~~~~~~~~~~~~
  36. Private networking gives you control over the networking setup for your
  37. instance(s) and is considered the preferred method for launching instances. To
  38. launch an instance with an explicit private network, supply a subnet within
  39. a network as an additional argument to the ``create`` method:
  40. .. code-block:: python
  41. provider.network.list() # Find a desired network ID
  42. net = provider.network.get('desired network ID')
  43. sn = net.subnets()[0] # Get a handle on the desired subnet to launch with
  44. inst = provider.compute.instances.create(
  45. name='CloudBridge-VPC', image=img, instance_type=inst_type,
  46. subnet=sn, key_pair=kp, security_groups=[sg])
  47. For more information on how to create and setup a private network, take a look
  48. at `Networking <./networking.html>`_.
  49. Block device mapping
  50. ~~~~~~~~~~~~~~~~~~~~
  51. Optionally, you may want to provide a block device mapping at launch,
  52. specifying volume or ephemeral storage mappings for the instance. While volumes
  53. can also be attached and mapped after instance boot using the volume service,
  54. specifying block device mappings at launch time is especially useful when it is
  55. necessary to resize the root volume.
  56. The code below demonstrates how to resize the root volume. For more information,
  57. refer to :class:`.LaunchConfig`.
  58. .. code-block:: python
  59. lc = provider.compute.instances.create_launch_config()
  60. lc.add_volume_device(source=img, size=11, is_root=True)
  61. inst = provider.compute.instances.create(
  62. name='CloudBridge-BDM', image=img, instance_type=inst_type,
  63. launch_config=lc, key_pair=kp, security_groups=[sg])
  64. where ``img`` is the :class:`.Image` object to use for the root volume.
  65. After launch
  66. ------------
  67. After an instance has launched, you can access its properties:
  68. .. code-block:: python
  69. # Wait until ready
  70. inst.wait_till_ready() # This is a blocking call
  71. inst.state
  72. # 'running'
  73. Depending on the provider's networking setup, it may be necessary to explicitly
  74. assign a floating IP address to your instance. This can be done as follows:
  75. .. code-block:: python
  76. # List all the IP addresses and find the desired one
  77. provider.network.floating_ips()
  78. # Assign the desired IP to the instance
  79. inst.add_floating_ip('149.165.168.143')
  80. inst.refresh()
  81. inst.public_ips
  82. # [u'149.165.168.143']