Created
August 29, 2018 11:58
-
-
Save raphink/f4ec215272fdeff091407859e482497a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################### | |
# This file is managed in puppetmaster-common # | |
# Do not edit locally # | |
############################################### | |
<%= require 'augeas' | |
basedir = File.expand_path('..', File.dirname(__FILE__)) | |
base_pf = File.expand_path(File.join(basedir, 'Puppetfile')) | |
base_pf_content = File.read(base_pf) | |
lens_dir = File.expand_path(File.join(basedir, 'lenses')) | |
def mod_regexp(name) | |
"*[label()!='#comment' and .=~regexp('([^/]+/)?#{name}')]" | |
end | |
Augeas.open(nil, lens_dir, Augeas::NO_MODL_AUTOLOAD) do |aug| | |
aug.set('/input', base_pf_content) | |
unless aug.text_store('Puppetfile.lns', '/input', '/parsed') | |
msg = aug.get('/augeas//error') | |
fail "Failed to parse common Puppetfile: #{msg}" | |
end | |
aug.set('/augeas/context', '/parsed') | |
all_modules = aug.match('*[label()!="#comment"]').map { |m| aug.get(m).split(%r{[/-]}).last } | |
whitelist = @configs['modules'].keys if @configs['modules'] | |
not_in_all = whitelist - all_modules if whitelist | |
fail "Module(s) #{not_in_all.join(', ')} not found in common Puppetfile" if not_in_all and !not_in_all.empty? | |
# Remove unnecessary modules | |
(all_modules - whitelist).each do |m| | |
aug.rm(mod_regexp(m)) | |
end if whitelist | |
# Amend | |
modified = @configs['modules'].reject { |m, v| v.nil? } if @configs['modules'] | |
modified.each do |m, c| | |
aug.set(mod_regexp(m), "#{c['user']}/#{m}") if c['user'] | |
if c['version'] | |
aug.rm("#{mod_regexp(m)}/git") | |
aug.rm("#{mod_regexp(m)}/ref") | |
aug.set("#{mod_regexp(m)}/@version", c['version']) | |
else | |
aug.rm("#{mod_regexp(m)}/@version") | |
aug.set("#{mod_regexp(m)}/git", c['git']) if c['git'] | |
aug.set("#{mod_regexp(m)}/ref", c['ref']) if c['ref'] | |
end | |
end if modified | |
aug.text_retrieve('Puppetfile.lns', '/input', '/parsed', '/output') | |
unless aug.match('/augeas/text/parsed/error').empty? | |
fail "Failed to generate Puppetfile: #{aug.get('/augeas/text/parsed/error')} | |
#{aug.get('/augeas/text/parsed/error/message')}" | |
end | |
aug.get('/output') | |
end -%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment