Allow code_manager profile to not error out on first run

Prior to this commit, the code manger profile could not complete
on the first run because the file function would error out

I implemented a new version of the file function that returns
nothing when the file does not exist instead of erroring out which
allows me to gate creating the webhook on whether there is content
in the file.

As a result this means that it takes 2 runs to get everything setup
but this is preferable over having to manually intervene in some
other way if the token file doesn't exist.
This commit is contained in:
Nick Walker 2015-11-15 10:44:53 -08:00
parent 2d7a9a72e1
commit b71ab8d42e
2 changed files with 43 additions and 4 deletions

View File

@ -0,0 +1,36 @@
require 'puppet/file_system'
Puppet::Parser::Functions::newfunction(
:no_fail_file, :arity => -2, :type => :rvalue,
:doc => "Loads a file from a module and returns its contents as a string.
This is a replacement to the file function that returns nothing
if the file specified cannot be found instead of erroring out.
The argument to this function should be a `<MODULE NAME>/<FILE>`
reference, which will load `<FILE>` from a module's `files`
directory. (For example, the reference `mysql/mysqltuner.pl` will load the
file `<MODULES DIRECTORY>/mysql/files/mysqltuner.pl`.)
This function can also accept:
* An absolute path, which can load a file from anywhere on disk.
* Multiple arguments, which will return the contents of the **first** file
found, skipping any files that don't exist.
"
) do |vals|
path = nil
vals.each do |file|
found = Puppet::Parser::Files.find_file(file, compiler.environment)
if found && Puppet::FileSystem.exist?(found)
path = found
break
end
end
if path
Puppet::FileSystem.read_preserve_line_endings(path)
else
nil
end
end

View File

@ -42,11 +42,14 @@ class profile::code_manager {
} }
if !empty($gms_api_token) {
#this file cannont be read until the next run after the above exec #this file cannont be read until the next run after the above exec
#because the file function runs on the master not on the agent #because the file function runs on the master not on the agent
$rbac_token = parsejson(file($token_filename))['token'] #so the file doesn't exist at the time the function is run
$rbac_token_file_contents = no_fail_file($token_filename)
if !empty($gms_api_token) and !empty($rbac_token_file_contents) {
$rbac_token = parsejson($rbac_token_file_contents)['token']
$code_manager_webhook_type = $git_management_system ? { $code_manager_webhook_type = $git_management_system ? {
'gitlab' => 'github', 'gitlab' => 'github',