Skip to content

Instantly share code, notes, and snippets.

@ekohl
Created November 22, 2020 20:53
Show Gist options
  • Save ekohl/6d22cd2abc30dbf743071ebdcde04b0f to your computer and use it in GitHub Desktop.
Save ekohl/6d22cd2abc30dbf743071ebdcde04b0f to your computer and use it in GitHub Desktop.

Revisions

  1. ekohl created this gist Nov 22, 2020.
    66 changes: 66 additions & 0 deletions Rakefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    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