Skip to content

Using_the_Rails_console

movitto edited this page Jan 16, 2013 · 3 revisions

Using the Rails console

The Rails console is an interactive command-line console allowing one to execute code in the context of the Rails application and its database.

Use from source

If you have Conductor’s source checked out, you should be able to cd to src/ and run rails console to start the console. .

In the event that you have an RPM version of the app already installed, and you have cloned the source just for access to the console, you will want to do the following the source:

  • Copy /etc/aeolus-conductor/database.yml to src/config/database.yml
  • Copy /etc/aeolus-conductor/oauth.json to src/config/oauth.json if you will be interacting with ] or ].

Use from RPM

The RPM does not include the Rails console, but if you clone the source code repo you can cheat. It is not officially supported, but you should be able to copy src/script/rails into the /usr/share/aeolus-conductor/script/ directory. To use it with the existing “production” database, you would cd to /usr/share/aeolus-conductor and run rails console production. Note that this requires the ability to read the privileged database.yml file, so you need to either be root or belong to the aeolus group.

Using the console

When the console is first started, you should see something like the following:

$ rails console
Using gem require instead of bundler
Loading development environment 
irb:001:0> 

The last line is the prompt, indicating that you are in the Rails console. Now, statements you type will be interpeted as Ruby code run within the context of the application.

Here are some examples of actions one might want to take in the Rails console. Lines beginning with a ‘#’ are comments, and the “irb(main):003:0>” bit is the prompt-- don’t type that.

Find and modify the admin user

# Find the user with login == 'admin'
irb(main):003:0> user = User.find_by_login('admin')
=> #<User id: 2, login: "admin", email: "[email protected]", crypted_password: "f6c810483edb84bd7953b3bcaf5eb4c7a29e7a3cdeb8d56b14f...", first_name: "Administrator", last_name: "", quota_id: 5, login_count: 182, failed_login_count: 10, last_request_at: nil, current_login_at: nil, last_login_at: "2012-05-25 15:24:17", current_login_ip: nil, last_login_ip: "127.0.0.1", created_at: "2012-03-01 16:34:09", updated_at: "2012-05-25 15:24:17">

# Update their email address in memory only:
irb(main):005:0> user.email = "[email protected]"
=> "[email protected]"

# View the changes you have made (informaton only; this is not necessary)
irb(main):006:0> user.changes
=> {"email"=>["[email protected]", "[email protected]"]}

# See if the object still passes validation (information only; this is not necessary)
irb(main):007:0> user.valid?
=> true

# Make it invalid to show what happens:
irb(main):008:0> user.email = ""
=> ""

irb(main):009:0> user.valid?
=> false

# See what is preventing the record from being saved:
irb(main):010:0> user.errors
=> #<OrderedHash {:email=>["is invalid"]}>

# Change it back to something valid:
irb(main):011:0> user.email = "[email protected]"
=> "[email protected]"

# Save our changes (commit them to the database):
irb(main):012:0> user.save
=> true

Find images in Image Warehouse using Conductor

irb(main):013:0> images = Aeolus::Image::Warehouse::Image.all
 <snip a whole bunch of output>

# The images are now in an array:
irb(main):014:0> images.size
=> 4

