class Docker::Swarm::Service

Attributes

hash[R]

Public Class Methods

DEFAULT_OPTIONS() click to toggle source
# File lib/docker/swarm/service.rb, line 90
  def self.DEFAULT_OPTIONS
    default_service_create_options = {
        "Name" => "<<Required>>",
        "TaskTemplate" => {
          "ContainerSpec" => {
            "Image" => "<<Required>>",
            "Mounts" => [],
            "User" => "root"
          },
          "Env" => [],
          "LogDriver" => {
            "Name" => "json-file",
            "Options" => {
              "max-file" => "3",
              "max-size" => "10M"
            }
          },
          "Placement" => {},
          "Resources" => {
            "Limits" => {
              "MemoryBytes" => 104857600
            },
            "Reservations" => {
#              "NanoCPUs" => ?
#              MemoryBytes =>
           }
          },
          "RestartPolicy" => {
            "Condition" => "on-failure",
            "Delay" => 1,
            "MaxAttempts" => 3
          }
        }, # End of TaskTemplate
        "Mode" => {
          "Replicated" => {
            "Replicas" => 1
          }
        },
        "UpdateConfig" => {
          "Delay" => 2,
          "Parallelism" => 2,
          "FailureAction" => "pause"
        },
        "EndpointSpec" => {
          "Ports" => [
            {
    #          "Protocol" => "http",
    #          "PublishedPort" => 2881,
    #          "TargetPort" => 2881
            }
          ]
        },
        "Labels" => {
          "foo" => "bar"
        }
      }
    return default_service_create_options
  end
new(swarm, hash) click to toggle source
# File lib/docker/swarm/service.rb, line 7
def initialize(swarm, hash)
  @swarm = swarm
  @hash = hash
end

Public Instance Methods

id() click to toggle source
# File lib/docker/swarm/service.rb, line 16
def id()
  return @hash['ID']
end
name() click to toggle source
# File lib/docker/swarm/service.rb, line 12
def name()
  @hash['Spec']['Name']
end
network_ids() click to toggle source
# File lib/docker/swarm/service.rb, line 26
def network_ids
  network_ids = []
  if (@hash['Endpoint']['VirtualIPs'])
    @hash['Endpoint']['VirtualIPs'].each do |network_info|
      network_ids << network_info['NetworkID']
    end
  end
  return network_ids
end
reload() click to toggle source
# File lib/docker/swarm/service.rb, line 20
def reload()
  s = @swarm.find_service(id())
  @hash = s.hash
  return self
end
remove(opts = {}) click to toggle source
# File lib/docker/swarm/service.rb, line 36
def remove(opts = {})
  query = {}
  @swarm.connection.delete("/services/#{self.id}", query, :body => opts.to_json)
end
replicas() click to toggle source
# File lib/docker/swarm/service.rb, line 67
def replicas
  @hash['Spec']['Mode']['Replicated']['Replicas']
end
restart() click to toggle source
# File lib/docker/swarm/service.rb, line 56
def restart
  options = {}
  options['TaskTemplate'] = {'ForceUpdate' => 1}
  update(options)
end
scale(count) click to toggle source
# File lib/docker/swarm/service.rb, line 62
def scale(count)
  @hash['Spec']['Mode']['Replicated']['Replicas'] = count
  self.update(@hash['Spec'])
end
tasks() click to toggle source
# File lib/docker/swarm/service.rb, line 48
def tasks
  service_tasks = []
  @swarm.tasks.each do |task|
    service_tasks << task if (task.service_id == self.id)
  end
  return service_tasks
end
tasks_running() click to toggle source
# File lib/docker/swarm/service.rb, line 82
def tasks_running
  return tasks_with_status(:running)
end
tasks_starting() click to toggle source
# File lib/docker/swarm/service.rb, line 86
def tasks_starting
  return tasks_with_status(:starting)
end
tasks_with_status(status) click to toggle source
# File lib/docker/swarm/service.rb, line 71
def tasks_with_status(status)
  running = []
  tasks.each do |task|
    if (task.status == status)
      running << task
    end
  end
  return running
end
update(options = {}) click to toggle source
# File lib/docker/swarm/service.rb, line 41
def update(options = {})
  specs = @hash['Spec'].deep_merge(options)
  query = {}
  version = @hash['Version']['Index']
  response = @swarm.connection.post("/services/#{self.id}/update?version=#{version}", query, :body => specs.to_json)
end