Skip to content

Repository files navigation

Fog::OpenStack

Gem VersionBuild StatusDependency StatusCoverage StatusCode ClimateJoin the chat at https://gitter.im/fog/fog-openstack

This is the plugin Gem to talk to OpenStack clouds via fog.

The main maintainers for the OpenStack sections are @dhague, @Ladas, @seanhandley, @mdarby and @jjasghar. Please send CC them on pull requests.

Supported OpenStack APIs

See the list of supported OpenStack projects.

Installation

Add this line to your application's Gemfile:

gem'fog-openstack'

And then execute:

$ bundle

Or install it yourself as:

$ gem install fog-openstack

Usage

Initial Setup

Require the gem:

require"fog/openstack"

Checklist:

  • Before you can do anything with an OpenStack cloud, you need to authenticate yourself with the identity service, "Keystone".
  • All following examples assume that @connection_params is a hash of valid connection information for an OpenStack cloud.
  • The :openstack_username and :openstack_api_key keys must map to a valid user/password combination in Keystone.
  • If you don't know what domain your user belongs to, chances are it's the default domain. By default, all users are a member of the default domain unless otherwise specified.
  • Keystone endpoints are version less. Version 3 is the default as v2.0 is deprecated. Meanwhile Keystone V3 still supports v2.0 for backward compatibility. Therefore passing a tenant instead of a project (along with a domain) makes Keystone provide v2.0 token. Connection parameters:
@connection_params={openstack_auth_url: "http://devstack.test:5000",openstack_username: "admin",openstack_api_key: "password",openstack_project_name: "admin",openstack_domain_id: "default"}

If you're using Keystone V2, you don't need to supply domain details but ensure to either provide a tenant name (openstack_tenant) or a tenant id (openstack_tenant_id). Alternatively you can use :openstack_identity_api_version parameter with 'v2.0'.

@connection_params={openstack_auth_url: "http://devstack.test:5000",openstack_username: "admin",openstack_api_key: "password",openstack_tenant: "admin"}

If you're not sure whether your OpenStack cloud uses Keystone V2 or V3 then you can find out by logging into the dashboard (Horizon) and navigating to "Access & Security" under the "Project" section. Select "API Access" and find the line for the Identity Service. If the endpoint has "v3" in it, you're on Keystone V3, if it has "v2" then (surprise) you're on Keystone V2.

If you need a version of OpenStack to test against, get youself a copy of DevStack.

Networking Gotcha

Note that tenants (aka projects) in OpenStack usually require that you create a default gateway router in order to allow external access to your instances.

The exception is if you're using Nova (and not Neutron) for your instance networking. If you're using Neutron, you'll want to set up your default gateway before you try to give instances public addresses (aka floating IPs).

Compute (Nova)

Initialise a connection to the compute service:

compute=Fog::OpenStack::Compute.new(@connection_params)

Get a list of available images for use with booting new instances:

pcompute.images# => <Fog::OpenStack::Compute::Images# filters={},# server=nil# [# <Fog::OpenStack::Compute::Image# id="57a67f8a-7bae-4578-b684-b9b4dcd48d7f",# ...# ># ]# >

List available flavors so we can decide how powerful to make this instance:

pcompute.flavors# => <Fog::OpenStack::Compute::Flavors# [# <Fog::OpenStack::Compute::Flavor# id="1",# name="m1.tiny",# ram=512,# disk=1,# vcpus=1,# ...# >,# <Fog::OpenStack::Compute::Flavor# id="2",# name="m1.small",# ram=2048,# disk=20,# vcpus=1,# ...# >,# ...

Now we know the id numbers of a valid image and a valid flavor, we can instantiate an instance:

flavor=compute.flavors[0]image=compute.images[0]instance=compute.servers.createname: 'test',image_ref: image.id,flavor_ref: flavor.id# Optionally, wait for the instance to provision before continuinginstance.wait_for{ready?}# => {:duration=>17.359134}pinstance# => <Fog::OpenStack::Compute::Server# id="63633125-26b5-4fe1-a909-0f44d1ab3337",# instance_name=nil,# addresses={"public"=>[{"OS-EXT-IPS-MAC:mac_addr"=>"fa:16:3e:f4:75:ab", "version"=>4, "addr"=>"1.2.3.4", "OS-EXT-IPS:type"=>"fixed"}]},# flavor={"id"=>"2"},# host_id="f5ea01262720d02e886508bc4fa994782c516557d232c72aeb79638e",# image={"id"=>"57a67f8a-7bae-4578-b684-b9b4dcd48d7f"},# name="test",# personality=nil,# progress=0,# accessIPv4="",# accessIPv6="",# availability_zone="nova",# user_data_encoded=nil,# state="ACTIVE",# created=2016-03-07 08:07:36 UTC,# updated=2016-03-07 08:07:52 UTC,# tenant_id="06a9a90c60074cdeae5f7fdd0048d9ac"# ...# >

