Class: CFoundry::V1::App
- Inherits:
-
Object
- Object
- CFoundry::V1::App
- Includes:
- UploadHelpers
- Defined in:
- lib/cfoundry/v1/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
Instance Attribute Summary collapse
-
#command ⇒ Object
Application startup command.
-
#debug_mode ⇒ Object
Application debug mode.
-
#env ⇒ Object
Application environment variables.
-
#framework ⇒ Object
Application framework.
-
#memory ⇒ Object
Application memory limit.
-
#name ⇒ Object
Application name.
-
#runtime ⇒ Object
Application runtime.
-
#services ⇒ Object
Services bound to the application.
-
#state ⇒ Object
(also: #status)
Application state.
-
#total_instances ⇒ Object
Application instance count.
-
#uris ⇒ Object
(also: #urls)
URIs mapped to the application.
Instance Method Summary collapse
-
#bind(*instances) ⇒ Object
Bind services to application.
- #binds?(instance) ⇒ Boolean
-
#crashes ⇒ Object
Retrieve crashed instances.
-
#create! ⇒ Object
Create the application on the target.
-
#delete! ⇒ Object
Delete the application from the target.
-
#eql?(other) ⇒ Boolean
(also: #==)
Basic equality test by name.
-
#exists? ⇒ Boolean
Check if the application exists on the target.
-
#file(*path) ⇒ Object
Retrieve file contents for the first instance of the application.
-
#files(*path) ⇒ Object
Retrieve file listing under path for the first instance of the application.
-
#health ⇒ Object
Determine application health.
-
#healthy? ⇒ Boolean
(also: #running?)
Check that all application instances are running.
-
#initialize(name, client, manifest = nil) ⇒ App
constructor
Create an App object.
-
#inspect ⇒ Object
Show string representing the application.
-
#instances ⇒ Object
Retrieve all of the instances of the app, as Instance objects.
-
#restart! ⇒ Object
Restart the application.
-
#start! ⇒ Object
Start the application.
-
#started? ⇒ Boolean
Is the application started?.
-
#stats ⇒ Object
Retrieve application statistics, e.g.
-
#stop! ⇒ Object
Stop the application.
-
#stopped? ⇒ Boolean
Is the application stopped?.
-
#unbind(*instances) ⇒ Object
Unbind services from application.
-
#update!(what = {}) ⇒ Object
Update application attributes.
-
#upload(path, check_resources = true) ⇒ Object
Upload application’s code to target.
-
#uri ⇒ Object
(also: #url)
Shortcut for uris.
-
#uri=(x) ⇒ Object
(also: #url=)
Shortcut for uris = [x].
Methods included from UploadHelpers
#check_unreachable_links, #find_sockets, #make_fingerprints, #prepare_package
Constructor Details
#initialize(name, client, manifest = nil) ⇒ App
Create an App object.
You’ll usually call Client#app instead
61 62 63 64 65 66 |
# File 'lib/cfoundry/v1/app.rb', line 61 def initialize(name, client, manifest = nil) @name = name @client = client @manifest = manifest @diff = {} end |
Instance Attribute Details
#command ⇒ Object
Application startup command.
Used for standalone apps.
44 45 46 |
# File 'lib/cfoundry/v1/app.rb', line 44 def command @command end |
#debug_mode ⇒ Object
Application debug mode.
47 48 49 |
# File 'lib/cfoundry/v1/app.rb', line 47 def debug_mode @debug_mode end |
#env ⇒ Object
Application environment variables.
30 31 32 |
# File 'lib/cfoundry/v1/app.rb', line 30 def env @env end |
#framework ⇒ Object
Application framework.
36 37 38 |
# File 'lib/cfoundry/v1/app.rb', line 36 def framework @framework end |
#memory ⇒ Object
Application memory limit.
33 34 35 |
# File 'lib/cfoundry/v1/app.rb', line 33 def memory @memory end |
#name ⇒ Object
Application name.
21 22 23 |
# File 'lib/cfoundry/v1/app.rb', line 21 def name @name end |
#runtime ⇒ Object
Application runtime.
39 40 41 |
# File 'lib/cfoundry/v1/app.rb', line 39 def runtime @runtime end |
#services ⇒ Object
Services bound to the application.
27 28 29 |
# File 'lib/cfoundry/v1/app.rb', line 27 def services @services end |
#state ⇒ Object Also known as: status
Application state.
50 51 52 |
# File 'lib/cfoundry/v1/app.rb', line 50 def state @state end |
#total_instances ⇒ Object
Application instance count.
24 25 26 |
# File 'lib/cfoundry/v1/app.rb', line 24 def total_instances @total_instances end |
#uris ⇒ Object Also known as: urls
URIs mapped to the application.
54 55 56 |
# File 'lib/cfoundry/v1/app.rb', line 54 def uris @uris end |
Instance Method Details
#bind(*instances) ⇒ Object
Bind services to application.
310 311 312 |
# File 'lib/cfoundry/v1/app.rb', line 310 def bind(*instances) update!(:services => services + instances) end |
#binds?(instance) ⇒ Boolean
322 323 324 |
# File 'lib/cfoundry/v1/app.rb', line 322 def binds?(instance) services.include? instance end |
#crashes ⇒ Object
Retrieve crashed instances
115 116 117 118 119 |
# File 'lib/cfoundry/v1/app.rb', line 115 def crashes @client.base.crashes(@name).collect do |i| Instance.new(self, i[:instance].to_s, @client, i) end end |
#create! ⇒ Object
Create the application on the target.
Call this after setting the various attributes.
94 95 96 97 |
# File 'lib/cfoundry/v1/app.rb', line 94 def create! @client.base.create_app(create_manifest) @diff = {} end |
#delete! ⇒ Object
Delete the application from the target.
Keeps the metadata, but clears target-specific state from it.
82 83 84 85 86 87 88 89 |
# File 'lib/cfoundry/v1/app.rb', line 82 def delete! @client.base.delete_app(@name) if @manifest @diff = read_manifest @manifest = nil end end |
#eql?(other) ⇒ Boolean Also known as: ==
Basic equality test by name.
74 75 76 |
# File 'lib/cfoundry/v1/app.rb', line 74 def eql?(other) other.is_a?(self.class) && other.name == @name end |
#exists? ⇒ Boolean
Check if the application exists on the target.
100 101 102 103 104 105 |
# File 'lib/cfoundry/v1/app.rb', line 100 def exists? @client.base.app(@name) true rescue CFoundry::NotFound false end |
#file(*path) ⇒ Object
Retrieve file contents for the first instance of the application.
- path
-
A sequence of strings representing path segments.
For example,
files("foo", "bar")
forfoo/bar
.
342 343 344 |
# File 'lib/cfoundry/v1/app.rb', line 342 def file(*path) Instance.new(self, "0", @client).file(*path) end |
#files(*path) ⇒ Object
Retrieve file listing under path for the first instance of the application.
- path
-
A sequence of strings representing path segments.
For example,
files("foo", "bar")
forfoo/bar
.
332 333 334 |
# File 'lib/cfoundry/v1/app.rb', line 332 def files(*path) Instance.new(self, "0", @client).files(*path) end |
#health ⇒ Object
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.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/cfoundry/v1/app.rb', line 168 def health s = state if s == "STARTED" healthy_count = running_instances expected = total_instances if healthy_count && expected > 0 ratio = healthy_count / expected.to_f if ratio == 1.0 "RUNNING" else "#{(ratio * 100).to_i}%" end else "N/A" end else s end end |
#healthy? ⇒ Boolean Also known as: running?
Check that all application instances are running.
190 191 192 193 194 |
# File 'lib/cfoundry/v1/app.rb', line 190 def healthy? # invalidate cache so the check is fresh @manifest = nil health == "RUNNING" end |
#inspect ⇒ Object
Show string representing the application.
69 70 71 |
# File 'lib/cfoundry/v1/app.rb', line 69 def inspect "#<App '#@name'>" end |
#instances ⇒ Object
Retrieve all of the instances of the app, as Instance objects.
108 109 110 111 112 |
# File 'lib/cfoundry/v1/app.rb', line 108 def instances @client.base.instances(@name).collect do |m| Instance.new(self, m[:index].to_s, @client, m) end end |
#restart! ⇒ Object
Restart the application.
157 158 159 160 |
# File 'lib/cfoundry/v1/app.rb', line 157 def restart! stop! start! end |
#start! ⇒ Object
Start the application.
152 153 154 |
# File 'lib/cfoundry/v1/app.rb', line 152 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?
206 207 208 |
# File 'lib/cfoundry/v1/app.rb', line 206 def started? state == "STARTED" end |
#stats ⇒ Object
Retrieve application statistics, e.g. CPU load and memory usage.
122 123 124 125 126 127 128 129 130 |
# File 'lib/cfoundry/v1/app.rb', line 122 def stats stats = {} @client.base.stats(@name).each do |idx, info| stats[idx.to_s] = info end stats end |
#stop! ⇒ Object
Stop the application.
147 148 149 |
# File 'lib/cfoundry/v1/app.rb', line 147 def stop! update! :state => "STOPPED" end |
#stopped? ⇒ Boolean
Is the application stopped?
198 199 200 |
# File 'lib/cfoundry/v1/app.rb', line 198 def stopped? state == "STOPPED" end |
#unbind(*instances) ⇒ Object
Unbind services from application.
315 316 317 318 319 320 |
# File 'lib/cfoundry/v1/app.rb', line 315 def unbind(*instances) update!(:services => services.reject { |s| instances.any? { |i| i.name == s.name } }) end |
#update!(what = {}) ⇒ Object
Update application attributes. Does not restart the application.
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/cfoundry/v1/app.rb', line 133 def update!(what = {}) what.each do |k, v| send(:"#{k}=", v) end @client.base.update_app(@name, update_manifest) @manifest = nil @diff = {} 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.
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/cfoundry/v1/app.rb', line 361 def upload(path, check_resources = true) unless File.exist? path raise "invalid application path '#{path}'" end zipfile = "#{Dir.tmpdir}/#{@name}.zip" tmpdir = "#{Dir.tmpdir}/.vmc_#{@name}_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(@name, packed && zipfile, resources || []) ensure FileUtils.rm_f(zipfile) if zipfile FileUtils.rm_rf(tmpdir) if tmpdir end |
#uri ⇒ Object Also known as: url
Shortcut for uris
246 247 248 |
# File 'lib/cfoundry/v1/app.rb', line 246 def uri uris[0] end |
#uri=(x) ⇒ Object Also known as: url=
Shortcut for uris = [x]
251 252 253 |
# File 'lib/cfoundry/v1/app.rb', line 251 def uri=(x) self.uris = [x] end |