Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Souza authored and Josh Souza committed Oct 14, 2014
1 parent 7377e01 commit fe0636c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
puppet_fixture_helper
Puppet Fixture Helper
=====================

A ruby script you can add to your build process for Puppet modules that will base your .fixtures.yml on a centralized Puppetfile repository.

Can be added to a Rakefile with something like:

```ruby
desc 'Prepare fixtures'
RSpec::Core::RakeTask.new(:fixtures) do |t|
t.pattern = 'spec/fixture_helper.rb'
t.rspec_opts = ['--color']
end
```

This is designed to support the ideal r10k workflow which utilizes a single repository for managing your module environments. It does actually check out the puppetfile repo locally, which might not be fully necessary, and the '.puppetfile' directory should be added to your '.gitignore', but beyond that it's pretty simple.
21 changes: 21 additions & 0 deletions dependencies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
# All dependencies are pulled from the latest dev version in the Puppetfile repo
# unless you specify a repo and version as shown below
config:
puppetfile_repo: https://github.com/your/puppetfile.git
branch: v0.0.1
# To use the regexp, you must have one group that matches to an int/float
# As of right now it does NOT support semver (so 1.2 will be chosen over 1.19)
# I hope to fix this in the future, but it's a low priority for me
# branch_re: v?([0-9]+\.?[0-9]?)

dependencies:
#- base:
# repo: 'https://github.com/puppetlabs/puppetlabs-apache.git'
# ref: '1.1.1'
- stdlib

symlinks:
your_module_name/files: "../../../../files"
your_module_name/manifests: "../../../../manifests"
your_module_name/modules: "../../../../modules"
84 changes: 84 additions & 0 deletions spec/fixture_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Puppet Fixture Helper
# Repo: https://github.com/joshsouza/puppet_fixture_helper
# Author: Josh Souza <[email protected]>

require 'yaml'
require 'r10k/puppetfile'
require 'git'

config = YAML.load_file('dependencies.yml')
repopath = config['config']['puppetfile_repo']
branch = config['config']['branch'] || nil
branch_re = config['config']['branch_re'] || "v?([0-9]+\.?[0-9]?)"
branch_re = Regexp.new branch_re
repodir = '.puppetfile'

requested_dependencies=config['dependencies'].sort_by { |key|
if key.class == Hash then
result=key.keys[0]
else
result=key
end
}

begin
repo = Git.clone(repopath,repodir)
puts "Loaded Puppetfile repository from #{repopath}"
rescue
repo = Git.open(repodir)
repo.pull
end

branches=repo.branches.remote
# If it's found, this will change it to the actual branch
# If it's not found, it'll be nil
branch = branches.find{ |i| i.name==branch}

if(branch == nil) then
branches = branches.select{ |i| i.name[branch_re] }
highest_dev_branch=branches.max_by{|k,v| k.name.gsub(branch_re,'\1').to_f}
branch = highest_dev_branch
end
if(branch == nil) then
puts "Unable to find requested Puppetfile branch, or apply the regex."
exit
end
puts "Using Puppetfile #{branch.name} for fixture defaults"
branch.checkout

puppet_mods = R10K::Puppetfile.new(repodir)
puppet_mods = puppet_mods.load()
available_mods = {}
puppet_mods.each do |i|
available_mods[i.name]={
'repo'=>i.instance_variable_get("@remote").to_s,
'ref'=>i.instance_variable_get("@ref").to_s
};
end


actual_dependencies = {}

requested_dependencies.each do |i|
if i.class == Hash then
actual_dependencies[i.keys[0]]=i.values[0]
else
if available_mods.has_key?(i) then
actual_dependencies[i]=available_mods[i]
else
puts "Unable to provide '#{i}' module from #{highest_dev_branch.name}"
end
end
end

fixtures = {
"fixtures" => {
"repositories" => actual_dependencies,
"symlinks" => config['symlinks']
}
}
File.open('.fixtures.yml','w') { |file|
file.write("# For puppetlabs_spec_helper documentation - see http://github.com/puppetlabs/puppetlabs_spec_helper")
file.write(fixtures.to_yaml)
}
puts "Fixtures updated"

0 comments on commit fe0636c

Please sign in to comment.