Created
November 22, 2020 20:53
-
-
Save ekohl/6d22cd2abc30dbf743071ebdcde04b0f to your computer and use it in GitHub Desktop.
Rakefile to deploy Puppet environment
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
require 'rake/clean' | |
require 'puppet/modulebuilder' | |
# Set defaults for the deployment step | |
DEFAULT_SERVER='puppet.example.com' | |
DEFAULT_DIRECTORY="/etc/puppetlabs/code/environments/production" | |
# What to copy | |
MODULE_DIRS = ['modules'] | |
FILES_TO_COPY = FileList['environment.conf', 'hiera.yaml', 'data/**/*.yaml'] | |
# Where to stage | |
STAGING_DIR = 'target' | |
CLOBBER.include [STAGING_DIR] | |
TO_BUILD = [] | |
FILES_TO_COPY.each do |path| | |
target = File.join(STAGING_DIR, path) | |
parent = File.dirname(target) | |
TO_BUILD << target | |
directory parent | |
file target => [path, parent] do |t| | |
cp t.prerequisites.first, t.name, preserve: true | |
end | |
end | |
MODULE_DIRS.each do |module_dir| | |
parent = File.join(STAGING_DIR, module_dir) | |
CLEAN.include [parent] | |
directory parent | |
file parent => module_dir do |t| | |
FileUtils.touch t.name, mtime: File.mtime(t.prerequisites.first) | |
end | |
Dir[File.join(module_dir, '*', 'metadata.json')].each do |metadata| | |
dir = File.dirname(metadata) | |
builder = Puppet::Modulebuilder::Builder.new(dir) | |
target = File.join(parent, builder.metadata['name'].split('-').last) | |
TO_BUILD << target | |
file target => [dir, parent] do |t| | |
source, parent = t.prerequisites | |
builder = Puppet::Modulebuilder::Builder.new(source, parent) | |
# Hack around and set the target | |
builder.instance_variable_set(:@release_name, File.basename(t.name)) | |
puts "Building #{builder.build_dir}" | |
builder.create_build_dir | |
builder.stage_module_in_build_dir | |
end | |
end | |
end | |
desc "Build target directory" | |
task build: TO_BUILD | |
desc "Deploy to a directory on a server" | |
task :deploy, [:server, :directory] => [:clean, :build] do |t, args| | |
args.with_defaults(:server => DEFAULT_SERVER, :directory => DEFAULT_DIRECTORY) | |
rsync_target = "#{args[:server]}:#{args[:directory]}" | |
sh 'rsync', '--archive', '--verbose', '--delete', "#{STAGING_DIR}/", rsync_target | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment