Class: CFoundry::V2::App

Inherits:
Model
  • Object
show all
Includes:
UploadHelpers
Defined in:
lib/cfoundry/v2/app.rb

Overview

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.

Constant Summary

Constants included from UploadHelpers

UploadHelpers::RESOURCE_CHECK_LIMIT, UploadHelpers::UPLOAD_EXCLUDE

Instance Attribute Summary

Attributes inherited from Model

#cache, #changes, #created_at, #diff, #guid, #updated_at

Attributes included from ModelMagic

#scoped_organization, #scoped_space

Instance Method Summary collapse

Methods included from UploadHelpers

#upload

Methods inherited from Model

#attribute_for_error, #changed?, #create, #create!, #create_endpoint_name, #delete, #eql?, #exists?, #hash, inherited, #initialize, #inspect, #invalidate!, #manifest, #object_name, objects, #partial?, #persisted?, #plural_object_name, #query_target, #to_key, #to_param

Methods included from ModelMagic

#attributes, #defaults, #inherited, #object_name, params_from, #plural_object_name, query_from_hash, #scoped_to_organization, #scoped_to_space, #to_many_relations, #to_one_relations

Methods included from ModelMagic::QueryableBy

#queryable_by

Methods included from ModelMagic::ToMany

#to_many

Methods included from ModelMagic::ToOne

#to_one

Methods included from ModelMagic::Attribute

#attribute

Methods included from ModelMagic::HasSummary

#has_summary

Methods included from ModelMagic::ClientExtensions

#add_client_methods

Constructor Details

This class inherits a constructor from CFoundry::V2::Model

Instance Method Details

#bind(*instances) ⇒ Object

Bind services to application.



251
252
253
254
255
256
257
258
259
260
# File 'lib/cfoundry/v2/app.rb', line 251

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) ⇒ Boolean

Returns:

  • (Boolean)


273
274
275
276
277
# File 'lib/cfoundry/v2/app.rb', line 273

def binds?(instance)
  service_bindings.any? { |b|
    b.service_instance == instance
  }
end

#crashesObject



58
59
60
61
62
# 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 = {}) ⇒ Object



50
51
52
# File 'lib/cfoundry/v2/app.rb', line 50

def delete!(opts = {})
  super(opts.merge(:recursive => true))
end

#domainObject



121
122
123
124
# File 'lib/cfoundry/v2/app.rb', line 121

def domain
  return nil if routes.empty?
  routes.first.domain.name
end

#envObject



78
79
80
81
82
# File 'lib/cfoundry/v2/app.rb', line 78

def env
  CFoundry::ChattyHash.new(
    method(:env=),
    stringify(environment_json))
end

#env=(x) ⇒ Object



84
85
86
# File 'lib/cfoundry/v2/app.rb', line 84

def env=(x)
  self.environment_json = stringify(x.to_hash)
end

#file(*path) ⇒ Object



283
284
285
# File 'lib/cfoundry/v2/app.rb', line 283

def file(*path)
  AppInstance.new(self.name, self.guid, "0", @client).file(*path)
end

#files(*path) ⇒ Object



279
280
281
# File 'lib/cfoundry/v2/app.rb', line 279

def files(*path)
  AppInstance.new(self.name, self.guid, "0", @client).files(*path)
end

#healthObject

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.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/cfoundry/v2/app.rb', line 182

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?Boolean Also known as: running?

Check that all application instances are running.

Returns:

  • (Boolean)


230
231
232
233
234
# File 'lib/cfoundry/v2/app.rb', line 230

def healthy?
  # invalidate cache so the check is fresh
  invalidate!
  health == "RUNNING"
end

#hostObject



116
117
118
119
# File 'lib/cfoundry/v2/app.rb', line 116

def host
  return nil if routes.empty?
  routes.first.host
end

#instancesObject



54
55
56
# File 'lib/cfoundry/v2/app.rb', line 54

def instances
  AppInstance.for_app(name, @guid, @client)
end

#percent_runningObject



204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/cfoundry/v2/app.rb', line 204

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) ⇒ Object

Restart the application.



144
145
146
147
# File 'lib/cfoundry/v2/app.rb', line 144

def restart!(&blk)
  stop!
  start!(&blk)
end

#running_instancesObject



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/cfoundry/v2/app.rb', line 217

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

#servicesObject



74
75
76
# File 'lib/cfoundry/v2/app.rb', line 74

def services
  service_bindings.collect(&:service_instance)
end

#start!(&blk) ⇒ Object

Start the application.



138
139
140
141
# File 'lib/cfoundry/v2/app.rb', line 138

def start!(&blk)
  self.state = "STARTED"
  update!(&blk)
end

#started?Boolean

Is the application started?

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

Returns:

  • (Boolean)


246
247
248
# File 'lib/cfoundry/v2/app.rb', line 246

def started?
  state == "STARTED"
end

#statsObject



64
65
66
67
68
69
70
71
72
# 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!Object

Stop the application.



132
133
134
135
# File 'lib/cfoundry/v2/app.rb', line 132

def stop!
  self.state = "STOPPED"
  update!
end

#stopped?Boolean

Is the application stopped?

Returns:

  • (Boolean)


238
239
240
# File 'lib/cfoundry/v2/app.rb', line 238

def stopped?
  state == "STOPPED"
end

#stream_file(*path, &blk) ⇒ Object



287
288
289
# File 'lib/cfoundry/v2/app.rb', line 287

def stream_file(*path, &blk)
  AppInstance.new(self.name, self.guid, "0", @client).stream_file(*path, &blk)
end

#stream_update_log(log_url) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/cfoundry/v2/app.rb', line 161

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

#unbind(*instances) ⇒ Object

Unbind services from application.



263
264
265
266
267
268
269
270
271
# File 'lib/cfoundry/v2/app.rb', line 263

def unbind(*instances)
  service_bindings.each do |b|
    if instances.include? b.service_instance
      b.delete!
    end
  end

  self
end

#update! {|| ... } ⇒ Object

Yields:

  • ()


149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cfoundry/v2/app.rb', line 149

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

#uriObject Also known as: url



105
106
107
108
109
110
111
112
113
# File 'lib/cfoundry/v2/app.rb', line 105

def uri
  if uris = @cache[:uris]
    return uris.first
  end

  if route = routes.first
    "#{route.host}.#{route.domain.name}"
  end
end

#uri=(x) ⇒ Object Also known as: url=



126
127
128
# File 'lib/cfoundry/v2/app.rb', line 126

def uri=(x)
  self.uris = [x]
end

#urisObject Also known as: urls



90
91
92
93
94
95
96
# File 'lib/cfoundry/v2/app.rb', line 90

def uris
  return @cache[:uris] if @cache[:uris]

  routes.collect do |r|
    "#{r.host}.#{r.domain.name}"
  end
end

#uris=(uris) ⇒ Object Also known as: urls=



99
100
101
102
# File 'lib/cfoundry/v2/app.rb', line 99

def uris=(uris)
  raise CFoundry::Deprecated,
    "App#uris= is invalid against V2 APIs; use add/remove_route"
end