Skip to content

Instantly share code, notes, and snippets.

@timbirk
Last active June 21, 2018 13:32
Show Gist options
  • Save timbirk/b8ec23934b4d5c6c4943d0f3c4105947 to your computer and use it in GitHub Desktop.
Save timbirk/b8ec23934b4d5c6c4943d0f3c4105947 to your computer and use it in GitHub Desktop.
Generate repo / forge modules for itv.yaml file from existing Puppetfile.lock (captures all deps)
#!/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
itvout = []
itvout << "\n# Generated by lp2itv.rb\n# https://gist.github.com/timbirk/b8ec23934b4d5c6c4943d0f3c4105947"
puppet_modules.each do |mod_type, sources|
sources = sources.sort_by { |k, _v| k }
itvout << if mod_type == :git
"\n repo_modules:"
else
"\n #{mod_type}_modules:"
end
sources.each do |mod, data|
case mod_type
when :forge
next if itvout.grep(/#{Regexp.escape(mod)}/).any?
itvout << " #{mod}:"
itvout << " repo: '#{mod}'"
itvout << " ref: '#{data[:version]}'"
when :git
next if itvout.grep(/#{Regexp.escape(data.to_h[:git])}/).any?
itvout << " #{mod}:"
itvout << " repo: '#{data[:git]}'"
itvout << " ref: '#{data[:ref]}'"
end
end
end
puts itvout
@timbirk
Copy link
Author

timbirk commented Jun 21, 2018

I had a need to decompose the existing Puppetfile / Puppetfile.lock into expected yaml(ish) hashes for our itv.yaml files. Rather than start typing out every module / version I re-purposed my lp2r10k.rb script to generate the forge_modules and repo_modules hashes. This doesn't take into account our profile_modules as it's designed to run against non-cp products to help refactor / align them.

Generates output like:

# Generated by lp2itv.rb
# https://gist.github.com/timbirk/b8ec23934b4d5c6c4943d0f3c4105947

    forge_modules:
      3fs-phantomjs:
        repo: '3fs-phantomjs'
        ref: '0.0.8'
      KyleAnderson-consul:
        repo: 'KyleAnderson-consul'
        ref: '1.0.5'
      bashtoni-masq:
        repo: 'bashtoni-masq'
        ref: '0.1.2'
      bashtoni-timezone:
        repo: 'bashtoni-timezone'
        ref: '1.0.0'
      bashtoni-varnish:
        repo: 'bashtoni-varnish'
        ref: '3.0.0'
      bfraser-grafana:
        repo: 'bfraser-grafana'
        ref: '2.5.0'
      camptocamp-archive:
        repo: 'camptocamp-archive'
        ref: '0.9.0'
      ......
      ......
      yelp-uchiwa:
        repo: 'yelp-uchiwa'
        ref: '1.0.1'

    repo_modules:
      ghoneycutt-nfs:
        repo: 'https://github.com/thanandorn/puppet-module-nfs.git'
        ref: 'feature/exports-parameters'
      itv-profile_ssh_ca:
        repo: 'git@github.com:ITV/puppet-profile-ssh_ca.git'
        ref: 'v0.3.5'
      jlambert121-curator:
        repo: 'https://github.com/thanandorn/jlambert121-curator.git'
        ref: 'systemd-timer'
      puppet-mcollective:
        repo: 'https://github.com/ITV/puppet-mcollective.git'
        ref: 'fix/metadata_fudge'
      rtyler-jenkins:
        repo: 'https://github.com/jenkinsci/puppet-jenkins.git'
        ref: '84967d753c7ba244f8fd3305909da175ec1239ce'
      saz-rsyslog:
        repo: 'https://github.com/BashtonLtd/puppet-rsyslog.git'
        ref: 'puppet4'
      thanandorn-dropwizard:
        repo: 'https://github.com/thanandorn/puppet-dropwizard.git'
        ref: 'single-config'
      torrancew-account:
        repo: 'https://github.com/torrancew/puppet-account.git'
        ref: 'v0.1.0'

To ^C^V into itv.yaml for use with the lugus gem / tasks.

@timbirk
Copy link
Author

timbirk commented Jun 21, 2018

You will almost certainly find issues with your modules and the dependencies for our Puppet profiles.

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