Skip to content

Instantly share code, notes, and snippets.

@frgomes
Last active March 3, 2021 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frgomes/4cb640a4507976f50a3b5847f8fd77d1 to your computer and use it in GitHub Desktop.
Save frgomes/4cb640a4507976f50a3b5847f8fd77d1 to your computer and use it in GitHub Desktop.
salt: Create Salt installation in a virtual environment
#!/bin/bash -ex
# This script creates a Salt installation inside a virtual environment so that
# a steteful infrastructure aiming customer A is absolutely oblivious of customer B.
#
# No changes are required on configuration files, so that you can be sure that
# whatever you keep in the source control is valid in production. All you have to do
# in production is copying the trees /etc/salt and /srv/salt from the source control
# to their glorified places /etc/salt and /srv/salt in production.
#
#see: https://docs.saltproject.io/en/3001/topics/development/hacking.html
function setup() {
local here=$(dirname $(readlink -f "${BASH_SOURCE[0]}"))
local url=${1:-http://github.com/${USER}/salt}
local branch=${2}
if [[ -z ${VIRTUAL_ENV} ]] ;then
echo "ERROR: You are not using any virtual environment at the moment."
echo "INFO: First start working on the virtual environment of your choice."
echo "INFO: Then run this script again for setting up Salt properly."
return 1
else
# Step 1 :: clone and pull updates
[[ -d ${HOME}/workspace ]] || mkdir -p ${HOME}/workspace
if [[ ! -d ${HOME}/workspace/salt ]] ;then
git -C ${HOME}/workspace/salt clone ${url}
git -C ${HOME}/workspace/salt remote add upstream https://github.com/saltstack/salt
git -C ${HOME}/workspace/salt fetch upstream
fi
git -C ${HOME}/workspace/salt checkout master
git -C ${HOME}/workspace/salt pull
if [[ ! -z "${branch}" ]] ;then
if [[ $(git -C ${HOME}/workspace/salt branch -a | fgrep ${branch}) ]] ;then
git -C ${HOME}/workspace/salt checkout ${branch}
else
git -C ${HOME}/workspace/salt checkout -b ${branch} master
fi
fi
# Step 2 :: Create default configuration for current project, if needed
# create ${here}/etc/salt, if necessary
[[ -d ${here}/etc/salt ]] || mkdir -p ${here}/etc/salt
[[ -d ${here}/etc/salt/master ]] || cp -rp ${HOME}/workspace/salt/conf/master ${here}/etc/salt/
[[ -d ${here}/etc/salt/minion ]] || cp -rp ${HOME}/workspace/salt/conf/minion ${here}/etc/salt/
[[ -d ${here}/etc/salt/pki/master ]] || mkdir -p ${here}/etc/salt/pki/master
[[ -d ${here}/etc/salt/pki/minion ]] || mkdir -p ${here}/etc/salt/pki/minion
# create ${here}/srv/salt, if necessary
[[ -d ${here}/srv/salt ]] || mkdir -p ${here}/srv/salt/{keys,states,formulas,grains,reactor}
# create symbolic links to ${here}/etc/salt and ${here}/srv/salt
[[ -d ${VIRTUAL_ENV}/etc ]] || mkdir -p ${VIRTUAL_ENV}/etc
[[ -d ${VIRTUAL_ENV}/srv ]] || mkdir -p ${VIRTUAL_ENV}/srv
[[ -L ${VIRTUAL_ENV}/etc/salt ]] || ln -s ${here}/etc/salt ${VIRTUAL_ENV}/etc
[[ -L ${VIRTUAL_ENV}/srv/salt ]] || ln -s ${here}/srv/salt ${VIRTUAL_ENV}/srv
[[ -L ${VIRTUAL_ENV}/srv/pillar ]] || ln -s ${here}/srv/pillar ${VIRTUAL_ENV}/srv
# Step 3 :: Build and deploy Salt onto a virtual environment
pushd ${HOME}/workspace/salt/
MIMIC_SALT_INSTALL=1 ./setup.py --salt-root-dir=${VIRTUAL_ENV} build
MIMIC_SALT_INSTALL=1 ./setup.py --salt-root-dir=${VIRTUAL_ENV} install
popd
fi
}
if [[ ("${1}" == "-h") || ("$1" == "--help") ]] ;then
echo 'setup.sh [ url [ branch ] ]'
echo 'where:'
echo ' url your cloned Salt source tree. Default: http://github.con/${USER}/salt'
echo ' branch a new branch to be created from master. Default: does not create a branch'
else
setup $*
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment