lib/chef/knife/solo_cook.rb in knife-solo-0.3.0.pre3 vs lib/chef/knife/solo_cook.rb in knife-solo-0.3.0.pre4

- old
+ new

@@ -1,8 +1,7 @@ require 'chef/knife' -require 'knife-solo' require 'knife-solo/ssh_command' require 'knife-solo/node_config_command' require 'knife-solo/tools' class Chef @@ -16,13 +15,15 @@ include KnifeSolo::NodeConfigCommand include KnifeSolo::Tools deps do require 'chef/cookbook/chefignore' + require 'knife-solo' + require 'knife-solo/berkshelf' + require 'knife-solo/librarian' require 'erubis' require 'pathname' - require 'tempfile' KnifeSolo::SshCommand.load_deps KnifeSolo::NodeConfigCommand.load_deps end banner "knife solo cook [USER@]HOSTNAME [JSON] (options)" @@ -38,10 +39,14 @@ option :sync_only, :long => '--sync-only', :description => 'Only sync the cookbook - do not run Chef' + option :berkshelf, + :long => '--no-berkshelf', + :description => 'Skip berks install' + option :librarian, :long => '--no-librarian', :description => 'Skip librarian-chef install' option :why_run, @@ -70,10 +75,11 @@ ui.msg "Running Chef on #{host}..." check_chef_version if config[:chef_check] generate_node_config + berkshelf_install if config_value(:berkshelf, true) librarian_install if config_value(:librarian, true) sync_kitchen generate_solorb cook unless config[:sync_only] end @@ -98,36 +104,38 @@ run_portable_mkdir_p(provisioning_path, '0700') cookbook_paths.each_with_index do |path, i| upload_to_provision_path(path, "/cookbooks-#{i + 1}", 'cookbook_path') end + upload_to_provision_path(node_config, 'dna.json') upload_to_provision_path(nodes_path, 'nodes') upload_to_provision_path(:role_path, 'roles') upload_to_provision_path(:data_bag_path, 'data_bags') upload_to_provision_path(:encrypted_data_bag_secret, 'data_bag_key') end + def expand_path(path) + Pathname.new(path).expand_path + end + def expanded_config_paths(key) - Array(Chef::Config[key]).map { |path| Pathname.new(path).expand_path } + Array(Chef::Config[key]).map { |path| expand_path path } end def cookbook_paths @cookbook_paths ||= expanded_config_paths(:cookbook_path) + [patch_cookbooks_path] end def add_cookbook_path(path) + path = expand_path path cookbook_paths.unshift(path) unless cookbook_paths.include?(path) end def patch_cookbooks_path KnifeSolo.resource('patch_cookbooks') end - def nodes_path - 'nodes' - end - def chefignore @chefignore ||= ::Chef::Cookbook::Chefignore.new("./") end # cygwin rsync path must be adjusted to work @@ -144,10 +152,14 @@ def adjust_rsync_path_on_client(path) return path unless windows_client? adjust_rsync_path(path) end + def rsync_debug + '-v' if debug? + 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 @@ -166,39 +178,20 @@ start = Time.now yield ui.msg "#{msg} finished in #{Time.now - start} seconds" end - def librarian_install - if !File.exist? 'Cheffile' - Chef::Log.debug "Cheffile not found" - elsif !load_librarian - ui.warn "Librarian-Chef could not be loaded" - ui.warn "Please add the librarian-chef gem to your Gemfile or install it manually with `gem install librarian-chef`" - else - ui.msg "Installing Librarian cookbooks..." - Librarian::Action::Resolve.new(librarian_env).run - Librarian::Action::Install.new(librarian_env).run - add_cookbook_path librarian_env.install_path - end + def berkshelf_install + path = KnifeSolo::Berkshelf.new(config, ui).install + add_cookbook_path(path) if path end - def load_librarian - begin - require 'librarian/action' - require 'librarian/chef' - rescue LoadError - false - else - true - end + def librarian_install + path = KnifeSolo::Librarian.new(config, ui).install + add_cookbook_path(path) if path end - def librarian_env - @librarian_env ||= Librarian::Chef::Environment.new - end - def generate_solorb ui.msg "Generating solo config..." template = Erubis::Eruby.new(KnifeSolo.resource('solo.rb.erb').read) write(template.result(binding), provisioning_path + '/solo.rb') end @@ -231,12 +224,14 @@ ensure file.unlink end def rsync(source_path, target_path, extra_opts = '--delete') - 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? + cmd = %Q{rsync -rl #{rsync_debug} #{rsync_permissions} --rsh="ssh #{ssh_args}" #{extra_opts}} + cmd << rsync_excludes.map { |ignore| " --exclude '#{ignore}'" }.join + cmd << %Q{ #{adjust_rsync_path_on_client(source_path)} :#{adjust_rsync_path_on_node(target_path)}} + Chef::Log.debug cmd system! cmd end def check_chef_version ui.msg "Checking Chef version..." @@ -245,16 +240,16 @@ 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 : '' + cmd = %q{sudo chef-solo --version 2>/dev/null | awk '$1 == "Chef:" {print $2}'} + run_command(cmd).stdout.strip end def cook ui.msg "Running Chef..." - cmd = "sudo chef-solo -c #{provisioning_path}/solo.rb -j #{provisioning_path}/#{node_config}" + cmd = "sudo chef-solo -c #{provisioning_path}/solo.rb -j #{provisioning_path}/dna.json" cmd << " -l debug" if debug? cmd << " -N #{config[:chef_node_name]}" if config[:chef_node_name] cmd << " -W" if config[:why_run] cmd << " -o #{config[:override_runlist]}" if config[:override_runlist]