How to launch and delete OpenStack instances using Ruby and Fog

What you need

This tutorial will guide you with step-by-step instructions on how to get a basic authentication token on DreamCompute and launch a virtual machine.

You will need to have your username, password and project ID available. This information can be easily retrieved from DreamCompute’s dashboard, in the openrc file.

You must also need to have

fog 1.19 or higher installed and working with ruby gems 1.9.

Authenticate

Add the following code to a file or copy and paste them in an interactive shell, when available. Make sure to use your username, password and project ID.

You can use the interactive Ruby shell irb to paste the code below:

require 'fog'

auth_username = 'your_auth_username'
auth_password = 'your_auth_password'
project_name = 'your_project_name_or_id'
auth_url = 'https://keystone.dream.io'
region_name = 'RegionOne'

conn = Fog::Compute.new({
    :provider            => 'openstack',
    :openstack_auth_url  => auth_url + '/v2.0/tokens',
    :openstack_username  => auth_username,
    :openstack_tenant    => project_name,
    :openstack_api_key   => auth_password,
})

This code creates the conn object that you can use in the next steps.

Flavors and images

To launch an instance, which is just a virtual machine, you choose a flavor and an image. The flavor represents the size of the instance, including the number of CPUs and amount of RAM and disk space. An image is a prepared OS installation from which you clone your instance. When you boot instances in a public cloud, larger flavors can be more expensive than smaller ones in terms of monetary cost.

To list the images that are available in your cloud, run some API calls:

images = conn.images.all
print images

This code returns output like this:

{"images"=>[{"id"=>"5e1c34f6-4044-4d19-88df-968ab2ca3df6",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/images/5e1c34f6-4044-4d19-88df-968ab2ca3df6",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/images/5e1c34f6-4044-4d19-88df-968ab2ca3df6",
"rel"=>"bookmark"},
{"href"=>"http://10.64.141.226:9292/4ad04455f5e6431c895340bf3630e7bb/images/5e1c34f6-4044-4d19-88df-968ab2ca3df6",
"type"=>"application/vnd.openstack.image", "rel"=>"alternate"}],
"name"=>"Debian-8.1"}, {"id"=>"e81771c0-2944-405c-ba92-3deb0e1b4ce3",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/images/e81771c0-2944-405c-ba92-3deb0e1b4ce3",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/images/e81771c0-2944-405c-ba92-3deb0e1b4ce3",
"rel"=>"bookmark"},
{"href"=>"http://10.64.141.226:9292/4ad04455f5e6431c895340bf3630e7bb/images/e81771c0-2944-405c-ba92-3deb0e1b4ce3",
"type"=>"application/vnd.openstack.image", "rel"=>"alternate"}],
"name"=>"CentOS-7.0"}, {"id"=>"90d5e049-aaed-4abc-aa75-60c2b1ed6516",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/images/90d5e049-aaed-4abc-aa75-60c2b1ed6516",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/images/90d5e049-aaed-4abc-aa75-60c2b1ed6516",
"rel"=>"bookmark"},
{"href"=>"http://10.64.141.226:9292/4ad04455f5e6431c895340bf3630e7bb/images/90d5e049-aaed-4abc-aa75-60c2b1ed6516",
"type"=>"application/vnd.openstack.image", "rel"=>"alternate"}],
"name"=>"Ubuntu-14.04"},
{"id"=>"f044ae8f-e0e1-4fb4-baff-0363c19a6638",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/images/f044ae8f-e0e1-4fb4-baff-0363c19a6638",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/images/f044ae8f-e0e1-4fb4-baff-0363c19a6638",
"rel"=>"bookmark"},
{"href"=>"http://10.64.141.226:9292/4ad04455f5e6431c895340bf3630e7bb/images/f044ae8f-e0e1-4fb4-baff-0363c19a6638",
"type"=>"application/vnd.openstack.image", "rel"=>"alternate"}],
"name"=>"CoreOS"}, {"id"=>"2827d7cc-8cbb-4ce9-9b61-dadc2436144e",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/images/2827d7cc-8cbb-4ce9-9b61-dadc2436144e",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/images/2827d7cc-8cbb-4ce9-9b61-dadc2436144e",
"rel"=>"bookmark"},
{"href"=>"http://10.64.141.226:9292/4ad04455f5e6431c895340bf3630e7bb/images/2827d7cc-8cbb-4ce9-9b61-dadc2436144e",
"type"=>"application/vnd.openstack.image", "rel"=>"alternate"}],
"name"=>"Fedora-20"}

Note

You may notice that the image IDs change often even if the names of the images remain the same as the images are updated over time.

You can also get information about available flavors using API calls:

flavors = conn.flavors.all
print flavors

This code returns output like this:

{"flavors"=>[{"id"=>"100",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/flavors/100",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/flavors/100",
"rel"=>"bookmark"}], "name"=>"subsonic"}, {"id"=>"200",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/flavors/200",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/flavors/200",
"rel"=>"bookmark"}], "name"=>"supersonic"}, {"id"=>"300",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/flavors/300",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/flavors/300",
"rel"=>"bookmark"}], "name"=>"lightspeed"}, {"id"=>"400",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/flavors/400",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/flavors/400",
"rel"=>"bookmark"}], "name"=>"warpspeed"}, {"id"=>"500",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/flavors/500",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/flavors/500",
"rel"=>"bookmark"}], "name"=>"hyperspeed"}, {"id"=>"600",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/flavors/600",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/flavors/600",
"rel"=>"bookmark"}], "name"=>"ridiculous"}, {"id"=>"700",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/flavors/700",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/flavors/700",
"rel"=>"bookmark"}], "name"=>"ludicrous"}, {"id"=>"800",
"links"=>[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/flavors/800",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/flavors/800",
"rel"=>"bookmark"}], "name"=>"plaid"}]}

Choose an image and flavor for your instance. For example, pick one of about 1GB RAM, 1 CPU, and a 1GB disk. This example uses the Ubuntu image with a small flavor, which is a safe choice.

First, tell the connection to get a specified image by using the ID of the image that you picked in the previous section:

image_id = '90d5e049-aaed-4abc-aa75-60c2b1ed6516'
image = conn.images.get image_id
print image

Next, choose which flavor you want to use:

flavor_id = '100'
flavor = conn.flavors.get flavor_id
print flavor

Now, you can launch the instance.

Launch an instance

Now you can create the instance:

instance_name = 'testing'
testing_instance = conn.servers.create(:name => instance_name,
                                       :flavor_ref => flavor.id,
                                       :image_ref => image.id)

If you list existing instances you’ll see the newly created one in there:

print conn.servers

The newly created instance will look something like:

[  <Fog::Compute::OpenStack::Server
    id="3dfd7960-a76e-47af-afe1-f40c95d93544",
    instance_name=nil,
    addresses={},
    flavor={"id"=>"100",
"links"=>[{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/flavors/100",
"rel"=>"bookmark"}]},
    host_id="a1dc2b7b8b78a71c437e361f8beb2f0c3577891219b319ce2439847b",
    image={"id"=>"90d5e049-aaed-4abc-aa75-60c2b1ed6516",
"links"=>[{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/images/90d5e049-aaed-4abc-aa75-60c2b1ed6516",
"rel"=>"bookmark"}]},
    metadata=    <Fog::Compute::OpenStack::Metadata
      [

      ]
    >,
    links=[{"href"=>"https://compute.dream.io:8774/v2/4ad04455f5e6431c895340bf3630e7bb/servers/3dfd7960-a76e-47af-afe1-f40c95d93544",
"rel"=>"self"},
{"href"=>"https://compute.dream.io:8774/4ad04455f5e6431c895340bf3630e7bb/servers/3dfd7960-a76e-47af-afe1-f40c95d93544",
"rel"=>"bookmark"}],
    name="testinstance",
    personality=nil,
    progress=0,
    accessIPv4="",
    accessIPv6="",
    availability_zone="iad-1",
    user_data_encoded=nil,
    state="BUILD",
    created=2015-09-10 17:30:22 UTC,
    updated=2015-09-10 17:30:28 UTC,
    tenant_id="4ad04455f5e6431c895340bf3630e7bb",
    user_id="17949030c6f4400b99f367fc1dc051ea",
    key_name=nil,
    fault=nil,
    config_drive="",
    os_dcf_disk_config="MANUAL",
    os_ext_srv_attr_host=nil,
    os_ext_srv_attr_hypervisor_hostname=nil,
    os_ext_srv_attr_instance_name=nil,
    os_ext_sts_power_state=0,
    os_ext_sts_task_state="spawning",
    os_ext_sts_vm_state="building"
  >]

Before you continue, you must do one more thing.

Destroy an instance

Cloud resources, such as running instances that you no longer use, can cost money. To avoid unexpected expenses, destroy cloud resources.

testing_instance.destroy

If you list the instances again, the instance disappears.

Did this article answer your questions?

Article last updated .