Skip to content

Conversation

@zaben903
Copy link

@zaben903 zaben903 commented Nov 6, 2025

High Level

  • Moved helper classes (Logger, GZipHelper, CD4PEClient, CD4PEJobRunner) into separate files under tasks/run_cd4pe_job/.
  • Updated specs to use new module/class structure and file paths.
  • Improved code organisation, encapsulation, and test maintainability.
  • Updated run_cd4pe_job.json to include the new files directory.
  • Fixed UTF-8 handling in RunCD4PEJob::CD4PEJobRunner.run_system_cmd.

Where This Started

While investigating a bug where some Rake tasks were causing a failure;

No job summary available: job failed to execute.

Bolt error:

msg: "Exited 1:\ninvalid byte sequence in US-ASCII\n/opt/puppetlabs/pxp-agent/spool/262/temp_task_a1d8-46ab-3424-ac56/cd4pe_jobs/tasks/run_cd4pe_job/cd4pe_job_runner.rb:324:in `gsub'\n/opt/puppetlabs/pxp-agent/spool/262/temp_task_a1d8-46ab-3424-ac56/cd4pe_jobs/tasks/run_cd4pe_job/cd4pe_job_runner.rb:324:in `scrub_secrets'\n/opt/puppetlabs/pxp-agent/spool/262/temp_task_a1d8-46ab-3424-ac56/cd4pe_jobs/tasks/run_cd4pe_job/cd4pe_job_runner.rb:300:in `run_system_cmd'\n/opt/puppetlabs/pxp-agent/spool/262/temp_task_a1d8-46ab-3424-ac56/cd4pe_jobs/tasks/run_cd4pe_job/cd4pe_job_runner.rb:203:in `run_with_system'\n/opt/puppetlabs/pxp-agent/spool/262/temp_task_a1d8-46ab-3424-ac56/cd4pe_jobs/tasks/run_cd4pe_job/cd4pe_job_runner.rb:183:in `execute_manifest'\n/opt/puppetlabs/pxp-agent/spool/262/temp_task_a1d8-46ab-3424-ac56/cd4pe_jobs/tasks/run_cd4pe_job/cd4pe_job_runner.rb:130:in `run_job'\n/opt/puppetlabs/pxp-agent/spool/262/temp_task_a1d8-46ab-3424-ac56/cd4pe_jobs/tasks/run_cd4pe_job.rb:67:in `run'\n/opt/puppetlabs/pxp-agent/spool/262/temp_task_a1d8-46ab-3424-ac56/cd4pe_jobs/tasks/run_cd4pe_job.rb:164:in `<main>'\n"

kind: "puppetlabs.tasks/task-error"

details: {"exit_code":1,"file":"/disk/7674251461420374569/builtin-modules/cd4pe_deployments/plans/cd4pe_job.pp","line":37}

This ended up being due to: Encoding.external_encoding => 'US-ASCII'. Where Open3.capture2e would return the output using this encoding.
While the following patch does resolve the issue in isolation, a longer term solution should be investigated to ensure Encoding.external_encoding should be UTF-8 or at least configurable to the expected encoding.

def run_system_cmd(cmd, log_output = true)
  @logger.log("Executing system command: #{cmd}") unless !log_output
  output, wait_thr = Open3.capture2e(cmd)
  output = output.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?') # Force encoding after the fact
  exit_code = wait_thr.exitstatus

  { exit_code:, message: scrub_secrets(output) }
end

Why Such A Large Change?

While attempting to investigate the bug, as a whole the existing file structure made it difficult to debug.
This PR includes a restructure of the existing code, while using current Ruby best practice.

Each class has been moved into their own file, while attempting define each classes responsibilities inline with their purpose.

YARD comments have been added to most methods to increase in-code documentation.

Moved helper classes (Logger, GZipHelper, CD4PEClient, CD4PEJobRunner) into separate files under tasks/run_cd4pe_job/.
Updated specs to use new module/class structure and file paths.
Improved code organisation, encapsulation, and test maintainability.
Updated run_cd4pe_job.json to include the new files directory.
Fixed UTF-8 handling in `RunCD4PEJob::CD4PEJobRunner.run_system_cmd`.
@zaben903 zaben903 self-assigned this Nov 6, 2025
@zaben903 zaben903 requested a review from a team as a code owner November 6, 2025 06:57
Update spec path for Github Actions
Update Github Action to use Ruby 3.2
Updating to Ruby 3.2 matches PDK's included Ruby version
@zaben903 zaben903 force-pushed the run_cd4pe_job_refactor branch from 5c2541f to 1025838 Compare November 7, 2025 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants