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 tasksHealthy tasksUnhealthy tasksRunning tasksStaged upgradeStrategy
deployments uris user version labels ]
DEFAULTS =
{
    :env => {},
    :labels => {},
    :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, marathon_instance = Marathon.singleton, 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 marathon_instance: MarathonInstance holding a connection to marathon

Raises:



23
24
25
26
27
28
29
# File 'lib/marathon/app.rb', line 23

def initialize(hash, marathon_instance = Marathon.singleton, 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
  @marathon_instance = marathon_instance
  refresh_attributes
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



16
17
18
# File 'lib/marathon/app.rb', line 16

def constraints
  @constraints
end

#containerObject (readonly)

Returns the value of attribute container.



16
17
18
# File 'lib/marathon/app.rb', line 16

def container
  @container
end

#healthChecksObject (readonly)

Returns the value of attribute healthChecks.



16
17
18
# File 'lib/marathon/app.rb', line 16

def healthChecks
  @healthChecks
end

#read_onlyObject (readonly)

Returns the value of attribute read_only.



16
17
18
# File 'lib/marathon/app.rb', line 16

def read_only
  @read_only
end

#tasksObject (readonly)

Returns the value of attribute tasks.



16
17
18
# File 'lib/marathon/app.rb', line 16

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.


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

def change(id, hash, force = false)
  Marathon.singleton.apps.change(id, hash, force)
end

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

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



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

def delete(id)
  Marathon.singleton.apps.delete(id)
end

.get(id) ⇒ Object

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



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

def get(id)
  Marathon.singleton.apps.get(id)
end

.list(cmd = nil, embed = nil, id = nil, label = 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.


187
188
189
# File 'lib/marathon/app.rb', line 187

def list(cmd = nil, embed = nil, id=nil, label=nil)
  Marathon.singleton.apps.list(cmd, embed, id, label)
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.


212
213
214
# File 'lib/marathon/app.rb', line 212

def restart(id, force = false)
  Marathon.singleton.apps.restart(id, force)
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


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

def start(hash)
  Marathon.singleton.apps.start(hash)
end

.version(id, version) ⇒ Object

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



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

def version(id, version)
  Marathon.singleton.apps.version(id, version)
end

.versions(id) ⇒ Object

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



228
229
230
# File 'lib/marathon/app.rb', line 228

def versions(id)
  Marathon.singleton.apps.versions(id)
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.


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/marathon/app.rb', line 82

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
  @marathon_instance.apps.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.



33
34
35
36
37
# File 'lib/marathon/app.rb', line 33

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.



52
53
54
55
56
57
58
# File 'lib/marathon/app.rb', line 52

def refresh
  check_read_only
  new_app = @marathon_instance.apps.get(id)
  @info = new_app.info
  refresh_attributes
  self
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.


71
72
73
74
# File 'lib/marathon/app.rb', line 71

def restart!(force = false)
  check_read_only
  @marathon_instance.apps.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.


99
100
101
# File 'lib/marathon/app.rb', line 99

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.


107
108
109
# File 'lib/marathon/app.rb', line 107

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.


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

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.


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

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

#to_pretty_sObject

Returns a string for listing the application.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/marathon/app.rb', line 123

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

#to_sObject



118
119
120
# File 'lib/marathon/app.rb', line 118

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.



43
44
45
46
47
48
49
# File 'lib/marathon/app.rb', line 43

def versions(version = nil)
  if version
    @marathon_instance.apps.version(id, version)
  else
    @marathon_instance.apps.versions(id)
  end
end