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
Instance Attribute Summary
Attributes inherited from Model
Instance Method Summary collapse
-
#bind(*instances) ⇒ Object
Bind services to application.
- #binds?(instance) ⇒ Boolean
- #crashes ⇒ Object
- #create_routes(*uris) ⇒ Object (also: #create_route)
-
#debug_mode ⇒ Object
TODO v2.
- #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! ⇒ Object
Restart the application.
- #running_instances ⇒ Object
- #services ⇒ Object
-
#start! ⇒ 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
- #total_instances ⇒ Object
-
#unbind(*instances) ⇒ Object
Unbind services from application.
-
#upload(path, check_resources = true) ⇒ Object
Upload application’s code to target.
- #uri ⇒ Object (also: #url)
- #uri=(x) ⇒ Object (also: #url=)
- #uris ⇒ Object (also: #urls)
- #uris=(uris) ⇒ Object (also: #urls=)
Methods included from UploadHelpers
#check_unreachable_links, #find_sockets, #make_fingerprints, #prepare_package
Methods inherited from Model
attribute, attributes, #create!, defaults, #delete!, #eql?, #exists?, has_summary, #hash, #initialize, #inspect, #invalidate!, #manifest, #object_name, #partial?, scoped_to_organization, scoped_to_space, to_many, to_many_relations, to_one, to_one_relations, #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.
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/cfoundry/v2/app.rb', line 259 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
281 282 283 284 285 |
# File 'lib/cfoundry/v2/app.rb', line 281 def binds?(instance) service_bindings.any? { |b| b.service_instance == instance } end |
#crashes ⇒ Object
86 87 88 89 90 |
# File 'lib/cfoundry/v2/app.rb', line 86 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
135 136 137 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 |
# File 'lib/cfoundry/v2/app.rb', line 135 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_mode ⇒ Object
TODO v2
116 117 118 |
# File 'lib/cfoundry/v2/app.rb', line 116 def debug_mode # TODO v2 nil end |
#env ⇒ Object
106 107 108 109 110 |
# File 'lib/cfoundry/v2/app.rb', line 106 def env @env ||= CFoundry::ChattyHash.new( method(:env=), environment_json) end |
#env=(x) ⇒ Object
112 113 114 |
# File 'lib/cfoundry/v2/app.rb', line 112 def env=(x) self.environment_json = x.to_hash end |
#file(*path) ⇒ Object
329 330 331 |
# File 'lib/cfoundry/v2/app.rb', line 329 def file(*path) Instance.new(self, "0", @client).file(*path) end |
#files(*path) ⇒ Object
325 326 327 |
# File 'lib/cfoundry/v2/app.rb', line 325 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.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/cfoundry/v2/app.rb', line 205 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.
238 239 240 241 242 |
# File 'lib/cfoundry/v2/app.rb', line 238 def healthy? # invalidate cache so the check is fresh invalidate! health == "RUNNING" end |
#instances ⇒ Object
80 81 82 83 84 |
# File 'lib/cfoundry/v2/app.rb', line 80 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.
194 195 196 197 |
# File 'lib/cfoundry/v2/app.rb', line 194 def restart! stop! start! end |
#running_instances ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/cfoundry/v2/app.rb', line 225 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
102 103 104 |
# File 'lib/cfoundry/v2/app.rb', line 102 def services service_bindings.collect(&:service_instance) end |
#start! ⇒ Object
Start the application.
188 189 190 191 |
# File 'lib/cfoundry/v2/app.rb', line 188 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?
254 255 256 |
# File 'lib/cfoundry/v2/app.rb', line 254 def started? state == "STARTED" end |
#stats ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/cfoundry/v2/app.rb', line 92 def stats stats = {} @client.base.stats(@guid).each do |idx, info| stats[idx.to_s] = info end stats end |
#stop! ⇒ Object
Stop the application.
182 183 184 185 |
# File 'lib/cfoundry/v2/app.rb', line 182 def stop! self.state = "STOPPED" update! end |
#stopped? ⇒ Boolean
Is the application stopped?
246 247 248 |
# File 'lib/cfoundry/v2/app.rb', line 246 def stopped? state == "STOPPED" end |
#stream_file(*path, &blk) ⇒ Object
333 334 335 |
# File 'lib/cfoundry/v2/app.rb', line 333 def stream_file(*path, &blk) Instance.new(self, "0", @client).stream_file(*path, &blk) end |
#total_instances ⇒ Object
75 |
# File 'lib/cfoundry/v2/app.rb', line 75 alias :total_instances :instances |
#unbind(*instances) ⇒ Object
Unbind services from application.
271 272 273 274 275 276 277 278 279 |
# File 'lib/cfoundry/v2/app.rb', line 271 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.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/cfoundry/v2/app.rb', line 302 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 |
#uri ⇒ Object Also known as: url
165 166 167 168 169 170 171 172 173 |
# File 'lib/cfoundry/v2/app.rb', line 165 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=
176 177 178 |
# File 'lib/cfoundry/v2/app.rb', line 176 def uri=(x) self.uris = [x] end |
#uris ⇒ Object Also known as: urls
120 121 122 123 124 125 126 |
# File 'lib/cfoundry/v2/app.rb', line 120 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=
129 130 131 132 |
# File 'lib/cfoundry/v2/app.rb', line 129 def uris=(uris) raise CFoundry::Deprecated, "App#uris= is invalid against V2 APIs; use add/remove_route" end |