class CFoundry::V2::App

Class for representing a user's application on a given target (via Client).

Does not guarantee that the app exists; used for both app creation and retrieval, as the attributes are all lazily retrieved. Setting attributes does not perform any requests; use update! to commit your changes.

Public Instance Methods

bind(*instances) click to toggle source

Bind services to application.

# File lib/cfoundry/v2/app.rb, line 261
def bind(*instances)
  instances.each do |i|
    binding = @client.service_binding
    binding.app = self
    binding.service_instance = i
    binding.create!
  end

  self
end
binds?(instance) click to toggle source
# File lib/cfoundry/v2/app.rb, line 283
def binds?(instance)
  service_bindings.any? { |b|
    b.service_instance == instance
  }
end
crashes() click to toggle source
# File lib/cfoundry/v2/app.rb, line 58
def crashes
  @client.base.crashes(@guid).collect do |m|
    AppInstance.new(self.name, self.guid, m[:instance], @client, m)
  end
end
delete!(opts = {}) click to toggle source
Calls superclass method
# File lib/cfoundry/v2/app.rb, line 50
def delete!(opts = {})
  super(opts.merge(:recursive => true))
end
domain() click to toggle source
# File lib/cfoundry/v2/app.rb, line 131
def domain
  return nil if routes.empty?
  routes.first.domain.name
end
env() click to toggle source
# File lib/cfoundry/v2/app.rb, line 88
def env
  CFoundry::ChattyHash.new(
    method(:env=),
    stringify(environment_json))
end
env=(x) click to toggle source
# File lib/cfoundry/v2/app.rb, line 94
def env=(x)
  self.environment_json = stringify(x.to_hash)
end
file(*path) click to toggle source
# File lib/cfoundry/v2/app.rb, line 293
def file(*path)
  AppInstance.new(self.name, self.guid, "0", @client).file(*path)
end
files(*path) click to toggle source
# File lib/cfoundry/v2/app.rb, line 289
def files(*path)
  AppInstance.new(self.name, self.guid, "0", @client).files(*path)
end
health() click to toggle source

Determine application health.

If all instances are running, returns “RUNNING”. If only some are started, returns the percentage of them that are healthy.

Otherwise, returns application's status.

# File lib/cfoundry/v2/app.rb, line 192
def health
  if state == "STARTED"
    healthy_count = running_instances
    expected = total_instances

    if expected > 0
      ratio = healthy_count / expected.to_f
      if ratio == 1.0
        "RUNNING"
      else
        "#{(ratio * 100).to_i}%"
      end
    else
      "N/A"
    end
  else
    state
  end
rescue CFoundry::StagingError, CFoundry::NotStaged
  "STAGING FAILED"
end
healthy?() click to toggle source

Check that all application instances are running.

# File lib/cfoundry/v2/app.rb, line 240
def healthy?
  # invalidate cache so the check is fresh
  invalidate!
  health == "RUNNING"
end
Also aliased as: running?
host() click to toggle source
# File lib/cfoundry/v2/app.rb, line 126
def host
  return nil if routes.empty?
  routes.first.host
end
instances() click to toggle source
# File lib/cfoundry/v2/app.rb, line 54
def instances
  AppInstance.for_app(name, @guid, @client)
end
percent_running() click to toggle source
# File lib/cfoundry/v2/app.rb, line 214
def percent_running
  if state == "STARTED"
    healthy_count = running_instances
    expected = total_instances

    expected > 0 ? (healthy_count / expected.to_f) * 100 : 0
  else
    0
  end
rescue CFoundry::StagingError, CFoundry::NotStaged
  0
end
restart!(&blk) click to toggle source

Restart the application.

# File lib/cfoundry/v2/app.rb, line 154
def restart!(&blk)
  stop!
  start!(&blk)
end
running?()
Alias for: healthy?
running_instances() click to toggle source
# File lib/cfoundry/v2/app.rb, line 227
def running_instances
  return @cache[:running_instances] if @cache[:running_instances]

  running = 0

  instances.each do |i|
    running += 1 if i.state == "RUNNING"
  end

  running
end
services() click to toggle source
# File lib/cfoundry/v2/app.rb, line 84
def services
  service_bindings.collect(&:service_instance)
end
start!(&blk) click to toggle source

Start the application.

# File lib/cfoundry/v2/app.rb, line 148
def start!(&blk)
  self.state = "STARTED"
  update!(&blk)
end
started?() click to toggle source

Is the application started?

Note that this does not imply that all instances are running. See healthy?

# File lib/cfoundry/v2/app.rb, line 256
def started?
  state == "STARTED"
end
stats() click to toggle source
# File lib/cfoundry/v2/app.rb, line 64
def stats
  stats = {}

  @client.base.stats(@guid).each do |idx, info|
    stats[idx.to_s] = info
  end

  stats
end
stop!() click to toggle source

Stop the application.

# File lib/cfoundry/v2/app.rb, line 142
def stop!
  self.state = "STOPPED"
  update!
end
stopped?() click to toggle source

Is the application stopped?

# File lib/cfoundry/v2/app.rb, line 248
def stopped?
  state == "STOPPED"
end
stream_file(*path, &blk) click to toggle source
# File lib/cfoundry/v2/app.rb, line 297
def stream_file(*path, &blk)
  AppInstance.new(self.name, self.guid, "0", @client).stream_file(*path, &blk)
end
stream_update_log(log_url) { |out| ... } click to toggle source
# File lib/cfoundry/v2/app.rb, line 171
def stream_update_log(log_url)
  offset = 0

  while true
    begin
      @client.stream_url(log_url + "&tail&tail_offset=#{offset}") do |out|
        offset += out.size
        yield out
      end
    rescue Timeout::Error
    end
  end
rescue CFoundry::APIError
end
system_env() click to toggle source
# File lib/cfoundry/v2/app.rb, line 74
def system_env
  system_env = {}

  @client.base.env(@guid).each do |idx, info|
    system_env[idx.to_s] = info
  end

  system_env
end
unbind(*instances) click to toggle source

Unbind services from application.

# File lib/cfoundry/v2/app.rb, line 273
def unbind(*instances)
  service_bindings.each do |b|
    if instances.include? b.service_instance
      b.delete!
    end
  end

  self
end
update!() { |response["x-app-staging-log"]| ... } click to toggle source
# File lib/cfoundry/v2/app.rb, line 159
def update!
  response = @client.base.update_app(@guid, @diff)

  yield response[:headers]["x-app-staging-log"] if block_given?

  @manifest = @client.base.send(:parse_json, response[:body])

  @diff.clear

  true
end
uri() click to toggle source
# File lib/cfoundry/v2/app.rb, line 115
def uri
  if uris = @cache[:uris]
    return uris.first
  end

  if route = routes.first
    "#{route.host}.#{route.domain.name}"
  end
end
Also aliased as: url
uri=(x) click to toggle source
# File lib/cfoundry/v2/app.rb, line 136
def uri=(x)
  self.uris = [x]
end
Also aliased as: url=
uris() click to toggle source
# File lib/cfoundry/v2/app.rb, line 100
def uris
  return @cache[:uris] if @cache[:uris]

  routes.collect do |r|
    "#{r.host}.#{r.domain.name}"
  end
end
Also aliased as: urls
uris=(uris) click to toggle source
# File lib/cfoundry/v2/app.rb, line 109
def uris=(uris)
  raise CFoundry::Deprecated,
    "App#uris= is invalid against V2 APIs; use add/remove_route"
end
Also aliased as: urls=
url()
Alias for: uri
url=(x)
Alias for: uri=
urls()
Alias for: uris
urls=(uris)
Alias for: uris=

Private Instance Methods

stringify(hash) click to toggle source
# File lib/cfoundry/v2/app.rb, line 303
def stringify(hash)
  new = {}

  hash.each do |k, v|
    new[k.to_s] = v.to_s
  end

  new
end