Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Add Comment
Please, Sign In to add comment