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

#guid

Instance Method Summary collapse

Methods included from UploadHelpers

#check_unreachable_links, #find_sockets, #make_fingerprints, #prepare_package

Methods inherited from Model

attribute, #create!, defaults, #delete!, #eql?, #exists?, #hash, #initialize, #inspect, #invalidate!, #manifest, #object_name, scoped_to_organization, scoped_to_space, to_many, to_one, #update!, 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.



187
188
189
190
191
192
193
194
195
196
# File 'lib/cfoundry/v2/app.rb', line 187

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)


209
210
211
212
213
# File 'lib/cfoundry/v2/app.rb', line 209

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

#crashesObject



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

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cfoundry/v2/app.rb', line 95

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
      }

    raise "Invalid domain '#{domain_name}'" unless domain

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

    unless route
      route = @client.route
      route.host = host
      route.domain = domain
      route.organization = @client.current_organization
      route.create!
    end

    add_route(route)
  end
end

#debug_modeObject

TODO v2



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

def debug_mode # TODO v2
  nil
end

#envObject



69
70
71
72
73
# File 'lib/cfoundry/v2/app.rb', line 69

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

#env=(x) ⇒ Object



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

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

#file(*path) ⇒ Object



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

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

#files(*path) ⇒ Object



253
254
255
# File 'lib/cfoundry/v2/app.rb', line 253

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.



157
158
159
# File 'lib/cfoundry/v2/app.rb', line 157

def health
  state
end

#healthy?Boolean Also known as: running?

Check that all application instances are running.

Returns:

  • (Boolean)


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

def healthy?
  # invalidate cache so the check is fresh
  @manifest = nil

  case health
  when "RUNNING", "STARTED"
    true
  end
end

#instancesObject



43
44
45
46
47
# File 'lib/cfoundry/v2/app.rb', line 43

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.



146
147
148
149
# File 'lib/cfoundry/v2/app.rb', line 146

def restart!
  stop!
  start!
end

#servicesObject



65
66
67
# File 'lib/cfoundry/v2/app.rb', line 65

def services
  service_bindings.collect(&:service_instance)
end

#start!Object

Start the application.



141
142
143
# File 'lib/cfoundry/v2/app.rb', line 141

def start!
  update! :state => "STARTED"
end

#started?Boolean

Is the application started?

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

Returns:

  • (Boolean)


182
183
184
# File 'lib/cfoundry/v2/app.rb', line 182

def started?
  state == "STARTED"
end

#statsObject



55
56
57
58
59
60
61
62
63
# File 'lib/cfoundry/v2/app.rb', line 55

def stats
  stats = {}

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

  stats
end

#stop!Object

Stop the application.



136
137
138
# File 'lib/cfoundry/v2/app.rb', line 136

def stop!
  update! :state => "STOPPED"
end

#stopped?Boolean

Is the application stopped?

Returns:

  • (Boolean)


174
175
176
# File 'lib/cfoundry/v2/app.rb', line 174

def stopped?
  state == "STOPPED"
end

#total_instancesObject



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

alias :total_instances :instances

#unbind(*instances) ⇒ Object

Unbind services from application.



199
200
201
202
203
204
205
206
207
# File 'lib/cfoundry/v2/app.rb', line 199

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.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/cfoundry/v2/app.rb', line 230

def upload(path, check_resources = true)
  unless File.exist? path
    raise "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



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

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

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



130
131
132
# File 'lib/cfoundry/v2/app.rb', line 130

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

#urisObject Also known as: urls



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

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

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



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

def uris=(uris)
  raise "App#uris= is invalid against V2 APIs. Use add/remove_route."
end