Class: Docker::Swarm::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/docker/swarm/service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(swarm, hash) ⇒ Service

Returns a new instance of Service.



7
8
9
10
# File 'lib/docker/swarm/service.rb', line 7

def initialize(swarm, hash)
  @swarm = swarm
  @hash = hash
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



5
6
7
# File 'lib/docker/swarm/service.rb', line 5

def hash
  @hash
end

Class Method Details

.DEFAULT_OPTIONSObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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

Instance Method Details

#idObject



16
17
18
# File 'lib/docker/swarm/service.rb', line 16

def id()
  return @hash['ID']
end

#nameObject



12
13
14
# File 'lib/docker/swarm/service.rb', line 12

def name()
  @hash['Spec']['Name']
end

#network_idsObject



26
27
28
29
30
31
32
33
34
# 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

#reloadObject



20
21
22
23
24
# File 'lib/docker/swarm/service.rb', line 20

def reload()
  s = @swarm.find_service(id())
  @hash = s.hash
  return self
end

#remove(opts = {}) ⇒ Object



36
37
38
39
# File 'lib/docker/swarm/service.rb', line 36

def remove(opts = {})
  query = {}
  @swarm.connection.delete("/services/#{self.id}", query, :body => opts.to_json)
end

#replicasObject



67
68
69
# File 'lib/docker/swarm/service.rb', line 67

def replicas
  @hash['Spec']['Mode']['Replicated']['Replicas']
end

#restartObject



56
57
58
59
60
# File 'lib/docker/swarm/service.rb', line 56

def restart
  options = {}
  options['TaskTemplate'] = {'ForceUpdate' => 1}
  update(options)
end

#scale(count) ⇒ Object



62
63
64
65
# File 'lib/docker/swarm/service.rb', line 62

def scale(count)
  @hash['Spec']['Mode']['Replicated']['Replicas'] = count
  self.update(@hash['Spec'])
end

#tasksObject



48
49
50
51
52
53
54
# 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_runningObject



82
83
84
# File 'lib/docker/swarm/service.rb', line 82

def tasks_running
  return tasks_with_status(:running)
end

#tasks_startingObject



86
87
88
# File 'lib/docker/swarm/service.rb', line 86

def tasks_starting
  return tasks_with_status(:starting)
end

#tasks_with_status(status) ⇒ Object



71
72
73
74
75
76
77
78
79
# 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 = {}) ⇒ Object



41
42
43
44
45
46
# 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