bin/cucumber-chef in cucumber-chef-2.0.7 vs bin/cucumber-chef in cucumber-chef-2.1.0.rc.0

- old
+ new

@@ -1,10 +1,12 @@ #!/usr/bin/env ruby require 'thor' require 'cucumber-chef' +# $logger = Cucumber::Chef.logger + class CucumberChef < Thor include Thor::Actions no_tasks do @@ -37,44 +39,29 @@ templates = { "readme.erb" => "features/#{project}/README", "example_feature.erb" => "features/#{project}/#{project}.feature", "example_steps.erb" => "features/#{project}/step_definitions/#{project}_steps.rb", "env.rb" => "features/support/env.rb", + "cc-hooks.rb" => "features/support/cc-hooks.rb", "readme-data_bags.erb" => "features/support/data_bags/README", "readme-roles.erb" => "features/support/roles/README", - "readme-keys.erb" => "features/support/keys/README" + "readme-keys.erb" => "features/support/keys/README", + "readme-environments.erb" => "features/support/environments/README" } templates.each do |source, destination| template(source, File.join(destination_dir, destination)) end end def load_config + tag = Cucumber::Chef.tag("cucumber-chef") + puts(set_color(tag, :green, true)) + Cucumber::Chef.load_config(tag) + $logger = Cucumber::Chef.logger - chef_repo = (Cucumber::Chef.locate_parent(".chef") rescue nil) - if ( !chef_repo || ( !File.exists?(chef_repo) || !File.directory?(chef_repo) ) ) - fatal("It does not look like you are inside a chef-repo! Please relocate to one and execute your command again!") - exit(255) - end - - $logger = ZTK::Logger.new(Cucumber::Chef.log_file) - Cucumber::Chef.is_rc? and ($logger.level = ZTK::Logger::DEBUG) - - message = "cucumber-chef v#{Cucumber::Chef::VERSION}" - puts(set_color(message, :green, true)) - - $logger.info { "================================================================================" } - $logger.info { message } - $logger.info { "UNAME: %s" % %x( uname -a ).chomp.strip } - $logger.info { "Ruby Version: #{RUBY_VERSION}" } - $logger.info { "Ruby Patch Level: #{RUBY_PATCHLEVEL}" } - $logger.info { "Ruby Platform: #{RUBY_PLATFORM}" } - $logger.info { "Ruby Engine: #{RUBY_ENGINE}" } - $logger.info { "================================================================================" } - - @options.test? ? Cucumber::Chef::Config.test : Cucumber::Chef::Config.load + @options.test? and Cucumber::Chef::Config.test end def fatal(message) puts(set_color(message, :red, :bold)) exit(255) @@ -216,25 +203,17 @@ sleep(1) end puts(set_color("...BOOM!", :red, true)) puts - ssh = ZTK::SSH.new - ssh.config.host_name = test_lab.labs_running.first.public_ip_address - ssh.config.user = "ubuntu" - ssh.config.keys = Cucumber::Chef.locate(:file, ".cucumber-chef", "id_rsa-#{ssh.config.user}") - ssh.exec("nohup sudo pkill -9 -f cc-server") - ssh.exec("nohup sudo BACKGROUND=yes cc-server #{Cucumber::Chef.external_ip}") + test_lab.ssh.exec("nohup sudo pkill -9 -f cc-server") + test_lab.ssh.exec("nohup sudo BACKGROUND=yes cc-server #{Cucumber::Chef.external_ip}") Cucumber::Chef.spinner do ZTK::TCPSocketCheck.new(:host => test_lab.labs_running.first.public_ip_address, :port => 8787, :data => "\n\n").wait end - - # initialize our drb object - test_lab_drb ||= DRbObject.new_with_uri("druby://#{test_lab.labs_running.first.public_ip_address}:8787") - test_lab_drb and DRb.start_service - test_lab_drb.servers = Hash.new(nil) - test_lab_drb.server_destroy(container) + test_lab.drb.servers = Hash.new(nil) + test_lab.drb.server_destroy(container) else puts puts(set_color("Whew! That was close!", :green, true)) end end @@ -249,10 +228,23 @@ fatal(e) end ################################################################################ + desc "test", "depreciated" + def test + puts + puts(set_color("The test task is depreciated! You should execute cucumber directly.", :red, true)) + puts + + rescue Cucumber::Chef::Error => e + $logger.fatal { e.backtrace.join("\n") } + fatal(e) + end + +################################################################################ + desc "up", "Startup the cucumber-chef test lab" def up load_config puts @@ -294,32 +286,17 @@ def ssh(*args) load_config puts if (test_lab = Cucumber::Chef::TestLab.new) && (test_lab.labs_running.count > 0) - ssh = ZTK::SSH.new - if args.size == 0 - ssh.config.host_name = test_lab.labs_running.first.public_ip_address - ssh.config.user = "ubuntu" - ssh.config.keys = Cucumber::Chef.locate(:file, ".cucumber-chef", "id_rsa-#{ssh.config.user}") - puts([set_color("Attempting SSH connection to cucumber-chef '", :blue, true), set_color("test lab", :cyan, true), set_color("'...", :blue, true)].join) - ssh.console + test_lab.ssh.console else container = args[0] - - ssh.config.proxy_host_name = test_lab.labs_running.first.public_ip_address - ssh.config.proxy_user = "ubuntu" - ssh.config.proxy_keys = Cucumber::Chef.locate(:file, ".cucumber-chef", "id_rsa-#{ssh.config.proxy_user}") - - ssh.config.host_name = container - ssh.config.user = "root" - ssh.config.keys = Cucumber::Chef.locate(:file, ".cucumber-chef", "id_rsa-#{ssh.config.proxy_user}") - puts([set_color("Attempting SSH connection to cucumber-chef container '", :blue, true), set_color(container, :cyan, true), set_color("'...", :blue, true)].join) - ssh.console + test_lab.proxy_ssh(container).console end else puts(set_color("No cucumber-chef test labs available to ssh to!", :red, true)) end @@ -330,42 +307,65 @@ fatal(e) end ################################################################################ + desc "log", "Streams the cucumber-chef local and test lab logs to the terminal." + def log + load_config + + if ($test_lab = Cucumber::Chef::TestLab.new) && ($test_lab.labs_running.count > 0) + $tail_thread_remote = Thread.new do + $test_lab.ssh.exec("tail -n 0 -f /home/#{$test_lab.ssh.config.user}/.cucumber-chef/cucumber-chef.log") + + Kernel.at_exit do + $test_lab.ssh.close + end + end + + log_file = File.open(Cucumber::Chef.log_file, "r") + log_file.seek(0, ::IO::SEEK_END) + loop do + if !(data = (log_file.readline rescue nil)).nil? + print(data) + else + sleep(1) + end + end + + $tail_thread_remote.join + end + + Kernel.at_exit do + $tail_thread_remote.kill + end + end + +################################################################################ + desc "diagnose <container>", "Provide diagnostics from the chef-client on the specified container." method_option :strace, :type => :boolean, :desc => "output the chef-client 'chef-stacktrace.out'", :aliases => "-s", :default => true method_option :log, :type => :boolean, :desc => "output the chef-client 'chef.log'", :aliases => "-l", :default => true method_option :lines, :type => :numeric, :desc => "output the last N lines of the chef-client 'chef.log'", :aliases => "-n", :default => 1 def diagnose(container) load_config puts if (test_lab = Cucumber::Chef::TestLab.new) && (test_lab.labs_running.count > 0) - ssh = ZTK::SSH.new - - ssh.config.proxy_host_name = test_lab.labs_running.first.public_ip_address - ssh.config.proxy_user = "ubuntu" - ssh.config.proxy_keys = Cucumber::Chef.locate(:file, ".cucumber-chef", "id_rsa-#{ssh.config.proxy_user}") - - ssh.config.host_name = container - ssh.config.user = "root" - ssh.config.keys = Cucumber::Chef.locate(:file, ".cucumber-chef", "id_rsa-#{ssh.config.proxy_user}") - puts([set_color("Attempting to collect diagnostic information on cucumber-chef container '", :blue, true), set_color(container, :cyan, true), set_color("'...", :blue, true)].join) if @options.strace? puts puts("chef-stacktrace.out:") puts(set_color("============================================================================", :bold)) - ssh.exec("[[ -e /var/chef/cache/chef-stacktrace.out ]] && cat /var/chef/cache/chef-stacktrace.out") + test_lab.proxy_ssh(container).exec("[[ -e /var/chef/cache/chef-stacktrace.out ]] && cat /var/chef/cache/chef-stacktrace.out") print("\n") end if @options.log? puts puts("chef.log:") puts(set_color("============================================================================", :bold)) - ssh.exec("[[ -e /var/log/chef/client.log ]] && tail -n #{@options.lines} /var/log/chef/client.log") + test_lab.proxy_ssh(container).exec("[[ -e /var/log/chef/client.log ]] && tail -n #{@options.lines} /var/log/chef/client.log") end end puts rescue Cucumber::Chef::Error => e @@ -395,19 +395,13 @@ def ps(*args) load_config puts if (test_lab = Cucumber::Chef::TestLab.new) && (test_lab.labs_running.count > 0) - ssh = ZTK::SSH.new - - ssh.config.host_name = test_lab.labs_running.first.public_ip_address - ssh.config.user = "ubuntu" - ssh.config.keys = Cucumber::Chef.locate(:file, ".cucumber-chef", "id_rsa-#{ssh.config.user}") - puts(set_color("Getting container processes from cucumber-chef test lab...", :blue, true)) puts puts(set_color("============================================================================", :bold)) - ssh.exec("lxc-ps --lxc -- #{args.join(" ")}") + test_lab.ssh.exec("lxc-ps --lxc -- #{args.join(" ")}") print("\n") end puts rescue Cucumber::Chef::Error => e