diff options
author | Silvio Rhatto <rhatto@riseup.net> | 2015-09-26 09:33:07 -0300 |
---|---|---|
committer | Silvio Rhatto <rhatto@riseup.net> | 2015-09-26 09:33:07 -0300 |
commit | f5ddf7507d440bf3642777d159a0a60ff55d0c5b (patch) | |
tree | a1535b01f5b2d2c49fd3449b534aa8b467f0d2b8 /lib | |
parent | 6542f3827dc1d675a2b129622b6b089a693ac0ca (diff) | |
download | hydra-f5ddf7507d440bf3642777d159a0a60ff55d0c5b.tar.gz hydra-f5ddf7507d440bf3642777d159a0a60ff55d0c5b.tar.bz2 |
Common hydra_deploy_setup
Diffstat (limited to 'lib')
-rw-r--r-- | lib/hydra/deploy | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/lib/hydra/deploy b/lib/hydra/deploy new file mode 100644 index 0000000..f537c42 --- /dev/null +++ b/lib/hydra/deploy @@ -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 +} |