Class: RHC::Rest::Application

Inherits:
Base show all
Includes:
Membership
Defined in:
lib/rhc/rest/application.rb

Direct Known Subclasses

Mock::MockRestApplication

Instance Method Summary collapse

Methods included from Membership

#compact_members, #default_member_role, #delete_members, included, #leave, #members, #owner, #supports_members?, #supports_update_members?, #update_members

Methods inherited from Base

#add_message, #has_param?, #initialize, #link_href, #links, #rest_method, #supports?

Methods included from AttributesClass

#define_attr, #model_name

Methods included from Attributes

#attribute, #attributes, #attributes=, #clear_attribute

Constructor Details

This class inherits a constructor from RHC::Rest::Base

Instance Method Details

#<=>(other) ⇒ Object



341
342
343
344
345
# File 'lib/rhc/rest/application.rb', line 341

def <=>(other)
  c = name.downcase <=> other.name.downcase
  return c unless c == 0
  domain_id <=> other.domain_id
end

#add_alias(app_alias) ⇒ Object



255
256
257
258
# File 'lib/rhc/rest/application.rb', line 255

def add_alias(app_alias)
  debug "Running add_alias for #{name}"
  rest_method "ADD_ALIAS", :event => "add-alias", :alias => app_alias
end

#add_cartridge(cart, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rhc/rest/application.rb', line 43

def add_cartridge(cart, options={})
  debug "Adding cartridge #{name}"
  clear_attribute :cartridges
  cart =
    if cart.is_a? String
      {:name => cart}
    elsif cart.respond_to? :[]
      cart
    else
      c = cart.url ? {:url => cart.url} : {:name => cart.name}
      if cart.respond_to?(:environment_variables) && cart.environment_variables.present?
        c[:environment_variables] = cart.environment_variables
      end
      if cart.respond_to?(:gear_size) && cart.gear_size.present?
        c[:gear_size] = cart.gear_size
      end
      cart = c
    end

  if cart.respond_to?(:[]) and cart[:url] and !has_param?('ADD_CARTRIDGE', 'url')
    raise RHC::Rest::DownloadingCartridgesNotSupported, "The server does not support downloading cartridges."
  end

  rest_method(
    "ADD_CARTRIDGE",
    cart,
    options
  )
end

#aliasesObject



269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rhc/rest/application.rb', line 269

def aliases
  debug "Getting all aliases for application #{name}"
  @aliases ||= begin
    aliases = attributes['aliases']
    if aliases.nil? or not aliases.is_a?(Array)
      supports?('LIST_ALIASES') ? rest_method("LIST_ALIASES") : []
    else
      aliases.map do |a|
        Alias.new(a.is_a?(String) ? {'id' => a} : a, client)
      end
    end
  end
end

#cartridgesObject



73
74
75
76
77
78
79
80
81
# File 'lib/rhc/rest/application.rb', line 73

def cartridges
  @cartridges ||=
    unless (carts = attributes['cartridges']).nil?
      carts.map{|x| Cartridge.new(x, client) }
    else
      debug "Getting all cartridges for application #{name}"
      rest_method "LIST_CARTRIDGES"
    end
end

#configure(options = {}) ⇒ Object



246
247
248
249
250
251
252
253
# File 'lib/rhc/rest/application.rb', line 246

def configure(options={})
  debug "Running update for #{name} with options #{options.inspect}"
  if supports? "UPDATE"
    rest_method "UPDATE", options
  else
    raise RHC::DeploymentsNotSupportedException
  end
end

#deployment_activationsObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/rhc/rest/application.rb', line 210

def deployment_activations
  items = []

  # building an array of activations with their deployments
  deployments.each do |deployment|
    deployment.activations.each do |activation|
      items << {:activation => activation, :deployment => deployment}
    end
  end

  items.sort! {|a,b| a[:activation].created_at <=> b[:activation].created_at }

  first_activation = {}

  items.each do |item|
    deployment = item[:deployment]
    activation = item[:activation]

    # set the currently active (last activation by date)
    item[:active] = item == items.last

    # mark rollbacks (activations whose deployment had previous activations)
    if rollback_to = first_activation[deployment.id]
      item[:rollback] = true
      item[:rollback_to] = rollback_to
      # mark rolled back (all in between a rollback and its original deployment)
      items.each {|i| i[:rolled_back] = true if i[:activation].created_at > rollback_to && i[:activation].created_at < activation.created_at }
    else
      first_activation[deployment.id] = activation.created_at
    end

  end

  items
end

#deploymentsObject



204
205
206
207
208
# File 'lib/rhc/rest/application.rb', line 204

def deployments
  debug "Listing deployments for application #{name}"
  raise RHC::DeploymentsNotSupportedException if !supports? "LIST_DEPLOYMENTS"
  rest_method("LIST_DEPLOYMENTS").sort
