Skip to content

Commit 797416c

Browse files
authored
Merge pull request #170 from relistan/support-labels
Support labels for containers
2 parents 75591a3 + d71a236 commit 797416c

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

lib/centurion/deploy_dsl.rb

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ def env_vars(new_vars)
2020
set(:env_vars, current)
2121
end
2222

23+
def labels(new_labels)
24+
current = fetch(:labels, {})
25+
new_labels.each_pair do |new_key, new_value|
26+
current[new_key.to_s] = new_value.to_s
27+
end
28+
set(:labels, current)
29+
end
30+
2331
def add_capability(new_cap_adds)
2432
if !valid_capability?(new_cap_adds)
2533
abort("Invalid capability addition #{new_cap_adds} specified.")

lib/centurion/service.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Service
66
extend ::Capistrano::DSL
77

88
attr_accessor :command, :dns, :extra_hosts, :image, :name, :volumes, :port_bindings, :network_mode, :cap_adds, :cap_drops, :ipc_mode
9-
attr_reader :memory, :cpu_shares, :env_vars
9+
attr_reader :memory, :cpu_shares, :env_vars, :labels
1010

1111
def initialize(name)
1212
@name = name
@@ -15,6 +15,7 @@ def initialize(name)
1515
@port_bindings = []
1616
@cap_adds = []
1717
@cap_drops = []
18+
@labels = {}
1819
@network_mode = 'bridge'
1920
end
2021

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

42+
s.add_labels(fetch(:labels, {}))
4143
s.add_env_vars(fetch(:env_vars, {}))
4244
end
4345
end
@@ -54,6 +56,10 @@ def add_volume(host_volume, container_volume)
5456
@volumes << Volume.new(host_volume, container_volume)
5557
end
5658

59+
def add_labels(labels)
60+
@labels.merge!(Hash[labels.map { |(k,v)| [ k.to_s, v.to_s ]}])
61+
end
62+
5763
def cap_adds=(capabilites)
5864
unless capabilites.is_a? Array
5965
raise ArgumentError, "invalid value for capability additions: #{capabilites}, value must be an array"
@@ -101,6 +107,7 @@ def build_config(server_hostname, &block)
101107
c['Cmd'] = command if command
102108
c['Memory'] = memory if memory
103109
c['CpuShares'] = cpu_shares if cpu_shares
110+
c['Labels'] = labels unless labels.nil? || labels.empty?
104111
end
105112

106113
unless port_bindings.empty?

spec/deploy_dsl_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ class DeployDSLTest
4040
)
4141
end
4242

43+
it 'adds new labels to the existing ones, as strings' do
44+
DeployDSLTest.labels(Shakespeare: 'Hamlet')
45+
DeployDSLTest.labels(Dickens: 'David Copperfield',
46+
Dickens_birth_year: 1812)
47+
48+
expect(DeployDSLTest.defined_service.labels).to eq(
49+
'Shakespeare' => 'Hamlet',
50+
'Dickens' => 'David Copperfield',
51+
'Dickens_birth_year' => '1812'
52+
)
53+
end
54+
4355
describe '#add_capability' do
4456
it 'adds one capability' do
4557
DeployDSLTest.add_capability 'IPC_LOCK'

spec/service_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
let(:service) { Centurion::Service.new(:redis) }
77
let(:hostname) { 'shakespeare' }
88
let(:image) { 'redis' }
9+
let(:labels) { { 'test' => '123' } }
910

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

1921
svc = Centurion::Service.from_env
2022
expect(svc.name).to eq('mycontainer')
@@ -24,6 +26,7 @@
2426
expect(svc.volumes.first.host_volume).to eq('/foo')
2527
expect(svc.port_bindings.size).to eq(1)
2628
expect(svc.port_bindings.first.container_port).to eq(80)
29+
expect(svc.labels).to eq(labels)
2730
end
2831

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

76+
it 'has labels and flattens them all to text' do
77+
service.add_labels(labels)
78+
service.add_labels(another: 'label')
79+
expect(service.labels).to eq(labels.merge('another' => 'label'))
80+
end
81+
7382
it 'has volume bindings' do
7483
service.add_volume('/volumes/redis/data', '/data')
7584
service.add_volume('/volumes/redis/config', '/config')

0 commit comments

Comments
 (0)