Class: Marathon::App

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

Overview

This class represents a Marathon App. See mesosphere.github.io/marathon/docs/rest-api.html#apps for full list of API’s methods.

Constant Summary collapse

ACCESSORS =
%w[ id args cmd cpus disk env executor instances mem ports requirePorts
storeUris tasksRunning tasksStaged uris user version ]
DEFAULTS =
{
  :env => {},
  :ports => [],
  :uris => []
}

Instance Attribute Summary collapse

Attributes inherited from Base

#info

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#to_json

Methods included from Error

error_class, error_message, from_response

Constructor Details

#initialize(hash, read_only = false) ⇒ App

Create a new application object. hash: Hash including all attributes.

See https://mesosphere.github.io/marathon/docs/rest-api.html#post-/v2/apps for full details.

read_only: prevent actions on this application

Raises:



20
21
22
23
24
25
# File 'lib/marathon/app.rb', line 20

def initialize(hash, read_only = false)
  super(Marathon::Util.merge_keywordized_hash(DEFAULTS, hash), ACCESSORS)
  raise ArgumentError, 'App must have an id' unless id
  @read_only = read_only
  refresh_attributes
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



14
15
16
# File 'lib/marathon/app.rb', line 14

def constraints
  @constraints
end

#containerObject (readonly)

Returns the value of attribute container.



14
15
16
# File 'lib/marathon/app.rb', line 14

def container
  @container
end

#healthChecksObject (readonly)

Returns the value of attribute healthChecks.



14
15
16
# File 'lib/marathon/app.rb', line 14

def healthChecks
  @healthChecks
end

#read_onlyObject (readonly)

Returns the value of attribute read_only.



14
15
16
# File 'lib/marathon/app.rb', line 14

def read_only
  @read_only
end

#tasksObject (readonly)

Returns the value of attribute tasks.



14
15
16
# File 'lib/marathon/app.rb', line 14

def tasks
  @tasks
end

Class Method Details

.change(id, hash, force = false) ⇒ Object

Change parameters of a running application. The new application parameters apply only to subsequently created tasks. Currently running tasks are restarted, while maintaining the minimumHealthCapacity. id: Application’s id. hash: A subset of app’s attributes. force: If the app is affected by a running deployment, then the update operation will fail.

The current deployment can be overridden by setting the `force` query parameter.


214
215
216
217
218
219
# File 'lib/marathon/app.rb', line 214

def change(id, hash, force = false)
  query = {}
  query[:force] = true if force
  json = Marathon.connection.put("/v2/apps/#{id}", query, :body => hash)
  Marathon::DeploymentInfo.new(json)
end

.delete(id) ⇒ Object Also known as: remove

Delete the application with id. id: Application’s id.



183
184
185
# File 'lib/marathon/app.rb', line 183

def delete(id)
  Marathon.connection.delete("/v2/apps/#{id}")
end

.get(id) ⇒ Object

List the application with id. id: Application’s id.



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

def get(id)
  json = Marathon.connection.get("/v2/apps/#{id}")['app']
  new(json)
end

.list(cmd = nil, embed = nil) ⇒ Object

List all applications. :cmd: Filter apps to only those whose commands contain cmd. :embed: Embeds nested resources that match the supplied path.

Possible values:
  "apps.tasks". Apps' tasks are not embedded in the response by default.
  "apps.failures". Apps' last failures are not embedded in the response by default.


173
174
175
176
177
178
179
# File 'lib/marathon/app.rb', line 173

def list(cmd = nil, embed = nil)
  query = {}
  query[:cmd] = cmd if cmd
  Marathon::Util.add_choice(query, :embed, embed, %w[apps.tasks apps.failures])
  json = Marathon.connection.get('/v2/apps', query)['apps']
  json.map { |j| new(j) }
end

.restart(id, force = false) ⇒ Object

Restart the application with id. id: Application’s id. force: If the app is affected by a running deployment, then the update operation will fail.

The current deployment can be overridden by setting the `force` query parameter.


201
202
203
204
205
206
# File 'lib/marathon/app.rb', line 201

def restart(id, force = false)
  query = {}
  query[:force] = true if force
  json = Marathon.connection.post("/v2/apps/#{id}/restart", query)
  Marathon::DeploymentInfo.new(json)
end

.start(hash) ⇒ Object Also known as: create

Create and start an application. hash: Hash including all attributes

see https://mesosphere.github.io/marathon/docs/rest-api.html#post-/v2/apps for full details


191
192
193
194
# File 'lib/marathon/app.rb', line 191

def start(hash)
  json = Marathon.connection.post('/v2/apps', nil, :body => hash)
  new(json)
end

.version(id, version) ⇒ Object