end

#destroyObject Also known as: delete



138
139
140
141
# File 'lib/rhc/rest/application.rb', line 138

def destroy
  debug "Deleting application #{name}"
  rest_method "DELETE"
end

#domainObject



32
33
34
# File 'lib/rhc/rest/application.rb', line 32

def domain
  domain_id
end

#environment_variablesObject



154
155
156
157
158
159
160
161
# File 'lib/rhc/rest/application.rb', line 154

def environment_variables
  debug "Getting all environment variables for application #{name}"
  if supports? "LIST_ENVIRONMENT_VARIABLES"
    rest_method "LIST_ENVIRONMENT_VARIABLES"
  else
    raise RHC::EnvironmentVariablesNotSupportedException.new
  end
end

#find_alias(name, options = {}) ⇒ Object



283
284
285
286
287
288
289
290
291
292
# File 'lib/rhc/rest/application.rb', line 283

def find_alias(name, options={})
  debug "Finding alias #{name} in app #{@name}"

  if name.is_a?(Hash)
    options = name
    name = options[:name]
  end
  aliases.each { |a| return a if a.is_a?(String) || a.id == name.downcase }
  raise RHC::AliasNotFoundException.new("Alias #{name} can't be found in application #{@name}.")
end

#find_cartridge(sought, options = {}) ⇒ Object

Find Cartridge by name



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/rhc/rest/application.rb', line 295

def find_cartridge(sought, options={})
  debug "Finding cartridge #{sought} in app #{name}"

  type = options[:type]

  cartridges.each { |cart| return cart if cart.name == sought and (type.nil? or cart.type == type) }

  suggested_msg = ""
  valid_cartridges = cartridges.select {|c| type.nil? or c.type == type}
  unless valid_cartridges.empty?
    suggested_msg = "\n\nValid cartridges:"
    valid_cartridges.each { |cart| suggested_msg += "\n#{cart.name}" }
  end
  raise RHC::CartridgeNotFoundException.new("Cartridge #{sought} can't be found in application #{name}.#{suggested_msg}")
end

#find_cartridges(name, options = {}) ⇒ Object

Find Cartridges by name or regex



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/rhc/rest/application.rb', line 312

