forked from adrienthebo/puppet-git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic-environments
executable file
·99 lines (83 loc) · 3.56 KB
/
dynamic-environments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env ruby
require 'fileutils'
# Set this to where you want to keep your environments
# /etc/puppet/environments is a reasonable default.
ENVIRONMENT_BASEDIR = "/etc/puppet/environments"
# 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'])
# 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",
}
# Set this to a group the puppet user is a member of
PUPPET_GROUP = "puppet"
# Set this to the octal mode the environment should have
ENVIRONMENT_MODE = 0750
# The git_dir environment variable will override the --git-dir, so we remove it
# to allow us to create new directories cleanly.
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(" ")
# Check if the branch name is valid.
branchname = refname.sub(%r{^refs/heads/(.*$)}) { $1 }
unless system("git check-ref-format --branch '#{branchname}' >& /dev/null")
puts %Q{Branchname "#{branchname}" is not valid.}
next
end
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
if newrev =~ /^0+$/
# We've received a push with a null revision, something like 000000000000,
# which means that we should delete the given branch.
puts "Deleting existing environment #{environment_name}"
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.
puts "Updating existing environment #{environment_name}"
Dir.chdir environment_path
%x{git fetch --all}
%x{git reset --hard "origin/#{branchname}"}
if File.exists? "#{environment_path}/.gitmodules"
# ensure that we remove deleted sub modules too
%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
%x{git submodule update --init --recursive}
end
else
# Instantiate a new environment from the current repository.
puts "Creating new environment #{environment_name}"
%x{git clone --recursive #{SOURCE_REPOSITORY} #{environment_path} --branch #{branchname}}
end
#Make sure the puppet user is able to read the environment
FileUtils.chown_R nil, PUPPET_GROUP, environment_path
FileUtils.chmod_R ENVIRONMENT_MODE, environment_path
end
end