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.

Defined Under Namespace

Classes: Instance

Constant Summary

Constants included from UploadHelpers

UploadHelpers::RESOURCE_CHECK_LIMIT, UploadHelpers::UPLOAD_EXCLUDE

Instance Attribute Summary

Attributes inherited from Model

#cache, #changes, #guid

Attributes included from ModelMagic

#scoped_organization, #scoped_space

Instance Method Summary collapse

Methods included from UploadHelpers

#upload

Methods inherited from Model

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

Methods included from ModelMagic

#attribute, #attributes, #defaults, #define_base_client_methods, #define_client_methods, #has_summary, #inherited, #object_name, params_from, #plural_object_name, #queryable_by, #scoped_to_organization, #scoped_to_space, #to_many, #to_many_relations, #to_one, #to_one_relations

Constructor Details

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

Instance Method Details

#bind(*instances) ⇒ Object

Bind services to application.



275
276
277
278
279
280
281
282
283
284
# File 'lib/cfoundry/v2/app.rb', line 275

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)


297
298
299
300
301
# File 'lib/cfoundry/v2/app.rb', line 297

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

#crashesObject



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

def crashes
  @client.base.crashes(@guid).collect do |m|
    Instance.new(self, m[:instance], @client, m)
  end
end

#create_routes(*uris) ⇒ Object Also known as: create_route



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/cfoundry/v2/app.rb', line 139

def create_routes(*uris)
  uris.each do |uri|
    host, domain_name = uri.split(".", 2)

    domain =
      @client.current_space.domains.find { |d|
        d.name == domain_name
      }

    unless domain
      raise CFoundry::Error, "Invalid domain '#{domain_name}'"
    end

    route = @client.routes.find { |r|
      r.host == host && r.domain == domain
    }

    unless route
      route = @client.route
      route.host = host
      route.domain = domain
      route.space = space
      route.create!
    end

    add_route(route)
  end
end

#debug_modeObject

TODO v2



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

def debug_mode # TODO v2
  nil
end

#envObject



110
111
112
113
114
# File 'lib/cfoundry/v2/app.rb', line 110

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

#env=(x) ⇒ Object



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

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

#file(*path) ⇒ Object



307
308
309
# File 'lib/cfoundry/v2/app.rb', line 307

def file(*path)
  Instance.new(self, "0", @client).file(*path)
end

#files(*path) ⇒ Object



303
304
305
# File 'lib/cfoundry/v2/app.rb', line 303

def files(*path)
  Instance.new(self, "0", @client).files(*path)
end

#healthObject

Determine application health.

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

Otherwise, returns application’s status.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/cfoundry/v2/app.rb', line 221

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
end

#healthy?Boolean Also known as: running?

Check that all application instances are running.

Returns:

  • (Boolean)


254
255
256
257
258
# File 'lib/cfoundry/v2/app.rb', line 254

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

#instancesObject



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

def instances
  @client.base.instances(@guid).collect do |i, m|
    Instance.new(self, i.to_s, @client, m)
  end
end

#restart!(async = false, &blk) ⇒ Object

Restart the application.



198
199
200
201
# File 'lib/cfoundry/v2/app.rb', line 198

def restart!(async = false, &blk)
  stop!
  start!(async, &blk)
end

#running_instancesObject



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/cfoundry/v2/app.rb', line 241

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



106
107
108
# File 'lib/cfoundry/v2/app.rb', line 106

def services
  service_bindings.collect(&:service_instance)
end

#start!(async = false, &blk) ⇒ Object

Start the application.



192
193
194
195
# File 'lib/cfoundry/v2/app.rb', line 192

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

#started?Boolean

Is the application started?

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

Returns:

  • (Boolean)


270
271
272
# File 'lib/cfoundry/v2/app.rb', line 270

def started?
  state == "STARTED"
end

#statsObject



96
97
98
99
100
101
102
103
104
# File 'lib/cfoundry/v2/app.rb', line 96

def stats
  stats = {}

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

  stats
end

#stop!Object

Stop the application.



186
187
188
189
# File 'lib/cfoundry/v2/app.rb', line 186

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

#stopped?Boolean

Is the application stopped?

Returns:

  • (Boolean)


262
263
264
# File 'lib/cfoundry/v2/app.rb', line 262

def stopped?
  state == "STOPPED"
end

#stream_file(*path, &blk) ⇒ Object



311
312
313
# File 'lib/cfoundry/v2/app.rb', line 311

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

#unbind(*instances) ⇒ Object

Unbind services from application.



287
288
289
290
291
292
293
294
295
# File 'lib/cfoundry/v2/app.rb', line 287

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

  self
end

#update!(async = false) {|| ... } ⇒ Object

Yields:

  • ()


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

def update!(async = false)
  response = @client.base.update_app(@guid, @diff, async)

  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



169
170
171
172
173
174
175
176
177
# File 'lib/cfoundry/v2/app.rb', line 169

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=



180
181
182
# File 'lib/cfoundry/v2/app.rb', line 180

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

#urisObject Also known as: urls



124
125
126
127
128
129
130
# File 'lib/cfoundry/v2/app.rb', line 124

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=



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

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