Skip to content

Commit

Permalink
Merge pull request #550 from gimmyxd/MODULES-11048
Browse files Browse the repository at this point in the history
(MODULES-11048) task to remove local filebucket
  • Loading branch information
ciprianbadescu authored Apr 20, 2021
2 parents e21a23c + 7ec1c2a commit 3669c16
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tasks/delete_local_filebucket.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"description": "Removes the local filebucket",
"parameters": {
"force": {
"description": "ignore nonexistent files and errors",
"type": "Optional[Boolean]"
}
},
"files": ["puppet_agent/files/rb_task_helper.rb"]
}
63 changes: 63 additions & 0 deletions tasks/delete_local_filebucket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

require 'json'
require 'puppet'

params = JSON.parse(STDIN.read)
force = params['force']
require_relative File.join(params['_installdir'], 'puppet_agent', 'files', 'rb_task_helper.rb')

module PuppetAgent
class DeleteLocalFilebucket
include PuppetAgent::RbTaskHelper

def initialize(force)
@force = force
end

def run
return error_result(
'puppet_agent/no-puppet-bin-error',
"Puppet executable '#{puppet_bin}' does not exist",
) unless puppet_bin_present?

begin
path = clientbucketdir
if path && !path.empty? && (File.directory?(path) || force)
FileUtils.rm_r(Dir.glob("#{path}/*"), secure: true, force: force)
{ "success": true }
else
error_result(
'puppet_agent/cannot-remove-error',
"clientbucketdir: '#{path}' does not exist or is not a directory"
)
end
rescue StandardError => e
error_result(
'puppet_agent/cannot-remove-error',
"#{e.class}: #{e.message}"
)
end
end

private

def clientbucketdir
options = {
failonfail: false,
override_locale: false,
}

command = "#{puppet_bin} config print clientbucketdir"
Puppet::Util::Execution.execute(command, options).strip
end

attr_reader :force
end
end

if __FILE__ == $PROGRAM_NAME
task = PuppetAgent::DeleteLocalFilebucket.new(force)
puts task.run
end

0 comments on commit 3669c16

Please sign in to comment.