Ruby Openstack Compute and Object-Store bindings for the v1.0 OSAPI.
Currently supports both v1.0 and v2.0 (keystone) auth.
Use OpenStack::Connection.create to get a handle to an OpenStack service - set the :service_type parameter to either ‘compute’ or ‘object-store’ (defaults to ‘compute’)
See the class definitions for documentation on specific methods and operations.
require 'openstack' os = OpenStack::Connection.create(:username => USERNAME, :api_key => API_KEY, :authtenant => TENANT, :auth_url => API_URL, :service_type => "compute") # Get a listing of all current servers >> os.servers => [{:name=>"Server1", :id=>110917}] # Access a specific server >> server = os.server(110917) >> server.name => "Server1" # See what type of server this is >> server.flavor.name => "256 server" >> server.image.name => "Ubuntu 8.04.2 LTS (hardy)" # Soft-reboot the server >> server.reboot => true # Create a new 512MB CentOS 5.2 server. The root password is returned in the adminPass method. >> image = os.get_image(8) => #<OpenStack::Compute::Image:0x1014a8060 ...>, status"ACTIVE" >> image.name => "CentOS 5.2" >> flavor = os.get_flavor(2) => #<OpenStack::Compute::Flavor:0x101469130 @disk=20, @name="512 server", @id=2, @ram=512> >> flavor.name => "512 server" >> newserver = os.create_server(:name => "New Server", :imageRef => image.id, :flavorRef => flavor.id) => #<OpenStack::Compute::Server:0x101433f08 .... >> newserver.status => "BUILD" >> newserver.progress => 0 >> newserver.adminPass => "NewServerMbhzUnO" >> newserver.refresh => true >> newserver.progress => 12 # Delete the new server >> newserver.delete! => true
os = OpenStack::Connection.create(:username => USERNAME, :api_key => API_KEY, :authtenant => TENANT, :auth_url => API_URL, :service_type => "object-store") # Get info on container count and bytes: >> os.get_info => {:count=>2, :bytes=>495041} # Get list of containers under this account: >> os.containers => ["another_containerfoo", "marios_test_container"] # Get details of containers under this account: >> os.containers_detail =>=> {"another_containerfoo"=>{:count=>"3", :bytes=>"1994"}, "marios_test_container"=>{:count=>"2", :bytes=>"493047"}} # Check if a container exists >> os.container_exists?("no_such_thing") => false # Create new container >> os.create_container("foo") => => #<OpenStack::Swift::Container:0xb7275c38 ...... (rest of OpenStack::Swift::Container object) # Delete container >> os.delete_container("foo") => true # Get a container (OpenStack::Swift::Container object): >> cont = os.container("foo") => #<OpenStack::Swift::Container:0xb7262124 ...... (rest of OpenStack::Swift::Container object) # Retrieve container metadata: >> cont.container_metadata =>{:count=>"2", :bytes=>"493047", :metadata=>{"foo"=>"bar", "author"=>"foobar", "jj"=>"foobar", "date"=>"today", "owner"=>"foo"}} # Retrieve user defined metadata: >> cont.metadata => {"foo"=>"bar", "author"=>"foobar", "jj"=>"foobar", "date"=>"today", "owner"=>"foo"} # Set user defined metadata: >> cont.set_metadata({"X-Container-Meta-Author"=> "msa", "version"=>"1.2", :date=>"today"}) => true # Get list of objects: >> cont.objects => ["fosdem2012.pdf", "moved_object"] # Get list of objects with details: >> cont.objects_detail => {"fosdem2012.pdf"=>{:bytes=>"493009", :content_type=>"application/json", :hash=>"494e444f92a8082dabac80a74cdf2c3b", :last_modified=>"2012-04-26T09:22:51.611230"}, "moved_object"=>{:bytes=>"38", :content_type=>"application/json", :hash=>"a7942f97fe6bd34920a4f61fe5e604a5", :last_modified=>"2012-04-26T09:35:33.839920"}} # Check if container is empty: >> cont.empty? => false # Check if object exists: >> cont.object_exists?("foo") => false # Create new object >> new_obj = cont.create_object("foo", {:metadata=>{"herpy"=>"derp"}, :content_type=>"text/plain"}, "this is the data") [can also supply File.open(/path/to/file) and the data] => #<OpenStack::Swift::StorageObject:0xb72fdac0 ... etc # Delete object >> cont.delete_object("foo") => true # Get handle to an OpenStack::Swift::StorageObject Object >> obj = cont.object("foo") => #<OpenStack::Swift::StorageObject:0xb72fdac0 ... etc # Get object metadata >> obj.object_metadata => # Get user defined metadata pairs >> obj.metadata => # Get data (non streaming - returned as a String) >> obj.data => "This is the text stored in the file" # Get data (streaming - requires a block) >> data = ""; object.data_stream do |chunk| data += chunk end => #<Net::HTTPOK 200 OK readbody=true> >> data => "This is the text stored in the file" # Set user defined metadata >> obj.set_metadata({:foo=>"bar", "X-Object-Meta-herpa"=>"derp", "author"=>"me"}) => true # (Over)Write object data >> object.write("This is new data") => true >> object.data => "This is new data" # Copy object: >>copied = obj.copy('copied_object', "destination_container", {:content_type=>"text/plain", :metadata=>{:herp=>"derp", "X-Object-Meta-foo"=>"bar} } ) => #<OpenStack::Swift::StorageObject:0xb728974c ..... etc # Move object: (copy and then delete original): >> moved = obj.move('moved_object', "destination_container", {:content_type=>"text/plain", :metadata=>{:herp=>"derp", "X-Object-Meta-foo"=>"bar"} } ) => #<OpenStack::Swift::StorageObject:0xb7266bd4 ... >> moved.metadata => {"foo"=>"bar", "herp"=>"derp", "herpy"=>"derp"} >> obj.metadata => OpenStack::Exception::ItemNotFound: The resource could not be found
By Dan Prince <[email protected]>, Naveed Massjouni <[email protected]>, Marios Andreou <[email protected]>
Initial code checkin on May 23rd 2012 - code refactored from and based on the Rackspace Cloud Servers gem (github.com/rackspace/ruby-openstack-compute) and Rackspace Cloud Files gem (github.com/rackspace/ruby-cloudfiles).
See COPYING for license information.