puppet-puppet/templates/server/post-receive.erb @ 20dd5bef
076d79da | Ohad Levy | #!/usr/bin/env ruby
|
|
require 'fileutils'
|
|||
# Set this to where you want to keep your environments
|
|||
06bc94b2 | Greg Sutcliffe | ENVIRONMENT_BASEDIR = "<%= scope.lookupvar("puppet::server::envs_dir") %>"
|
|
076d79da | Ohad Levy | ||
# post-receive hooks set GIT_DIR to the current repository. If you want to
|
|||
# clone from a non-local repository, set this to the URL of the repository,
|
|||
# such as git@git.host:puppet.git
|
|||
SOURCE_REPOSITORY = File.expand_path(ENV['GIT_DIR'])
|
|||
b05cfed0 | Ohad Levy | # Mapping of branches to directories. In many cases, the master branch is
|
|
# checked out to the 'development' environment.
|
|||
BRANCH_MAP = {
|
|||
# This will clone/pull the master branch into the development puppet environment
|
|||
# "master" => "development",
|
|||
}
|
|||
076d79da | Ohad Levy | # The git_dir environment variable will override the --git-dir, so we remove it
|
|
b05cfed0 | Ohad Levy | # to allow us to create new directories cleanly.
|
|
076d79da | Ohad Levy | ENV.delete('GIT_DIR')
|
|
# Ensure that we have the underlying directories, otherwise the later commands
|
|||
# may fail in somewhat cryptic manners.
|
|||
unless File.directory? ENVIRONMENT_BASEDIR
|
|||
puts %Q{#{ENVIRONMENT_BASEDIR} does not exist, cannot create environment directories.}
|
|||
exit 1
|
|||
end
|
|||
# You can push multiple refspecs at once, like 'git push origin branch1 branch2',
|
|||
# so we need to handle each one.
|
|||
$stdin.each_line do |line|
|
|||
oldrev, newrev, refname = line.split(" ")
|
|||
# Determine the branch name from the refspec we're received, which is in the
|
|||
b05cfed0 | Ohad Levy | # format refs/heads/<branch>, and make sure that it doesn't have any possibly
|
|
076d79da | Ohad Levy | # dangerous characters
|
|
branchname = refname.sub(%r{^refs/heads/(.*$)}) { $1 }
|
|||
b05cfed0 | Ohad Levy | if branchname =~ /[\W]/
|
|
puts %Q{Branch "#{branchname}" contains non-word characters, ignoring it.}
|
|||
076d79da | Ohad Levy | next
|
|
end
|
|||
b05cfed0 | Ohad Levy | if BRANCH_MAP[branchname] != nil
|
|
environment_name = BRANCH_MAP[branchname]
|
|||
environment_path = "#{ENVIRONMENT_BASEDIR}/#{BRANCH_MAP[branchname]}"
|
|||
else
|
|||
environment_name = branchname
|
|||
environment_path = "#{ENVIRONMENT_BASEDIR}/#{branchname}"
|
|||
end
|
|||
076d79da | Ohad Levy | ||
if newrev =~ /^0+$/
|
|||
# We've received a push with a null revision, something like 000000000000,
|
|||
# which means that we should delete the given branch.
|
|||
b05cfed0 | Ohad Levy | puts "Deleting existing environment #{environment_name}"
|
|
076d79da | Ohad Levy | if File.directory? environment_path
|
|
FileUtils.rm_rf environment_path, :secure => true
|
|||
end
|
|||
else
|
|||
# We have been given a branch that needs to be created or updated. If the
|
|||
# environment exists, update it. Else, create it.
|
|||
if File.directory? environment_path
|
|||
# Update an existing environment. We do a fetch and then reset in the
|
|||
# case that someone did a force push to a branch.
|
|||
b05cfed0 | Ohad Levy | puts "Updating existing environment #{environment_name}"
|
|
076d79da | Ohad Levy | Dir.chdir environment_path
|
|
%x{git fetch --all}
|
|||
%x{git reset --hard "origin/#{branchname}"}
|
|||
2af00f08 | Ohad Levy | if File.exists? "#{environment_path}/.gitmodules"
|
|
b05cfed0 | Ohad Levy | # ensure that we remove deleted sub modules too
|
|
2af00f08 | Ohad Levy | %x{git status --short}.split("\n").each do |file|
|
|
# ?? old_submodule/
|
|||
if file =~ /\s*\?{2}\s*(\S*)/
|
|||
puts "Found a few unknown files.. deleting #{$1}"
|
|||
FileUtils.rm_rf $1, :secure => true
|
|||
end
|
|||
end
|
|||
20dd5bef | Ewoud Kohl van Wijngaarden | %x{git submodule sync}
|
|
b05cfed0 | Ohad Levy | %x{git submodule update --init --recursive}
|
|
2af00f08 | Ohad Levy | end
|
|
076d79da | Ohad Levy | else
|
|
# Instantiate a new environment from the current repository.
|
|||
b05cfed0 | Ohad Levy | puts "Creating new environment #{environment_name}"
|
|
076d79da | Ohad Levy | %x{git clone --recursive #{SOURCE_REPOSITORY} #{environment_path} --branch #{branchname}}
|
|
end
|
|||
end
|
|||
end
|