Class: Coral::Cloud::Server

Inherits:
Core
  • Object
show all
Defined in:
lib/coral_cloud/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server


Constructor / Destructor



9
10
11
12
13
14
# File 'lib/coral_cloud/server.rb', line 9

def initialize(options = {})
  super(options)
  
  self.machine = ( options.has_key?(:machine) ? options[:machine] : '' )    
  @cloud       = ( options.has_key?(:cloud) ? options[:cloud] : Coral.create_cloud(@name, options) )    
end

Instance Attribute Details

#cloudObject (readonly)


Property accessors / modifiers



19
20
21
# File 'lib/coral_cloud/server.rb', line 19

def cloud
  @cloud
end

#machineObject


Property accessors / modifiers



19
20
21
# File 'lib/coral_cloud/server.rb', line 19

def machine
  @machine
end

#nameObject (readonly)


Property accessors / modifiers



19
20
21
# File 'lib/coral_cloud/server.rb', line 19

def name
  @name
end

Instance Method Details

#commit(options = {}) ⇒ Object




220
221
222
223
224
225
226
227
# File 'lib/coral_cloud/server.rb', line 220

def commit(options = {})    
  repositories(options).each do |name, repo|
    if repo.can_persist?
      repo.commit('.', options)
    end      
  end
  return true    
end

#destroy(options = {}) ⇒ Object




172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/coral_cloud/server.rb', line 172

def destroy(options = {})    
  return true if ! @machine
  
  if @machine.created?
    do_destroy = false

    if options[:force]
      do_destroy = true
    else
      choice = nil
      begin
        choice = ui.ask("Are you sure you want to remove: #{@name}?")
      rescue Errors::UIExpectsTTY
      end
      do_destroy = choice.upcase == "Y"
    end

    if do_destroy
      @machine.destroy
    end
  end
  return true    
end

#hostnameObject




36
37
38
# File 'lib/coral_cloud/server.rb', line 36

def hostname
  return @cloud.server(@name, 'hostname', '', :string)
end

#hostname=(hostname) ⇒ Object




42
43
44
# File 'lib/coral_cloud/server.rb', line 42

def hostname=hostname
  @cloud.set_server(@name, 'hostname', hostname)
end

#internal_ipObject




60
61
62
# File 'lib/coral_cloud/server.rb', line 60

def internal_ip
  return @cloud.server(@name, 'internal_ip', '', :string)
end

#internal_ip=(internal_ip) ⇒ Object




66
67
68
# File 'lib/coral_cloud/server.rb', line 66

def internal_ip=internal_ip
  @cloud.set_server(@name, 'internal_ip', internal_ip)
end

#process_puppet_message(line) ⇒ Object


Utilities



249
250
251
# File 'lib/coral_cloud/server.rb', line 249

def process_puppet_message(line)
  return line.match(/err:\s+/) ? { :success => false, :prefix => 'FAIL' } : true
end

#public_ipObject




48
49
50
# File 'lib/coral_cloud/server.rb', line 48

def public_ip
  return @cloud.server(@name, 'public_ip', '', :string)
end

#public_ip=(public_ip) ⇒ Object




54
55
56
# File 'lib/coral_cloud/server.rb', line 54

def public_ip=public_ip
  @cloud.set_server(@name, 'public_ip', public_ip)
end

#push(options = {}) ⇒ Object




231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/coral_cloud/server.rb', line 231

def push(options = {})  
  sync_remotes(options)
     
  success = true
  repositories(options).each do |name, repo|
    if repo.can_persist?
      success = repo.push!(@name, options) do |line|
        process_puppet_message(line)  
      end
      break unless success
    end
  end
  return success
end

#repositories(options = {}, return_objects = true) ⇒ Object




96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/coral_cloud/server.rb', line 96

def repositories(options = {}, return_objects = true)
  repositories = array(options[:repos], @cloud.repositories.keys, true)
  results      = ( return_objects ? {} : [] )    
  
  return results unless repositories.is_a?(Array)
  
  repositories.each do |repo_name|
    if @cloud.repositories.has_key?(repo_name)
      if return_objects
        results[repo_name] = @cloud.repositories[repo_name]
      else
        results << repo_name
      end
    end
  end
  return results
end

#shares(options = {}, return_objects = true) ⇒ Object




116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/coral_cloud/server.rb', line 116

def shares(options = {}, return_objects = true)
  shares  = array(options[:shares], @cloud.shares.keys, true)
  results = ( return_objects ? {} : [] )    
  
  return results unless shares.is_a?(Array)
  
  shares.each do |share_name|
    if @cloud.shares.has_key?(share_name)
      if return_objects
        results[share_name] = @cloud.shares[share_name]
      else
        results << share_name
      end
    end
  end
  return results
end

#start(options = {}) ⇒ Object


Management



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/coral_cloud/server.rb', line 137

def start(options = {})
  sync_remotes(options)
      
  return true if ! @machine
  
  options["provision.enabled"] = false
  
  if @machine.created?
    @machine.start(options)
  else
    @machine.up(options)
  end
  return true
end

#sync_remotes(options = {}) ⇒ Object


Repository operations



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/coral_cloud/server.rb', line 199

def sync_remotes(options = {})
  repositories(options).each do |repo_name, repo|
    if repo.can_persist?
      if @name != 'all'
        repo.set_remote(@name, public_ip, options)
      end        
      
      server_ips = []      
      @cloud.servers.each do |server_name, server|
        server_ips << server.public_ip
      end
      
      options[:add] = true
      repo.set_remote('all', server_ips, options)
    end
  end
  return true
end

#update(options = {}) ⇒ Object




154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/coral_cloud/server.rb', line 154

def update(options = {})
  sync_remotes(options)
  
  success = true    
  return success if ! @machine    
  
  if @machine.created?
    if @machine.state == :running
      success = Coral::Command.new("vagrant provision #{@name}").exec!(options) do |line|
        process_puppet_message(line)
      end    
    end
  end
  return success   
end

#virtual_hostnameObject




72
73
74
# File 'lib/coral_cloud/server.rb', line 72

def virtual_hostname
  return @cloud.search(@name, 'virtual_hostname', '', :string)
end

#virtual_hostname=(virtual_hostname) ⇒ Object




78
79
80
# File 'lib/coral_cloud/server.rb', line 78

def virtual_hostname=virtual_hostname
  @cloud.set_server(@name, 'virtual_hostname', virtual_hostname)
end

#virtual_ipObject




84
85
86
# File 'lib/coral_cloud/server.rb', line 84

def virtual_ip
  return @cloud.search(@name, 'virtual_ip', '', :string)
end

#virtual_ip=(virtual_ip) ⇒ Object




90
91
92
# File 'lib/coral_cloud/server.rb', line 90

def virtual_ip=virtual_ip
  @cloud.set_server(@name, 'virtual_ip', virtual_ip)
end