fabrica/fabrica

205 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
WORKDIR="./.terraform"
TERRA="terraform"
PACKER_ROOT="./.packer"
[ -f /etc/lsb-release ] && source /etc/lsb-release
# FIXME
function needGitPull()
{
br=$(git branch --show-current)
diff=$(git rev-list HEAD...origin/master --count)
return ${diff}
}
## Install & Uninstall ##
function install_hashicorp_tools()
{
packer_version="1.6.1"
terraform_version="0.14.3"
if [[ ${DISTRIB_ID} == "ManjaroLinux" ]]
then
pacman -Syq make terraform packer --noconfirm
return ${?}
elif [[ ${DISTRIB_ID} == "Ubuntu" ]]
then
apt-get -y install software-properties-common make
gpg_key_url="https://apt.releases.hashicorp.com/gpg"
curl -fsSL ${gpg_key_url} | apt-key add -
apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
apt-get update
apt-get -y install --allow-downgrades terraform=${terraform_version} packer=${packer_version}
return ${?}
fi
}
function setup_nginx()
{
root=${1}
if [ -d /etc/nginx/web.d ]
then
sed -i -e "s!%%ROOT%%!${root}!g" ${root}/.configs/nginx/fabrica.conf
ln -s ${root}/.configs/nginx/fabrica.conf /etc/nginx/web.d/
echo "Don't forget to restart nginx"
fi
return 0
}
function install_fabrica()
{
root=${1}
[ ! -d ${root} ] && mkdir -p $root
cp -rp * ${root}
cp -rp .packer ${root}
cp -rp .terraform ${root}
cp -rp .configs ${root}
return ${?}
}
function install()
{
ROOT=${2}
ret=0
if [[ -z ${ROOT} ]]
then
echo "You need to provide an installation directory as paramter"
echo "./fabrica install /srv/fabrica"
return 1
fi
if [[ -e ${ROOT}/.ist ]]
then
echo "Flag file ${ROOT}/.ist exists !"
echo " ==> Fabrica is allready installed !"
echo "Nothing to do !"
return 0
fi
install_hashicorp_tools
ret=$((ret+${?}))
install_fabrica ${ROOT}
ret=$((ret+${?}))
setup_nginx ${ROOT}
ret=$((ret+${?}))
if [ ${ret} -eq 0 ]
then
touch ${ROOT}/.ist
fi
return ${ret}
}
## TERRAFORM ##
# Run Terraform command with the correct options
function trun()
{
#needGitPull
cd ${WORKDIR}
if [[ ${?} -ne 0 ]]
then
echo "Branch is not up 2 date !"
echo "--> Git pull needed !"
return 1
fi
VAR_FILES=$(ls tfvars/*.tfvars)
OPTS=""
for fl in ${VAR_FILES}
do
OPTS="${OPTS} -var-file ${fl}"
done
${TERRA} ${1} ${OPTS}
res=${?}
cd -
return ${res}
}
# Run terraform plan
function plan()
{
trun plan
return $?
}
# Run terraform apply
function apply()
{
trun apply
return $?
}
# Run terraform destroy
function destroy()
{
trun destroy
return $?
}
# Run terraform taint
function taint()
{
target=$1
resource=$2
cd ${WORKDIR}
$TERRA taint module.vms[${resource}].opennebula_template.vm_template
$TERRA taint module.vms[${resource}].opennebula_virtual_machine.vm
$TERRA taint module.vms[${resource}].opennebula_image.system
cd -
}
# Run terraform taint for all resources
function taint_all()
{
target=$1
resource=$2
cd ${WORKDIR}
$TERRA taint module.vms[${resource}].opennebula_template.vm_template
$TERRA taint module.vms[${resource}].opennebula_virtual_machine.vm
$TERRA taint module.vms[${resource}].opennebula_image.system
$TERRA taint module.vms[${resource}].opennebula_image.data
cd -
}
# Run terraform init
function init()
{
trun init
return ${?}
}
## PACKER ##
# Run packer build
function build()
{
target=$2
cd ${PACKER_ROOT}
echo "Building ${target}"
make ${target}
return ${?}
}
# List available recipes
function recipes()
{
echo "Need to be done"
}
$1 $@
exit ${?}