Skip to content

Commit 6eebbef

Browse files
committed
inital
1 parent 947ef51 commit 6eebbef

File tree

6 files changed

+143
-0
lines changed

6 files changed

+143
-0
lines changed

Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source 'https://gem.coop'
2+
3+
gem 'rake'
4+
gem 'puppetfile-resolver'
5+
gem 'puppet_forge'
6+
gem 'pry'
7+

Rakefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
task :default do
2+
system("rake -T")
3+
end
4+
5+
desc 'Update the Forge->Source module mappings'
6+
task :update do
7+
require 'puppet_forge'
8+
require 'yaml'
9+
10+
PuppetForge.user_agent = "PuppetfileSourceDereferencer/1.0.0"
11+
12+
data = {}
13+
PuppetForge::Module.all.unpaginated.each do |mod|
14+
data[mod.slug] = mod.releases.first.metadata[:source]
15+
end
16+
17+
File.write('data/cache.yaml', data.to_yaml)
18+
end
19+

bin/puppetfile-source-dereference

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#! /usr/bin/env ruby
2+
3+
require 'puppetfile-source-dereference'
4+
require 'puppetfile-source-dereference/version'
5+
require 'optparse'
6+
7+
options = {
8+
:source => 'Puppetfile',
9+
:output => 'Puppetfile.out',
10+
:dryrun => false,
11+
:mapping => 'https://raw.githubusercontent.com/overlookinfra/puppetfile-source-dereference/refs/heads/main/data/cache.yaml'
12+
}
13+
OptionParser.new do |opts|
14+
Version = PuppetfileSourceDereference::VERSION
15+
opts.banner = "Usage: puppetfile-source-dereference [options]"
16+
17+
opts.on("-s", "--source [FILE]", "Pass the name of a source file (Puppetfile)") do |v|
18+
options[:source] = v
19+
end
20+
opts.on("-o", "--output [FILE]", "Pass the name of an output file (Puppetfile.out)") do |v|
21+
options[:output] = v
22+
end
23+
opts.on("-d", "--[no-]dry-run", "Print output to the terminal instead of a file") do |v|
24+
options[:dryrun] = v
25+
end
26+
opts.on("-m", "--mapping_uri [URI]", "The file or URI with cached Forge module mappings") do |v|
27+
options[:mapping] = v
28+
end
29+
end.parse!
30+
31+
PuppetfileSourceDereference.new(options).resolve

data/cache.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
puppetlabs-puppetdb: https://github.com/puppetlabs/puppetlabs-puppetdb.git
3+
saz-sudo: https://github.com/saz/puppet-sudo
4+
puppetlabs-stdlib: https://github.com/puppetlabs/puppetlabs-stdlib
5+
puppet-archive: https://github.com/voxpupuli/puppet-archive
6+
puppetlabs-concat: https://github.com/puppetlabs/puppetlabs-concat
7+
puppetlabs-ntp: https://github.com/puppetlabs/puppetlabs-ntp
8+
elastic-elastic_stack: https://github.com/elastic/puppet-elastic-stack
9+
puppet-yum: https://github.com/voxpupuli/puppet-yum.git
10+
tracywebtech-pip: https://github.com/TracyWebTech/puppet-pip
11+
puppet-collectd: https://github.com/voxpupuli/puppet-collectd
12+
threatstack-threatstack: https://github.com/threatstack/threatstack-puppet
13+
rmueller-cron: https://github.com/roman-mueller/rmueller-cron
14+
camptocamp-archive: https://github.com/camptocamp/puppet-archive
15+
puppet-nginx: https://github.com/voxpupuli/puppet-nginx.git
16+
Slashbunny-phpfpm: https://github.com/Slashbunny/puppet-phpfpm
17+
pdxcat-nrpe: https://github.com/pdxcat/puppet-module-nrpe
18+
stahnma-epel: https://github.com/stahnma/puppet-module-epel
19+
puppetlabs-docker: https://github.com/puppetlabs/puppetlabs-docker
20+
puppetlabs-apt: https://github.com/puppetlabs/puppetlabs-apt
21+
garethr-docker: https://github.com/garethr/garethr-docker.git
22+
puppetlabs-stdlib: https://github.com/puppetlabs/puppetlabs-stdlib.git
23+
puppetlabs-apache: https://github.com/puppetlabs/puppetlabs-apache.git
24+
binford2k-itemize: https://github.com/binford2k/binford2k-itemize.git
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'puppetfile-source-dereference'
2+
require "puppetfile-resolver"
3+
require "puppetfile-resolver/puppetfile/parser/r10k_eval"
4+
require 'puppet_forge'
5+
require 'yaml'
6+
7+
class PuppetfileSourceDereference
8+
attr_accessor :source, :output, :dryrun, :mapping
9+
10+
def initialize(options = {})
11+
[:source, :output, :dryrun, :mapping,].each do |key|
12+
send("#{key}=", options[key])
13+
end
14+
15+
@output = File.open(@output, 'w+') if @output.is_a? String
16+
end
17+
18+
def resolve
19+
puppetfile = PuppetfileResolver::Puppetfile::Parser::R10KEval.parse(File.read(@source))
20+
mapping = YAML.load(open(@mapping).read)
21+
22+
unless puppetfile.valid?
23+
logger.error("Puppetfile source is not valid")
24+
puppetfile.validation_errors.each { |err| logger.error(err) }
25+
return false
26+
end
27+
28+
File.open(@output, 'w') do |output|
29+
puppetfile.modules.each do |mod|
30+
if mod.module_type == :git
31+
output.write <<~EOF
32+
mod '#{mod.title}',
33+
:git => '#{mod.remote}',
34+
:ref => '#{mod.ref}'
35+
EOF
36+
elsif mapping.include? mod.title
37+
source = mapping[mod.title]
38+
39+
if mod.version.is_a? String
40+
output.write <<~EOF
41+
mod '#{mod.title}',
42+
:git => '#{source}',
43+
:ref => '#{mod.version.delete_prefix('=')}'
44+
EOF
45+
else
46+
output.write <<~EOF
47+
mod '#{mod.title}',
48+
:git => '#{source}'
49+
EOF
50+
end
51+
else
52+
output.write "mod '#{mod.title}', #{version}\n"
53+
end
54+
end
55+
end
56+
end
57+
58+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class PuppetfileSourceDereference
2+
VERSION = '0.0.1'
3+
end
4+

0 commit comments

Comments
 (0)