Skip to content

Commit

Permalink
Merge pull request newrelic#99 from OpenSourceProjects/dry_run
Browse files Browse the repository at this point in the history
Offer dry-run capability. Finally getting this merged @frankywahl . Thanks!
  • Loading branch information
relistan committed May 9, 2015
2 parents 626f40b + 81b1d9a commit 64b5a5b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bin/centurion
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ opts = Trollop::options do
opt :registry_user, 'user for registry auth', type: String, short: :none
opt :registry_password,'password for registry auth', type: String, short: :none
opt :override_env, 'override environment variables, comma separated', type: String
opt :dry_run, 'print out docker command do run', type: :flag, default: false, short: :none
end

set_current_environment(opts[:environment].to_sym)
Expand Down Expand Up @@ -77,4 +78,9 @@ set :registry_user, opts[:registry_user] if opts[:registry_user]
set :registry_password, opts[:registry_password] if opts[:registry_password]

invoke('centurion:setup')
if opts[:dry_run]
Centurion::Mock.new(env).run
exit
end

invoke(opts[:action])
49 changes: 49 additions & 0 deletions lib/centurion/mock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Centurion
class Mock
def initialize(opts)
environment = opts[:current_environment]
@options = opts[environment]
end

def run
require 'pry-byebug'

result = hosts.map do |host|
string = "docker -H=tcp://#{host}:2375 run"

string << environment_vars unless env_vars.empty?
string << ports_vars unless ports_vars.empty?
end.join("\n\n\n ******* \n\n\n")
puts "#{result}"
end

private

attr_reader :options


def env_vars
options[:env_vars]
end

def environment_vars
env_vars.reduce('') { |string, (k, v)| " -e #{k.split('"')[0]}='#{v}'#{string}" }.rstrip
end

def ports
options[:port_bindings]
end

def hosts
options[:hosts]
end

def ports_vars
ports.reduce('') do |string, (host_port_protocol, container_port)|
h = host_port_protocol.split('/')
c = container_port.first['HostPort']
" -p #{h[0]}:#{c}/#{h[1]}#{string}"
end.rstrip
end
end
end

0 comments on commit 64b5a5b

Please sign in to comment.