Skip to content

Commit

Permalink
Merge pull request #170 from relistan/support-labels
Browse files Browse the repository at this point in the history
Support labels for containers
  • Loading branch information
intjonathan authored Jan 27, 2017
2 parents 75591a3 + d71a236 commit 797416c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/centurion/deploy_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def env_vars(new_vars)
set(:env_vars, current)
end

def labels(new_labels)
current = fetch(:labels, {})
new_labels.each_pair do |new_key, new_value|
current[new_key.to_s] = new_value.to_s
end
set(:labels, current)
end

def add_capability(new_cap_adds)
if !valid_capability?(new_cap_adds)
abort("Invalid capability addition #{new_cap_adds} specified.")
Expand Down
9 changes: 8 additions & 1 deletion lib/centurion/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Service
extend ::Capistrano::DSL

attr_accessor :command, :dns, :extra_hosts, :image, :name, :volumes, :port_bindings, :network_mode, :cap_adds, :cap_drops, :ipc_mode
attr_reader :memory, :cpu_shares, :env_vars
attr_reader :memory, :cpu_shares, :env_vars, :labels

def initialize(name)
@name = name
Expand All @@ -15,6 +15,7 @@ def initialize(name)
@port_bindings = []
@cap_adds = []
@cap_drops = []
@labels = {}
@network_mode = 'bridge'
end

Expand All @@ -38,6 +39,7 @@ def self.from_env
s.cpu_shares = fetch(:cpu_shares, 0)
s.ipc_mode = fetch(:ipc_mode, nil)

s.add_labels(fetch(:labels, {}))
s.add_env_vars(fetch(:env_vars, {}))
end
end
Expand All @@ -54,6 +56,10 @@ def add_volume(host_volume, container_volume)
@volumes << Volume.new(host_volume, container_volume)
end

def add_labels(labels)
@labels.merge!(Hash[labels.map { |(k,v)| [ k.to_s, v.to_s ]}])
end

def cap_adds=(capabilites)
unless capabilites.is_a? Array
raise ArgumentError, "invalid value for capability additions: #{capabilites}, value must be an array"
Expand Down Expand Up @@ -101,6 +107,7 @@ def build_config(server_hostname, &block)
c['Cmd'] = command if command
c['Memory'] = memory if memory
c['CpuShares'] = cpu_shares if cpu_shares
c['Labels'] = labels unless labels.nil? || labels.empty?
end

unless port_bindings.empty?
Expand Down
12 changes: 12 additions & 0 deletions spec/deploy_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ class DeployDSLTest
)
end

it 'adds new labels to the existing ones, as strings' do
DeployDSLTest.labels(Shakespeare: 'Hamlet')
DeployDSLTest.labels(Dickens: 'David Copperfield',
Dickens_birth_year: 1812)

expect(DeployDSLTest.defined_service.labels).to eq(
'Shakespeare' => 'Hamlet',
'Dickens' => 'David Copperfield',
'Dickens_birth_year' => '1812'
)
end

describe '#add_capability' do
it 'adds one capability' do
DeployDSLTest.add_capability 'IPC_LOCK'
Expand Down
9 changes: 9 additions & 0 deletions spec/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
let(:service) { Centurion::Service.new(:redis) }
let(:hostname) { 'shakespeare' }
let(:image) { 'redis' }
let(:labels) { { 'test' => '123' } }

it 'creates a service from the environment' do
extend Capistrano::DSL
Expand All @@ -15,6 +16,7 @@
set(:hostname, hostname)
set(:binds, [ Centurion::Service::Volume.new('/foo', '/foo/bar') ])
set(:port_bindings, [ Centurion::Service::PortBinding.new(12340, 80, 'tcp') ])
set(:labels, labels)

svc = Centurion::Service.from_env
expect(svc.name).to eq('mycontainer')
Expand All @@ -24,6 +26,7 @@
expect(svc.volumes.first.host_volume).to eq('/foo')
expect(svc.port_bindings.size).to eq(1)
expect(svc.port_bindings.first.container_port).to eq(80)
expect(svc.labels).to eq(labels)
end

it 'starts with a command' do
Expand Down Expand Up @@ -70,6 +73,12 @@
expect(service.env_vars).to eq(SLAVE_OF: '127.0.0.1', USE_AOF: '1')
end

it 'has labels and flattens them all to text' do
service.add_labels(labels)
service.add_labels(another: 'label')
expect(service.labels).to eq(labels.merge('another' => 'label'))
end

it 'has volume bindings' do
service.add_volume('/volumes/redis/data', '/data')
service.add_volume('/volumes/redis/config', '/config')
Expand Down

0 comments on commit 797416c

Please sign in to comment.