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::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

#check_unreachable_links, #find_sockets, #make_fingerprints, #prepare_package

Methods inherited from Model

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

Methods included from ModelMagic

#attribute, #attributes, #defaults, #has_summary, #inherited, #object_name, params_from, #queryable_by, #scoped_to_organization, #scoped_to_space, #to_many, #to_many_relations, #to_one, #to_one_relations, validate_type, value_matches?

Constructor Details

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

Instance Method Details

#bind(*instances) ⇒ Object

Bind services to application.



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

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)


284
285
286
287
288
# File 'lib/cfoundry/v2/app.rb', line 284

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

#crashesObject



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

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



138
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
# File 'lib/cfoundry/v2/app.rb', line 138

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



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

def debug_mode # TODO v2
  nil
end

#envObject



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

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

#env=(x) ⇒ Object



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

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

#file(*path) ⇒ Object



332
333
334
# File 'lib/cfoundry/v2/app.rb', line 332

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

#files(*path) ⇒ Object



328
329
330
# File 'lib/cfoundry/v2/app.rb', line 328

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.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/cfoundry/v2/app.rb', line 208

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)


241
242
243
244
245
# File 'lib/cfoundry/v2/app.rb', line 241

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

#instancesObject



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

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

#restart!Object

Restart the application.



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

def restart!
  stop!
  start!
end

#running_instancesObject



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/cfoundry/v2/app.rb', line 228

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



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

def services
  service_bindings.collect(&:service_instance)
end

#start!Object

Start the application.



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

def start!
  self.state = "STARTED"
  update!
end

#started?Boolean

Is the application started?

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

Returns:

  • (Boolean)


257
258
259
# File 'lib/cfoundry/v2/app.rb', line 257

def started?
  state == "STARTED"
end

#statsObject



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

def stats
  stats = {}

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

  stats
end

#stop!Object

Stop the application.



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

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

#stopped?Boolean

Is the application stopped?

Returns:

  • (Boolean)


249
250
251
# File 'lib/cfoundry/v2/app.rb', line 249

def stopped?
  state == "STOPPED"
end

#stream_file(*path, &blk) ⇒ Object



336
337
338
# File 'lib/cfoundry/v2/app.rb', line 336

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

#total_instancesObject



78
# File 'lib/cfoundry/v2/app.rb', line 78

alias :total_instances :instances

#unbind(*instances) ⇒ Object

Unbind services from application.



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

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

  self
end

#upload(path, check_resources = true) ⇒ Object

Upload application’s code to target. Do this after #create! and before #start!

path

A path pointing to either a directory, or a .jar, .war, or .zip file.

If a .vmcignore file is detected under the given path, it will be used to exclude paths from the payload, similar to a .gitignore.

check_resources

If set to ‘false`, the entire payload will be uploaded without checking the resource cache.

Only do this if you know what you’re doing.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/cfoundry/v2/app.rb', line 305

def upload(path, check_resources = true)
  unless File.exist? path
    raise CFoundry::Error, "Invalid application path '#{path}'"
  end

  zipfile = "#{Dir.tmpdir}/#{@guid}.zip"
  tmpdir = "#{Dir.tmpdir}/.vmc_#{@guid}_files"

  FileUtils.rm_f(zipfile)
  FileUtils.rm_rf(tmpdir)

  prepare_package(path, tmpdir)

  resources = determine_resources(tmpdir) if check_resources

  packed = CFoundry::Zip.pack(tmpdir, zipfile)

  @client.base.upload_app(@guid, packed && zipfile, resources || [])
ensure
  FileUtils.rm_f(zipfile) if zipfile
  FileUtils.rm_rf(tmpdir) if tmpdir
end

#uriObject Also known as: url



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

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=



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

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

#urisObject Also known as: urls



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

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=



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

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