Class: Docker::Swarm::Service
- Inherits:
-
Object
- Object
- Docker::Swarm::Service
- Defined in:
- lib/docker/swarm/service.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Class Method Summary collapse
Instance Method Summary collapse
- #id ⇒ Object
-
#initialize(swarm, hash) ⇒ Service
constructor
A new instance of Service.
- #name ⇒ Object
- #network_ids ⇒ Object
- #reload ⇒ Object
- #remove(opts = {}) ⇒ Object
- #replicas ⇒ Object
- #restart ⇒ Object
- #scale(count) ⇒ Object
- #tasks ⇒ Object
- #tasks_running ⇒ Object
- #tasks_starting ⇒ Object
- #tasks_with_status(status) ⇒ Object
- #update(options = {}) ⇒ Object
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
#hash ⇒ Object (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_OPTIONS ⇒ Object
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 = { "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 end |
Instance Method Details
#id ⇒ Object
16 17 18 |
# File 'lib/docker/swarm/service.rb', line 16 def id() return @hash['ID'] end |
#name ⇒ Object
12 13 14 |
# File 'lib/docker/swarm/service.rb', line 12 def name() @hash['Spec']['Name'] end |
#network_ids ⇒ Object
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 |
#reload ⇒ Object
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 |
#replicas ⇒ Object
67 68 69 |
# File 'lib/docker/swarm/service.rb', line 67 def replicas @hash['Spec']['Mode']['Replicated']['Replicas'] end |
#restart ⇒ Object
56 57 58 59 60 |
# File 'lib/docker/swarm/service.rb', line 56 def restart = {} ['TaskTemplate'] = {'ForceUpdate' => 1} update() 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 |
#tasks ⇒ Object
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_running ⇒ Object
82 83 84 |
# File 'lib/docker/swarm/service.rb', line 82 def tasks_running return tasks_with_status(:running) end |
#tasks_starting ⇒ Object
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( = {}) specs = @hash['Spec'].deep_merge() query = {} version = @hash['Version']['Index'] response = @swarm.connection.post("/services/#{self.id}/update?version=#{version}", query, :body => specs.to_json) end |