def find_cartridges(name, options={})
  if name.is_a?(Hash)
    options = name
    name = options[:name]
  end

  type = options[:type]
  regex = options[:regex]
  debug "Finding cartridge #{name || regex} in app #{@name}"

  filtered = Array.new
  cartridges.each do |cart|
    if regex
      filtered.push(cart) if cart.name.match(/(?i:#{regex})/) and (type.nil? or cart.type == type)
    else
      filtered.push(cart) if cart.name.downcase == name.downcase and (type.nil? or cart.type == type)
    end
  end
  filtered
end

#find_environment_variable(env_var_name) ⇒ Object



163
164
165
# File 'lib/rhc/rest/application.rb', line 163

def find_environment_variable(env_var_name)
  find_environment_variables(env_var_name).first
end

#find_environment_variables(env_var_names = nil) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/rhc/rest/application.rb', line 167

def find_environment_variables(env_var_names=nil)
  return environment_variables if env_var_names.nil?
  env_var_names = [env_var_names].flatten
  debug "Finding environment variable(s) #{env_var_names.inspect} in app #{@name}"
  env_vars = environment_variables.select { |item| env_var_names.include? item.name }
  raise RHC::EnvironmentVariableNotFoundException.new("Environment variable(s) #{env_var_names.join(', ')} can't be found in application #{name}.") if env_vars.empty?
  env_vars
end

#gear_groupsObject



87
88
89
90
# File 'lib/rhc/rest/application.rb', line 87

def gear_groups
  debug "Getting all gear groups for application #{name}"
  rest_method "GET_GEAR_GROUPS"
end

#gear_infoObject



83
84
85
# File 'lib/rhc/rest/application.rb', line 83

def gear_info
  { :gear_count => gear_count, :gear_profile => gear_profile } unless gear_count.nil?
end

#gear_ssh_url(gear_id) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
101
# File 'lib/rhc/rest/application.rb', line 96

def gear_ssh_url(gear_id)
  gear = gears.find{ |g| g['id'] == gear_id }

  raise ArgumentError, "Gear #{gear_id} not found" if gear.nil?
  gear['ssh_url'] or raise NoPerGearOperations
end

#gearsObject



92
93
94
# File 'lib/rhc/rest/application.rb', line 92

def gears
  gear_groups.map{ |group| group.gears }.flatten
end

#hostObject



333
334
335
# File 'lib/rhc/rest/application.rb', line 333

def host
  @host ||= URI.parse(app_url).host rescue nil
end

#idObject



20
21
22
# File 'lib/rhc/rest/application.rb', line 20

def id
  attributes['id'] || uuid
end

#reloadObject



144
145
146
147
# File 'lib/rhc/rest/application.rb', line 144

def reload
  debug "Reload application #{name}"
  rest_method "RELOAD", :event => "reload"
end

#remove_alias(app_alias) ⇒ Object



260
261
262
263
264
265
266
267
# File 'lib/rhc/rest/application.rb', line 260

def remove_alias(app_alias)
  debug "Running remove_alias for #{name}"
  if (client.api_version_negotiated >= 1.4)
    find_alias(app_alias).destroy
  else
    rest_method "REMOVE_ALIAS", :event => "remove-alias", :alias => app_alias
  end
end

#restartObject



133
134
135
136
# File 'lib/rhc/rest/application.rb', line 133

def restart
  debug "Restarting application #{name}"
  rest_method "RESTART", :event => "restart"
end

#scalable?Boolean

Query helper to say consistent with cartridge

Returns:

  • (Boolean)


16
17
18
# File 'lib/rhc/rest/application.rb', line 16

def scalable?
  scalable
end

#scalable_cartsObject



36
37
38
39
40
41
# File 'lib/rhc/rest/application.rb', line 36

def scalable_carts
  return [] unless scalable?
  carts = cartridges.select(&:scalable?)
  scales_with = carts.map(&:scales_with)
  carts.delete_if{|x| scales_with.include?(x.name)}
end

#scale_downObject



112
113
114
# File 'lib/rhc/rest/application.rb', line 112

def scale_down
  rest_method 'SCALE_DOWN', :event => "scale-down"
end

#scale_upObject



108
109
110
# File 'lib/rhc/rest/application.rb', line 108

def scale_up
  rest_method 'SCALE_UP', :event => "scale-up"
end

#set_environment_variables(env_vars = []) ⇒ Object

Parameters:



177
178
179
180
181
182
183
184
# File 'lib/rhc/rest/application.rb', line 177

def set_environment_variables(env_vars=[])
  debug "Adding environment variable(s) #{env_vars.inspect} for #{name}"
  if supports? "SET_UNSET_ENVIRONMENT_VARIABLES"
    rest_method "SET_UNSET_ENVIRONMENT_VARIABLES", :environment_variables => env_vars.map{|item| item.to_hash}
  else
    raise RHC::EnvironmentVariablesNotSupportedException.new
  end
end

#ssh_stringObject



337
338
339
# File 'lib/rhc/rest/application.rb', line 337

def ssh_string
  RHC::Helpers.ssh_string(ssh_url)
end

#startObject



116
117
118
119
# File 'lib/rhc/rest/application.rb', line 116

def start
  debug "Starting application #{name}"
  rest_method 'START', :event => "start"
end

#stop(force = false) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rhc/rest/application.rb', line 121

def stop(force=false)
  debug "Stopping application #{name} force-#{force}"

  if force
    payload = {:event=> "force-stop"}
  else
    payload = {:event=> "stop"}
  end

  rest_method "STOP", payload
end

#supports_add_cartridge_with_env_vars?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/rhc/rest/application.rb', line 196

def supports_add_cartridge_with_env_vars?
  has_param?('ADD_CARTRIDGE', 'environment_variables')
end

#supports_add_cartridge_with_gear_size?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/rhc/rest/application.rb', line 200

def supports_add_cartridge_with_gear_size?
  has_param?('ADD_CARTRIDGE', 'gear_size')
end

#threaddumpObject



149
150
151
152
# File 'lib/rhc/rest/application.rb', line 149

def threaddump
  debug "Running thread dump for #{name}"
  rest_method "THREAD_DUMP", :event => "thread-dump"
end

#tidyObject



103
104
105
106
# File 'lib/rhc/rest/application.rb', line 103

def tidy
  debug "Starting application #{name}"
  rest_method 'TIDY', :event => "tidy"
end

#unset_environment_variables(env_vars = []) ⇒ Object

Parameters:

  • Array (Array<String>)

    of env var names like [‘FOO’, ‘BAR’]



187
188
189
190
191
192
193
194
# File 'lib/rhc/rest/application.rb', line 187

def unset_environment_variables(env_vars=[])
  debug "Removing environment variable(s) #{env_vars.inspect} for #{name}"
  if supports? "SET_UNSET_ENVIRONMENT_VARIABLES"
    rest_method "SET_UNSET_ENVIRONMENT_VARIABLES", :environment_variables => env_vars.map{|item| {:name => item}}
  else
    raise RHC::EnvironmentVariablesNotSupportedException.new
  end
end

#uuidObject



24
25
26
27
28
29
30
# File 'lib/rhc/rest/application.rb', line 24

def uuid
  if (client.api_version_negotiated >= 1.6)
    attributes['id']
  else
    attributes['uuid']
  end
end