Skip to content

Instantly share code, notes, and snippets.

@nickanderson
Created January 22, 2020 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickanderson/746c5e31445c787a84798bafde5ec41b to your computer and use it in GitHub Desktop.
Save nickanderson/746c5e31445c787a84798bafde5ec41b to your computer and use it in GitHub Desktop.

How can I upgrade each package that has an update available witout manually specifying each one

Created From: Notes
bundle agent example_package_updates_installed
{
  vars:
    debian|ubuntu::

      # Get the data about package updates available. packageupdatesmatching()
      # returns data (best undersood as a JSON array) about pacakges that have
      # updates available based on a cache of information returned from the
      # underlying package manager (apt, yum, etc ...)

      "packages_with_updates"
        # data => packageupdatesmatching( ".*",
        #                                  ".*",
        #                                  ".*",
        #                                  "apt_get");
        #
        # Here is a mock response
        #
        data => '
      [
        {
          "arch":"default",
          "method":"dpkg",
          "name":"syncthing",
          "version":"1.1.0~ds1-1"
        },
        {
          "arch":"default",
          "method":"dpkg",
          "name":"google-chrome-stable",
          "version": "79.0.3945.130-1"
        }
        ]';



      # We get the index so that we can iterate.
      "p" slist => getindices( packages_with_updates );

      # Here we define a list of packages that we want to exclude
      "excluded_packages"
        slist => { "google-chrome-stable" };

      # We build a new data structure with just the names of the packages we want to update
      "update_candidate_names[$(p)]" string => "$(packages_with_updates[$(p)][name])";

      # Finally, we generate a list of the package names we want to update
      # excluding any packages we don't want
      "updates" slist => difference( getvalues( update_candidate_names ), excluded_packages );

  packages:
    debian|ubuntu::

      # 
      # Now we promise that each package we want to update is at the latest
      # version available (most package modules support this).
      # 
      # "$(updates)"
      #   version => "latest";

  reports:
    "I should upgrade $(updates)";
}
bundle agent __main__
{
  methods: "example_package_updates_installed";
}
R: I should upgrade syncthing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment