From f75f2a0de5bf3bd976ee68c336fca530a74a06d9 Mon Sep 17 00:00:00 2001
From: Nico Schottelius <nico@kr.ethz.ch>
Date: Thu, 3 Mar 2011 09:43:04 +0100
Subject: [PATCH] Cleanup local and remote base_dir before execution

Signed-off-by: Nico Schottelius <nico@kr.ethz.ch>
---
 bin/cdist-config    | 16 ++++++++++++++++
 bin/cdist-deploy-to | 14 ++++----------
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/bin/cdist-config b/bin/cdist-config
index 98d8abf7..e56cd77a 100755
--- a/bin/cdist-config
+++ b/bin/cdist-config
@@ -128,6 +128,22 @@ __cdist_usage()
    __cdist_exit_err "$__cdist_myname: $@"
 }
 
+__cdist_init_deploy()
+{
+   echo "Creating clean directory structur ..."
+
+   # Ensure there is no old stuff, neither local nor remote
+   rm -rf "$__cdist_local_base_dir"
+   ssh "${__cdist_remote_user}@$1" "rm -rf ${__cdist_remote_base_dir}"
+
+   # Init base
+   mkdir -p "$__cdist_local_base_dir"
+   ssh "${__cdist_remote_user}@$1" "mkdir -p ${__cdist_remote_base_dir}"
+
+   # Link configuration source directory - consistent with remote
+   ln -sf "$__cdist_conf_dir" "$__cdist_local_base_dir/$__cdist_name_conf_dir"
+}
+
 # __cdist_cache_host()
 # {
 #    echo "${__cdist_cache_hosts}/${__cdist_target_host}"
diff --git a/bin/cdist-deploy-to b/bin/cdist-deploy-to
index 68c86be9..5c78f695 100755
--- a/bin/cdist-deploy-to
+++ b/bin/cdist-deploy-to
@@ -33,20 +33,14 @@ export $__cdist_name_var_target_host="$__cdist_target_host"
 # Export variables for core, which others do not reset
 export __cdist_local_base_dir
 
+echo "cdist $__cdist_version: Configuring $__cdist_target_host"
+
 ################################################################################
 # See cdist-stages(7)
 #
 
-echo "cdist $__cdist_version: Configuring $__cdist_target_host"
-
-# Init base
-mkdir -p "$__cdist_local_base_dir"
-
-# Ensure there is no old link, otherwise the new link is created below the old
-rm -f "$__cdist_local_base_dir/$__cdist_name_conf_dir"
-
-# Link configuration source directory - consistent with remote
-ln -sf "$__cdist_conf_dir" "$__cdist_local_base_dir/$__cdist_name_conf_dir"
+# Prepare local and remote directories
+__cdist_init_deploy "$__cdist_target_host"
 
 # Transfer cdist "binaries"
 cdist-bin-transfer            "$__cdist_target_host" "$__cdist_remote_bin_dir"