Skip to content

Instantly share code, notes, and snippets.

@tlehman
Created August 20, 2024 18:46
Show Gist options
  • Save tlehman/db41b33247325e2cb559ed283ebfc8fc to your computer and use it in GitHub Desktop.
Save tlehman/db41b33247325e2cb559ed283ebfc8fc to your computer and use it in GitHub Desktop.
Bump Puppetfile module versions to latest
# Bump the module versions in the Puppetfile to the latest version
# published on https://forge.puppet.com
#
# How to use: `cd` to the directory where your Puppetfile is, then run `ruby bump-mods.rb` and then check your git diff
# to verify that the upgrades look correct.
require 'open-uri'
require 'rss'
def parse_puppetfile_get_mod_versions
module_re = /^mod 'puppetlabs-([a-z\_\-]+)', *'([0-9\.]+)'$/i
versions = {}
File.foreach('Puppetfile') do |line|
match = module_re.match(line)
if match
versions[match[1].strip] = match[2].strip
end
end
versions
end
def latest_version_for_module(mod)
url = "https://forge.puppet.com/modules/puppetlabs/#{mod}/rss"
feed = RSS::Parser.parse(URI.open(url))
feed.items&.first&.description
end
# get modules to update
versions_old = parse_puppetfile_get_mod_versions
versions_new = {}
versions_old.each do |mod, ver|
ver_new = latest_version_for_module(mod)
if !ver_new.nil? && (ver != ver_new)
versions_new[mod] = ver_new
end
end
def puppetfile_set_mod_versions!(versions_new)
module_re = /^mod 'puppetlabs-([a-z\_\-]+)', *'([0-9\.]+)'$/i
lines = File.open('Puppetfile').readlines
# update lines
lines.each do |line|
if line.start_with?("mod 'puppetlabs")
match = module_re.match(line)
if match
mod = match[1].strip
ver_old = match[2].strip
ver_new = versions_new[mod]
if !ver_new.nil?
line.gsub!(/'([0-9\.]+)'/, "'#{ver_new}'")
puts "Updated #{mod} from #{ver_old} to #{ver_new}"
end
end
end
end
# update Puppetfile
File.open('Puppetfile', 'w').puts(lines.join)
end
# Update the Puppetfile with the new versions
puppetfile_set_mod_versions!(versions_new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment