From 4a998b0cd92acc9b154ff2eb2d5d6120aea90b03 Mon Sep 17 00:00:00 2001 From: gimmy Date: Thu, 15 Apr 2021 15:18:11 +0300 Subject: [PATCH] (MODULES-11048) task to remove local filebucket MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds a task that removes the local filebucket which was disabled by default in Puppet 7. The location of filebucket is determine using the `clientbucketdir` puppet config. ``` ❯ bolt task show puppet_agent::delete_local_filebucket puppet_agent::delete_local_filebucket Removes the local filebucket Usage bolt task run puppet_agent::delete_local_filebucket --targets [force=] Parameters force Optional[Boolean] ignore nonexistent files and errors ``` --- tasks/delete_local_filebucket.json | 10 ++++++ tasks/delete_local_filebucket.rb | 50 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tasks/delete_local_filebucket.json create mode 100644 tasks/delete_local_filebucket.rb diff --git a/tasks/delete_local_filebucket.json b/tasks/delete_local_filebucket.json new file mode 100644 index 00000000..2b4458bc --- /dev/null +++ b/tasks/delete_local_filebucket.json @@ -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"] +} diff --git a/tasks/delete_local_filebucket.rb b/tasks/delete_local_filebucket.rb new file mode 100644 index 00000000..43a6b170 --- /dev/null +++ b/tasks/delete_local_filebucket.rb @@ -0,0 +1,50 @@ +#!/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 + begin + FileUtils.remove_dir(clientbucketdir, force) + { "success": true } + rescue StandardError => e + return error_result( + 'puppet_agent/cannot-remove', + "#{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