Skip to content

Commit 5b15ff6

Browse files
committed
vcsrepo: add mode option
Signed-off-by: Robin H. Johnson <[email protected]> Reference: #598
1 parent 6462c93 commit 5b15ff6

File tree

13 files changed

+74
-24
lines changed

13 files changed

+74
-24
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55

66
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org).
77

8+
## UNRELEASED
9+
10+
### Added
11+
12+
- support for directory modes for repos [\#599](https://github.com/puppetlabs/puppetlabs-vcsrepo/issues/599) ([robbat2](https://github.com/robbat2))
13+
814
## [v6.1.0](https://github.com/puppetlabs/puppetlabs-vcsrepo/tree/v6.1.0) - 2023-06-13
915

1016
[Full Changelog](https://github.com/puppetlabs/puppetlabs-vcsrepo/compare/v6.0.1...v6.1.0)

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -797,37 +797,37 @@ For information on the classes and types, see the [REFERENCE.md](https://github.
797797

798798
Features: `bare_repositories`, `depth`, `multiple_remotes`, `reference_tracking`, `ssh_identity`, `submodules`, `user`
799799

800-
Parameters: `depth`, `ensure`, `excludes`, `force`, `group`, `identity`, `owner`, `path`, `provider`, `remote`, `revision`, `source`, `user`
800+
Parameters: `depth`, `ensure`, `excludes`, `force`, `group`, `identity`, `owner`, `path`, `provider`, `remote`, `revision`, `source`, `user`, `mode`
801801

802802
##### `bzr` - Supports the Bazaar VCS.
803803

804804
Features: `reference_tracking`
805805

806-
Parameters: `ensure`, `excludes`, `force`, `group`, `owner`, `path`, `provider`, `revision`, `source`
806+
Parameters: `ensure`, `excludes`, `force`, `group`, `owner`, `path`, `provider`, `revision`, `source`, `mode`
807807

808808
##### `cvs` - Supports the CVS VCS.
809809

810810
Features: `cvs_rsh`, `gzip_compression`, `modules`, `reference_tracking`, `user`
811811

812-
Parameters: `compression`, `cvs_rsh`, `ensure`, `excludes`, `force`, `group`, `module`, `owner`, `path`, `provider`
812+
Parameters: `compression`, `cvs_rsh`, `ensure`, `excludes`, `force`, `group`, `module`, `owner`, `path`, `provider`, `mode`
813813

814814
##### `hg` - Supports the Mercurial VCS.
815815

816816
Features: `reference_tracking`, `ssh_identity`, `user`
817817

818-
Parameters: `ensure`, `excludes`, `force`, `group`, `identity`, `owner`, `path`, `provider`, `revision`, `source`, `user`
818+
Parameters: `ensure`, `excludes`, `force`, `group`, `identity`, `owner`, `path`, `provider`, `revision`, `source`, `user`, `mode`
819819

820820
##### `p4` - Supports the Perforce VCS.
821821

822822
Features: `p4config`, `reference_tracking`
823823

824-
Parameters: `ensure`, `excludes`, `force`, `group`, `owner`, `p4config`, `path`, `provider`, `revision`, `source`
824+
Parameters: `ensure`, `excludes`, `force`, `group`, `owner`, `p4config`, `path`, `provider`, `revision`, `source`, `mode`
825825

826826
##### `svn` - Supports the Subversion VCS.
827827

828828
Features: `basic_auth`, `configuration`, `conflict`, `depth`, `filesystem_types`, `reference_tracking`
829829

830-
Parameters: `basic_auth_password`, `basic_auth_username`, `configuration`, `conflict`, `ensure`, `excludes`, `force`, `fstype`, `group`, `includes`, `owner`, `path`, `provider`, `revision`, `source`, `trust_server_cert`
830+
Parameters: `basic_auth_password`, `basic_auth_username`, `configuration`, `conflict`, `ensure`, `excludes`, `force`, `fstype`, `group`, `includes`, `owner`, `path`, `provider`, `revision`, `source`, `trust_server_cert`, `mode`
831831

832832
<a id="features"></a>
833833
#### Features

REFERENCE.md

+4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ The following parameters are available in the `vcsrepo` type.
154154
* [`trust_server_cert`](#trust_server_cert)
155155
* [`umask`](#umask)
156156
* [`user`](#user)
157+
* [`mode`](#mode)
157158

158159
##### <a name="basic_auth_password"></a>`basic_auth_password`
159160

@@ -280,3 +281,6 @@ Sets the umask to be used for all repo operations
280281

281282
The user to run for repository operations
282283

284+
##### <a name="mode"></a>`mode`
285+
286+
Sets the mode for the repository directory (non-recursive).

lib/puppet/provider/vcsrepo.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,26 @@ def check_force
1616

1717
private
1818

19-
def set_ownership
19+
def set_ownership_and_permissions
2020
owner = @resource.value(:owner) || nil
2121
group = @resource.value(:group) || nil
22+
mode = @resource.value(:mode) || nil
2223
excludes = @resource.value(:excludes) || nil
24+
# Change the permission on the repo itself.
25+
# The VCS should maintain permissions on files within the checkout.
26+
FileUtils.chmod(mode, @resource.value(:path)) unless mode.nil?
27+
# We might have no work to do, and this makes it easier for the callers.
28+
return if owner.nil? && group.nil?
29+
2330
if excludes.nil? || excludes.empty?
24-
FileUtils.chown_R(owner, group, @resource.value(:path))
31+
FileUtils.chown_R(owner, group, @resource.value(:path)) unless owner.nil? && group.nil?
2532
else
26-
FileUtils.chown(owner, group, files)
33+
FileUtils.chown(owner, group, files) unless owner.nil? && group.nil?
2734
end
2835
end
2936

3037
def files
38+
# Expensive on large repos
3139
excludes = @resource.value(:excludes)
3240
path = @resource.value(:path)
3341
Dir["#{path}/**/*"].reject { |f| excludes.any? { |p| f.start_with?("#{path}/#{p}") } }

lib/puppet/provider/vcsrepo/bzr.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,6 @@ def clone_repository(revision)
102102
end
103103

104104
def update_owner
105-
set_ownership if @resource.value(:owner) || @resource.value(:group)
105+
set_ownership_and_permissions if @resource.value(:owner) || @resource.value(:group) || @resource.value(:mode)
106106
end
107107
end

lib/puppet/provider/vcsrepo/cvs.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def create_repository(path)
135135
end
136136

137137
def update_owner
138-
set_ownership if @resource.value(:owner) || @resource.value(:group)
138+
set_ownership_and_permissions if @resource.value(:owner) || @resource.value(:group) || @resource.value(:mode)
139139
end
140140

141141
def runcvs(*args)

lib/puppet/provider/vcsrepo/git.rb

+15-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def create
2828
init_repository
2929
self.skip_hooks = @resource.value(:skip_hooks) unless @resource.value(:skip_hooks).nil?
3030
end
31-
update_owner_and_excludes
31+
update_owner_permission_and_excludes
3232
end
3333

3434
def destroy
@@ -87,8 +87,10 @@ def revision=(desired)
8787
end
8888
end
8989
# TODO: Would this ever reach here if it is bare?
90-
update_submodules if !ensure_bare_or_mirror? && @resource.value(:submodules) == :true
91-
update_owner_and_excludes
90+
if !ensure_bare_or_mirror? && @resource.value(:submodules) == :true
91+
update_submodules
92+
end
93+
update_owner_permission_and_excludes
9294
end
9395

9496
def bare_exists?
@@ -228,7 +230,7 @@ def update_references
228230
at_path do
229231
git_remote_action('fetch', @resource.value(:remote))
230232
git_remote_action(*fetch_tags_args, @resource.value(:remote))
231-
update_owner_and_excludes
233+
update_owner_permission_and_excludes
232234
end
233235
end
234236

@@ -246,6 +248,8 @@ def convert_working_copy_to_bare
246248
FileUtils.mv(File.join(@resource.value(:path), '.git'), tempdir)
247249
FileUtils.rm_rf(@resource.value(:path))
248250
FileUtils.mv(tempdir, @resource.value(:path))
251+
FileUtils.chown(@resource.value(:user), @resource.value(:group), @resource.value(:path)) if @resource.value(:user) || @resource.value(:group)
252+
FileUtils.chmod(@resource.value(:mode), @resource.value(:path)) if @resource.value(:mode)
249253
at_path do
250254
exec_git('config', '--local', '--bool', 'core.bare', 'true')
251255
return unless @resource.value(:ensure) == :mirror
@@ -266,13 +270,15 @@ def convert_bare_to_working_copy
266270
notice 'Converting bare repository to working copy repository'
267271
FileUtils.mv(@resource.value(:path), tempdir)
268272
FileUtils.mkdir(@resource.value(:path))
273+
FileUtils.chown(@resource.value(:user), @resource.value(:group), @resource.value(:path)) if @resource.value(:user) || @resource.value(:group)
274+
FileUtils.chmod(@resource.value(:mode), @resource.value(:path)) if @resource.value(:mode)
269275
FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
270276
if commits?
271277
at_path do
272278
exec_git('config', '--local', '--bool', 'core.bare', 'false')
273279
reset('HEAD')
274280
git_with_identity('checkout', '--force')
275-
update_owner_and_excludes
281+
update_owner_permission_and_excludes
276282
end
277283
end
278284
set_no_mirror if mirror?
@@ -398,7 +404,8 @@ def init_repository
398404
else
399405
# normal init
400406
FileUtils.mkdir(@resource.value(:path))
401-
FileUtils.chown(@resource.value(:user), nil, @resource.value(:path)) if @resource.value(:user)
407+
FileUtils.chown(@resource.value(:user), @resource.value(:group), @resource.value(:path)) if @resource.value(:user) || @resource.value(:group)
408+
FileUtils.chmod(@resource.value(:mode), @resource.value(:path)) if @resource.value(:mode)
402409
args = ['init']
403410
args << '--bare' if @resource.value(:ensure) == :bare
404411
at_path do
@@ -580,8 +587,8 @@ def get_revision(rev = 'HEAD')
580587
end
581588

582589
# @!visibility private
583-
def update_owner_and_excludes
584-
set_ownership if @resource.value(:owner) || @resource.value(:group)
590+
def update_owner_permission_and_excludes
591+
set_ownership_and_permissions if @resource.value(:owner) || @resource.value(:group) || @resource.value(:mode)
585592
set_excludes if @resource.value(:excludes)
586593
end
587594

lib/puppet/provider/vcsrepo/hg.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def clone_repository(revision)
115115
end
116116

117117
def update_owner
118-
set_ownership if @resource.value(:owner) || @resource.value(:group)
118+
set_ownership_and_permissions if @resource.value(:owner) || @resource.value(:group) || @resource.value(:mode)
119119
end
120120

121121
def sensitive?

lib/puppet/provider/vcsrepo/p4.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def source=(_desired)
101101
private
102102

103103
def update_owner
104-
set_ownership if @resource.value(:owner) || @resource.value(:group)
104+
set_ownership_and_permissions if @resource.value(:owner) || @resource.value(:group) || @resource.value(:mode)
105105
end
106106

107107
# Sync the client workspace files to head or specified revision.

lib/puppet/provider/vcsrepo/svn.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def create_repository(path)
242242
end
243243

244244
def update_owner
245-
set_ownership if @resource.value(:owner) || @resource.value(:group)
245+
set_ownership_and_permissions if @resource.value(:owner) || @resource.value(:group) || @resource.value(:mode)
246246
end
247247

248248
def update_includes(paths)

lib/puppet/type/vcsrepo.rb

+4
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ def insync?(is)
213213
desc 'The group/gid that owns the repository files'
214214
end
215215

216+
newparam :mode do
217+
desc 'The permission for the repository directory itself (not recursive)'
218+
end
219+
216220
newparam :user do
217221
desc 'The user to run for repository operations'
218222
end

spec/acceptance/clone_repo_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -636,4 +636,25 @@
636636
it { is_expected.to be_mode '664' }
637637
end
638638
end
639+
640+
context 'with mode' do
641+
pp = <<-MANIFEST
642+
vcsrepo { "#{tmpdir}/testrepo_mode":
643+
ensure => present,
644+
provider => git,
645+
source => "file://#{tmpdir}/testrepo.git",
646+
mode => 'u=rwX,g=rX,o=X',
647+
}
648+
MANIFEST
649+
it 'clones a repo' do
650+
# Run it twice and test for idempotency
651+
idempotent_apply(pp)
652+
end
653+
654+
describe file("#{tmpdir}/testrepo_mode") do
655+
# NOTE: '0664' is not supported by 'be_mode'; this must be three digits
656+
# unless the first octet is non-zero.
657+
it { is_expected.to be_mode '751' }
658+
end
659+
end
639660
end

spec/unit/puppet/provider/vcsrepo/git_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def branch_a_list(include_branch = nil?)
313313
.with('config', '--local', '--bool', 'core.bare', 'false').and_return(true)
314314
expect(provider).to receive(:reset).with('HEAD').and_return(true)
315315
expect(provider).to receive(:git_with_identity).with('checkout', '--force').and_return(true)
316-
expect(provider).to receive(:update_owner_and_excludes).and_return(true)
316+
expect(provider).to receive(:update_owner_permission_and_excludes).and_return(true)
317317
expect(provider).to receive(:mirror?).and_return(false)
318318
provider.instance_eval { convert_bare_to_working_copy }
319319
end
@@ -330,7 +330,7 @@ def branch_a_list(include_branch = nil?)
330330
.with('config', '--local', '--bool', 'core.bare', 'false').and_return(true)
331331
expect(provider).to receive(:reset).with('HEAD').and_return(true)
332332
expect(provider).to receive(:git_with_identity).with('checkout', '--force').and_return(true)
333-
expect(provider).to receive(:update_owner_and_excludes).and_return(true)
333+
expect(provider).to receive(:update_owner_permission_and_excludes).and_return(true)
334334
expect(provider).to receive(:exec_git).with('config', '--unset', 'remote.origin.mirror')
335335
expect(provider).to receive(:mirror?).and_return(true)
336336
provider.instance_eval { convert_bare_to_working_copy }

0 commit comments

Comments
 (0)