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::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, to_many, to_one, #update!

Constructor Details

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

Instance Method Details

#bind(*instances) ⇒ Object

Bind services to application.



129
130
131
132
133
134
135
136
137
138
# File 'lib/cfoundry/v2/app.rb', line 129

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)


151
152
153
154
155
# File 'lib/cfoundry/v2/app.rb', line 151

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

#commandObject

TODO v2



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

def command # TODO v2
  nil
end

#consoleObject

TODO v2



62
63
64
# File 'lib/cfoundry/v2/app.rb', line 62

def console # TODO v2
  nil
end

#debug_modeObject

TODO v2



58
59
60
# File 'lib/cfoundry/v2/app.rb', line 58

def debug_mode # TODO v2
  nil
end

#envObject



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

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

#env=(hash) ⇒ Object



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

def env=(hash)
  @env = hash
  @diff["environment_json"] = hash
  hash
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.



103
104
105
# File 'lib/cfoundry/v2/app.rb', line 103

def health
  state
end

#healthy?Boolean Also known as: running?

Check that all application instances are running.

Returns:

  • (Boolean)


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

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

#restart!Object

Restart the application.



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

def restart!
  stop!
  start!
end

#servicesObject



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

def services
  service_bindings.collect(&:service_instance)
end

#start!Object

Start the application.



87
88
89
# File 'lib/cfoundry/v2/app.rb', line 87

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)


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

def started?
  state == "STARTED"
end

#stop!Object

Stop the application.



82
83
84
# File 'lib/cfoundry/v2/app.rb', line 82

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

#stopped?Boolean

Is the application stopped?

Returns:

  • (Boolean)


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

def stopped?
  state == "STOPPED"
end

#unbind(*instances) ⇒ Object

Unbind services from application.



141
142
143
144
145
146
147
148
149
# File 'lib/cfoundry/v2/app.rb', line 141

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.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cfoundry/v2/app.rb', line 172

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



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

def uri
  uris[0]
end

#urisObject Also known as: urls

TODO v2



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

def uris # TODO v2
  []
end

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



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

def uris=(x)
  nil
end