Class: CFoundry::V2::App
- 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, #diff, #guid
Attributes included from ModelMagic
#scoped_organization, #scoped_space
Instance Method Summary collapse
-
#bind(*instances) ⇒ Object
Bind services to application.
- #binds?(instance) ⇒ Boolean
- #crashes ⇒ Object
- #delete!(opts = {}) ⇒ Object
- #env ⇒ Object
- #env=(x) ⇒ Object
- #file(*path) ⇒ Object
- #files(*path) ⇒ Object
-
#health ⇒ Object
Determine application health.
-
#healthy? ⇒ Boolean
(also: #running?)
Check that all application instances are running.
- #instances ⇒ Object
-
#restart!(async = false, &blk) ⇒ Object
Restart the application.
- #running_instances ⇒ Object
- #services ⇒ Object
-
#start!(async = false, &blk) ⇒ Object
Start the application.
-
#started? ⇒ Boolean
Is the application started?.
- #stats ⇒ Object
-
#stop! ⇒ Object
Stop the application.
-
#stopped? ⇒ Boolean
Is the application stopped?.
- #stream_file(*path, &blk) ⇒ Object
- #stream_update_log(log_url) ⇒ Object
-
#unbind(*instances) ⇒ Object
Unbind services from application.
- #update!(async = false) {|| ... } ⇒ Object
- #uri ⇒ Object (also: #url)
- #uri=(x) ⇒ Object (also: #url=)
- #uris ⇒ Object (also: #urls)
- #uris=(uris) ⇒ Object (also: #urls=)
Methods included from UploadHelpers
Methods inherited from Model
#attribute_for_error, #changed?, #create, #create!, #delete, #eql?, #exists?, #hash, inherited, #initialize, #inspect, #invalidate!, #manifest, #object_name, objects, #partial?, #persisted?, #plural_object_name, #query_target, #to_key, #to_param
Methods included from ModelMagic
#attributes, #defaults, #inherited, #object_name, params_from, #plural_object_name, #scoped_to_organization, #scoped_to_space, #to_many_relations, #to_one_relations
Methods included from ModelMagic::QueryableBy
Methods included from ModelMagic::ToMany
Methods included from ModelMagic::ToOne
Methods included from ModelMagic::Attribute
Methods included from ModelMagic::HasSummary
Methods included from ModelMagic::ClientExtensions
Constructor Details
This class inherits a constructor from CFoundry::V2::Model
Instance Method Details
#bind(*instances) ⇒ Object
Bind services to application.
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/cfoundry/v2/app.rb', line 228 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
250 251 252 253 254 |
# File 'lib/cfoundry/v2/app.rb', line 250 def binds?(instance) service_bindings.any? { |b| b.service_instance == instance } end |
#crashes ⇒ Object
60 61 62 63 64 |
# File 'lib/cfoundry/v2/app.rb', line 60 def crashes @client.base.crashes(@guid).collect do |m| Instance.new(self, m[:instance], @client, m) end end |
#delete!(opts = {}) ⇒ Object
50 51 52 |
# File 'lib/cfoundry/v2/app.rb', line 50 def delete!(opts = {}) super(opts.merge(:recursive => true)) end |
#env ⇒ Object
80 81 82 83 84 |
# File 'lib/cfoundry/v2/app.rb', line 80 def env CFoundry::ChattyHash.new( method(:env=), stringify(environment_json)) end |
#env=(x) ⇒ Object
86 87 88 |
# File 'lib/cfoundry/v2/app.rb', line 86 def env=(x) self.environment_json = stringify(x.to_hash) end |
#file(*path) ⇒ Object
260 261 262 |
# File 'lib/cfoundry/v2/app.rb', line 260 def file(*path) Instance.new(self, "0", @client).file(*path) end |
#files(*path) ⇒ Object
256 257 258 |
# File 'lib/cfoundry/v2/app.rb', line 256 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.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/cfoundry/v2/app.rb', line 174 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.
207 208 209 210 211 |
# File 'lib/cfoundry/v2/app.rb', line 207 def healthy? # invalidate cache so the check is fresh invalidate! health == "RUNNING" end |
#instances ⇒ Object
54 55 56 57 58 |
# File 'lib/cfoundry/v2/app.rb', line 54 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.
136 137 138 139 |
# File 'lib/cfoundry/v2/app.rb', line 136 def restart!(async = false, &blk) stop! start!(async, &blk) end |
#running_instances ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/cfoundry/v2/app.rb', line 194 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 |
#services ⇒ Object
76 77 78 |
# File 'lib/cfoundry/v2/app.rb', line 76 def services service_bindings.collect(&:service_instance) end |
#start!(async = false, &blk) ⇒ Object
Start the application.
130 131 132 133 |
# File 'lib/cfoundry/v2/app.rb', line 130 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?
223 224 225 |
# File 'lib/cfoundry/v2/app.rb', line 223 def started? state == "STARTED" end |
#stats ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/cfoundry/v2/app.rb', line 66 def stats stats = {} @client.base.stats(@guid).each do |idx, info| stats[idx.to_s] = info end stats end |
#stop! ⇒ Object
Stop the application.
124 125 126 127 |
# File 'lib/cfoundry/v2/app.rb', line 124 def stop! self.state = "STOPPED" update! end |
#stopped? ⇒ Boolean
Is the application stopped?
215 216 217 |
# File 'lib/cfoundry/v2/app.rb', line 215 def stopped? state == "STOPPED" end |
#stream_file(*path, &blk) ⇒ Object
264 265 266 |
# File 'lib/cfoundry/v2/app.rb', line 264 def stream_file(*path, &blk) Instance.new(self, "0", @client).stream_file(*path, &blk) end |
#stream_update_log(log_url) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cfoundry/v2/app.rb', line 153 def stream_update_log(log_url) offset = 0 while true begin @client.stream_url(log_url + "&tail&tail_offset=#{offset}") do |out| offset += out.size yield out end rescue Timeout::Error end end rescue CFoundry::APIError end |
#unbind(*instances) ⇒ Object
Unbind services from application.
240 241 242 243 244 245 246 247 248 |
# File 'lib/cfoundry/v2/app.rb', line 240 def unbind(*instances) service_bindings.each do |b| if instances.include? b.service_instance b.delete! end end self end |
#update!(async = false) {|| ... } ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/cfoundry/v2/app.rb', line 141 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 |
#uri ⇒ Object Also known as: url
107 108 109 110 111 112 113 114 115 |
# File 'lib/cfoundry/v2/app.rb', line 107 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=
118 119 120 |
# File 'lib/cfoundry/v2/app.rb', line 118 def uri=(x) self.uris = [x] end |
#uris ⇒ Object Also known as: urls
92 93 94 95 96 97 98 |
# File 'lib/cfoundry/v2/app.rb', line 92 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=
101 102 103 104 |
# File 'lib/cfoundry/v2/app.rb', line 101 def uris=(uris) raise CFoundry::Deprecated, "App#uris= is invalid against V2 APIs; use add/remove_route" end |