# Look at one of them:
irb(main):015:0> images[0]
=> #<Aeolus::Image::Warehouse::Image:0x7faba7300250 @uuid="12dab92d-f1e9-494c-a722-491994487401", @latest_build="62dd8550-76af-4ae1-a296-1966dc6ccc03", @obj=#<Aeolus::Image::Warehouse::BucketObject:0x7faba73006d8 @path="/images/12dab92d-f1e9-494c-a722-491994487401", @key="12dab92d-f1e9-494c-a722-491994487401", @connection=#<Aeolus::Image::Warehouse::Connection:0x7faba7322d28 @consumer=#<OAuth::Consumer:0x7faba7322738 @http=#<Net::HTTPSession localhost:9090 open=false>, @options={:access_token_path=>"/oauth/access_token", :http_method=>:post, :signature_method=>"HMAC-SHA1", :site=>"http://localhost:9090", :request_token_path=>"/oauth/request_token", :proxy=>nil, :authorize_path=>"/oauth/authorize", :scheme=>:header, :oauth_version=>"1.0"}, @key="nzSuuTHx6MsRaCE2ccCSubUSHWT/4OEd", @uri=#<URI::HTTP:0x7faba73205a0 URL:http://localhost:9090>, @secret="QjkY98JLseLC9Xiw159XQRGunsPdAwb9">, @token=#<OAuth::AccessToken:0x7faba73225d0 @params={}, @consumer=#<OAuth::Consumer:0x7faba7322738 @http=#<Net::HTTPSession localhost:9090 open=false>, @options={:access_token_path=>"/oauth/access_token", :http_method=>:post, :signature_method=>"HMAC-SHA1", :site=>"http://localhost:9090", :request_token_path=>"/oauth/request_token", :proxy=>nil, :authorize_path=>"/oauth/authorize", :scheme=>:header, :oauth_version=>"1.0"}, @key="nzSuuTHx6MsRaCE2ccCSubUSHWT/4OEd", @uri=#<URI::HTTP:0x7faba73205a0 URL:http://localhost:9090>, @secret="QjkY98JLseLC9Xiw159XQRGunsPdAwb9">, @token="", @secret="", @response=#<Net::HTTPOK 200 OK readbody=true>>, @uri="http://localhost:9090">, @bucket=#<Aeolus::Image::Warehouse::Bucket:0x7faba7322c88 @connection=#<Aeolus::Image::Warehouse::Connection:0x7faba7322d28 @consumer=#<OAuth::Consumer:0x7faba7322738 @http=#<Net::HTTPSession localhost:9090 open=false>, @options={:access_token_path=>"/oauth/access_token", :http_method=>:post, :signature_method=>"HMAC-SHA1", :site=>"http://localhost:9090", :request_token_path=>"/oauth/request_token", :proxy=>nil, :authorize_path=>"/oauth/authorize", :scheme=>:header, :oauth_version=>"1.0"}, @key="nzSuuTHx6MsRaCE2ccCSubUSHWT/4OEd", @uri=#<URI::HTTP:0x7faba73205a0 URL:http://localhost:9090>, @secret="QjkY98JLseLC9Xiw159XQRGunsPdAwb9">, @token=#<OAuth::AccessToken:0x7faba73225d0 @params={}, @consumer=#<OAuth::Consumer:0x7faba7322738 @http=#<Net::HTTPSession localhost:9090 open=false>, @options={:access_token_path=>"/oauth/access_token", :http_method=>:post, :signature_method=>"HMAC-SHA1", :site=>"http://localhost:9090", :request_token_path=>"/oauth/request_token", :proxy=>nil, :authorize_path=>"/oauth/authorize", :scheme=>:header, :oauth_version=>"1.0"}, @key="nzSuuTHx6MsRaCE2ccCSubUSHWT/4OEd", @uri=#<URI::HTTP:0x7faba73205a0 URL:http://localhost:9090>, @secret="QjkY98JLseLC9Xiw159XQRGunsPdAwb9">, @token="", @secret="", @response=#<Net::HTTPOK 200 OK readbody=true>>, @uri="http://localhost:9090">, @name="images">>, @xml_body=#<Nokogiri::XML::Document:0x3fd5d391b278 name="document" children=[#<Nokogiri::XML::Element:0x3fd5d3c57144 name="image" children=[#<Nokogiri::XML::Element:0x3fd5d3c56b2c name="name" children=[#<Nokogiri::XML::Text:0x3fd5d3c560f0 "img1">]>]>]>, @environment="default", @attrs={"environment"=>"default", "object_type"=>"image", "latest_build"=>"62dd8550-76af-4ae1-a296-1966dc6ccc03", "uuid"=>"12dab92d-f1e9-494c-a722-491994487401"}, @object_type="image">

# Find its name (rather than searching through that long block of output!)
irb(main):016:0> images[0].name
=> "img1"

# Find its UUID
irb(main):018:0> images[0].uuid
=> "12dab92d-f1e9-494c-a722-491994487401"

