From d452be2e71cf0adcc179a719d13240c50263c264 Mon Sep 17 00:00:00 2001 From: Fletcher Nichol Date: Sat, 1 Dec 2012 13:50:30 -0700 Subject: [PATCH] Extract Jamie code to jamie gem, whoo boy. --- Gemfile | 5 +- Rakefile | 2 +- Vagrantfile | 2 +- test/jamie.rb | 110 ---------------------------------------- test/jamie/rake_task.rb | 51 ------------------- test/jamie/vagrant.rb | 69 ------------------------- 6 files changed, 4 insertions(+), 235 deletions(-) delete mode 100644 test/jamie.rb delete mode 100644 test/jamie/rake_task.rb delete mode 100644 test/jamie/vagrant.rb diff --git a/Gemfile b/Gemfile index 66287116..bf0d4403 100644 --- a/Gemfile +++ b/Gemfile @@ -8,7 +8,6 @@ gem 'foodcritic', :platforms => :ruby_19 gem 'chef', (ENV['CHEF_VERSION'] || '>= 0.10.10') group :integration do - gem 'berkshelf', '>= 1.0.0.rc3' - gem 'hashie' - gem 'mixlib-shellout' + gem 'berkshelf', '>= 1.0.0' + gem 'jamie', :git => 'git://github.com/jamie-ci/jamie.git' end diff --git a/Rakefile b/Rakefile index 0806efe1..07d58864 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,7 @@ #!/usr/bin/env rake require 'foodcritic' -require_relative 'test/jamie/rake_task' +require 'jamie/rake_task' FoodCritic::Rake::LintTask.new do |t| t.options = { :fail_tags => ['any'] } diff --git a/Vagrantfile b/Vagrantfile index 9a7e99db..cde46575 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -2,7 +2,7 @@ # vi: set ft=ruby : require 'berkshelf/vagrant' -require_relative 'test/jamie/vagrant' +require 'jamie/vagrant' Vagrant::Config.run do |config| Jamie::Vagrant.define_vms(config) diff --git a/test/jamie.rb b/test/jamie.rb deleted file mode 100644 index c010ba82..00000000 --- a/test/jamie.rb +++ /dev/null @@ -1,110 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'yaml' -require 'hashie/dash' -require 'mixlib/shellout' - -module Jamie - class Platform < Hashie::Dash - property :name, :required => true - property :vagrant_box - property :vagrant_box_url - property :base_run_list, :default => [] - end - - class Suite < Hashie::Dash - property :name, :required => true - property :run_list, :required => true - property :json, :default => Hash.new - end - - class Config - attr_writer :yaml - attr_writer :platforms - attr_writer :suites - attr_writer :backend - attr_writer :log_level - attr_writer :data_bags_base_path - - def yaml - @yaml ||= File.join(Dir.pwd, '.jamie.yml') - end - - def platforms - @platforms ||= - Array(yaml_data["platforms"]).map { |hash| Platform.new(hash) } - end - - def suites - @suites ||= - Array(yaml_data["suites"]).map { |hash| Suite.new(hash) } - end - - def backend - @backend ||= backend_for(yaml_data["backend"] || "vagrant") - end - - def log_level - @log_level ||= :info - end - - def data_bags_base_path - default_path = File.join(Dir.pwd, 'test/integration') - - @data_bags_path ||= File.directory?(default_path) ? default_path : nil - end - - def instances - result = [] - suites.each do |suite| - platforms.each do |platform| - result << instance_name(suite, platform) - end - end - result - end - - private - - def yaml_data - @yaml_data ||= YAML.load_file(yaml) - end - - def instance_name(suite, platform) - "#{suite.name}-#{platform.name}".gsub(/_/, '-').gsub(/\./, '') - end - - def backend_for(backend) - klass = Jamie::Backend.const_get(backend.capitalize) - klass.new - end - end - - module Backend - class CommandFailed < StandardError ; end - - class Vagrant - def up(instance) - exec! "vagrant up #{instance}" - rescue Mixlib::ShellOut::ShellCommandFailed => ex - raise CommandFailed, ex.message - end - - def destroy(instance) - exec! "vagrant destroy #{instance} -f" - rescue Mixlib::ShellOut::ShellCommandFailed => ex - raise CommandFailed, ex.message - end - - def exec!(cmd) - puts "-----> [vagrant command] #{cmd}" - shellout = Mixlib::ShellOut.new( - cmd, :live_stream => STDOUT, :timeout => 60000 - ) - shellout.run_command - puts "-----> Command '#{cmd}' ran in #{shellout.execution_time} seconds." - shellout.error! - end - end - end -end diff --git a/test/jamie/rake_task.rb b/test/jamie/rake_task.rb deleted file mode 100644 index db03b284..00000000 --- a/test/jamie/rake_task.rb +++ /dev/null @@ -1,51 +0,0 @@ -# -*- encoding: utf-8 -*- - -require 'rake' -require 'rake/tasklib' -require_relative '../jamie' - -module Jamie - module Rake - class Tasks < ::Rake::TaskLib - attr_accessor :name - - def initialize(name = :jamie) - @name = name - yield self if block_given? - define - end - - def define - config = Jamie::Config.new - - namespace(name) do - config.instances.each do |instance_name| - desc "Run #{instance_name} integration" - task(instance_name) do - puts "-----> Cleaning up any prior instances of #{instance_name}" - config.backend.destroy(instance_name) - puts "-----> Bringing up instance #{instance_name}" - config.backend.up(instance_name) - puts "-----> Instance #{instance_name} completed." - end - - namespace(instance_name) do - desc "Destroy #{instance_name} instance" - task :destroy do - puts "-----> Destroying any prior instances of #{instance_name}" - config.backend.destroy(instance_name) - puts "-----> Instance #{instance_name} destruction complete." - end - end - end - - desc "Destroy all instances" - task :destroy => config.instances.map { |i| "#{i}:destroy" } - end - - desc "Run Jamie integration" - task name => config.instances - end - end - end -end diff --git a/test/jamie/vagrant.rb b/test/jamie/vagrant.rb deleted file mode 100644 index eff8542f..00000000 --- a/test/jamie/vagrant.rb +++ /dev/null @@ -1,69 +0,0 @@ -# -*- encoding: utf-8 -*- - -require_relative '../jamie' -require 'vagrant' -require 'forwardable' - -module Jamie - module Vagrant - class Config < ::Vagrant::Config::Base - extend Forwardable - - def_delegators :@config, :yaml, :yaml=, :platforms, :platforms=, - :suites, :suites=, :log_level, :log_level=, - :data_bags_base_path, :data_bags_base_path=, :yaml_data - - def initialize - @config = Jamie::Config.new - end - end - - def self.init! - ::Vagrant.config_keys.register(:jamie) { Jamie::Vagrant::Config } - end - - def self.define_vms(config) - config.jamie.suites.each do |suite| - config.jamie.platforms.each do |platform| - define_vagrant_vm(config, suite, platform) - end - end - end - - private - - def self.define_vagrant_vm(config, suite, platform) - name = "#{suite.name}-#{platform.name}".gsub(/_/, '-').gsub(/\./, '') - - config.vm.define name do |c| - c.vm.box = platform.vagrant_box - c.vm.box_url = platform.vagrant_box_url if platform.vagrant_box_url - c.vm.host_name = "#{name}.vagrantup.com" - c.vm.customize ["modifyvm", :id, "--memory", "256"] - - c.vm.provision :chef_solo do |chef| - chef.log_level = config.jamie.log_level - chef.run_list = platform.base_run_list + Array(suite.run_list) - chef.json = suite.json - chef.data_bags_path = calculate_data_bags_path(config, name) - end - end - end - - def self.calculate_data_bags_path(config, instance_name) - base_path = config.jamie.data_bags_base_path - instance_data_bags_path = File.join(base_path, instance_name, "data_bags") - common_data_bags_path = File.join(base_path, "data_bags") - - if File.directory?(instance_data_bags_path) - instance_data_bags_path - elsif File.directory?(common_data_bags_path) - common_data_bags_path - else - nil - end - end - end -end - -Jamie::Vagrant.init!