Class: Centurion::Service
- Inherits:
-
Object
- Object
- Centurion::Service
- Extended by:
- Capistrano::DSL
- Defined in:
- lib/centurion/service.rb
Defined Under Namespace
Classes: PortBinding, RestartPolicy, Volume
Instance Attribute Summary collapse
-
#cap_adds ⇒ Object
Returns the value of attribute cap_adds.
-
#cap_drops ⇒ Object
Returns the value of attribute cap_drops.
-
#command ⇒ Object
Returns the value of attribute command.
-
#cpu_shares ⇒ Object
Returns the value of attribute cpu_shares.
-
#dns ⇒ Object
Returns the value of attribute dns.
-
#env_vars ⇒ Object
readonly
Returns the value of attribute env_vars.
-
#extra_hosts ⇒ Object
Returns the value of attribute extra_hosts.
-
#image ⇒ Object
Returns the value of attribute image.
-
#memory ⇒ Object
Returns the value of attribute memory.
-
#name ⇒ Object
Returns the value of attribute name.
-
#network_mode ⇒ Object
Returns the value of attribute network_mode.
-
#port_bindings ⇒ Object
Returns the value of attribute port_bindings.
-
#volumes ⇒ Object
Returns the value of attribute volumes.
Class Method Summary collapse
Instance Method Summary collapse
- #add_env_vars(new_vars) ⇒ Object
- #add_port_bindings(host_port, container_port, type = 'tcp', host_ip = nil) ⇒ Object
- #add_volume(host_volume, container_volume) ⇒ Object
- #build_config(server_hostname, &block) ⇒ Object
- #build_console_config(server_name, &block) ⇒ Object
- #build_host_config(restart_policy = nil) ⇒ Object
-
#initialize(name) ⇒ Service
constructor
A new instance of Service.
- #port_bindings_config ⇒ Object
- #public_ports ⇒ Object
- #volume_binds_config ⇒ Object
Methods included from Capistrano::DSL
Methods included from Capistrano::DSL::Env
#any?, #clear_env, #current_environment, #delete, #env, #fetch, #set, #set_current_environment
Constructor Details
#initialize(name) ⇒ Service
Returns a new instance of Service.
11 12 13 14 15 16 17 18 19 |
# File 'lib/centurion/service.rb', line 11 def initialize(name) @name = name @env_vars = {} @volumes = [] @port_bindings = [] @cap_adds = [] @cap_drops = [] @network_mode = 'bridge' end |
Instance Attribute Details
#cap_adds ⇒ Object
Returns the value of attribute cap_adds.
8 9 10 |
# File 'lib/centurion/service.rb', line 8 def cap_adds @cap_adds end |
#cap_drops ⇒ Object
Returns the value of attribute cap_drops.
8 9 10 |
# File 'lib/centurion/service.rb', line 8 def cap_drops @cap_drops end |
#command ⇒ Object
Returns the value of attribute command.
8 9 10 |
# File 'lib/centurion/service.rb', line 8 def command @command end |
#cpu_shares ⇒ Object
Returns the value of attribute cpu_shares.
9 10 11 |
# File 'lib/centurion/service.rb', line 9 def cpu_shares @cpu_shares end |
#dns ⇒ Object
Returns the value of attribute dns.
8 9 10 |
# File 'lib/centurion/service.rb', line 8 def dns @dns end |
#env_vars ⇒ Object (readonly)
Returns the value of attribute env_vars.
9 10 11 |
# File 'lib/centurion/service.rb', line 9 def env_vars @env_vars end |
#extra_hosts ⇒ Object
Returns the value of attribute extra_hosts.
8 9 10 |
# File 'lib/centurion/service.rb', line 8 def extra_hosts @extra_hosts end |
#image ⇒ Object
Returns the value of attribute image.
8 9 10 |
# File 'lib/centurion/service.rb', line 8 def image @image end |
#memory ⇒ Object
Returns the value of attribute memory.
9 10 11 |
# File 'lib/centurion/service.rb', line 9 def memory @memory end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/centurion/service.rb', line 8 def name @name end |
#network_mode ⇒ Object
Returns the value of attribute network_mode.
8 9 10 |
# File 'lib/centurion/service.rb', line 8 def network_mode @network_mode end |
#port_bindings ⇒ Object
Returns the value of attribute port_bindings.
8 9 10 |
# File 'lib/centurion/service.rb', line 8 def port_bindings @port_bindings end |
#volumes ⇒ Object
Returns the value of attribute volumes.
8 9 10 |
# File 'lib/centurion/service.rb', line 8 def volumes @volumes end |
Class Method Details
.from_env ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/centurion/service.rb', line 21 def self.from_env Service.new(fetch(:name)).tap do |s| s.image = if fetch(:tag, nil) "#{fetch(:image, nil)}:#{fetch(:tag)}" else fetch(:image, nil) end s.cap_adds = fetch(:cap_adds, []) s.cap_drops = fetch(:cap_drops, []) s.dns = fetch(:dns, nil) s.extra_hosts = fetch(:extra_hosts, nil) s.volumes = fetch(:binds, []) s.port_bindings = fetch(:port_bindings, []) s.network_mode = fetch(:network_mode, 'bridge') s.command = fetch(:command, nil) s.add_env_vars(fetch(:env_vars, {})) end end |
Instance Method Details
#add_env_vars(new_vars) ⇒ Object
42 43 44 |
# File 'lib/centurion/service.rb', line 42 def add_env_vars(new_vars) @env_vars.merge!(new_vars) end |
#add_port_bindings(host_port, container_port, type = 'tcp', host_ip = nil) ⇒ Object
46 47 48 |
# File 'lib/centurion/service.rb', line 46 def add_port_bindings(host_port, container_port, type = 'tcp', host_ip = nil) @port_bindings << PortBinding.new(host_port, container_port, type, host_ip) end |
#add_volume(host_volume, container_volume) ⇒ Object
50 51 52 |
# File 'lib/centurion/service.rb', line 50 def add_volume(host_volume, container_volume) @volumes << Volume.new(host_volume, container_volume) end |
#build_config(server_hostname, &block) ⇒ 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 |
# File 'lib/centurion/service.rb', line 90 def build_config(server_hostname, &block) container_config = {}.tap do |c| c['Image'] = image c['Hostname'] = block.call(server_hostname) if block_given? c['Cmd'] = command if command c['Memory'] = memory if memory c['CpuShares'] = cpu_shares if cpu_shares end unless port_bindings.empty? container_config['ExposedPorts'] = port_bindings.reduce({}) do |config, binding| config["#{binding.container_port}/#{binding.type}"] = {} config end end unless env_vars.empty? container_config['Env'] = env_vars.map do |k,v| "#{k}=#{interpolate_var(v, server_hostname)}" end end unless volumes.empty? container_config['Volumes'] = volumes.inject({}) do |memo, v| memo[v.container_volume] = {} memo end end container_config end |
#build_console_config(server_name, &block) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/centurion/service.rb', line 158 def build_console_config(server_name, &block) build_config(server_name, &block).merge({ 'Cmd' => ['/bin/bash'], 'AttachStdin' => true, 'Tty' => true, 'OpenStdin' => true, }) end |
#build_host_config(restart_policy = nil) ⇒ Object
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 148 149 150 151 152 153 154 155 156 |
# File 'lib/centurion/service.rb', line 122 def build_host_config(restart_policy = nil) host_config = {} # Set capability additions and drops host_config['CapAdd'] = cap_adds if cap_adds host_config['CapDrop'] = cap_drops if cap_drops # Map some host volumes if needed host_config['Binds'] = volume_binds_config if volume_binds_config # Bind the ports host_config['PortBindings'] = port_bindings_config # Set the network mode host_config['NetworkMode'] = network_mode # DNS if specified host_config['Dns'] = dns if dns # Add ExtraHosts if needed host_config['ExtraHosts'] = extra_hosts if extra_hosts # Restart Policy if restart_policy host_config['RestartPolicy'] = {} restart_policy_name = restart_policy.name restart_policy_name = 'on-failure' unless ["always", "on-failure", "no"].include?(restart_policy_name) host_config['RestartPolicy']['Name'] = restart_policy_name host_config['RestartPolicy']['MaximumRetryCount'] = restart_policy.max_retry_count || 10 if restart_policy_name == 'on-failure' end host_config end |
#port_bindings_config ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/centurion/service.rb', line 171 def port_bindings_config @port_bindings.inject({}) do |memo, binding| config = {} config['HostPort'] = binding.host_port.to_s config['HostIp'] = binding.host_ip if binding.host_ip memo["#{binding.container_port}/#{binding.type}"] = [config] memo end end |
#public_ports ⇒ Object
181 182 183 |
# File 'lib/centurion/service.rb', line 181 def public_ports @port_bindings.map(&:host_port) end |
#volume_binds_config ⇒ Object
167 168 169 |
# File 'lib/centurion/service.rb', line 167 def volume_binds_config @volumes.map { |volume| "#{volume.host_volume}:#{volume.container_volume}" } end |