# Find its associated provider images
irb(main):020:0> images[0].provider_images
=> [#<Aeolus::Image::Warehouse::ProviderImage:0x7faba75794a0 @uuid="ab2f9be1-a555-456e-9bc6-f3ebda209de6", @target_image="3b3a918c-023a-42d3-9258-58a49d3092ac", @obj=#<Aeolus::Image::Warehouse::BucketObject:0x7faba7579428 @path="/provider_images/ab2f9be1-a555-456e-9bc6-f3ebda209de6", @key="ab2f9be1-a555-456e-9bc6-f3ebda209de6", @connection=#<Aeolus::Image::Warehouse::Connection:0x7faba75a0730 @consumer=#<OAuth::Consumer:0x7faba75a0578 @http=#<Net::HTTPSession localhost:9090 open=false>, @options={:access_token_path=>"/oauth/access_token", :http_method=>:post, :signature_method=>"HMAC-SHA1", :site=>"http://localhost:9090", :request_token_path=>"/oauth/request_token", :proxy=>nil, :authorize_path=>"/oauth/authorize", :scheme=>:header, :oauth_version=>"1.0"}, @key="nzSuuTHx6MsRaCE2ccCSubUSHWT/4OEd", @uri=#<URI::HTTP:0x7faba759ea20 URL:http://localhost:9090>, @secret="QjkY98JLseLC9Xiw159XQRGunsPdAwb9">, @token=#<OAuth::AccessToken:0x7faba75a0488 @params={}, @consumer=#<OAuth::Consumer:0x7faba75a0578 @http=#<Net::HTTPSession localhost:9090 open=false>, @options={:access_token_path=>"/oauth/access_token", :http_method=>:post, :signature_method=>"HMAC-SHA1", :site=>"http://localhost:9090", :request_token_path=>"/oauth/request_token", :proxy=>nil, :authorize_path=>"/oauth/authorize", :scheme=>:header, :oauth_version=>"1.0"}, @key="nzSuuTHx6MsRaCE2ccCSubUSHWT/4OEd", @uri=#<URI::HTTP:0x7faba759ea20 URL:http://localhost:9090>, @secret="QjkY98JLseLC9Xiw159XQRGunsPdAwb9">, @token="", @secret="", @response=#<Net::HTTPOK 200 OK readbody=true>>, @uri="http://localhost:9090">, @bucket=#<Aeolus::Image::Warehouse::Bucket:0x7faba75a0708 @connection=#<Aeolus::Image::Warehouse::Connection:0x7faba75a0730 @consumer=#<OAuth::Consumer:0x7faba75a0578 @http=#<Net::HTTPSession localhost:9090 open=false>, @options={:access_token_path=>"/oauth/access_token", :http_method=>:post, :signature_method=>"HMAC-SHA1", :site=>"http://localhost:9090", :request_token_path=>"/oauth/request_token", :proxy=>nil, :authorize_path=>"/oauth/authorize", :scheme=>:header, :oauth_version=>"1.0"}, @key="nzSuuTHx6MsRaCE2ccCSubUSHWT/4OEd", @uri=#<URI::HTTP:0x7faba759ea20 URL:http://localhost:9090>, @secret="QjkY98JLseLC9Xiw159XQRGunsPdAwb9">, @token=#<OAuth::AccessToken:0x7faba75a0488 @params={}, @consumer=#<OAuth::Consumer:0x7faba75a0578 @http=#<Net::HTTPSession localhost:9090 open=false>, @options={:access_token_path=>"/oauth/access_token", :http_method=>:post, :signature_method=>"HMAC-SHA1", :site=>"http://localhost:9090", :request_token_path=>"/oauth/request_token", :proxy=>nil, :authorize_path=>"/oauth/authorize", :scheme=>:header, :oauth_version=>"1.0"}, @key="nzSuuTHx6MsRaCE2ccCSubUSHWT/4OEd", @uri=#<URI::HTTP:0x7faba759ea20 URL:http://localhost:9090>, @secret="QjkY98JLseLC9Xiw159XQRGunsPdAwb9">, @token="", @secret="", @response=#<Net::HTTPOK 200 OK readbody=true>>, @uri="http://localhost:9090">, @name="provider_images">>, @provider_account_identifier="mockuser", @attrs={"provider"=>"mock", "target_image"=>"3b3a918c-023a-42d3-9258-58a49d3092ac", "object_type"=>"provider_image", "target_identifier"=>"img1", "provider_account_identifier"=>"mockuser", "uuid"=>"ab2f9be1-a555-456e-9bc6-f3ebda209de6"}, @object_type="provider_image", @target_identifier="img1", @provider="mock">]
Clone this wiki locally