Skip to content

Commit

Permalink
Merge pull request #33 from ZeroPointEnergy/refactor_tasks
Browse files Browse the repository at this point in the history
Refactor tasks
  • Loading branch information
bastelfreak authored Jun 8, 2019
2 parents 4e29992 + 868f69c commit 1b93ffb
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 111 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Available settings are:
| puppetfile_path | Directroy where the Puppetfile is (Default: basedir) |
| puppetfile_name | The Puppetfile name (Default: basedir/Puppetfile) |
| force | Overwrite locally changed files on install (Default: false) |
| purge | Purge unmanaged modules from the modulesdir (Default: false) |

## Rake tasks

Expand Down
122 changes: 11 additions & 111 deletions lib/ra10ke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,136 +2,36 @@
require 'rake/tasklib'
require 'ra10ke/version'
require 'ra10ke/solve'
require 'ra10ke/syntax'
require 'ra10ke/dependencies'
require 'ra10ke/install'
require 'git'
require 'semverse'

module Ra10ke
class RakeTask < ::Rake::TaskLib
include Ra10ke::Solve
include Ra10ke::Syntax
include Ra10ke::Dependencies
include Ra10ke::Install

attr_accessor :basedir, :moduledir, :puppetfile_path, :puppetfile_name, :force
attr_accessor :basedir, :moduledir, :puppetfile_path, :puppetfile_name, :force, :purge

def initialize(*args)
@basedir = Dir.pwd
@moduledir = nil
@puppetfile_path = nil
@puppetfile_name = nil
@force = nil
@purge = false

yield(self) if block_given?

namespace :r10k do
define_task_solve_dependencies(*args)

desc "Print outdated forge modules"
task :dependencies do
require 'r10k/puppetfile'
require 'puppet_forge'

PuppetForge.user_agent = "ra10ke/#{Ra10ke::VERSION}"
puppetfile = get_puppetfile
puppetfile.load!
PuppetForge.host = puppetfile.forge if puppetfile.forge =~ /^http/

# ignore file allows for "don't tell me about this"
ignore_modules = []
if File.exist?('.r10kignore')
ignore_modules = File.readlines('.r10kignore').each {|l| l.chomp!}
end

puppetfile.modules.each do |puppet_module|
next if ignore_modules.include? puppet_module.title
if puppet_module.class == R10K::Module::Forge
module_name = puppet_module.title.gsub('/', '-')
forge_version = PuppetForge::Module.find(module_name).current_release.version
installed_version = puppet_module.expected_version
if installed_version != forge_version
puts "#{puppet_module.title} is OUTDATED: #{installed_version} vs #{forge_version}"
end
end

if puppet_module.class == R10K::Module::Git
# use helper; avoid `desired_ref`
# we do not want to deal with `:control_branch`
ref = puppet_module.version
next unless ref

remote = puppet_module.instance_variable_get(:@remote)
remote_refs = Git.ls_remote(remote)

# skip if ref is a branch
next if remote_refs['branches'].key?(ref)

if remote_refs['tags'].key?(ref)
# there are too many possible versioning conventions
# we have to be be opinionated here
# so semantic versioning (vX.Y.Z) it is for us
# as well as support for skipping the leading v letter
tags = remote_refs['tags'].keys
latest_tag = tags.map do |tag|
begin
Semverse::Version.new tag
rescue Semverse::InvalidVersionFormat
# ignore tags that do not comply to semver
nil
end
end.select { |tag| !tag.nil? }.sort.last.to_s.downcase
latest_ref = tags.detect { |tag| [tag.downcase, "v#{tag.downcase}"].include?(latest_tag) }
latest_ref = 'undef (tags do not match semantic versioning)' if latest_ref.nil?
elsif ref.match(/^[a-z0-9]{40}$/)
# for sha just assume head should be tracked
latest_ref = remote_refs['head'][:sha]
else
raise "Unable to determine ref type for #{puppet_module.title}"
end

puts "#{puppet_module.title} is OUTDATED: #{ref} vs #{latest_ref}" if ref != latest_ref
end
end
end

desc "Syntax check Puppetfile"
task :syntax do
require 'r10k/action/puppetfile/check'

puppetfile = R10K::Action::Puppetfile::Check.new({
:root => @basedir,
:moduledir => @moduledir,
:puppetfile => @puppetfile_path
}, '')

unless puppetfile.call
abort("Puppetfile syntax check failed")
end
end

desc "Install modules specified in Puppetfile"
task :install do
require 'r10k/puppetfile'

puppetfile = get_puppetfile
puppetfile.load!

