Class: Conjure::Service::CloudServer
- Inherits:
-
Object
- Object
- Conjure::Service::CloudServer
- Defined in:
- lib/conjure/service/cloud_server.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .connection ⇒ Object
- .each_with_name_prefix(prefix, &block) ⇒ Object
- .ensure_unique_name(name) ⇒ Object
- .increment_numeric_suffix(name) ⇒ Object
Instance Method Summary collapse
- #account ⇒ Object
- #add_resource_id(options, type) ⇒ Object
- #connection ⇒ Object
- #existing_server ⇒ Object
- #fog_credentials ⇒ Object
-
#initialize(name) ⇒ CloudServer
constructor
A new instance of CloudServer.
- #ip_address ⇒ Object
- #new_server ⇒ Object
- #prepare_bootstrap_options(options) ⇒ Object
- #remote_shell ⇒ Object
- #resource_id(collection, name) ⇒ Object
- #run(command, options = {}, &block) ⇒ Object
- #server ⇒ Object
- #set_fog_credentials ⇒ Object
Constructor Details
#initialize(name) ⇒ CloudServer
Returns a new instance of CloudServer.
8 9 10 |
# File 'lib/conjure/service/cloud_server.rb', line 8 def initialize(name) @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/conjure/service/cloud_server.rb', line 6 def name @name end |
Class Method Details
.connection ⇒ Object
36 37 38 |
# File 'lib/conjure/service/cloud_server.rb', line 36 def self.connection new("").connection end |
.each_with_name_prefix(prefix, &block) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/conjure/service/cloud_server.rb', line 40 def self.each_with_name_prefix(prefix, &block) return unless connection connection.servers.all.select{|s| s.name.match(/^#{prefix}/)}.each do |server| block.call new(server.name) end end |
.ensure_unique_name(name) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/conjure/service/cloud_server.rb', line 47 def self.ensure_unique_name(name) return name unless connection existing_names = connection.servers.all.map{ |s| s.name } name = increment_numeric_suffix(name) while existing_names.include? name name end |
.increment_numeric_suffix(name) ⇒ Object
54 55 56 57 58 |
# File 'lib/conjure/service/cloud_server.rb', line 54 def self.increment_numeric_suffix(name) parts = name.split("-") parts[2] = parts[2] ? ((parts[2].to_i)+1).to_s : "2" parts.join("-") end |
Instance Method Details
#account ⇒ Object
67 68 69 |
# File 'lib/conjure/service/cloud_server.rb', line 67 def account @account ||= Provider.all(:cloud_account).first end |
#add_resource_id(options, type) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/conjure/service/cloud_server.rb', line 75 def add_resource_id(, type) id = "#{type}_id".to_sym name = "#{type}_name".to_sym collection = "#{type}s".to_sym [id] = resource_id(collection, [name]) if [name] end |
#connection ⇒ Object
71 72 73 |
# File 'lib/conjure/service/cloud_server.rb', line 71 def connection @connection ||= Fog::Compute.new account. if account end |
#existing_server ⇒ Object
32 33 34 |
# File 'lib/conjure/service/cloud_server.rb', line 32 def existing_server @existing_server ||= connection.servers.find{|s| s.name == @name } if connection end |
#fog_credentials ⇒ Object
98 99 100 101 102 103 |
# File 'lib/conjure/service/cloud_server.rb', line 98 def fog_credentials { private_key_path: Conjure.identity.private_key_path, public_key_path: Conjure.identity.public_key_path, } end |
#ip_address ⇒ Object
28 29 30 |
# File 'lib/conjure/service/cloud_server.rb', line 28 def ip_address server.public_ip_address end |
#new_server ⇒ Object
60 61 62 63 64 65 |
# File 'lib/conjure/service/cloud_server.rb', line 60 def new_server Log.info " [cloud] Launching new server #{@name}" = account..merge(:name => @name) = ().merge(fog_credentials) connection.servers.bootstrap end |
#prepare_bootstrap_options(options) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/conjure/service/cloud_server.rb', line 82 def () add_resource_id(, :flavor) add_resource_id(, :region) add_resource_id(, :image) end |
#remote_shell ⇒ Object
105 106 107 108 109 110 |
# File 'lib/conjure/service/cloud_server.rb', line 105 def remote_shell @remote_shell ||= RemoteShell.new( :ip_address => server.public_ip_address, :username => "root", ) end |
#resource_id(collection, name) ⇒ Object
89 90 91 |
# File 'lib/conjure/service/cloud_server.rb', line 89 def resource_id(collection, name) connection.send(collection).find{|i| i.name == name}.id end |
#run(command, options = {}, &block) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/conjure/service/cloud_server.rb', line 12 def run(command, = {}, &block) set_fog_credentials file_set = RemoteFileSet.new(:shell => remote_shell, :files => [:files]) file_set.upload result = remote_shell.run(command, :stream_stdin => [:stream_stdin], &block) file_set.remove result end |
#server ⇒ Object
21 22 23 24 25 26 |
# File 'lib/conjure/service/cloud_server.rb', line 21 def server @server ||= existing_server @server ||= new_server @server.wait_for { ready? } @server end |