Class: CFoundry::App

Inherits:
Object
  • Object
show all
Defined in:
lib/cfoundry/app.rb

Overview

Class for representing a user’s application on a given target (via Client).

Goes 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 collapse

UPLOAD_EXCLUDE =

Default paths to exclude from upload payload.

Value: .git, _darcs, .svn

%w{.git _darcs .svn}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, client, manifest = nil) ⇒ App

Create an App object.

You’ll usually call Client#app instead



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

def initialize(name, client, manifest = nil)
  @name = name
  @client = client
  @manifest = manifest
end

Instance Attribute Details

#commandObject

:nodoc:



40
41
42
# File 'lib/cfoundry/app.rb', line 40

def command
  @command
end

#debug_modeObject

:nodoc:



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

def debug_mode
  @debug_mode
end

#envObject

Application environment variables.



26
27
28
# File 'lib/cfoundry/app.rb', line 26

def env
  @env
end

#frameworkObject

:nodoc:



32
33
34
# File 'lib/cfoundry/app.rb', line 32

def framework
  @framework
end

#memoryObject

:nodoc:



29
30
31
# File 'lib/cfoundry/app.rb', line 29

def memory
  @memory
end

#nameObject (readonly)

Application name.



17
18
19
# File 'lib/cfoundry/app.rb', line 17

def name
  @name
end

#runtimeObject

:nodoc:



35
36
37
# File 'lib/cfoundry/app.rb', line 35

def runtime
  @runtime
end

#servicesObject

Services bound to the application.



23
24
25
# File 'lib/cfoundry/app.rb', line 23

def services
  @services
end

#stateObject Also known as: status

Application state.



46
47
48
# File 'lib/cfoundry/app.rb', line 46

def state
  @state
end

#total_instancesObject

Application instance count.



20
21
22
# File 'lib/cfoundry/app.rb', line 20

def total_instances
  @total_instances
end

#urisObject Also known as: urls

URIs mapped to the application.



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

def uris
  @uris
end

Instance Method Details

#bind(*service_names) ⇒ Object

Bind services to application.



280
281
282
# File 'lib/cfoundry/app.rb', line 280

def bind(*service_names)
  update!("services" => services + service_names)
end

#create!Object

Create the application on the target.

Call this after setting the various attributes.



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

def create!
  @client.rest.create_app(@manifest.merge("name" => @name))
  @manifest = nil
end

#delete!Object

Delete the application from the target.

Keeps the metadata, but clears target-specific state from it.



70
71
72
73
74
75
76
77
78
# File 'lib/cfoundry/app.rb', line 70

def delete!
  @client.rest.delete_app(@name)

  if @manifest
    @manifest.delete "meta"
    @manifest.delete "version"
    @manifest.delete "state"
  end
end

#exists?Boolean

Check if the application exists on the target.

Returns:

  • (Boolean)


89
90
91
92
93
94
# File 'lib/cfoundry/app.rb', line 89

def exists?
  @client.rest.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") for foo/bar.



308
309
310
# File 'lib/cfoundry/app.rb', line 308

def file(*path)
  Instance.new(@name, 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") for foo/bar.



298
299
300
# File 'lib/cfoundry/app.rb', line 298

def files(*path)
  Instance.new(@name, 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.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cfoundry/app.rb', line 140

def health
  s = state
  if s == "STARTED"
    healthy_count = manifest["runningInstances"]
    expected = manifest["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.

Returns:

  • (Boolean)


161
162
163
164
165
# File 'lib/cfoundry/app.rb', line 161

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

#inspectObject

:nodoc:



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

def inspect # :nodoc:
  "#<App '#@name'>"
end

#instancesObject

Retrieve all of the instances of the app, as Instance objects.



97
98
99
100
101
# File 'lib/cfoundry/app.rb', line 97

def instances
  @client.rest.instances(@name).collect do |m|
    Instance.new(@name, m["index"], @client, m)
  end
end

#restart!Object

Restart the application.



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

def restart!
  stop!
  start!
end

#start!Object

Start the application.



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

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)


177
178
179
# File 'lib/cfoundry/app.rb', line 177

def started?
  state == "STARTED"
end

#statsObject

Retrieve application statistics, e.g. CPU load and memory usage.



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

def stats
  @client.rest.stats(@name)
end

#stop!Object

Stop the application.



119
120
121
# File 'lib/cfoundry/app.rb', line 119

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

#stopped?Boolean

Is the application stopped?

Returns:

  • (Boolean)


169
170
171
# File 'lib/cfoundry/app.rb', line 169

def stopped?
  state == "STOPPED"
end

#unbind(*service_names) ⇒ Object

Unbind services from application.



285
286
287
288
289
290
# File 'lib/cfoundry/app.rb', line 285

def unbind(*service_names)
  update!("services" =>
            services.reject { |s|
              service_names.include?(s)
            })
end

#update!(what = {}) ⇒ Object

Update application attributes. Does not restart the application.



109
110
111
112
113
114
115
116
# File 'lib/cfoundry/app.rb', line 109

def update!(what = {})
  # TODO: hacky; can we not just set in meta field?
  # we write to manifest["debug"] but read from manifest["meta"]["debug"]
  what[:debug] = debug_mode

  @client.rest.update_app(@name, manifest.merge(what))
  @manifest = nil
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.



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/cfoundry/app.rb', line 332

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.rest.upload_app(@name, packed && zipfile, resources || [])
ensure
  FileUtils.rm_f(zipfile) if zipfile
  FileUtils.rm_rf(tmpdir) if tmpdir
end

#uriObject Also known as: url

Shortcut for uris



200
201
202
# File 'lib/cfoundry/app.rb', line 200

def uri
  uris[0]
end

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

Shortcut for uris = [x]



205
206
207
# File 'lib/cfoundry/app.rb', line 205

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