lib/spoon.rb in docker-spoon-0.6.0 vs lib/spoon.rb in docker-spoon-0.7.0

- old
+ new

@@ -10,10 +10,15 @@ include Methadone::SH version(Spoon::VERSION) main do |instance| + # Read config file & set options + if File.exists?(options[:config]) + eval(File.open(options[:config]).read) + end + D options.inspect if options[:list] instance_list elsif options["list-images"] image_list @@ -41,26 +46,25 @@ # Configurables options[:builddir] = '.' on("--builddir DIR", "Directory containing Dockerfile") on("--pre-build-commands", "List of commands to run locally before building image") + # These are config only options + options[:copy_on_create] = [] + options[:add_authorized_keys] = false options[:url] = Docker.url on("-u", "--url URL", "Docker url to connect to") on("-L", "--list-images", "List available spoon images") options[:image] = "spoon-pairing" on("-i", "--image NAME", "Use image for spoon instance") options[:prefix] = 'spoon-' on("-p", "--prefix PREFIX", "Prefix for container names") options[:command] = '' options[:config] = "#{ENV['HOME']}/.spoonrc" - on("-c", "--config FILE", "Config file to use for spoon options") + on("-c FILE", "--config", "Config file to use for spoon options") on("--debug", "Enable debug") - # Read config file & set options - if File.exists?(options[:config]) - eval(File.open(options[:config]).read) - end arg(:instance, :optional, "Spoon instance to connect to") use_log_level_option @@ -121,10 +125,12 @@ def self.instance_connect(name, command='') docker_url if not instance_exists? name puts "The `#{name}` container doesn't exist, creating..." instance_create(name) + instance_copy_authorized_keys(name, options[:add_authorized_keys]) + instance_copy_files(name) end container = get_container(name) unless is_running?(container) instance_start(container) @@ -136,11 +142,11 @@ def self.instance_list docker_url puts "List of available spoon containers:" container_list = get_all_containers.select { |c| c.info["Names"].first.to_s.start_with? "/#{options[:prefix]}" } - .sort { |c1, c2| c1.info["Names"].first.to_s <=> c2.info["Names"].first.to_s } + .sort { |c1, c2| c1.info["Names"].first.to_s <=> c2.info["Names"].first.to_s } max_width_container_name = remove_prefix(container_list.max_by {|c| c.info["Names"].first.to_s.length }.info["Names"].first.to_s) max_name_width = max_width_container_name.length container_list.each do |container| name = container.info["Names"].first.to_s running = is_running?(container) ? Rainbow("Running").green : Rainbow("Stopped").red @@ -229,10 +235,40 @@ else puts "No container named: #{container.inspect}" end end + def self.instance_copy_authorized_keys(name, keyfile) + if keyfile + container = get_container(name) + host = URI.parse(options[:url]).host + key = File.read("#{ENV['HOME']}/.ssh/#{keyfile}") + if container + ssh_port = get_port('22', container) + puts "Waiting for #{name}:#{ssh_port}..." until host_available?(host, ssh_port) + system("ssh -t -o StrictHostKeyChecking=no -p #{ssh_port} pairing@#{host} \"mkdir -p .ssh && chmod 700 .ssh && echo '#{key}' >> ~/.ssh/authorized_keys\"") + else + puts "No container named: #{container.inspect}" + end + end + end + + def self.instance_copy_files(name) + options[:copy_on_create].each do |file| + puts "Copying file #{file}" + container = get_container(name) + host = URI.parse(options[:url]).host + if container + ssh_port = get_port('22', container) + puts "Waiting for #{name}:#{ssh_port}..." until host_available?(host, ssh_port) + system("scp -o StrictHostKeyChecking=no -P #{ssh_port} #{ENV['HOME']}/#{file} pairing@#{host}:#{file}") + else + puts "No container named: #{container.inspect}" + end + end + end + def self.get_all_containers Docker::Container.all(:all => true) end def self.get_running_containers @@ -295,9 +331,6 @@ end go! end - # option :debug, :type => :boolean, :default => true - - - # private +# option :debug, :type => :boolean, :default => true