And destroy it when we're done:

instance.destroy# => true

You'll probably need your instances to be accessible via SSH. Learn more about SSH keypairs.

Allow TCP traffic through port 22:

security_group=compute.security_groups.createname: "Test SSH",description: "Allow access to port 22"# => <Fog::OpenStack::Compute::SecurityGroup# id="e5d53d00-b3f9-471a-b90f-985694b966ed",# name="Test SSH",# description="Allow access to port 22",# security_group_rules= <Fog::OpenStack::Compute::SecurityGroupRules# [# ]# >,# tenant_id="06a9a90c60074cdeae5f7fdd0048d9ac"# >compute.security_group_rules.createparent_group_id: security_group.id,ip_protocol: "tcp",from_port: 22,to_port: 22key_pair=compute.key_pairs.createname: "My Public Key",public_key: "/full/path/to/ssh.pub"# => <Fog::OpenStack::Compute::KeyPair# name="My Public Key",# ...# user_id="20746f49211e4037a91269df6a3fbf7b",# id=nil# >

Now create a new server using the security group and keypair we created:

instance=compute.servers.createname: "Test 2",image_ref: image.id,flavor_ref: flavor.id,key_name: key_pair.name,security_groups: security_group# => <Fog::OpenStack::Compute::Server# id="e18ebdfb-e5f5-4a45-929f-4cc9926dc2c7",# name="Test 2",# state="ACTIVE",# tenant_id="06a9a90c60074cdeae5f7fdd0048d9ac",# key_name="My Public Key",# ># (some data omitted for brevity)

Finally, assign a floating IP address to make this instance sit under a world-visible public IP address:

pool_name=compute.addresses.get_address_pools[0]['name']floating_ip_address=compute.addresses.createpool: pool_nameinstance.associate_addressfloating_ip_address.ippfloating_ip_address# => <Fog::OpenStack::Compute::Address# id="54064324-ce7d-448d-9753-94497b29dc91",# ip="1.2.3.4",# pool="external",# fixed_ip="192.168.0.96",# instance_id="e18ebdfb-e5f5-4a45-929f-4cc9926dc2c7"# >

Now you can SSH into the instance:

$ ssh cirros@1.2.3.4
The authenticity of host '1.2.3.4 (1.2.3.4)' can't be established.
RSA key fingerprint is SHA256:cB0L/owUtcHsMhFhsuSZXxK4oRg/uqP/6IriUomQnQQ.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '1.2.3.4' (RSA) to the list of known hosts.
$ pwd
/home/cirros

Volume (Cinder)

Create and attach a volume to a running instance:

compute=Fog::OpenStack::Compute.new(@connection_params)volume=compute.volumes.createname: "Test",description: "Testing",size: 1# => <Fog::OpenStack::Compute::Volume# id="4a212986-c6b6-4a93-8319-c6a98e347750",# name="Test",# description="Testing",# size=1,# availability_zone="Production",# created_at="2016-03-07T13:40:43.914063",# attachments=[{}]# >flavor=compute.flavors[3]image=compute.images[0]instance=compute.servers.createname: "test",image_ref: image.id,flavor_ref: flavor.idinstance.wait_for{ready?}volume.reloadinstance.attach_volume(volume.id,"/dev/vdb")

Detach volume and create a snapshot:

instance.detach_volume(volume.id)volume.reloadcompute.snapshots.createvolume_id: volume.id,name: "test",description: "test"# => <Fog::OpenStack::Compute::Snapshot# id="7a8c9192-25ee-4364-be91-070b7a6d9855",# name="test",# description="test",# volume_id="4a212986-c6b6-4a93-8319-c6a98e347750",# status="creating",# size=1,# created_at="2016-03-07T13:47:11.543814"# >

Destroy a volume:

volume.destroy# => true

Image (Glance)

Download Glance image:

image=Fog::OpenStack::Image.new(@connection_params)image_out=File.open("/tmp/cirros-image-download",'wb')streamer=lambdado |chunk,_,_|
image_out.writechunkendimage.download_image(image.images.first.id,response_block: streamer)

Create Glance image from file or URL:

cirros_location="http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img"image_out=File.open("/tmp/cirros-image-#{SecureRandom.hex}",'wb')streamer=lambdado |chunk,_,_|
image_out.writechunkendExcon.getcirros_location,response_block: streamerimage_out.closeimage_handle=image.images.createname: "cirros",disk_format: "qcow2",container_format: "bare"# => <Fog::OpenStack::Image::V2::Image# id="67c4d02c-5601-4619-bd14-d2f7f96a046c",# name="cirros",# visibility="private",# tags=[],# self="/v2/images/67c4d02c-5601-4619-bd14-d2f7f96a046c",# size=nil,# disk_format="qcow2",# container_format="bare",# id="67c4d02c-5601-4619-bd14-d2f7f96a046c",# checksum=nil,# self="/v2/images/67c4d02c-5601-4619-bd14-d2f7f96a046c",# file="/v2/images/67c4d02c-5601-4619-bd14-d2f7f96a046c/file",# min_disk=0,# created_at="2016-10-31T03:38:28Z",# updated_at="2016-10-31T03:38:28Z",# protected=false,# status="queued",# min_ram=0,# owner="6b9ec8080b8443c5afe2267a39b9bf74",# properties=nil,# metadata=nil,# location=nil,# network_allocated=nil,# base_image_ref=nil,# image_type=nil,# instance_uuid=nil,# user_id=nil# >image_handle.upload_dataFile.binread(image_out.path)

Destroy image:

cirros=image.images.get("4beedb46-e32f-4ef3-a87b-7f1234294dc1")cirros.destroy

Identity (Keystone)

List domains (Keystone V3 only):

identity=Fog::OpenStack::Identity.new(@connection_params)identity.domains# => <Fog::OpenStack::Identity::V3::Domains# [# <Fog::OpenStack::Identity::V3::Domain# id="default",# description="",# enabled=true,# name="Default",# ># ]# >

List projects (aka tenants):

identity.projects# => <Fog::OpenStack::Identity::V3::Projects# [# <Fog::OpenStack::Identity::V3::Project# id="008e5537d3424295a03560abc923693c",# domain_id="default",# description="Project 1",# enabled=true,# name="project_1",# >,# ...# ]# On Keystone V2identity.tenants# => <Fog::OpenStack::Identity::V2::Tenants# [ ... ]

List users:

identity.users# => <Fog::OpenStack::Identity::V3::Users# [ ... ]

Create/destroy new user:

project_id=identity.projects[0].iduser=identity.users.createname: "test",project_id: project_id,email: "test@test.com",password: "test"# => <Fog::OpenStack::Identity::V3::User# id="474a59153ebd4e709938e5e9b614dc57",# default_project_id=nil,# description=nil,# domain_id="default",# email="test@test.com",# enabled=true,# name="test",# password="test"# >user.destroy# => true

Create/destroy new tenant:

project=identity.projects.createname: "test",description: "test"# => <Fog::OpenStack::Identity::V3::Project# id="423559128a7249f2973cdb7d5d581c4d",# domain_id="default",# description="test",# enabled=true,# name="test",# parent_id=nil,# subtree=nil,# parents=nil# >project.destroy# => true

Grant user role on tenant and revoke it:

role=identity.roles.select{|role| role.name == "_member_"}[0]# => <Fog::OpenStack::Identity::V3::Role# id="9fe2ff9ee4384b1894a90878d3e92bab",# name="_member_",# >project.grant_role_to_user(role.id,user.id)project.revoke_role_from_user(role.id,user.id)

Networking (Neutron)

Set up a project's public gateway (needed for external access):

identity=Fog::OpenStack::Identity.new(@connection_params)tenants=identity.projects.selectdo |project|
project.name == @connection_params[:openstack_project_name]endtenant_id=tenants[0].idneutron=Fog::OpenStack::Network.new(@connection_params)network=neutron.networks.createname: "default",tenant_id: tenant_idsubnet=network.subnets.createname: "default",cidr: "192.168.0.0/24",network_id: network.id,ip_version: 4,dns_nameservers: ["8.8.8.8","8.8.4.4"],tenant_id: tenant_idexternal_network=neutron.networks.select(&:router_external)[0]router=neutron.routers.createname: 'default',tenant_id: tenant_id,external_gateway_info: external_network.idneutron.add_router_interfacerouter.id,subnet.id

Further Reading

Development

$ git clone https://github.com/fog/fog-openstack.git # Clone repository
$ cd fog-openstack; bin/setup # Install dependencies from project directory
$ bundle exec rake test # Run tests
$ bundle exec rake spec # Run tests
$ bin/console # Run interactive prompt that allows you to experiment (optional)
$ bundle exec rake install # Install gem to your local machine (optional)

You can also use a docker image for development and running tests. Once you have cloned the repository, it can be run with:

$ docker-compose up test
$ docker-compose up ruby # Start a container with the ruby environment

In order to release a new version, perform the following steps:

  1. Update version number in version.rb.
  2. Run bundle exec rake release, which will create a git tag for the version.
  3. Push git commits and tags.
  4. Push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/fog/fog-openstack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

About

Fog for OpenStack Platform

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

69 stars

Watchers

10 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages