From 9a832d88b57b33e2e21b3e532b7fab027c291b1e Mon Sep 17 00:00:00 2001
From: Darko Poljak <darko.poljak@gmail.com>
Date: Thu, 15 Feb 2018 15:45:31 +0100
Subject: [PATCH] Add option for directly downloading on target host.

---
 .../type/__consul/files/versions/1.0.6/cksum  |  1 +
 .../type/__consul/files/versions/1.0.6/source |  1 +
 cdist/conf/type/__consul/gencode-remote       | 59 +++++++++++++++++++
 cdist/conf/type/__consul/man.rst              | 15 ++++-
 cdist/conf/type/__consul/manifest             | 23 +++++---
 cdist/conf/type/__consul/parameter/boolean    |  1 +
 .../type/__consul/parameter/default/version   |  2 +-
 7 files changed, 90 insertions(+), 12 deletions(-)
 create mode 100644 cdist/conf/type/__consul/files/versions/1.0.6/cksum
 create mode 100644 cdist/conf/type/__consul/files/versions/1.0.6/source
 create mode 100755 cdist/conf/type/__consul/gencode-remote
 create mode 100644 cdist/conf/type/__consul/parameter/boolean

diff --git a/cdist/conf/type/__consul/files/versions/1.0.6/cksum b/cdist/conf/type/__consul/files/versions/1.0.6/cksum
new file mode 100644
index 00000000..b70b55f4
--- /dev/null
+++ b/cdist/conf/type/__consul/files/versions/1.0.6/cksum
@@ -0,0 +1 @@
+4120550353 48801129 consul
diff --git a/cdist/conf/type/__consul/files/versions/1.0.6/source b/cdist/conf/type/__consul/files/versions/1.0.6/source
new file mode 100644
index 00000000..769d3134
--- /dev/null
+++ b/cdist/conf/type/__consul/files/versions/1.0.6/source
@@ -0,0 +1 @@
+https://releases.hashicorp.com/consul/1.0.6/consul_1.0.6_linux_amd64.zip
diff --git a/cdist/conf/type/__consul/gencode-remote b/cdist/conf/type/__consul/gencode-remote
new file mode 100755
index 00000000..5b3f5573
--- /dev/null
+++ b/cdist/conf/type/__consul/gencode-remote
@@ -0,0 +1,59 @@
+#!/bin/sh -e
+#
+# 2018 Darko Poljak (darko.poljak 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 <http://www.gnu.org/licenses/>.
+#
+
+#set -x
+
+if [ ! -f "$__object/parameter/direct" ]; then
+    # Nothing here, staged file is used.
+    exit 0
+fi
+
+state=$(cat "$__object/parameter/state")
+destination="/usr/local/bin/consul"
+
+if [ "$state" = "absent" ]; then
+    printf 'rm -f "%s"' "$destination"
+    exit 0
+fi
+
+versions_dir="$__type/files/versions"
+version="$(cat "$__object/parameter/version")"
+version_dir="$versions_dir/$version"
+
+source=$(cat "$version_dir/source")
+source_file_name="${source##*/}"
+cksum_should=$(cat "$version_dir/cksum" | cut -d' ' -f1,2)
+
+cat << eof
+    tmpdir=\$(mktemp -d --tmpdir="/tmp" "${__type##*/}.XXXXXXXXXX")
+    curl -s -L "$source" > "\$tmpdir/$source_file_name"
+    unzip -p "\$tmpdir/$source_file_name" > "$destination"
+    rm -rf "\$tmpdir"
+
+    cksum_is=\$(cksum "$destination" | cut -d' ' -f1,2)
+    if [ "\$cksum_is" = "$cksum_should" ]; then
+        chown root:root "$destination"
+        chmod 755 "$destination"
+    else
+        rm -f "$destination"
+        echo "Failed to verify checksum for $__object_name" >&2
+        exit 1
+    fi
+eof
diff --git a/cdist/conf/type/__consul/man.rst b/cdist/conf/type/__consul/man.rst
index 19ceb535..09e547f9 100644
--- a/cdist/conf/type/__consul/man.rst
+++ b/cdist/conf/type/__consul/man.rst
@@ -10,7 +10,8 @@ DESCRIPTION
 -----------
 Downloads and installs the consul binary from https://dl.bintray.com/mitchellh/consul.
 Note that the consul binary is downloaded on the server (the machine running
-cdist) and then deployed to the target host using the __file type.
+cdist) and then deployed to the target host using the __file type unless --direct
+parameter is used.
 
 
 REQUIRED PARAMETERS
@@ -28,6 +29,12 @@ version
    supported versions. Defaults to the latest known version.
 
 
+BOOLEAN PARAMETERS
+------------------
+direct
+    Download and deploy consul binary directly on the target machine.
+
+
 EXAMPLES
 --------
 
@@ -36,6 +43,9 @@ EXAMPLES
     # just install using defaults
     __consul
 
+    # install by downloading consul binary directly on the target machine
+    __consul --direct
+
     # specific version
     __consul \
        --version 0.4.1
@@ -43,7 +53,8 @@ EXAMPLES
 
 AUTHORS
 -------
-Steven Armstrong <steven-cdist--@--armstrong.cc>
+| Steven Armstrong <steven-cdist--@--armstrong.cc>
+| Darko Poljak <darko.poljak--@--gmail.com>
 
 
 COPYING
diff --git a/cdist/conf/type/__consul/manifest b/cdist/conf/type/__consul/manifest
index cd79e5d9..0dd50f53 100755
--- a/cdist/conf/type/__consul/manifest
+++ b/cdist/conf/type/__consul/manifest
@@ -2,6 +2,7 @@
 #
 # 2015 Steven Armstrong (steven-cdist at armstrong.cc)
 # 2016 Nico Schottelius (nico-cdist at schottelius.org)
+# 2018 Darko Poljak (darko.poljak at gmail.com)
 #
 # This file is part of cdist.
 #
@@ -44,12 +45,16 @@ if [ ! -d "$version_dir" ]; then
    exit 1
 fi
 
-__staged_file /usr/local/bin/consul \
-   --source "$(cat "$version_dir/source")" \
-   --cksum "$(cat "$version_dir/cksum")" \
-   --fetch-command 'curl -s -L "%s"' \
-   --prepare-command 'unzip -p "%s"' \
-   --state "$(cat "$__object/parameter/state")" \
-   --group root \
-   --owner root \
-   --mode 755
+if [ -f "$__object/parameter/direct" ]; then
+    __package unzip
+else
+    __staged_file /usr/local/bin/consul \
+       --source "$(cat "$version_dir/source")" \
+       --cksum "$(cat "$version_dir/cksum")" \
+       --fetch-command 'curl -s -L "%s"' \
+       --prepare-command 'unzip -p "%s"' \
+       --state "$(cat "$__object/parameter/state")" \
+       --group root \
+       --owner root \
+       --mode 755
+fi
diff --git a/cdist/conf/type/__consul/parameter/boolean b/cdist/conf/type/__consul/parameter/boolean
new file mode 100644
index 00000000..aa81b5e0
--- /dev/null
+++ b/cdist/conf/type/__consul/parameter/boolean
@@ -0,0 +1 @@
+direct
diff --git a/cdist/conf/type/__consul/parameter/default/version b/cdist/conf/type/__consul/parameter/default/version
index d2b13eb6..af0b7ddb 100644
--- a/cdist/conf/type/__consul/parameter/default/version
+++ b/cdist/conf/type/__consul/parameter/default/version
@@ -1 +1 @@
-0.6.4
+1.0.6