Skip to content

Commit

Permalink
Merge pull request #188 from h0tbird/mvillacorta/lambda-functions
Browse files Browse the repository at this point in the history
Assign lambdas to environment variables
  • Loading branch information
intjonathan authored Jul 27, 2018
2 parents 2f292df + c63032e commit 492d7ee
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ Most of the DSL items (`host_port`, `host_volume`, `env_vars`, `host`) can be
specified more than once and will append to the configuration. However, there
can only be one `command`; the last one will take priority.

You can also assign lambdas to environment variables. The lambda function will
be invoked by Centurion with `server_hostname` as its only argument. This is
useful to assign a sticky identity for each of the containers in the deploy.

You can cause your container to be started with a specific DNS server
IP address (the equivalent of `docker run --dns 172.17.42.1 ...`) like this:
```ruby
Expand Down
2 changes: 1 addition & 1 deletion lib/centurion/deploy_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def on_first_docker_host(&block)
def env_vars(new_vars)
current = fetch(:env_vars, {})
new_vars.each_pair do |new_key, new_value|
current[new_key.to_s] = new_value.to_s
current[new_key.to_s] = new_value.respond_to?(:call) ? new_value : new_value.to_s
end
set(:env_vars, current)
end
Expand Down
6 changes: 5 additions & 1 deletion lib/centurion/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ def build_config(server_hostname, &block)

unless env_vars.empty?
container_config['Env'] = env_vars.map do |k,v|
"#{k}=#{interpolate_var(v, server_hostname)}"
if v.respond_to? :call
"#{k}=#{v.call(server_hostname)}"
else
"#{k}=#{interpolate_var(v, server_hostname)}"
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/centurion/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Centurion
VERSION = '1.9.0'
VERSION = '1.9.2'
end
9 changes: 6 additions & 3 deletions spec/deploy_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ class DeployDSLTest
expect(DeployDSLTest.defined_service.command).to eq(command)
end

it 'adds new env_vars to the existing ones, as strings' do
it 'adds new env_vars to the existing ones, maintaining lambdas' do
lambda = ->() { Random.rand(5) }
DeployDSLTest.env_vars('SHAKESPEARE' => 'Hamlet')
DeployDSLTest.env_vars('DICKENS' => 'David Copperfield',
DICKENS_BIRTH_YEAR: 1812)
DICKENS_BIRTH_YEAR: 1812,
RANDOM_NUMBER: lambda)

expect(DeployDSLTest.defined_service.env_vars).to eq(
'SHAKESPEARE' => 'Hamlet',
'DICKENS' => 'David Copperfield',
'DICKENS_BIRTH_YEAR' => '1812'
'DICKENS_BIRTH_YEAR' => '1812',
'RANDOM_NUMBER' => lambda
)
end

Expand Down
6 changes: 6 additions & 0 deletions spec/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@
expect { service.add_env_vars(SOMETHING: true) }.not_to raise_error
end

it 'does supports lambdas as env vars' do
service = Centurion::Service.new(:redis)
service.add_env_vars(DYNAMIC_VAR: ->(hostname) { "the-#{hostname}" })
expect(service.build_config('example.com')['Env']).to eq(['DYNAMIC_VAR=the-example.com'])
end

it 'builds a valid docker host configuration' do
service = Centurion::Service.new(:redis)
service.dns = 'example.com'
Expand Down

0 comments on commit 492d7ee

Please sign in to comment.