Skip to content

Conversation

@tom-lejeune-2
Copy link

@tom-lejeune-2 tom-lejeune-2 commented Oct 26, 2025

PR progress checklist (to be filled in by reviewers)

  • Changes to documentation are appropriate (or tick if not required)
  • Changes to tests are appropriate (or tick if not required)
  • Reviews completed

What type of PR is this?

Primary type

  • [build] Changes related to the build system
  • [chore] Changes to the build process or auxiliary tools and libraries such as documentation generation
  • [ci] Changes to the continuous integration configuration
  • [feat] A new feature
  • [fix] A bug fix
  • [perf] A code change that improves performance
  • [refactor] A code change that neither fixes a bug nor adds a feature
  • [revert] A change used to revert a previous commit
  • [style] Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc.)

Secondary type

  • [docs] Documentation changes
  • [test] Adding missing or correcting existing tests

Does this PR introduce a BREAKING CHANGE?

No.

Related issues and/or pull requests

Describe the changes you're proposing

In it's current state, the packages.archives module is not idempotent and always re-download and re-deploy the archives and binaries, even if they are already in place.

This PR adds support for onlyif and unless so test commands can be passed to the module to allow skip the tasks if needed.

Pillar / config required to test the proposed changes

packages:
  archives:
    wanted:
      yq:
        onlyif: test ! -f /usr/local/bin/yq # new, optional
        unless: test -f /usr/local/bin/yq # new, optional
        dest: /usr/local/bin
        destname: yq
        dl:
          format: bin
          source: https://github.com/mikefarah/yq/releases/download/v4.44.3/yq_linux_amd64
          hashsum: a2c097180dd884a8d50c956ee16a9cec070f30a7947cf4ebf87d5f36213e9ed7

Debug log showing how the proposed changes work

# 1. Make sure that the file already exist
tom@salt-master ~ $ sudo salt 'test-minion-1' cmd.shell 'find /usr/local/bin/ | grep -E "yq"'
test-minion-1:
    /usr/local/bin/yq

# 2. Run state.test, to confirm it's idempotent (no changes)
tom@salt-master ~ $ sudo salt 'test-minion-1' state.test packages saltenv=archives-add-onlyif-and-unless pillarenv=archives-add-onlyif-and-unless
test-minion-1:

Summary for test-minion-1
-------------
Succeeded: 26
Failed:     0
-------------
Total states run:     26
Total run time:    3.044 s

# 3. Delete the file
tom@salt-master ~ $ sudo salt 'test-minion-1' cmd.shell 'rm /usr/local/bin/yq'
test-minion-1:

# 4. Make sure the file is deleted
tom@salt-master ~ $ sudo salt 'test-minion-1' cmd.shell 'find /usr/local/bin/ | grep -E "yq"'
test-minion-1:
ERROR: Minions returned with non-zero exit code

# 5. Run state.test to see the changes
tom@salt-master ~ $ sudo salt 'test-minion-1' state.test packages saltenv=archives-add-onlyif-and-unless pillarenv=archives-add-onlyif-and-unless
test-minion-1:
----------
          ID: packages-archive-wanted-download-yq
    Function: file.managed
        Name: /usr/local/bin/yq
      Result: None
     Comment: The file /usr/local/bin/yq is set to be changed
              Note: No changes made, actual changes may
              be different due to other states.  The state would be retried every 60 seconds (with a splay of up to 10 seconds) a maximum of 3 times or until a result of True is returned
     Started: 22:46:19.930718
    Duration: 45.763 ms
     Changes:   
              ----------
              newfile:
                  /usr/local/bin/yq

Summary for test-minion-1
-------------
Succeeded: 26 (unchanged=1, changed=1)
Failed:     0
-------------
Total states run:     26
Total run time:    2.969 s

# 6. Run state.apply to download the package
tom@salt-master ~ $ sudo salt 'test-minion-1' state.apply packages saltenv=archives-add-onlyif-and-unless pillarenv=archives-add-onlyif-and-unless
test-minion-1:
----------
          ID: packages-archive-wanted-download-yq
    Function: file.managed
        Name: /usr/local/bin/yq
      Result: True
     Comment: File /usr/local/bin/yq updated
     Started: 22:46:56.527290
    Duration: 137.25 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0755

Summary for test-minion-1
-------------
Succeeded: 26 (changed=1)
Failed:     0
-------------
Total states run:     26
Total run time:    3.650 s

# 7. Run state.test to verify idempotency
tom@salt-master ~ $ sudo salt 'test-minion-1' state.test packages saltenv=archives-add-onlyif-and-unless pillarenv=archives-add-onlyif-and-unless 
test-minion-1:

Summary for test-minion-1
-------------
Succeeded: 26
Failed:     0
-------------
Total states run:     26
Total run time:    3.135 s

Documentation checklist

  • Updated the README (e.g. Available states).
  • Updated pillar.example.

Testing checklist

  • Included in Kitchen (i.e. under state_top).
  • Covered by new/existing tests (e.g. InSpec, Serverspec, etc.).
  • Updated the relevant test pillar.

Additional context

@tom-lejeune-2 tom-lejeune-2 changed the title Add idempotency support to packages.archives (using onlyif and unless) feat(archive): Add idempotency support to packages.archives (using onlyif and unless) Oct 26, 2025
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.

1 participant