Skip to content

Use shared tmpdir for inventory report storage - #1262

Merged
nofaralfasi merged 3 commits into
theforeman:developfrom
nofaralfasi:use-rails-tmp-for-inventory
Sep 28, 2026
Merged

nofaralfasi merged 3 commits into
theforeman:developfrom
nofaralfasi:use-rails-tmp-for-inventory

Conversation

@nofaralfasi

Copy link
Copy Markdown
Contributor

Change base_folder from /var/lib/foreman/red_hat_inventory/ to /var/run/foreman/red_hat_inventory/.

In containerized deployments, /var/run/foreman is already shared between Foreman and Dynflow via the foreman-data-run volume. Writing inventory reports there lets Dynflow's generated archives be visible to Foreman's send_file without mounting an additional volume.

In development, /var/run/foreman does not exist, so the path falls back to Rails.root/tmp/red_hat_inventory/ (gitignored).

Ref: theforeman/foremanctl#825

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="lib/foreman_inventory_upload.rb" line_range="10" />
<code_context>
+    # directory does not exist, so fall back to Rails.root/tmp.
     @base_folder ||= File.join(
-      Dir.glob('/var/lib/foreman').first || File.dirname(Dir.getwd),
+      Dir.exist?(SHARED_TMPDIR) ? SHARED_TMPDIR : Rails.root.join('tmp').to_s,
       'red_hat_inventory/'
     )
</code_context>
<issue_to_address>
**issue (bug_risk):** When `/var/run/foreman` exists but is not writable by the Foreman process, `base_folder` selects it and `generated_reports_folder` subsequently raises `Errno::EACCES` from `FileUtils.mkdir_p` instead of falling back to `Rails.root/tmp`.

**Triggers:** When the shared directory is present but owned by another user or has restrictive permissions, such as in a non-container development or misconfigured deployment.

**Suggested fix:** Select the shared directory only when it exists and is writable, or rescue the directory-creation failure and use the Rails temporary directory.
</issue_to_address>

### Comment 2
<location path="README.md" line_range="32-37" />
<code_context>
     # generate report for specific organization (don't upload)
     export organization_id=1
-    export target=/var/lib/foreman/red_hat_inventory/generated_reports/
+    export target=/var/run/foreman/red_hat_inventory/generated_reports/
     /usr/sbin/foreman-rake rh_cloud_inventory:report:generate

</code_context>
<issue_to_address>
**issue:** These instructions set the `target` environment variable explicitly, which bypasses `ForemanInventoryUpload.base_folder` and therefore bypasses its development fallback. In a development environment where the documented target does not exist or is not writable, `report:generate` switches to an unrelated `Dir.mktmpdir`, while the subsequent documented `report:upload` still reads from the unwritable target and raises when moving the report.

**Triggers:** When a developer follows the documented generate/upload commands without creating and granting access to `/var/run/foreman/red_hat_inventory/generated_reports/`.

**Suggested fix:** Use the plugin's resolved default folder in development, or instruct developers to create the target directory and ensure it is writable before running both commands.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and the report location changes to a shared runtime directory, so a wrong path or permission setup could make generated reports unavailable to the upload process or cause them to disappear on restart. Reverting restores the old behavior, and any misplaced reports are bounded artifacts that can be removed or regenerated.

Blocking findings: lib/foreman_inventory_upload.rb:10, README.md:37


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread lib/foreman_inventory_upload.rb Outdated
Comment thread README.md
Change base_folder from /var/lib/foreman/red_hat_inventory/ to
/var/run/foreman/red_hat_inventory/.

In containerized deployments (foremanctl), /var/run/foreman is already
shared between Foreman and Dynflow via the foreman-data-run volume.
Writing inventory reports there lets Dynflow's generated archives be
visible to Foreman's send_file without mounting an additional volume.

In development, /var/run/foreman does not exist, so the path falls back
to Rails.root/tmp/red_hat_inventory/ (gitignored).

Ref: theforeman/foremanctl#825
@nofaralfasi
nofaralfasi force-pushed the use-rails-tmp-for-inventory branch 2 times, most recently from 2093e81 to 6b52ec2 Compare September 24, 2026 14:58
Use File.writable? instead of Dir.exist? so the fallback to
Rails.root/tmp also kicks in when /var/run/foreman exists but is
not writable by the current process. Freeze the constant to satisfy
Style/MutableConstant.
@nofaralfasi
nofaralfasi force-pushed the use-rails-tmp-for-inventory branch from 6b52ec2 to dd1ea37 Compare September 24, 2026 15:03
Comment thread lib/foreman_inventory_upload.rb Outdated
@ehelms
ehelms self-requested a review September 24, 2026 15:12
@nofaralfasi
nofaralfasi force-pushed the use-rails-tmp-for-inventory branch from 575d192 to 390fb34 Compare September 24, 2026 15:32
Comment thread lib/foreman_inventory_upload.rb Outdated
Rails.root/tmp is symlinked to /var/run/foreman inside the container,
so there is no need to detect the shared volume path at runtime.
Drop the SHARED_TMPDIR constant and File.writable? check.
@nofaralfasi
nofaralfasi force-pushed the use-rails-tmp-for-inventory branch from 390fb34 to 16ba476 Compare September 24, 2026 15:39
@nofaralfasi
nofaralfasi merged commit 5f010bc into theforeman:develop Sep 28, 2026
18 of 19 checks passed
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.

4 participants