]> gitweb.fluxo.info Git - hydra.git/commitdiff
Common hydra_deploy_setup
authorSilvio Rhatto <rhatto@riseup.net>
Sat, 26 Sep 2015 12:33:07 +0000 (09:33 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Sat, 26 Sep 2015 12:33:07 +0000 (09:33 -0300)
lib/hydra/deploy [new file with mode: 0644]
share/hydra/deploy
share/hydractl/deploy

diff --git a/lib/hydra/deploy b/lib/hydra/deploy
new file mode 100644 (file)
index 0000000..f537c42
--- /dev/null
@@ -0,0 +1,101 @@
+#!/bin/bash
+
+# Setup deployment parameters
+# TODO: check environment passing to sudo, chroot and ssh
+function hydra_deploy_setup {
+  # Common parameters
+  # Exclude eventual keys and version control files
+  DEPLOY_RSYNC="rsync -CrltDv --no-perms --exclude=keys --exclude=hiera/secrets --delete"
+
+  if [ "$1" == "remote" ]; then
+    # Deploy in a local folder
+    if [ ! -z "$2" ]; then
+      NODE="$2"
+      DEPLOY_COMMAND="$HYDRA_CONNECT $NODE sudo"
+      DEPLOY_RSYNC="$DEPLOY_RSYNC --rsync-path \"sudo rsync\" $HYDRA_FOLDER/puppet/ $NODE:/etc/puppet/"
+      FQDN="`$DEPLOY_COMMAND facter fqdn`"
+      DEPLOY_DEST="$FQDN:"
+      DEPLOY_COPY="$DEPLOY_RSYNC"
+      PUPPET_MANIFEST="/etc/puppet/manifests/nodes/$FQDN.pp"
+    else
+      echo "No folder specified."
+      exit 1
+    fi
+  elif [ "$1" == "folder" ]; then
+    # Deploy in a remote host
+    if [ ! -z "$2" ]; then
+      FOLDER="$2"
+      DEPLOY_COMMAND="$SUDO chroot $FOLDER"
+      DEPLOY_RSYNC="$DEPLOY_RSYNC $HYDRA_FOLDER/puppet/ $FOLDER/etc/puppet/"
+      DEPLOY_COPY="$SUDO cp"
+      DEPLOY_DEST="$FOLDER"
+      PUPPET_MANIFEST="/etc/puppet/manifests/nodes/$FQDN.pp"
+
+      if [ ! -d "$FOLDER"]; then
+        echo "folder not found: $FOLDER"
+        exit 1
+      fi
+
+      # Fix hostname
+      if [ -s "$FOLDER/etc/hostname" ]; then
+        FQDN="`cat $FOLDER/etc/hostname`"
+      fi
+    else
+      echo "No node specified."
+      exit 1
+    fi
+  else
+    # Deploy on the localhost
+    DEPLOY_COMMAND="$SUDO"
+    FQDN="`facter fqdn`"
+    PUPPET_OPTS="--confdir=$HYDRA_FOLDER/puppet --modulepath=$HYDRA_FOLDER/modules"
+    PUPPET_MANIFEST="$HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp"
+  fi
+
+  # Common parameters
+  DOMAIN="`echo $FQDN | cut -d . -f 2-`"
+  ROLE="`hydra_yaml_param nodo::role $HYDRA_FOLDER/$DOMAIN/$FQDN.yaml`"
+  LOCATION="`hydra_yaml_param nodo::location $HYDRA_FOLDER/$DOMAIN/$FQDN.yaml`"
+  DEPLOY_ENV="LC_ALL=C FACTER_role=$ROLE FACTER_location=$LOCATION"
+  DEPLOY_PUPPET="$DEPLOY_ENV puppet appy $PUPPET_OPTS $PUPPET_MANIFEST"
+  DEPLOY_APPLY="$DEPLOY_COMMAND $DEPLOY_PUPPET"
+
+  # Check for manifest
+  if [ ! -e "$HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp" ]; then
+    echo "Not found: $HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp"
+    exit 1
+  fi
+}
+
+# Return a simple parameter from an YAML file
+function hydra_yaml_param {
+  local param="$1"
+  local file="$2"
+
+  grep "^$param: " $file | cut -d ' ' -f 2 | sed -e "s/'//g" -e 's/"//g'
+}
+
+# Create puppet folder structure
+function hydra_deploy_mkdirs {
+  # Saner defaults
+  $DEPLOY_COMMAND mkdir -p       /etc/puppet
+  $DEPLOY_COMMAND chown -R root. /etc/puppet
+  $DEPLOY_COMMAND chmod -R 640   /etc/puppet
+}
+
+# Create hiera folder structure
+function hydra_hiera_copy {
+  $DEPLOY_COMMAND mkdir -p $FOLDER/etc/puppet/secrets/{domain,location,node,role}
+
+  if [ ! -z "$DOMAIN" ] && [ -e "$HYDRA_FOLDER/puppet/hiera/secrets/domain/$DOMAIN.yaml" ]; then
+    $DEPLOY_COPY $HYDRA_FOLDER/puppet/hiera/secrets/domain/$DOMAIN.yaml $DEPLOY_DEST/etc/puppet/hiera/secrets/domain/
+  fi
+
+  if [ ! -z "$LOCATION" ] && [ -e "$HYDRA_FOLDER/puppet/hiera/secrets/domain/$LOCATION.yaml" ]; then
+    $DEPLOY_COPY $HYDRA_FOLDER/puppet/hiera/secrets/location/$LOCATION.yaml $DEPLOY_DEST/etc/puppet/hiera/secrets/location/
+  fi
+
+  if [ ! -z "$ROLE" ] && [ -e "$HYDRA_FOLDER/puppet/hiera/secrets/domain/$ROLE.yaml" ]; then
+    $DEPLOY_COPY $HYDRA_FOLDER/puppet/hiera/secrets/location/$ROLE.yaml $DEPLOY_DEST/etc/puppet/hiera/secrets/role/
+  fi
+}
index 940c17bdc65abf1c9a1b9805f8e6f7f1ff81286e..0afd879eaf285a2005a494237a257b0c5955dd52 100755 (executable)
@@ -32,40 +32,16 @@ fi
 for node in $NODES; do
   echo "Deploying to $node..."
 
-  # Saner defaults
-  $HYDRA_CONNECT $node sudo mkdir -p       /etc/puppet
-  $HYDRA_CONNECT $node sudo chown -R root. /etc/puppet
-  $HYDRA_CONNECT $node sudo chmod -R 640   /etc/puppet
+  # Setup deploy environment
+  hydra_deploy_setyp remote $node
+  hydra_deploy_mkdirs
 
   # Sync repository to server
-  # Exclude eventual keys and version control files
-  rsync -CrltDv --no-perms --exclude=keys --exclude=hiera/secrets --delete --rsync-path "sudo rsync" $HYDRA_FOLDER/puppet/ $node:/etc/puppet/
+  $DEPLOY_RSYNC
 
-  # Setup custom facts
-  FQDN="`$HYDRA_CONNECT $node facter fqdn`"
-  DOMAIN="`echo $FQDN | cut -d . -f 2-`"
-  ROLE="`grep "^nodo::role:         " domain/$DOMAIN/$FQDN.yaml | cut -d ' ' -f 2 | sed -e "s/'//g" -e 's/"//g'`"
-  LOCATION="`grep "^nodo::location: " domain/$DOMAIN/$FQDN.yaml | cut -d ' ' -f 2 | sed -e "s/'//g" -e 's/"//g'`"
-
-  # Check for manifest
-  if [ ! -e "$HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp" ]; then
-    echo "Not found: $HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp"
-    exit 1
-  fi
-
-  # Copy needed hiera YAMLs
-  $HYDRA_CONNECT $node sudo mkdir -p $FOLDER/etc/puppet/secrets/{domain,location,node,role}
-  $HYDRA_CONNECT $node sudo cp $HYDRA_FOLDER/puppet/hiera/secrets/domain/$DOMAIN.yaml     $FOLDER/etc/puppet/hiera/domain/
-  $HYDRA_CONNECT $node sudo cp $HYDRA_FOLDER/puppet/hiera/secrets/location/$LOCATION.yaml $FOLDER/etc/puppet/hiera/location/
-  $HYDRA_CONNECT $node sudo cp $HYDRA_FOLDER/puppet/hiera/secrets/location/$ROLE.yaml     $FOLDER/etc/puppet/hiera/role/
+  # Copy hiera configuration
+  hydra_hiera_copy
 
   # Run puppet
-  $HYDRA_CONNECT $node <<EOF
-  ##### BEGIN REMOTE SCRIPT #####
-  sudo chown -R root. /etc/puppet
-  sudo chmod -R 640   /etc/puppet
-  LC_ALL=C FACTER_role=$ROLE FACTER_location=$LOCATION \
-  sudo puppet apply /etc/puppet/manifests/nodes/$FQDN.pp
-  ##### END REMOTE SCRIPT #######
-EOF
+  $DEPLOY_APPLY
 done
index 9c82ddf4ade97c19ae6447b7ff52f18725bc6553..b48869887cf59a4ed57007bafd75ea831f42a0c1 100755 (executable)
@@ -22,7 +22,6 @@ hydra_config_load
 
 # Parameters
 FOLDER="$1"
-FQDN="`facter fqdn`"
 
 # Set sudo config
 local sudo device rsync
@@ -32,55 +31,27 @@ fi
 
 # Dispatch
 if [ ! -z "$FOLDER" ]; then
-  if [ ! -d "$FOLDER"]; then
-    echo "folder not found: $FOLDER"
-    exit 1
-  fi
-
-  # Fix hostname
-  if [ -s "$FOLDER/etc/hostname" ]; then
-    FQDN="`cat $FOLDER/etc/hostname`"
-  fi
-
   echo "Deploying to $FOLDER..."
 
-  # Saner defaults
-  $sudo mkdir -p       $FOLDER/etc/puppet
-  $sudo chown -R root. $FOLDER/etc/puppet
-  $sudo chmod -R 640   $FOLDER/etc/puppet
+  # Setup deploy environment
+  hydra_deploy_setyp remote $node
+  hydra_deploy_mkdirs
 
   # Sync repository to server
-  # Exclude eventual keys and version control files
-  $sudo rsync -CrltDv --no-perms --exclude=keys --exclude=hiera/secrets --delete $HYDRA_FOLDER/puppet/ $FOLDER/etc/puppet/
-
-  # Setup custom facts
-  DOMAIN="`echo $FQDN | cut -d . -f 2-`"
-  ROLE="`grep "^nodo::role:         " domain/$DOMAIN/$FQDN.yaml | cut -d ' ' -f 2 | sed -e "s/'//g" -e 's/"//g'`"
-  LOCATION="`grep "^nodo::location: " domain/$DOMAIN/$FQDN.yaml | cut -d ' ' -f 2 | sed -e "s/'//g" -e 's/"//g'`"
+  $DEPLOY_RSYNC
 
-  # Copy needed hiera YAMLs
-  $sudo mkdir -p $FOLDER/etc/puppet/secrets/{domain,location,node,role}
-  $sudo cp $HYDRA_FOLDER/puppet/hiera/secrets/domain/$DOMAIN.yaml     $FOLDER/etc/puppet/hiera/domain/
-  $sudo cp $HYDRA_FOLDER/puppet/hiera/secrets/location/$LOCATION.yaml $FOLDER/etc/puppet/hiera/location/
-  $sudo cp $HYDRA_FOLDER/puppet/hiera/secrets/location/$ROLE.yaml     $FOLDER/etc/puppet/hiera/role/
-
-  # Check for manifest
-  if [ ! -e "$HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp" ]; then
-    echo "Not found: $HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp"
-    exit 1
-  fi
+  # Copy hiera configuration
+  hydra_hiera_copy
 
   # Run puppet
-  LC_ALL=C FACTER_role=$ROLE FACTER_location=$LOCATION \
-  $sudo chroot $FOLDER puppet apply /etc/puppet/manifests/nodes/$FQDN.pp
+  $DEPLOY_APPLY
 elif [ -e "$HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp" ]; then
-  # Setup custom facts
-  DOMAIN="`echo $FQDN | cut -d . -f 2-`"
-  ROLE="`grep "^nodo::role:         " domain/$DOMAIN/$FQDN.yaml | cut -d ' ' -f 2 | sed -e "s/'//g" -e 's/"//g'`"
-  LOCATION="`grep "^nodo::location: " domain/$DOMAIN/$FQDN.yaml | cut -d ' ' -f 2 | sed -e "s/'//g" -e 's/"//g'`"
+  echo "Deploying to localhost..."
+
+  # Setup deploy environment
+  hydra_deploy_setup
 
+  # TODO: override FQDN and DOMAIN
   # Run puppet
-  LC_ALL=C FACTER_role=$ROLE FACTER_location=$LOCATION \
-  $sudo puppet apply --confdir=$HYDRA_FOLDER/puppet    \
-                     --modulepath=$HYDRA_FOLDER/modules $HYDRA_FOLDER/puppet/manifests/nodes/$FQDN.pp
+  $DEPLOY_APPLY
 fi