lib/chef/knife/solo_cook.rb in knife-solo-0.1.0 vs lib/chef/knife/solo_cook.rb in knife-solo-0.2.0.pre1

- old
+ new

@@ -8,13 +8,11 @@ class Chef class Knife # Approach ported from spatula (https://github.com/trotter/spatula) # Copyright 2009, Trotter Cashion class SoloCook < Knife - OMNIBUS_EMBEDDED_PATHS ||= %w[/opt/chef/embedded/bin /opt/opscode/embedded/bin] - OMNIBUS_EMBEDDED_GEM_PATHS ||= %w[/opt/chef/embedded/lib/ruby/gems/1.9.1 /opt/opscode/embedded/lib/ruby/gems/1.9.1] - CHEF_VERSION_CONSTRAINT ||= ">=0.10.4" + CHEF_VERSION_CONSTRAINT = ">=0.10.4" unless defined? CHEF_VERSION_CONSTRAINT include KnifeSolo::SshCommand include KnifeSolo::KitchenCommand include KnifeSolo::NodeConfigCommand include KnifeSolo::Tools @@ -28,38 +26,53 @@ KnifeSolo::NodeConfigCommand.load_deps end banner "knife solo cook [USER@]HOSTNAME [JSON] (options)" + option :chef_check, + :long => '--no-chef-check', + :description => 'Skip the Chef version check on the node', + :default => true + option :skip_chef_check, :long => '--skip-chef-check', - :description => 'Skip the version check on the Chef gem' + :description => 'Deprecated. Replaced with --no-chef-check.' option :sync_only, :long => '--sync-only', :description => 'Only sync the cookbook - do not run Chef' + option :librarian, + :long => '--no-librarian', + :description => 'Skip librarian-chef install', + :default => true + option :why_run, :short => '-W', :long => '--why-run', :description => 'Enable whyrun mode' def run time('Run') do + if config[:skip_chef_check] + ui.warn '`--skip-chef-check` is deprecated, please use `--no-chef-check`.' + config[:chef_check] = false + end + validate! Chef::Config.from_file('solo.rb') - check_chef_version unless config[:skip_chef_check] + check_chef_version if config[:chef_check] generate_node_config - librarian_install + librarian_install if config[:librarian] rsync_kitchen add_patches cook unless config[:sync_only] end end def validate! - validate_first_cli_arg_is_a_hostname! + validate_ssh_options! validate_kitchen! end def chef_path Chef::Config.file_cache_path @@ -69,19 +82,33 @@ @chefignore ||= ::Chef::Cookbook::Chefignore.new("./") end # cygwin rsync path must be adjusted to work def adjust_rsync_path(path) - return path unless windows_node? path.gsub(/^(\w):/) { "/cygdrive/#{$1}" } end + def adjust_rsync_path_on_node(path) + return path unless windows_node? + adjust_rsync_path(path) + end + + def adjust_rsync_path_on_client(path) + return path unless windows_client? + adjust_rsync_path(path) + end + + # see http://stackoverflow.com/questions/5798807/rsync-permission-denied-created-directories-have-no-permissions + def rsync_permissions + '--chmod=ugo=rwX' if windows_client? + end + def patch_path Array(Chef::Config.cookbook_path).first + "/chef_solo_patches/libraries" end - def rsync_exclude + def rsync_excludes (%w{revision-deploys tmp '.*'} + chefignore.ignores).uniq end def debug? config[:verbosity] and config[:verbosity] > 0 @@ -107,32 +134,39 @@ @librarian_env ||= Librarian::Chef::Environment.new end def rsync_kitchen time('Rsync kitchen') do - cmd = %Q{rsync -rl --rsh="ssh #{ssh_args}" --delete #{rsync_exclude.collect{ |ignore| "--exclude #{ignore} " }.join} ./ :#{adjust_rsync_path(chef_path)}} - ui.msg cmd if debug? - system! cmd + rsync('./', chef_path, '--delete') end end def add_patches run_portable_mkdir_p(patch_path) Dir[Pathname.new(__FILE__).dirname.join("patches", "*.rb").to_s].each do |patch| time(patch) do - system! %Q{rsync -rl --rsh="ssh #{ssh_args}" #{patch} :#{adjust_rsync_path(patch_path)}} + rsync(patch, patch_path) end end end + def rsync(source_path, target_path, extra_opts = '') + cmd = %Q{rsync -rl #{rsync_permissions} --rsh="ssh #{ssh_args}" #{extra_opts} #{rsync_excludes.collect{ |ignore| "--exclude #{ignore} " }.join} #{adjust_rsync_path_on_client(source_path)} :#{adjust_rsync_path_on_node(target_path)}} + ui.msg cmd if debug? + system! cmd + end + def check_chef_version ui.msg "Checking Chef version..." - result = run_command <<-BASH - export PATH="#{OMNIBUS_EMBEDDED_PATHS.join(":")}:$PATH" - export GEM_PATH="#{OMNIBUS_EMBEDDED_GEM_PATHS.join(":")}:$GEM_PATH" - ruby -rubygems -e "gem 'chef', '#{CHEF_VERSION_CONSTRAINT}'" - BASH - raise "Couldn't find Chef #{CHEF_VERSION_CONSTRAINT} on #{host}. Please run `#{$0} prepare #{ssh_args}` to ensure Chef is installed and up to date." unless result.success? + unless Gem::Requirement.new(CHEF_VERSION_CONSTRAINT).satisfied_by? Gem::Version.new(chef_version) + raise "Couldn't find Chef #{CHEF_VERSION_CONSTRAINT} on #{host}. Please run `knife solo prepare #{ssh_args}` to ensure Chef is installed and up to date." + end + end + + # Parses "Chef: x.y.z" from the chef-solo version output + def chef_version + v = run_command('sudo chef-solo --version').stdout.split(':') + v[0].strip == 'Chef' ? v[1].strip : '' end def cook cmd = "sudo chef-solo -c #{chef_path}/solo.rb -j #{chef_path}/#{node_config}" cmd << " -l debug" if debug?