diff --git a/lib/r10k/action/puppetfile.rb b/lib/r10k/action/puppetfile.rb new file mode 100644 index 000000000..298853d50 --- /dev/null +++ b/lib/r10k/action/puppetfile.rb @@ -0,0 +1,10 @@ +module R10K + module Action + module Puppetfile + require 'r10k/action/puppetfile/cri_runner' + require 'r10k/action/puppetfile/install' + require 'r10k/action/puppetfile/check' + require 'r10k/action/puppetfile/purge' + end + end +end diff --git a/lib/r10k/action/puppetfile/check.rb b/lib/r10k/action/puppetfile/check.rb new file mode 100644 index 000000000..60d9f475a --- /dev/null +++ b/lib/r10k/action/puppetfile/check.rb @@ -0,0 +1,41 @@ +require 'r10k/puppetfile' +require 'r10k/util/setopts' +require 'r10k/errors/formatting' +require 'r10k/logging' + +module R10K + module Action + module Puppetfile + class Check + include R10K::Logging + include R10K::Util::Setopts + + def initialize(opts, argv) + @opts = opts + @argv = argv + + setopts(opts, { + :root => :self, + :moduledir => :self, + :puppetfile => :path, + :trace => :self, + }) + end + + def call + pf = R10K::Puppetfile.new(@root, @moduledir, @path) + + begin + pf.load + $stderr.puts "Syntax OK" + true + rescue R10K::Error => e + $stderr.puts R10K::Errors::Formatting.format_exception(e, @trace) + false + end + end + end + end + end +end + diff --git a/lib/r10k/action/puppetfile/install.rb b/lib/r10k/action/puppetfile/install.rb new file mode 100644 index 000000000..df5004f1e --- /dev/null +++ b/lib/r10k/action/puppetfile/install.rb @@ -0,0 +1,53 @@ +require 'r10k/puppetfile' +require 'r10k/util/setopts' +require 'r10k/errors/formatting' +require 'r10k/logging' + +module R10K + module Action + module Puppetfile + class Install + include R10K::Logging + include R10K::Util::Setopts + + def initialize(opts, argv) + @opts = opts + @argv = argv + + @ok = true + + setopts(opts, { + :root => :self, + :moduledir => :self, + :puppetfile => :path, + :trace => :self, + }) + end + + def call + pf = R10K::Puppetfile.new(@root, @moduledir, @path) + pf.accept(self) + @ok + end + + def visit(type, other, &block) + send("visit_#{type}", other, &block) + rescue => e + logger.error R10K::Errors::Formatting.format_exception(e, @trace) + @ok = false + end + + def visit_puppetfile(pf) + pf.load + yield + pf.purge! + end + + def visit_module(mod) + logger.info "Updating module #{mod.path}" + mod.sync + end + end + end + end +end diff --git a/lib/r10k/action/puppetfile/purge.rb b/lib/r10k/action/puppetfile/purge.rb new file mode 100644 index 000000000..2ce44da10 --- /dev/null +++ b/lib/r10k/action/puppetfile/purge.rb @@ -0,0 +1,37 @@ +require 'r10k/puppetfile' +require 'r10k/util/setopts' +require 'r10k/errors/formatting' +require 'r10k/logging' + +module R10K + module Action + module Puppetfile + class Purge + include R10K::Logging + include R10K::Util::Setopts + + def initialize(opts, argv) + @opts = opts + @argv = argv + + setopts(opts, { + :root => :self, + :moduledir => :self, + :puppetfile => :path, + :trace => :self, + }) + end + + def call + pf = R10K::Puppetfile.new(@root, @moduledir, @path) + pf.load + pf.purge! + true + rescue => e + logger.error R10K::Errors::Formatting.format_exception(e, opts[:trace]) + false + end + end + end + end +end diff --git a/lib/r10k/cli/puppetfile.rb b/lib/r10k/cli/puppetfile.rb index 072dda093..f0d3d8923 100644 --- a/lib/r10k/cli/puppetfile.rb +++ b/lib/r10k/cli/puppetfile.rb @@ -1,5 +1,6 @@ require 'r10k/cli' require 'r10k/puppetfile' +require 'r10k/action/puppetfile' require 'cri' @@ -30,54 +31,25 @@ def self.command usage 'install' summary 'Install all modules from a Puppetfile' - run do |opts, args, cmd| - puppetfile_root = Dir.getwd - puppetfile_path = ENV['PUPPETFILE_DIR'] - puppetfile = ENV['PUPPETFILE'] - - puppetfile = R10K::Puppetfile.new(puppetfile_root, puppetfile_path, puppetfile) - - runner = R10K::TaskRunner.new(:trace => opts[:trace]) - task = R10K::Task::Puppetfile::Sync.new(puppetfile) - runner.append_task task - - runner.run - - exit runner.exit_value - end + # @todo add --moduledir option + # @todo add --puppetfile option + # @todo add --no-purge option + runner R10K::Action::Puppetfile::CriRunner.wrap(R10K::Action::Puppetfile::Install) end end end - self.command.add_command(Install.command) + module Check def self.command @cmd ||= Cri::Command.define do name 'check' usage 'check' summary 'Try and load the Puppetfile to verify the syntax is correct.' - run do |opts,args,cmd| - puppetfile_root = Dir.getwd - puppetfile_path = ENV['PUPPETFILE_DIR'] - puppetfile = ENV['PUPPETFILE'] - - puppetfile = R10K::Puppetfile.new(puppetfile_root, puppetfile_path, puppetfile) - begin - puppetfile.load - rescue SyntaxError, LoadError => e - $stderr.puts "ERROR: Could not parse Puppetfile" - $stderr.puts e.message - if opts[:trace] - $stderr.puts e.backtrace.join("\n") - end - exit 1 - end - puts "Syntax OK" - exit 0 - end + runner R10K::Action::Puppetfile::CriRunner.wrap(R10K::Action::Puppetfile::Check) end end end - self.command.add_command(Check.command) + module Purge def self.command @cmd ||= Cri::Command.define do @@ -85,25 +57,15 @@ def self.command usage 'purge' summary 'Purge unmanaged modules from a Puppetfile managed directory' - run do |opts, args, cmd| - puppetfile_root = Dir.getwd - puppetfile_path = ENV['PUPPETFILE_DIR'] - puppetfile = ENV['PUPPETFILE'] - - puppetfile = R10K::Puppetfile.new(puppetfile_root, puppetfile_path, puppetfile) - - runner = R10K::TaskRunner.new(:trace => opts[:trace]) - task = R10K::Task::Puppetfile::Purge.new(puppetfile) - runner.append_task task - - runner.run - - exit runner.exit_value - end + runner R10K::Action::Puppetfile::CriRunner.wrap(R10K::Action::Puppetfile::Purge) end end end - self.command.add_command(Purge.command) end - self.command.add_command(Puppetfile.command) end + +R10K::CLI.command.add_command(R10K::CLI::Puppetfile.command) + +R10K::CLI::Puppetfile.command.add_command(R10K::CLI::Puppetfile::Install.command) +R10K::CLI::Puppetfile.command.add_command(R10K::CLI::Puppetfile::Check.command) +R10K::CLI::Puppetfile.command.add_command(R10K::CLI::Puppetfile::Purge.command)