Class: CFoundry::V1::App

Inherits:
Model
  • Object
show all
Includes:
UploadHelpers
Defined in:
lib/cfoundry/v1/app.rb

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

#changes, #guid

Attributes included from ModelMagic

#guid_name

Instance Method Summary collapse

Methods included from UploadHelpers

#check_unreachable_links, #determine_resources, #find_sockets, #make_fingerprints, #prepare_package, #upload

Methods inherited from Model

#base_object_name, #changed?, #create!, #delete!, #eql?, #exists?, #find_path, #guid_name, #hash, #initialize, #inspect, #invalidate!, #manifest, #object_name, plural_base_object_name, plural_object_name, #put, #read_manifest, #update!, #write_manifest

Methods included from ModelMagic

#attribute, #define_client_methods, #read_locations, #write_locations

Constructor Details

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

Instance Method Details

#bind(*instances) ⇒ Object

Bind services to application.



224
225
226
227
# File 'lib/cfoundry/v1/app.rb', line 224

def bind(*instances)
  self.services += instances
  update!
end

#binds?(instance) ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/cfoundry/v1/app.rb', line 235

def binds?(instance)
  services.include? instance
end

#crashesObject

Retrieve crashed instances



95
96
97
98
99
# File 'lib/cfoundry/v1/app.rb', line 95

def crashes
  @client.base.crashes(@guid).collect do |i|
    Instance.new(self, i[:instance].to_s, @client, i)
  end
end

#envObject



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/cfoundry/v1/app.rb', line 192

def env
  e = env_array || []

  env = {}
  e.each do |pair|
    name, val = pair.split("=", 2)
    env[name] = val
  end

  CFoundry::ChattyHash.new(method(:env=), env)
end

#env=(hash) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/cfoundry/v1/app.rb', line 204

def env=(hash)
  unless hash.is_a?(Array)
    hash = hash.collect { |k, v| "#{k}=#{v}" }
  end

  self.env_array = hash
end

#env_arrayObject



59
# File 'lib/cfoundry/v1/app.rb', line 59

alias_method :env_array, :env

#env_array=Object



60
# File 'lib/cfoundry/v1/app.rb', line 60

alias_method :env_array=, :env=

#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.



255
256
257
# File 'lib/cfoundry/v1/app.rb', line 255

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") for foo/bar.



245
246
247
# File 'lib/cfoundry/v1/app.rb', line 245

def files(*path)
  Instance.new(self, "0", @client).files(*path)
end

#frameworkObject



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

def framework
  @client.framework(framework_name)
end

#framework=(obj) ⇒ Object



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

def framework=(obj)
  set_named(:framework, obj)
end

#framework_nameObject



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

alias_method :framework_name, :framework

#framework_name=Object



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

alias_method :framework_name=, :framework=

#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.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cfoundry/v1/app.rb', line 136

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.

Returns:

  • (Boolean)


158
159
160
161
162
# File 'lib/cfoundry/v1/app.rb', line 158

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

#instancesObject

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



88
89
90
91
92
# File 'lib/cfoundry/v1/app.rb', line 88

def instances
  @client.base.instances(@guid).collect do |m|
    Instance.new(self, m[:index].to_s, @client, m)
  end
end

#restart!Object

Restart the application.



125
126
127
128
# File 'lib/cfoundry/v1/app.rb', line 125

def restart!
  stop!
  start!
end

#runtimeObject



70
71
72
# File 'lib/cfoundry/v1/app.rb', line 70

def runtime
  @client.runtime(runtime_name)
end

#runtime=(obj) ⇒ Object



74
75
76
# File 'lib/cfoundry/v1/app.rb', line 74

def runtime=(obj)
  set_named(:runtime, obj)
end

#runtime_nameObject



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

alias_method :runtime_name, :runtime

#runtime_name=Object



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

alias_method :runtime_name=, :runtime=

#service_namesObject



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

alias_method :service_names, :services

#service_names=Object



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

alias_method :service_names=, :services=

#servicesObject



78
79
80
# File 'lib/cfoundry/v1/app.rb', line 78

def services
  service_names.collect { |name| @client.service_instance(name) }
end

#services=(instances) ⇒ Object



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

def services=(objs)
  set_many_named(:service, objs)
end

#start!Object

Start the application.



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

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?

Returns:

  • (Boolean)


174
175
176
# File 'lib/cfoundry/v1/app.rb', line 174

def started?
  state == "STARTED"
end

#statsObject

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



102
103
104
105
106
107
108
109
110
# File 'lib/cfoundry/v1/app.rb', line 102

def stats
  stats = {}

  @client.base.stats(@guid).each do |idx, info|
    stats[idx.to_s] = info
  end

  stats
end

#stop!Object

Stop the application.



113
114
115
116
# File 'lib/cfoundry/v1/app.rb', line 113

def stop!
  self.state = "STOPPED"
  update!
end

#stopped?Boolean

Is the application stopped?

Returns:

  • (Boolean)


166
167
168
# File 'lib/cfoundry/v1/app.rb', line 166

def stopped?
  state == "STOPPED"
end

#total_instancesObject



38
# File 'lib/cfoundry/v1/app.rb', line 38

alias_method :total_instances, :instances

#unbind(*instances) ⇒ Object

Unbind services from application.



230
231
232
233
# File 'lib/cfoundry/v1/app.rb', line 230

def unbind(*instances)
  self.services -= instances
  update!
end

#uriObject Also known as: url

Shortcut for uris



180
181
182
# File 'lib/cfoundry/v1/app.rb', line 180

def uri
  uris[0]
end

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

Shortcut for uris = [x]



185
186
187
# File 'lib/cfoundry/v1/app.rb', line 185

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