1
1
#!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
#
3
5
# Managed by Puppet
4
6
#
5
7
8
+ require 'English'
6
9
require 'fileutils'
7
10
require 'etc'
8
11
9
12
$stdout. sync = true
10
13
$stderr. sync = true
11
14
12
15
# Set this to where you want to keep your environments
13
- ENVIRONMENT_BASEDIR = "<%= scope.lookupvar(" puppet ::server ::config ::primary_envs_dir " ) %>"
16
+ ENVIRONMENT_BASEDIR = "<%= scope.lookupvar(' puppet::server::config::primary_envs_dir' ) %>"
14
17
15
18
# post-receive hooks set GIT_DIR to the current repository. If you want to
16
19
# clone from a non-local repository, set this to the URL of the repository,
@@ -25,7 +28,7 @@ BRANCH_MAP = {
25
28
"<%= g %>" => "<%= p %>",
26
29
<% end -%>
27
30
<% end -%>
28
- }
31
+ }.freeze
29
32
30
33
# The git_dir environment variable will override the --git-dir, so we remove it
31
34
# to allow us to create new directories cleanly.
@@ -34,110 +37,110 @@ ENV.delete('GIT_DIR')
34
37
# Ensure that we have the underlying directories, otherwise the later commands
35
38
# may fail in somewhat cryptic manners.
36
39
unless File.directory? ENVIRONMENT_BASEDIR
37
- puts %Q{ #{ ENVIRONMENT_BASEDIR } does not exist, cannot create environment directories.}
40
+ puts %( #{ ENVIRONMENT_BASEDIR } does not exist, cannot create environment directories.)
38
41
exit 1
39
42
end
40
43
41
44
# If we're running as root we change our UID to the owner of this file.
42
- file_uid = File.stat($0 ).uid
43
- if file_uid != Process.uid and Process.uid == 0
45
+ file_uid = File.stat($PROGRAM_NAME ).uid
46
+ if ( file_uid != Process.uid) && Process.uid.zero?
44
47
Process::UID.change_privilege(file_uid)
45
48
# Set LOGNAME and HOME directories to file-owning user's values
46
49
# so git can read ~/.config/git/attributes (for example) without error
47
- file_pwuid = Etc:: getpwuid(file_uid)
48
- ENV.store('LOGNAME',file_pwuid.name)
49
- ENV.store('HOME',file_pwuid.dir)
50
+ file_pwuid = Etc. getpwuid(file_uid)
51
+ ENV.store('LOGNAME', file_pwuid.name)
52
+ ENV.store('HOME', file_pwuid.dir)
50
53
end
51
54
52
55
# Run a command, return its output and abort if it fails
53
56
def do_cmd(cmd)
54
- ret = %x{#{ cmd } }
55
- if $? .exitstatus != 0
56
- puts("'#{ cmd } ' failed. Giving up.")
57
- exit 1
58
- end
59
- ret
57
+ ret = %x{#{ cmd } }
58
+ if $CHILD_STATUS .exitstatus != 0
59
+ puts("'#{ cmd } ' failed. Giving up.")
60
+ exit 1
61
+ end
62
+ ret
60
63
end
61
64
62
65
# You can push multiple refspecs at once, like 'git push origin branch1 branch2',
63
66
# so we need to handle each one.
64
- $stdin.each_line do |line|
65
- oldrev , newrev, refname = line.split(" " )
67
+ $stdin.each_line do |line| # rubocop:disable Metrics/BlockLength
68
+ _oldrev , newrev, refname = line.split(' ' )
66
69
67
70
# Determine the branch name from the refspec we're received, which is in the
68
71
# format refs/heads/<branch>, and make sure that it doesn't have any possibly
69
72
# dangerous characters
70
- branchname = refname.sub(%r{^refs/heads/(.*$)}) { $1 }
71
- if branchname =~ /[ \W ] /
72
- puts %Q{ Branch "#{ branchname } " contains non-word characters, ignoring it.}
73
+ branchname = refname.sub(%r{^refs/heads/(.*$)}) { Regexp.last_match(1) }
74
+ if branchname =~ /\W /
75
+ puts %( Branch "#{ branchname } " contains non-word characters, ignoring it.)
73
76
next
74
77
end
75
78
76
- if BRANCH_MAP[branchname] != nil
79
+ if ! BRANCH_MAP[branchname]. nil?
77
80
environment_name = BRANCH_MAP[branchname]
78
81
environment_path = "#{ ENVIRONMENT_BASEDIR } /#{ BRANCH_MAP [ branchname ] } "
79
82
else
80
83
environment_name = branchname
81
84
environment_path = "#{ ENVIRONMENT_BASEDIR } /#{ branchname } "
82
85
end
83
86
87
+ # Perform one of these operations:
88
+ # - delete
89
+ # - update
90
+ # - create
84
91
if newrev =~ /^0+$/
85
92
# We've received a push with a null revision, something like 000000000000,
86
93
# which means that we should delete the given branch.
87
94
puts "Deleting existing environment #{ environment_name } "
88
- if File.directory? environment_path
89
- FileUtils.rm_rf environment_path, :secure => true
90
- end
91
- else
92
- # We have been given a branch that needs to be created or updated. If the
93
- # environment exists, update it. Else, create it.
94
-
95
- if File.directory? environment_path
96
- # Update an existing environment. We do a fetch and then reset in the
97
- # case that someone did a force push to a branch.
98
-
99
- puts "Updating existing environment #{ environment_name } "
100
- Dir.chdir environment_path
101
- do_cmd("git fetch --all")
102
- do_cmd("git reset --hard 'origin/#{ branchname } '")
103
- if File.exist? "#{ environment_path } /.gitmodules"
104
- # ensure that we remove deleted sub modules too
105
- do_cmd("git status --short").split("\n ").each do |file|
106
- # ?? old_submodule/
107
- if file =~ /\s *\? {2}\s *(\S *)/
108
- puts "Found a few unknown files.. deleting #{ $1} "
109
- FileUtils.rm_rf $1, :secure => true
110
- end
111
- end
112
- do_cmd("git submodule sync")
113
- do_cmd("git submodule update --init --recursive")
114
- <% if @git_repo_r10k -%>
115
- if File.exist? 'Puppetfile'
116
- puts("Installing modules using r10k")
117
- do_cmd("r10k puppetfile install")
95
+ FileUtils.rm_rf environment_path, secure: true if File.directory? environment_path
96
+ elsif File.directory? environment_path
97
+ # Update an existing environment. We do a fetch and then reset in the
98
+ # case that someone did a force push to a branch.
99
+ # We have been given a branch that needs to be updated.
100
+
101
+ puts "Updating existing environment #{ environment_name } "
102
+ Dir.chdir environment_path
103
+ do_cmd('git fetch --all')
104
+ do_cmd("git reset --hard 'origin/#{ branchname } '")
105
+ if File.exist? "#{ environment_path } /.gitmodules"
106
+ # ensure that we remove deleted sub modules too
107
+ do_cmd('git status --short').split("\n ").each do |file|
108
+ # ?? old_submodule/
109
+ if file =~ /\s *\? {2}\s *(\S *)/
110
+ puts "Found a few unknown files.. deleting #{ Regexp . last_match ( 1 ) } "
111
+ FileUtils.rm_rf Regexp.last_match(1), secure: true
118
112
end
119
- <% end -%>
120
- <% if @git_repo_gen_types -%>
121
- puts("Generating types for #{ environment_name } )"
122
- do_cmd("puppet generate types --environmentpath #{ ENVIRONMENT_BASEDIR } --environment #{ environment_name } ")
123
- <% end -%>
124
113
end
125
- else
126
- # Instantiate a new environment from the current repository.
127
-
128
- puts "Creating new environment #{ environment_name } "
129
- do_cmd("git clone --recursive #{ SOURCE_REPOSITORY } #{ environment_path } --branch #{ branchname } ")
114
+ do_cmd('git submodule sync')
115
+ do_cmd('git submodule update --init --recursive')
130
116
<% if @git_repo_r10k -%>
131
- Dir.chdir environment_path
132
117
if File.exist? 'Puppetfile'
133
- puts(" Installing modules using r10k" )
134
- do_cmd(" r10k puppetfile install" )
118
+ puts(' Installing modules using r10k' )
119
+ do_cmd(' r10k puppetfile install' )
135
120
end
136
121
<% end -%>
137
122
<% if @git_repo_gen_types -%>
138
- puts("Generating types for #{ environment_name } )"
123
+ puts("Generating types for #{ environment_name } ")
139
124
do_cmd("puppet generate types --environmentpath #{ ENVIRONMENT_BASEDIR } --environment #{ environment_name } ")
140
125
<% end -%>
141
126
end
127
+
128
+ else
129
+ # Instantiate a new environment from the current repository.
130
+ # We have been given a new branch that needs to be created
131
+
132
+ puts "Creating new environment #{ environment_name } "
133
+ do_cmd("git clone --recursive #{ SOURCE_REPOSITORY } #{ environment_path } --branch #{ branchname } ")
134
+ Dir.chdir environment_path
135
+ <% if @git_repo_r10k -%>
136
+ if File.exist? 'Puppetfile'
137
+ puts('Installing modules using r10k')
138
+ do_cmd('r10k puppetfile install')
139
+ end
140
+ <% end -%>
141
+ <% if @git_repo_gen_types -%>
142
+ puts("Generating types for #{ environment_name } ")
143
+ do_cmd("puppet generate types --environmentpath #{ ENVIRONMENT_BASEDIR } --environment #{ environment_name } ")
144
+ <% end -%>
142
145
end
143
146
end
0 commit comments