From e87b26f2de9de29daec0403a1d226b6f027c7a29 Mon Sep 17 00:00:00 2001 From: Jake Guffey Date: Tue, 9 Sep 2014 17:10:32 -0400 Subject: [PATCH 1/3] Introduce __package_pkgng_freebsd type Create __package_pkgng_freebsd type to handle package management via pkg-ng Add requirement generation in __package/manifest Add explorer inside __package to find if pkg-ng is installed on the target --- .../conf/type/__package/explorer/pkgng_exists | 31 ++++ cdist/conf/type/__package/manifest | 9 +- .../explorer/pkg_version | 37 +++++ .../__package_pkgng_freebsd/gencode-remote | 134 ++++++++++++++++++ .../type/__package_pkgng_freebsd/man.text | 97 +++++++++++++ .../__package_pkgng_freebsd/parameter/boolean | 1 + .../parameter/optional | 5 + 7 files changed, 313 insertions(+), 1 deletion(-) create mode 100755 cdist/conf/type/__package/explorer/pkgng_exists create mode 100755 cdist/conf/type/__package_pkgng_freebsd/explorer/pkg_version create mode 100755 cdist/conf/type/__package_pkgng_freebsd/gencode-remote create mode 100644 cdist/conf/type/__package_pkgng_freebsd/man.text create mode 100644 cdist/conf/type/__package_pkgng_freebsd/parameter/boolean create mode 100644 cdist/conf/type/__package_pkgng_freebsd/parameter/optional diff --git a/cdist/conf/type/__package/explorer/pkgng_exists b/cdist/conf/type/__package/explorer/pkgng_exists new file mode 100755 index 00000000..7c4f2256 --- /dev/null +++ b/cdist/conf/type/__package/explorer/pkgng_exists @@ -0,0 +1,31 @@ +#!/bin/sh +# +# 2014 Jake Guffey (jake.guffey at eprotex.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 . +# +# +# Retrieve the status of a package - parsed dpkg output +# + +if [ "$(cat "$__global/explorer/os")" = "freebsd" ]; then + if command -v pkg >&-; then + printf "pkg-ng installed" + else + printf "pkg-ng not found" + fi +fi + diff --git a/cdist/conf/type/__package/manifest b/cdist/conf/type/__package/manifest index 0ebf0099..2e085158 100755 --- a/cdist/conf/type/__package/manifest +++ b/cdist/conf/type/__package/manifest @@ -33,7 +33,14 @@ else amazon|centos|fedora|redhat) type="yum" ;; archlinux) type="pacman" ;; debian|ubuntu) type="apt" ;; - freebsd) type="pkg_freebsd" ;; + freebsd) + exists="$(cat "$__object/explorer/pkgng_exists")" + if [ "$exists" = "pkg-ng installed" ]; then + type="pkgng_freebsd" + else + type="pkg_freebsd" + fi + ;; gentoo) type="emerge" ;; suse) type="zypper" ;; openwrt) type="opkg" ;; diff --git a/cdist/conf/type/__package_pkgng_freebsd/explorer/pkg_version b/cdist/conf/type/__package_pkgng_freebsd/explorer/pkg_version new file mode 100755 index 00000000..fe940724 --- /dev/null +++ b/cdist/conf/type/__package_pkgng_freebsd/explorer/pkg_version @@ -0,0 +1,37 @@ +#!/bin/sh +# +# 2012 Jake Guffey (jake.guffey at eprotex.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 . +# +# +# Retrieve the status of a package - parsed dpkg output +# + +if [ -f "$__object/parameter/name" ]; then + name="$(cat "$__object/parameter/name")" +else + name="$__object_id" +fi + +# Don't produce "no pkgs installed" output -- breaks things +PKG_OUTPUT=$(pkg info 2>&1) +echo -n "$(echo "$PKG_OUTPUT" \ + | awk '{print $1}' \ + | sed 's/^\(.*\)-\([^-]*\)$/name:\1 ver:\2/g' \ + | grep "name:$name ver:" \ + | sed 's/^.*ver:\(.*\)/\1/g')" + diff --git a/cdist/conf/type/__package_pkgng_freebsd/gencode-remote b/cdist/conf/type/__package_pkgng_freebsd/gencode-remote new file mode 100755 index 00000000..36af987b --- /dev/null +++ b/cdist/conf/type/__package_pkgng_freebsd/gencode-remote @@ -0,0 +1,134 @@ +#!/bin/sh +# +# 2012 Jake Guffey (jake.guffey at eprotex.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 . +# +# +# Manage packages with pkg on FreeBSD +# + +# Debug +#exec >&2 +#set -x + +if [ -f "$__object/parameter/name" ]; then + name="$(cat "$__object/parameter/name")" +else + name="$__object_id" +fi + +if [ -f "$__object/parameter/flavor" ]; then + flavor="$(cat "$__object/parameter/flavor")" +fi + +if [ -f "$__object/parameter/version" ]; then + version="$(cat "$__object/parameter/version")" +fi + +if [ -f "$__object/parameter/upgrade" ]; then + upgrade="true" +else + upgrade="false" +fi + +if [ -f "$__object/parameter/repo" ]; then + repo="$(cat "$__object/parameter/repo")" +fi + +if [ -f "$__object/parameter/state" ]; then + state="$(cat "$__object/parameter/state")" +else + state="present" +fi +curr_version="$(cat "$__object/explorer/pkg_version")" +add_cmd="pkg install -y" +rm_cmd="pkg delete -y" +upg_cmd="pkg upgrade -y" +cmd="" + +# Print the command to be executed +# Parms: $1 -- mode, "rm", "add", or "upg" +# $2 -- the command to be echoed +execcmd(){ + local _cmd="" + + case "$1" in + add) + _cmd="${add_cmd} $2" + ;; + rm) + _cmd="${rm_cmd} $2" + ;; + upg) + _cmd="${upg_cmd} $2" + ;; + *) + printf "Error. Don't understand command: %s" "$1" >&2 + exit 1 + ;; + esac + + echo "$_cmd 2>&- >&-" # Silence the output of the command + echo "status=\$?" + echo "if [ \"\$status\" -ne \"0\" ]; then" + echo " echo \"Error: ${_cmd} exited nonzero with \$status\"'!' >&2" + echo " exit 1" + echo "fi" +} + +if [ -n "$curr_version" ]; then # PKG *is* installed + if [ -n "$repo" ]; then + cmd="-r ${repo} ${name}" + else + cmd="${name}" + fi + if [ -n "$flavor" ]; then + cmd="${cmd}-${flavor}" + fi + if [ "$state" = "absent" ]; then + execcmd "rm" "${cmd}" + elif [ "$version" != "$curr_version" ]; then + if [ "$upgrade" = "true" ]; then + execcmd "upg" "${cmd}" + else + printf "Version %s is already installed and pkg-ng can't upgrade directly to version %s.\nTo upgrade to the latest version, use the --upgrade flag.\n" "$curr_version" "$version" >&2 + exit 1 + fi + fi +else # PKG *isn't* installed + if [ "$state" = "absent" ]; then # Shouldn't be installed + exit 0 + else # Should be installed + if [ -n "$repo" ]; then + cmd="-r ${repo} ${name}" + else + cmd="${name}" + fi + if [ -n "$flavor" ]; then + cmd="${cmd}-${flavor}" + fi + if [ -n "$version" ]; then + cmd="${cmd}-${version}" + fi + + execcmd "add" "$cmd" + exit 0 + fi +fi + +# Debug +#set +x diff --git a/cdist/conf/type/__package_pkgng_freebsd/man.text b/cdist/conf/type/__package_pkgng_freebsd/man.text new file mode 100644 index 00000000..da44da83 --- /dev/null +++ b/cdist/conf/type/__package_pkgng_freebsd/man.text @@ -0,0 +1,97 @@ +cdist-type__package_pkgng_freebsd(7) +================================== +Jake Guffey + + +NAME +---- +cdist-type__package_pkgng_freebsd - Manage FreeBSD packages with pkg-ng + + +DESCRIPTION +----------- +This type is usually used on FreeBSD to manage packages. + + +REQUIRED PARAMETERS +------------------- +None + + +OPTIONAL PARAMETERS +------------------- +name:: + If supplied, use the name and not the object id as the package name. + +flavor:: + If supplied, use to avoid ambiguity. + +version:: + If supplied, use to install a specific version of the package named. + +repo:: + If supplied, use to install the package named from a particular repo. + +state:: + Either "present" or "absent", defaults to "present" + + +BOOLEAN PARAMETERS +------------------ +upgrade:: + If supplied, allow upgrading to the latest version of a package. + + +CAVEATS +------- +This type requires that repository definitions already exist in /etc/pkg/*.conf. +Ensure that they exist prior to use of this type with __file. + +pkg-ng can't upgrade a package to a specific version. If this type needs to +upgrade a package, it can only ugprade to the latest available version. If the +"upgrade" parameter is not given and an upgrade needs to occur, an error will result. + + +MESSAGES +-------- +install:: + The package was installed +remove:: + The package was removed +upgrade:: + The package was upgraded +exist:: + The package was already present and thus not installed + + +EXAMPLES +-------- + +-------------------------------------------------------------------------------- +# Ensure zsh is installed +__package_pkgng_freebsd zsh --state present + +# Ensure vim is installed, use flavor no_x11 +__package_pkgng_freebsd vim --state present --flavor no_x11 + +# If you don't want to follow pythonX packages, but always use python +__package_pkgng_freebsd python --state present --name python2 + +# Install a package from a particular repository when multiples exist +__package_pkgng_freebsd bash --state present --repo myrepo + +# Remove obsolete package +__package_pkgng_freebsd puppet --state absent +-------------------------------------------------------------------------------- + + +SEE ALSO +-------- +- cdist-type(7) +- cdist-type__package(7) + + +COPYING +------- +Copyright \(C) 2014 Jake Guffey. Free use of this software is +granted under the terms of the GNU General Public License version 3 (GPLv3). diff --git a/cdist/conf/type/__package_pkgng_freebsd/parameter/boolean b/cdist/conf/type/__package_pkgng_freebsd/parameter/boolean new file mode 100644 index 00000000..007ead00 --- /dev/null +++ b/cdist/conf/type/__package_pkgng_freebsd/parameter/boolean @@ -0,0 +1 @@ +upgrade \ No newline at end of file diff --git a/cdist/conf/type/__package_pkgng_freebsd/parameter/optional b/cdist/conf/type/__package_pkgng_freebsd/parameter/optional new file mode 100644 index 00000000..6e67f838 --- /dev/null +++ b/cdist/conf/type/__package_pkgng_freebsd/parameter/optional @@ -0,0 +1,5 @@ +name +flavor +version +repo +state From 37a8b4af2b75ee19c76fe70855ea6941888469f8 Mon Sep 17 00:00:00 2001 From: Jake Guffey Date: Wed, 10 Sep 2014 15:54:13 -0400 Subject: [PATCH 2/3] Minor fixes Fixed pkgng_exists explorer (can't reference $__global) Fixed logic in __package/pkgng_freebsd/gencode-remote Wasn't installing package if it already exists, but after explorer execution --- cdist/conf/type/__package/explorer/pkgng_exists | 4 +++- cdist/conf/type/__package_pkgng_freebsd/gencode-remote | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cdist/conf/type/__package/explorer/pkgng_exists b/cdist/conf/type/__package/explorer/pkgng_exists index 7c4f2256..d0919243 100755 --- a/cdist/conf/type/__package/explorer/pkgng_exists +++ b/cdist/conf/type/__package/explorer/pkgng_exists @@ -21,7 +21,9 @@ # Retrieve the status of a package - parsed dpkg output # -if [ "$(cat "$__global/explorer/os")" = "freebsd" ]; then +# Can't access $__global/explorer/os from here, so recreate the functionality +# because there's no point in executing this explorer on non-FreeBSD targets. +if [ $(uname) = "FreeBSD" ]; then if command -v pkg >&-; then printf "pkg-ng installed" else diff --git a/cdist/conf/type/__package_pkgng_freebsd/gencode-remote b/cdist/conf/type/__package_pkgng_freebsd/gencode-remote index 36af987b..a7ae8b58 100755 --- a/cdist/conf/type/__package_pkgng_freebsd/gencode-remote +++ b/cdist/conf/type/__package_pkgng_freebsd/gencode-remote @@ -99,15 +99,20 @@ if [ -n "$curr_version" ]; then # PKG *is* installed if [ -n "$flavor" ]; then cmd="${cmd}-${flavor}" fi + # PKG is supposed to be removed if [ "$state" = "absent" ]; then execcmd "rm" "${cmd}" - elif [ "$version" != "$curr_version" ]; then + # PKG is supposed to be installed to a particular version + elif [ -n "$version" ] && [ "$version" != "$curr_version" ]; then if [ "$upgrade" = "true" ]; then execcmd "upg" "${cmd}" else printf "Version %s is already installed and pkg-ng can't upgrade directly to version %s.\nTo upgrade to the latest version, use the --upgrade flag.\n" "$curr_version" "$version" >&2 exit 1 fi + # PKG is supposed to be installed to the latest version + else + : # Do nothing. fi else # PKG *isn't* installed if [ "$state" = "absent" ]; then # Shouldn't be installed From 6eff4bb898a7ea7d8bcfd9cd23a3193f9fb6b80a Mon Sep 17 00:00:00 2001 From: Jake Guffey Date: Wed, 17 Sep 2014 17:43:29 -0400 Subject: [PATCH 3/3] Implemented telmich's suggestions Using $($__explorer/os), command -v directly Fix spacing Fix copyright year --- .../conf/type/__package/explorer/pkgng_exists | 10 ++----- cdist/conf/type/__package/manifest | 3 +- .../explorer/pkg_version | 2 +- .../__package_pkgng_freebsd/gencode-remote | 30 +++++++++---------- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/cdist/conf/type/__package/explorer/pkgng_exists b/cdist/conf/type/__package/explorer/pkgng_exists index d0919243..355c5d65 100755 --- a/cdist/conf/type/__package/explorer/pkgng_exists +++ b/cdist/conf/type/__package/explorer/pkgng_exists @@ -21,13 +21,7 @@ # Retrieve the status of a package - parsed dpkg output # -# Can't access $__global/explorer/os from here, so recreate the functionality -# because there's no point in executing this explorer on non-FreeBSD targets. -if [ $(uname) = "FreeBSD" ]; then - if command -v pkg >&-; then - printf "pkg-ng installed" - else - printf "pkg-ng not found" - fi +if [ "$($__explorer/os)" = "freebsd" ]; then + command -v pkg fi diff --git a/cdist/conf/type/__package/manifest b/cdist/conf/type/__package/manifest index 2e085158..3e8928c0 100755 --- a/cdist/conf/type/__package/manifest +++ b/cdist/conf/type/__package/manifest @@ -34,8 +34,7 @@ else archlinux) type="pacman" ;; debian|ubuntu) type="apt" ;; freebsd) - exists="$(cat "$__object/explorer/pkgng_exists")" - if [ "$exists" = "pkg-ng installed" ]; then + if [ -n "$(cat "$__object/explorer/pkgng_exists")" ]; then type="pkgng_freebsd" else type="pkg_freebsd" diff --git a/cdist/conf/type/__package_pkgng_freebsd/explorer/pkg_version b/cdist/conf/type/__package_pkgng_freebsd/explorer/pkg_version index fe940724..947857b9 100755 --- a/cdist/conf/type/__package_pkgng_freebsd/explorer/pkg_version +++ b/cdist/conf/type/__package_pkgng_freebsd/explorer/pkg_version @@ -1,6 +1,6 @@ #!/bin/sh # -# 2012 Jake Guffey (jake.guffey at eprotex.com) +# 2014 Jake Guffey (jake.guffey at eprotex.com) # # This file is part of cdist. # diff --git a/cdist/conf/type/__package_pkgng_freebsd/gencode-remote b/cdist/conf/type/__package_pkgng_freebsd/gencode-remote index a7ae8b58..39965aca 100755 --- a/cdist/conf/type/__package_pkgng_freebsd/gencode-remote +++ b/cdist/conf/type/__package_pkgng_freebsd/gencode-remote @@ -1,6 +1,6 @@ #!/bin/sh # -# 2012 Jake Guffey (jake.guffey at eprotex.com) +# 2014 Jake Guffey (jake.guffey at eprotex.com) # # This file is part of cdist. # @@ -26,27 +26,27 @@ #set -x if [ -f "$__object/parameter/name" ]; then - name="$(cat "$__object/parameter/name")" + name="$(cat "$__object/parameter/name")" else - name="$__object_id" + name="$__object_id" fi if [ -f "$__object/parameter/flavor" ]; then - flavor="$(cat "$__object/parameter/flavor")" + flavor="$(cat "$__object/parameter/flavor")" fi if [ -f "$__object/parameter/version" ]; then - version="$(cat "$__object/parameter/version")" + version="$(cat "$__object/parameter/version")" fi if [ -f "$__object/parameter/upgrade" ]; then - upgrade="true" + upgrade="true" else upgrade="false" fi if [ -f "$__object/parameter/repo" ]; then - repo="$(cat "$__object/parameter/repo")" + repo="$(cat "$__object/parameter/repo")" fi if [ -f "$__object/parameter/state" ]; then @@ -82,15 +82,15 @@ execcmd(){ ;; esac - echo "$_cmd 2>&- >&-" # Silence the output of the command - echo "status=\$?" - echo "if [ \"\$status\" -ne \"0\" ]; then" - echo " echo \"Error: ${_cmd} exited nonzero with \$status\"'!' >&2" - echo " exit 1" - echo "fi" + echo "$_cmd 2>&- >&-" # Silence the output of the command + echo "status=\$?" + echo "if [ \"\$status\" -ne \"0\" ]; then" + echo " echo \"Error: ${_cmd} exited nonzero with \$status\"'!' >&2" + echo " exit 1" + echo "fi" } -if [ -n "$curr_version" ]; then # PKG *is* installed +if [ -n "$curr_version" ]; then # PKG *is* installed if [ -n "$repo" ]; then cmd="-r ${repo} ${name}" else @@ -114,7 +114,7 @@ if [ -n "$curr_version" ]; then # PKG *is* installed else : # Do nothing. fi -else # PKG *isn't* installed +else # PKG *isn't* installed if [ "$state" = "absent" ]; then # Shouldn't be installed exit 0 else # Should be installed