-
-
Save amosshapira/5856072 to your computer and use it in GitHub Desktop.
This file contains 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
# | |
# file_content.rb | |
# | |
# Amos Shapira: cloned from exists.rb at https://gist.github.com/chrisleavoy/5312010, | |
# returns the content of the file found | |
# | |
# James Fellows 8/8/12: cloned from git://gist.github.com/1160472.git then | |
# modified to resolve puppet:/// paths | |
# | |
# Copyright 2011 Puppet Labs Inc. | |
# Copyright 2011 Krzysztof Wilczynski | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
module Puppet::Parser::Functions | |
newfunction(:file_content, :type => :rvalue, :doc => <<-EOS | |
Returns the content of the file if it is found on Puppet Master. | |
Prototype: | |
file_content(x) | |
Where x is a file name. | |
The function will also look in the puppetmaster modules directory if the | |
file path is relative rather than absolute, e.g. 'puppet:///modules/my_module/file' | |
An error will be thrown if the a module by that name doesn't exist. | |
EOS | |
) do |arguments| | |
# | |
# This is to ensure that whenever we call this function from within | |
# the Puppet manifest or alternatively from a template it will always | |
# do the right thing ... | |
# | |
arguments = arguments.shift if arguments.first.is_a?(Array) | |
raise Puppet::ParseError, "file_conent(): Wrong number of arguments " + | |
"given (#{arguments.size} for 1)" if arguments.size < 1 | |
file = arguments.shift | |
raise Puppet::ParseError, 'file_content(): Requires a string type ' + | |
'to work with' unless file.is_a?(String) | |
if file.slice!('puppet:///') | |
# Perform relative lookup in modules/files dir. | |
# strip off the modules prefix too if it's there | |
file.slice!('modules/') | |
env = compiler.environment.to_s | |
mod_name, file = file.split(File::SEPARATOR, 2) | |
mod = Puppet::Module.find(mod_name, env) | |
raise Puppet::Error, "file_content(): invalid module name #{mod_name}" unless mod | |
path = mod.path | |
file = File.join(path, "files", file) | |
else | |
file = File.expand_path(file) | |
end | |
result = File.read(file) | |
end | |
end | |
# vim: set ts=2 sw=2 et : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've created a clone of this called "file_content" which returns the content of the file. This is useful because file() doesn't seem to work with relative paths, and I need them in order to work with fileserver mount points.