#!/usr/bin/env ruby require 'rubygems' require 'thor' require 'pathname' require 'fileutils' require 'yaml' require 'awesome_print' require 'chef' require 'chef/knife' require 'chef/knife/bootstrap' require 'chef/knife/ssh' require 'socket' require 'net/ssh/multi' require 'readline' require 'stringio' $LOAD_PATH << File.join(File.dirname(__FILE__), '../lib') require 'cucumber-chef' GEM_DIR = Gem.default_dir class CucumberChef < Thor include Thor::Actions def self.source_root File.dirname(__FILE__) end no_tasks do def create_directory_structure(project_dir) %w{step_definitions support}.each do |dir| FileUtils.mkdir_p(project_dir + "features" + dir) end end def generate_project_skeleton(project_dir) template_dir = self.gem_root + 'lib' + 'cucumber' + 'chef' + 'templates' templates = { "readme.erb" => 'README', "example_feature.erb" => 'features/example.feature', "example_step.erb" => 'features/step_definitions/example_step.rb', "env.rb" => "features/support/env.rb" } templates.each do |filename, destination| template((template_dir + filename).realpath, project_dir + destination) end end def gem_root Pathname.new(__FILE__).parent.parent end def error(message) warn message exit 255 end end desc "project " , "Create a project template for testing an infrastructure" def project(project_name) @project = project_name project_dir = Pathname.new(".") + "cucumber-chef" + @project create_directory_structure(project_dir) generate_project_skeleton(project_dir) end desc "setup", "Set up a cucumber-chef test lab in Amazon EC2" method_option :test, :type => :boolean def setup begin if options.test? config = Cucumber::Chef::Config.test_config else config = Cucumber::Chef::Config.new end config.verify $stdout.sync provisioner = ::Cucumber::Chef::Provisioner.new server = provisioner.build_test_lab(config, $stdout) sleep(10) provisioner.upload_cookbook(config) provisioner.upload_role(config) provisioner.bootstrap_node(server.dns_name, config).run rescue ::Cucumber::Chef::Error => err error(err.message) end end desc "displayconfig", "Display the current config from knife.rb" method_option :test, :type => :boolean def displayconfig if options.test? config = Cucumber::Chef::Config.test_config else config = Cucumber::Chef::Config.new end puts config.list.join("\n") config.verify rescue ::Cucumber::Chef::Error => err error(err.message) end desc "info", "Display information about the current test lab" method_option :test, :type => :boolean def info if options.test? config = Cucumber::Chef::Config.test_config else config = Cucumber::Chef::Config.new end config.verify lab = Cucumber::Chef::TestLab.new(config) if lab.exists? puts lab.info else end end desc "upload", "Upload a cucumber-chef test suite to the test lab platform" def upload puts "Project uploaded to the test-lab." end desc "test", "Run a cucumber-chef test suite from a workstation." def test puts "Running test..." puts puts "Test results will appear here." end end CucumberChef.start