From 2f89d8a514a5996a76ca5a34397b3d087e2fe5c2 Mon Sep 17 00:00:00 2001 From: Stu Zhao Date: Tue, 28 Aug 2018 23:27:13 -0700 Subject: [PATCH 1/3] Fix __package_update_index processing error exit 1 in explorer will abort cdist. --- cdist/conf/type/__package_update_index/explorer/currage | 1 - docs/changelog | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/cdist/conf/type/__package_update_index/explorer/currage b/cdist/conf/type/__package_update_index/explorer/currage index cd042bd5..e86e3f13 100644 --- a/cdist/conf/type/__package_update_index/explorer/currage +++ b/cdist/conf/type/__package_update_index/explorer/currage @@ -29,6 +29,5 @@ case "$os" in ;; *) echo "Your operating system ($os) is currently not supported by this type (${__type##*/})." >&2 echo "Please contribute an implementation for it if you can." >&2 - exit 1 ;; esac diff --git a/docs/changelog b/docs/changelog index 311d7a57..12bdb1e0 100644 --- a/docs/changelog +++ b/docs/changelog @@ -5,6 +5,7 @@ next: * Type __letsencrypt_cert: Add support for devuan ascii (Darko Poljak) * Type __systemd_unit: Fix minor issues and add masking unit files support (Adam Dej) * Type __grafana_dashboard: Fix devuan ascii support (Dominique Roux) + * Type __package_update_index: Fix error when using OS not using apt (Stu Zhao) 4.10.1: 2018-06-21 * Type __letsencrypt_cert: Fix temp file location and removal (Darko Poljak) From b12c02138df4086e2e8e5e366e2156fa2ff95b3a Mon Sep 17 00:00:00 2001 From: Stu Zhao Date: Wed, 29 Aug 2018 00:07:19 -0700 Subject: [PATCH 2/3] Refactor __package_update_index explorers * add an type explorer to unify detecting of package type. * update currage use the type explorer, so if os and passed in type does not match, it behaves correctly. --- .../__package_update_index/explorer/currage | 8 ++--- .../type/__package_update_index/explorer/type | 34 +++++++++++++++++++ .../__package_update_index/gencode-remote | 18 +--------- 3 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 cdist/conf/type/__package_update_index/explorer/type diff --git a/cdist/conf/type/__package_update_index/explorer/currage b/cdist/conf/type/__package_update_index/explorer/currage index e86e3f13..45653f91 100644 --- a/cdist/conf/type/__package_update_index/explorer/currage +++ b/cdist/conf/type/__package_update_index/explorer/currage @@ -17,17 +17,17 @@ # You should have received a copy of the GNU General Public License # along with cdist. If not, see . -os="$("$__explorer/os")" +type="$($__type_explorer/type)" -case "$os" in - debian|ubuntu|devuan) +case "$type" in + apt) if [ -f "/var/cache/apt/pkgcache.bin" ]; then echo $(($(date +"%s")-$(stat --format '%Y' /var/cache/apt/pkgcache.bin))) else echo 0 fi ;; - *) echo "Your operating system ($os) is currently not supported by this type (${__type##*/})." >&2 + *) echo "Your specified type ($type) is currently not supported." >&2 echo "Please contribute an implementation for it if you can." >&2 ;; esac diff --git a/cdist/conf/type/__package_update_index/explorer/type b/cdist/conf/type/__package_update_index/explorer/type new file mode 100644 index 00000000..805b9f04 --- /dev/null +++ b/cdist/conf/type/__package_update_index/explorer/type @@ -0,0 +1,34 @@ +#!/bin/sh +# +# 2018 Stu Zhao (z12y12l12 at gmail.com) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . + +if [ -f "$__object/parameter/type" ]; then + cat "$__object/parameter/type" +else + # By default determine package manager based on operating system + os="$($__explorer/os)" + case "$os" in + amazon|scientific|centos|fedora|redhat) echo "yum" ;; + debian|ubuntu|devuan) echo "apt" ;; + archlinux) echo "pacman" ;; + *) + echo "Don't know how to manage packages on: $os" >&2 + exit 1 + ;; + esac +fi diff --git a/cdist/conf/type/__package_update_index/gencode-remote b/cdist/conf/type/__package_update_index/gencode-remote index 37bfe7ab..132a3daa 100755 --- a/cdist/conf/type/__package_update_index/gencode-remote +++ b/cdist/conf/type/__package_update_index/gencode-remote @@ -21,28 +21,12 @@ # Update the package index with the appropriate package manager # -type="$__object/parameter/type" +type=$(cat "$__object/explorer/type") if [ -f "$__object/parameter/maxage" ]; then maxage="$(cat "$__object/parameter/maxage")" currage="$(cat "$__object/explorer/currage")" fi -if [ -f "$type" ]; then - type="$(cat "$type")" -else - # By default determine package manager based on operating system - os="$(cat "$__global/explorer/os")" - case "$os" in - amazon|scientific|centos|fedora|redhat) type="yum" ;; - debian|ubuntu|devuan) type="apt" ;; - archlinux) type="pacman" ;; - *) - echo "Don't know how to manage packages on: $os" >&2 - exit 1 - ;; - esac -fi - if [ -n "$maxage" ] && [ "$type" != "apt" ]; then echo "ERROR: \"--maxage\" only supported for \"apt\" pkg-manager." >&2 exit 1 From f143a70463e0740937370faa9492daf5cba9a2fd Mon Sep 17 00:00:00 2001 From: Stu Zhao Date: Wed, 29 Aug 2018 00:30:27 -0700 Subject: [PATCH 3/3] Support pacman with --maxage parameter --- .../__package_update_index/explorer/currage | 7 ++++ .../__package_update_index/gencode-remote | 35 ++++++++++--------- .../conf/type/__package_update_index/man.rst | 6 ++-- docs/changelog | 1 + 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/cdist/conf/type/__package_update_index/explorer/currage b/cdist/conf/type/__package_update_index/explorer/currage index 45653f91..50474fb3 100644 --- a/cdist/conf/type/__package_update_index/explorer/currage +++ b/cdist/conf/type/__package_update_index/explorer/currage @@ -27,6 +27,13 @@ case "$type" in echo 0 fi ;; + pacman) + if [ -d "/var/lib/pacman/sync" ]; then + echo $(($(date +"%s")-$(stat --format '%Y' /var/lib/pacman/sync))) + else + echo 0 + fi + ;; *) echo "Your specified type ($type) is currently not supported." >&2 echo "Please contribute an implementation for it if you can." >&2 ;; diff --git a/cdist/conf/type/__package_update_index/gencode-remote b/cdist/conf/type/__package_update_index/gencode-remote index 132a3daa..2cc76b81 100755 --- a/cdist/conf/type/__package_update_index/gencode-remote +++ b/cdist/conf/type/__package_update_index/gencode-remote @@ -22,32 +22,33 @@ # type=$(cat "$__object/explorer/type") +currage="$(cat "$__object/explorer/currage")" if [ -f "$__object/parameter/maxage" ]; then maxage="$(cat "$__object/parameter/maxage")" - currage="$(cat "$__object/explorer/currage")" fi -if [ -n "$maxage" ] && [ "$type" != "apt" ]; then - echo "ERROR: \"--maxage\" only supported for \"apt\" pkg-manager." >&2 - exit 1 +if [ -n "$maxage" ]; then + if [ "$type" != "apt" -a "$type" != "pacman" ]; then + echo "ERROR: \"--maxage\" only supported for \"apt\" or \"pacman\" pkg-manager." >&2 + exit 1 + elif [ $currage -lt $maxage ]; then + exit 0 # no need to update + fi fi + case "$type" in yum) ;; - apt) if [ -n "$maxage" ]; then - ## check if we need to update: - if [ $currage -ge $maxage ]; then - echo "apt-get --quiet update" - echo "apt-cache updated (age was: $currage)" >> "$__messages_out" - fi - else - echo "apt-get --quiet update" - echo "apt-cache updated (age was: $currage)" >> "$__messages_out" - fi - ;; - pacman) echo "pacman --noprogressbar --sync --refresh" ;; + apt) + echo "apt-get --quiet update" + echo "apt-cache updated (age was: $currage)" >> "$__messages_out" + ;; + pacman) + echo "pacman --noprogressbar --sync --refresh" + echo "pacman package database synced (age was: $currage)" >> "$__messages_out" + ;; *) - echo "Don't know how to manage packages on: $os" >&2 + echo "Don't know how to manage packages for type: $type" >&2 exit 1 ;; esac diff --git a/cdist/conf/type/__package_update_index/man.rst b/cdist/conf/type/__package_update_index/man.rst index b63af654..3cd787b9 100644 --- a/cdist/conf/type/__package_update_index/man.rst +++ b/cdist/conf/type/__package_update_index/man.rst @@ -28,8 +28,8 @@ type * pacman for Arch Linux maxage - Available for package manager apt, max time in seconds since last update. - Repo update is skipped if maxage is not reached yet. + Available for package manager apt and pacman, max time in seconds since + last update. Repo update is skipped if maxage is not reached yet. MESSAGES -------- @@ -51,6 +51,7 @@ EXAMPLES # Only update every hour: __package_update_index --maxage 3600 --type apt + # same as above (on apt-type systems): __package_update_index --maxage 3600 @@ -58,6 +59,7 @@ AUTHORS ------- | Ricardo Catalinas Jiménez | Thomas Eckert +| Stu Zhao COPYING diff --git a/docs/changelog b/docs/changelog index 12bdb1e0..e989fb61 100644 --- a/docs/changelog +++ b/docs/changelog @@ -6,6 +6,7 @@ next: * Type __systemd_unit: Fix minor issues and add masking unit files support (Adam Dej) * Type __grafana_dashboard: Fix devuan ascii support (Dominique Roux) * Type __package_update_index: Fix error when using OS not using apt (Stu Zhao) + * Type __package_update_index: Support --maxage for type pacman (Stu Zhao) 4.10.1: 2018-06-21 * Type __letsencrypt_cert: Fix temp file location and removal (Darko Poljak)