Class: Marathon::Group

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

Overview

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

Constant Summary collapse

ACCESSORS =
%w[ id dependencies version ]
DEFAULTS =
{
  :dependencies => []
}

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) ⇒ Group

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

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

Raises:



16
17
18
19
20
21
22
# File 'lib/marathon/group.rb', line 16

def initialize(hash)
  super(Marathon::Util.merge_keywordized_hash(DEFAULTS, hash), ACCESSORS)
  raise ArgumentError, 'Group must have an id' unless id
  refresh_attributes
  raise ArgumentError, 'Group can have either groups or apps, not both' \
    if apps.size > 0 and groups.size > 0 and id != '/'
end

Instance Attribute Details

#appsObject (readonly)

Returns the value of attribute apps.



11
12
13
# File 'lib/marathon/group.rb', line 11

def apps
  @apps
end

#groupsObject (readonly)

Returns the value of attribute groups.



11
12
13
# File 'lib/marathon/group.rb', line 11

def groups
  @groups
end

Class Method Details

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

Change parameters of a deployed application group. Changes to application parameters will result in a restart of this application. A new application added to the group is started. An existing application removed from the group gets stopped. If there are no changes to the application definition, no restart is triggered. During restart marathon keeps track, that the configured amount of minimal running instances are always available. A deployment can run forever. This is the case, when the new application has a problem and does not become healthy. In this case, human interaction is needed with 2 possible choices: Rollback to an existing older version (send an existing version in the body) Update with a newer version of the group which does not have the problems of the old one. If there is an upgrade process already in progress, a new update will be rejected unless the force flag is set. With the force flag given, a running upgrade is terminated and a new one is started. Since the deployment of the group can take a considerable amount of time, this endpoint returns immediatly with a version. The failure or success of the action is signalled via event. There is a group_change_success and group_change_failed event with the given version. id: Group’s id. hash: Hash of attributes to change. force: If the group is affected by a running deployment, then the update operation will fail.

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

dry_run: Get a preview of the deployment steps Marathon would run for a given group update.



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/marathon/group.rb', line 165

def change(id, hash, force = false, dry_run = false)
  query = {}
  query[:force] = true if force
  query[:dryRun] = true if dry_run
  json = Marathon.connection.put("/v2/groups/#{id}", query, :body => hash)
  if dry_run
    json['steps'].map { |e| Marathon::DeploymentStep.new(e) }
  else
    Marathon::DeploymentInfo.new(json)
  end
end

.delete(id, force = false) ⇒ Object Also known as: remove

Delete the application group with id. id: Group’s id. force: If the group is affected by a running deployment, then the update operation will fail.

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


124
125
126
127
128
# File 'lib/marathon/group.rb', line 124

def delete(id, force = false)
  query = {}
  query[:force] = true if force
  Marathon.connection.delete("/v2/groups/#{id}", query)
end

.get(id) ⇒ Object

List the group with the specified ID. id: Group’s id.



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

def get(id)
  json = Marathon.connection.get("/v2/groups/#{id}")
  new(json)
end

.listObject

List all groups.



115
116
117
118
# File 'lib/marathon/group.rb', line 115

def list
  json = Marathon.connection.get('/v2/groups')
  new(json)
end

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

Create and start a new application group. Application groups can contain other application groups. An application group can either hold other groups or applications, but can not be mixed in one. Since the deployment of the group can take a considerable amount of time, this endpoint returns immediatly with a version. The failure or success of the action is signalled via event. There is a group_change_success and group_change_failed event with the given version. hash: Hash including all attributes

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


138
139
140
141
# File 'lib/marathon/group.rb', line 138

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

Instance Method Details

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

Change parameters of a deployed application group. Changes to application parameters will result in a restart of this application. A new application added to the group is started. An existing application removed from the group gets stopped. If there are no changes to the application definition, no restart is triggered. During restart marathon keeps track, that the configured amount of minimal running instances are always available. A deployment can run forever. This is the case, when the new application has a problem and does not become healthy. In this case, human interaction is needed with 2 possible choices: Rollback to an existing older version (send an existing version in the body) Update with a newer version of the group which does not have the problems of the old one. If there is an upgrade process already in progress, a new update will be rejected unless the force flag is set. With the force flag given, a running upgrade is terminated and a new one is started. Since the deployment of the group can take a considerable amount of time, this endpoint returns immediatly with a version. The failure or success of the action is signalled via event. There is a group_change_success and group_change_failed event with the given version. hash: Hash of attributes to change. force: If the group is affected by a running deployment, then the update operation will fail.

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

dry_run: Get a preview of the deployment steps Marathon would run for a given group update.



59
60
61
62
63
64
65
66
67
68
# File 'lib/marathon/group.rb', line 59

def change!(hash, force = false, dry_run = false)
  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, dry_run)
end

#refreshObject

Reload attributes from marathon API.



25
26
27
28
29
# File 'lib/marathon/group.rb', line 25

def refresh
  new_app = self.class.get(id)
  @info = new_app.info
  refresh_attributes
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 group is affected by a running deployment, then the update operation will fail.

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


75
76
77
# File 'lib/marathon/group.rb', line 75

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

#start!Object

Create and start a the application group. Application groups can contain other application groups. An application group can either hold other groups or applications, but can not be mixed in one. Since the deployment of the group can take a considerable amount of time, this endpoint returns immediatly with a version. The failure or success of the action is signalled via event. There is a group_change_success and group_change_failed event with the given version.



36
37
38
# File 'lib/marathon/group.rb', line 36

def start!
  self.class.start(info)
end

#to_pretty_sObject

Returns a string for listing the group.



84
85
86
87
88
89
90
91
# File 'lib/marathon/group.rb', line 84

def to_pretty_s
  %Q[
Group ID:   #{id}
#{pretty_array(apps)}
#{pretty_array(groups)}
Version:    #{version}
  ].gsub(/\n\n+/, "\n").strip
end

#to_sObject



79
80
81
# File 'lib/marathon/group.rb', line 79

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