puts "Processing Puppetfile for fixtures"
puppetfile.modules.each do |mod|
if mod.status == :insync
puts "Skipping #{mod.name} (#{mod.version}) already in sync"
else
if mod.status == :absent
msg = "installed #{mod.name}"
else
msg = "updated #{mod.name} from #{mod.version} to"
end
mod.sync
if mod.status != :insync
puts "Failed to sync #{mod.name}".red
else
puts "Successfully #{msg} #{mod.version}".green
end
end
end
end

define_task_syntax(*args)
define_task_dependencies(*args)
define_task_install(*args)
end
end

Expand Down
72 changes: 72 additions & 0 deletions lib/ra10ke/dependencies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module Ra10ke::Dependencies
def define_task_dependencies(*_args)
desc "Print outdated forge modules"
task :dependencies do
require 'r10k/puppetfile'
require 'puppet_forge'

PuppetForge.user_agent = "ra10ke/#{Ra10ke::VERSION}"
puppetfile = get_puppetfile
puppetfile.load!
PuppetForge.host = puppetfile.forge if puppetfile.forge =~ /^http/

# ignore file allows for "don't tell me about this"
ignore_modules = []
if File.exist?('.r10kignore')
ignore_modules = File.readlines('.r10kignore').each {|l| l.chomp!}
end

puppetfile.modules.each do |puppet_module|
next if ignore_modules.include? puppet_module.title
if puppet_module.class == R10K::Module::Forge
module_name = puppet_module.title.gsub('/', '-')
forge_version = PuppetForge::Module.find(module_name).current_release.version
installed_version = puppet_module.expected_version
if installed_version != forge_version
puts "#{puppet_module.title} is OUTDATED: #{installed_version} vs #{forge_version}"
end
end

if puppet_module.class == R10K::Module::Git
# use helper; avoid `desired_ref`
# we do not want to deal with `:control_branch`
ref = puppet_module.version
next unless ref

remote = puppet_module.instance_variable_get(:@remote)
remote_refs = Git.ls_remote(remote)

# skip if ref is a branch
next if remote_refs['branches'].key?(ref)

if remote_refs['tags'].key?(ref)
# there are too many possible versioning conventions
# we have to be be opinionated here
# so semantic versioning (vX.Y.Z) it is for us
# as well as support for skipping the leading v letter
tags = remote_refs['tags'].keys
latest_tag = tags.map do |tag|
begin
Semverse::Version.new tag[/\Av?(.*)\Z/, 1]
rescue Semverse::InvalidVersionFormat
# ignore tags that do not comply to semver
nil
end
end.select { |tag| !tag.nil? }.sort.last.to_s.downcase
latest_ref = tags.detect { |tag| tag[/\Av?(.*)\Z/, 1] == latest_tag }
latest_ref = 'undef (tags do not match semantic versioning)' if latest_ref.nil?
elsif ref.match(/^[a-z0-9]{40}$/)
# for sha just assume head should be tracked
latest_ref = remote_refs['head'][:sha]
else
raise "Unable to determine ref type for #{puppet_module.title}"
end

puts "#{puppet_module.title} is OUTDATED: #{ref} vs #{latest_ref}" if ref != latest_ref
end
end
end

end
end

32 changes: 32 additions & 0 deletions lib/ra10ke/install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Ra10ke::Install
def define_task_install(*_args)
desc "Install modules specified in Puppetfile"
task :install do
require 'r10k/puppetfile'

puppetfile = get_puppetfile
puppetfile.load!

puts "Processing Puppetfile for fixtures"
puppetfile.modules.each do |mod|
if mod.status == :insync
puts "Skipping #{mod.name} (#{mod.version}) already in sync"
else
if mod.status == :absent
msg = "installed #{mod.name}"
else
msg = "updated #{mod.name} from #{mod.version} to"
end
mod.sync
if mod.status != :insync
puts "Failed to sync #{mod.name}".red
else
puts "Successfully #{msg} #{mod.version}".green
end
end
end

puppetfile.purge! if @purge
end
end
end
18 changes: 18 additions & 0 deletions lib/ra10ke/syntax.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Ra10ke::Syntax
def define_task_syntax(*_args)
desc "Syntax check Puppetfile"
task :syntax do
require 'r10k/action/puppetfile/check'

puppetfile = R10K::Action::Puppetfile::Check.new({
:root => @basedir,
:moduledir => @moduledir,
:puppetfile => @puppetfile_path
}, '')

unless puppetfile.call
abort("Puppetfile syntax check failed")
end
end
end
end

0 comments on commit 1b93ffb

Please sign in to comment.