Skip to content

Instantly share code, notes, and snippets.

@timbirk
Last active August 14, 2018 14:55
Show Gist options
  • Save timbirk/0308d8128927ec0f66011d7726980a9e to your computer and use it in GitHub Desktop.
Save timbirk/0308d8128927ec0f66011d7726980a9e to your computer and use it in GitHub Desktop.
A ruby script to generate a complete R10k ready Puppetfile from a librarian puppet style file.
#!/usr/bin/env ruby
require 'librarian/puppet'
lockfile = Librarian::Puppet::Lockfile.new(
Librarian::Puppet::Environment.new, 'Puppetfile.lock'
)
puppet_modules = {}
lockfile.load(File.read(lockfile.path)).manifests.each do |mod|
mod_type = mod.source.class.name.split('::').last.downcase.to_sym
puppet_modules[mod_type] = {} unless puppet_modules.key?(mod_type)
puppet_modules[mod_type][mod.name.to_sym] = {}
case mod_type
when :forge
puppet_modules[mod_type][mod.name.to_sym][:version] = mod.version.to_s
when :git
puppet_modules[mod_type][mod.name.to_sym][:git] = mod.source.uri
puppet_modules[mod_type][mod.name.to_sym][:ref] = mod.source.ref
end
end
puppetfile = []
File.readlines('Puppetfile').each do |line|
break if line =~ /#{Regexp.escape("# Puppetfile.lock dependencies")}/
puppetfile << line
end
puppetfile.pop if puppetfile.last == "\n"
puppetfile << "\n# Puppetfile.lock dependencies"
puppet_modules.each do |mod_type, sources|
sources = sources.sort_by { |k, _v| k }
puppetfile << "\n# #{mod_type} modules"
sources.each do |mod, data|
case mod_type
when :forge
next if puppetfile.grep(/#{Regexp.escape(mod)}/).any?
puppetfile << "mod '#{mod}', '#{data[:version]}'"
when :git
next if puppetfile.grep(/#{Regexp.escape(data.to_h[:git])}/).any?
puppetfile << "mod '#{mod}',"
puppetfile << " :git => '#{data[:git]}',"
puppetfile << " :ref => '#{data[:ref]}'"
end
end
end
puts puppetfile
@madAndroid
Copy link

Impressive!! From 2 minutes down to 5 seconds?? that's an amazing improvement

@timbirk
Copy link
Author

timbirk commented Mar 5, 2018

That is on a MacBook Pro with a Core i7. Of course. Mileage may vary when it comes to EC2 instances which generally have less powerful CPU’s and are prone to context switching due to a lower number of cores. Taking advantage of g10k’s concurrency (downloads, gz extracts) should still see an impressive improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment