Skip to content
This repository was archived by the owner on Mar 27, 2018. It is now read-only.

Commit 9ecd991

Browse files
committed
more refactoring of complexity
1 parent d9e614d commit 9ecd991

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

lib/docker/rails/app.rb

+53-38
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def is_configured?
6060
@is_configured || false
6161
end
6262

63-
def extract
63+
def extract_all
6464

6565
# For each container, process extractions
6666
# Containers are defined in compose, extractions are defined at root under container name e.g.:
@@ -87,22 +87,26 @@ def extract
8787
next
8888
end
8989

90-
extractions.each do |extraction|
91-
if extraction =~ /:/
92-
tokens = extraction.split(':')
93-
from = tokens[0]
94-
to = tokens[1]
95-
else
96-
from = extraction
97-
to = '.'
98-
end
90+
extract(container, service_name, extractions)
91+
end
92+
end
9993

100-
puts "\nExtracting #{service_name} #{from} to #{to}"
101-
begin
102-
extract_files(container, from, to)
103-
rescue => e
104-
puts e.message
105-
end
94+
def extract(container, service_name, extractions)
95+
extractions.each do |extraction|
96+
if extraction =~ /:/
97+
tokens = extraction.split(':')
98+
from = tokens[0]
99+
to = tokens[1]
100+
else
101+
from = extraction
102+
to = '.'
103+
end
104+
105+
puts "\nExtracting #{service_name} #{from} to #{to}"
106+
begin
107+
extract_files(container, from, to)
108+
rescue => e
109+
puts e.message
106110
end
107111
end
108112
end
@@ -207,28 +211,9 @@ def bash_connect(service_name)
207211

208212
def run_ssh_agent
209213
return if @config[:'ssh-agent'].nil?
210-
211-
ssh_agent_name = @config.ssh_agent_name
212-
begin
213-
Docker::Container.get(ssh_agent_name)
214-
puts "Gem data volume container #{ssh_agent_name} already exists."
215-
rescue Docker::Error::NotFoundError => e
216-
exec "docker run -d --name=#{ssh_agent_name} #{ssh_agent_image}"
217-
puts "SSH Agent forwarding container #{ssh_agent_name} running."
218-
end
219-
220-
ssh_base_cmd = "docker run --rm --volumes-from=#{ssh_agent_name} -v ~/.ssh:/ssh #{ssh_agent_image}"
221-
222-
ssh_keys = @config[:'ssh-agent'][:keys]
223-
puts "Forwarding SSH key(s): #{ssh_keys.join(',')} into container(s): #{@config[:'ssh-agent'][:containers].join(',')}"
224-
ssh_keys.each do |key_file_name|
225-
local_key_file = "#{ENV['HOME']}/.ssh/#{key_file_name}"
226-
raise "Local key file #{local_key_file} doesn't exist." unless File.exists? local_key_file
227-
exec "#{ssh_base_cmd} ssh-add /ssh/#{key_file_name}"
228-
end
229-
230-
# add known hosts
231-
exec "#{ssh_base_cmd} cp /ssh/known_hosts /root/.ssh/known_hosts"
214+
run_ssh_agent_daemon
215+
ssh_add_keys
216+
ssh_add_known_hosts
232217
end
233218

234219
def rm_ssh_agent
@@ -375,6 +360,36 @@ def kill(container)
375360
end
376361
end
377362

363+
def ssh_add_known_hosts
364+
exec "#{ssh_base_cmd} cp /ssh/known_hosts /root/.ssh/known_hosts"
365+
end
366+
367+
def ssh_base_cmd
368+
ssh_agent_name = @config.ssh_agent_name
369+
"docker run --rm --volumes-from=#{ssh_agent_name} -v ~/.ssh:/ssh #{ssh_agent_image}"
370+
end
371+
372+
def ssh_add_keys
373+
ssh_keys = @config[:'ssh-agent'][:keys]
374+
puts "Forwarding SSH key(s): #{ssh_keys.join(',')} into container(s): #{@config[:'ssh-agent'][:containers].join(',')}"
375+
ssh_keys.each do |key_file_name|
376+
local_key_file = "#{ENV['HOME']}/.ssh/#{key_file_name}"
377+
raise "Local key file #{local_key_file} doesn't exist." unless File.exists? local_key_file
378+
exec "#{ssh_base_cmd} ssh-add /ssh/#{key_file_name}"
379+
end
380+
end
381+
382+
def run_ssh_agent_daemon
383+
ssh_agent_name = @config.ssh_agent_name
384+
begin
385+
Docker::Container.get(ssh_agent_name)
386+
puts "Gem data volume container #{ssh_agent_name} already exists."
387+
rescue Docker::Error::NotFoundError => e
388+
exec "docker run -d --name=#{ssh_agent_name} #{ssh_agent_image}"
389+
puts "SSH Agent forwarding container #{ssh_agent_name} running."
390+
end
391+
end
392+
378393
def rm_v(container)
379394
container.remove(v: true, force: true)
380395
end

lib/docker/rails/cli/main.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def ci(target)
4646
def extract(target)
4747
app = App.configured(target, options)
4848
invoke :compose, [target], []
49-
app.extract
49+
app.extract_all
5050
end
5151

5252
desc 'cleanup <target>', 'Runs container cleanup functions stop, rm_volumes, rm_compose, rm_dangling, ps_all e.g. docker-rails cleanup --build=222 development'

0 commit comments

Comments
 (0)