Class: Conjure::Service::CloudServer

Inherits:
Object
  • Object
show all
Defined in:
lib/conjure/service/cloud_server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject (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

.connectionObject



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

#accountObject



67
68
69
# File 'lib/conjure/service/cloud_server.rb', line 67

def 
  @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(options, type)
  id = "#{type}_id".to_sym
  name = "#{type}_name".to_sym
  collection = "#{type}s".to_sym
  options[id] = resource_id(collection, options[name]) if options[name]
end

#connectionObject



71
72
73
# File 'lib/conjure/service/cloud_server.rb', line 71

def connection
  @connection ||= Fog::Compute.new .compute_options if 
end

#existing_serverObject



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_credentialsObject



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_addressObject



28
29
30
# File 'lib/conjure/service/cloud_server.rb', line 28

def ip_address
  server.public_ip_address
end

#new_serverObject



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}"
  bootstrap_options = .bootstrap_options.merge(:name => @name)
  options = prepare_bootstrap_options(bootstrap_options).merge(fog_credentials)
  connection.servers.bootstrap options
end

#prepare_bootstrap_options(options) ⇒ Object



82
83
84
85
86
87
# File 'lib/conjure/service/cloud_server.rb', line 82

def prepare_bootstrap_options(options)
  add_resource_id(options, :flavor)
  add_resource_id(options, :region)
  add_resource_id(options, :image)
  options
end

#remote_shellObject



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, options = {}, &block)
  set_fog_credentials
  file_set = RemoteFileSet.new(:shell => remote_shell, :files => options[:files])
  file_set.upload
  result = remote_shell.run(command, :stream_stdin => options[:stream_stdin], &block)
  file_set.remove
  result
end

#serverObject



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

#set_fog_credentialsObject



93
94
95
96
# File 'lib/conjure/service/cloud_server.rb', line 93

def set_fog_credentials
  Fog.credential = Conjure.identity.unique_identifier
  Fog.credentials.merge! fog_credentials
end