List the configuration of the application with id at version. id: Application id version: Version name



231
232
233
234
# File 'lib/marathon/app.rb', line 231

def version(id, version)
  json = Marathon.connection.get("/v2/apps/#{id}/versions/#{version}")
  new(json, true)
end

.versions(id) ⇒ Object

List the versions of the application with id. id: Application id



223
224
225
226
# File 'lib/marathon/app.rb', line 223

def versions(id)
  json = Marathon.connection.get("/v2/apps/#{id}/versions")
  json['versions']
end

Instance Method Details

#change!(hash, force = false) ⇒ Object

Change parameters of a running application. The new application parameters apply only to subsequently created tasks. Currently running tasks are restarted, while maintaining the minimumHealthCapacity. hash: Hash of attributes to change. force: If the app is affected by a running deployment, then the update operation will fail.

The current deployment can be overridden by setting the `force` query parameter.


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/marathon/app.rb', line 77

def change!(hash, force = false)
  check_read_only
  Marathon::Util.keywordize_hash!(hash)
  if hash[:version] and hash.size > 1
    # remove :version if it's not the only key
    new_hash = Marathon::Util.remove_keys(hash, [:version])
  else
    new_hash = hash
  end
  self.class.change(id, new_hash, force)
end

#check_read_onlyObject

Prevent actions on read only instances. Raises an ArgumentError when triying to change read_only instances.



29
30
31
32
33
# File 'lib/marathon/app.rb', line 29

def check_read_only
  if read_only
    raise Marathon::Error::ArgumentError, "This app is 'read only' and does not support any actions"
  end
end

#refreshObject

Reload attributes from marathon API.



48
49
50
51
52
53
# File 'lib/marathon/app.rb', line 48

def refresh
  check_read_only
  new_app = self.class.get(id)
  @info = new_app.info
  refresh_attributes
end

#restart!(force = false) ⇒ Object

Initiates a rolling restart of all running tasks of the given app. This call respects the configured minimumHealthCapacity. force: If the app is affected by a running deployment, then the update operation will fail.

The current deployment can be overridden by setting the `force` query parameter.


66
67
68
69
# File 'lib/marathon/app.rb', line 66

def restart!(force = false)
  check_read_only
  self.class.restart(id, force)
end

#roll_back!(version, force = false) ⇒ Object

Create a new version with parameters of an old version. Currently running tasks are restarted, while maintaining the minimumHealthCapacity. version: Version name of the old version. force: If the app is affected by a running deployment, then the update operation will fail.

The current deployment can be overridden by setting the `force` query parameter.


94
95
96
# File 'lib/marathon/app.rb', line 94

def roll_back!(version, force = false)
  change!({:version => version}, force)
end

#scale!(instances, force = false) ⇒ Object

Change the number of desired instances. instances: Number of running instances. force: If the app is affected by a running deployment, then the update operation will fail.

The current deployment can be overridden by setting the `force` query parameter.


102
103
104
# File 'lib/marathon/app.rb', line 102

def scale!(instances, force = false)
  change!({:instances => instances}, force)
end

#start!(force = false) ⇒ Object

Create and start the application. force: If the app is affected by a running deployment, then the update operation will fail.

The current deployment can be overridden by setting the `force` query parameter.


58
59
60
# File 'lib/marathon/app.rb', line 58

def start!(force = false)
  change!(info, force)
end

#suspend!(force = false) ⇒ Object

Change the number of desired instances to 0. force: If the app is affected by a running deployment, then the update operation will fail.

The current deployment can be overridden by setting the `force` query parameter.


109
110
111
# File 'lib/marathon/app.rb', line 109

def suspend!(force = false)
  scale!(0, force)
end

#to_pretty_sObject

Returns a string for listing the application.



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/marathon/app.rb', line 118

def to_pretty_s
  %Q[
App ID:     #{id}
Instances:  #{tasks.size}/#{instances}
Command:    #{cmd}
CPUs:       #{cpus}
Memory:     #{mem} MB
#{pretty_uris()}
#{pretty_env()}
#{pretty_constraints()}
Version:    #{version}
  ].gsub(/\n\n+/, "\n").strip
end

#to_sObject



113
114
115
# File 'lib/marathon/app.rb', line 113

def to_s
  "Marathon::App { :id => #{id} }"
end

#versions(version = nil) ⇒ Object

List the versions of the application. version: Get a specific versions Returns Array of Strings if ++version = nil++, else returns Hash with version information.



39
40
41
42
43
44
45
# File 'lib/marathon/app.rb', line 39

def versions(version = nil)
  if version
    self.class.version(id, version)
  else
    self.class.versions(id)
  end
end