Skip to content

Commit

Permalink
[cli/puppetfile] Extract puppetfile actions
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienthebo committed Dec 1, 2014
1 parent 27716f6 commit 3b8204d
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 53 deletions.
10 changes: 10 additions & 0 deletions lib/r10k/action/puppetfile.rb
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions lib/r10k/action/puppetfile/check.rb
Original file line number Diff line number Diff line change
@@ -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

53 changes: 53 additions & 0 deletions lib/r10k/action/puppetfile/install.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions lib/r10k/action/puppetfile/purge.rb
Original file line number Diff line number Diff line change
@@ -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
68 changes: 15 additions & 53 deletions lib/r10k/cli/puppetfile.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'r10k/cli'
require 'r10k/puppetfile'
require 'r10k/action/puppetfile'

require 'cri'

Expand Down Expand Up @@ -30,80 +31,41 @@ 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
name 'purge'
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)

0 comments on commit 3b8204d

Please sign in to comment.