11#!/usr/bin/env ruby
2+ # frozen_string_literal: true
3+
24#
35# Managed by Puppet
46#
57
8+ require 'English'
69require 'fileutils'
710require 'etc'
811
912$stdout. sync = true
1013$stderr. sync = true
1114
1215# 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' ) %>"
1417
1518# post-receive hooks set GIT_DIR to the current repository. If you want to
1619# clone from a non-local repository, set this to the URL of the repository,
@@ -25,7 +28,7 @@ BRANCH_MAP = {
2528 "<%= g %>" => "<%= p %>",
2629<% end -%>
2730<% end -%>
28- }
31+ }.freeze
2932
3033# The git_dir environment variable will override the --git-dir, so we remove it
3134# to allow us to create new directories cleanly.
@@ -34,110 +37,109 @@ ENV.delete('GIT_DIR')
3437# Ensure that we have the underlying directories, otherwise the later commands
3538# may fail in somewhat cryptic manners.
3639unless 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.)
3841 exit 1
3942end
4043
4144# 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?
4447 Process::UID.change_privilege(file_uid)
4548 # Set LOGNAME and HOME directories to file-owning user's values
4649 # 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)
5053end
5154
5255# Run a command, return its output and abort if it fails
5356def 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
6063end
6164
6265# You can push multiple refspecs at once, like 'git push origin branch1 branch2',
6366# 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(' ' )
6669
6770 # Determine the branch name from the refspec we're received, which is in the
6871 # format refs/heads/<branch>, and make sure that it doesn't have any possibly
6972 # 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.)
7376 next
7477 end
7578
76- if BRANCH_MAP[branchname] != nil
79+ if ! BRANCH_MAP[branchname]. nil?
7780 environment_name = BRANCH_MAP[branchname]
7881 environment_path = "#{ ENVIRONMENT_BASEDIR } /#{ BRANCH_MAP [ branchname ] } "
7982 else
8083 environment_name = branchname
8184 environment_path = "#{ ENVIRONMENT_BASEDIR } /#{ branchname } "
8285 end
8386
87+ # Perform one of these operations:
88+ # - delete
89+ # - update
90+ # - create
8491 if newrev =~ /^0+$/
8592 # We've received a push with a null revision, something like 000000000000,
8693 # which means that we should delete the given branch.
8794 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
118112 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 -%>
124113 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')
130116 <% if @git_repo_r10k -%>
131- Dir.chdir environment_path
132117 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' )
135120 end
136121 <% end -%>
137122 <% if @git_repo_gen_types -%>
138- puts("Generating types for #{ environment_name } )"
123+ puts("Generating types for #{ environment_name } ")
139124 do_cmd("puppet generate types --environmentpath #{ ENVIRONMENT_BASEDIR } --environment #{ environment_name } ")
140125 <% end -%>
141126 end
127+
128+ else
129+ # Instantiate a new environment from the current repository.
130+
131+ puts "Creating new environment #{ environment_name } "
132+ do_cmd("git clone --recursive #{ SOURCE_REPOSITORY } #{ environment_path } --branch #{ branchname } ")
133+ Dir.chdir environment_path
134+ <% if @git_repo_r10k -%>
135+ if File.exist? 'Puppetfile'
136+ puts('Installing modules using r10k')
137+ do_cmd('r10k puppetfile install')
138+ end
139+ <% end -%>
140+ <% if @git_repo_gen_types -%>
141+ puts("Generating types for #{ environment_name } ")
142+ do_cmd("puppet generate types --environmentpath #{ ENVIRONMENT_BASEDIR } --environment #{ environment_name } ")
143+ <% end -%>
142144 end
143145end
0 commit comments