From 3851c247a326d9c826d46bdd24033c7d7902efa8 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Tue, 23 Mar 2021 10:47:39 +0100 Subject: [PATCH] Adding self installation command for fabrica --- configs/nginx/fabrica.conf | 5 ++ configs/nginx/factory.conf | 17 ----- fabrica | 109 ++++++++++++++++++++++++--- terraform/04-virtual-machines.tfvars | 4 +- 4 files changed, 104 insertions(+), 31 deletions(-) create mode 100644 configs/nginx/fabrica.conf delete mode 100644 configs/nginx/factory.conf diff --git a/configs/nginx/fabrica.conf b/configs/nginx/fabrica.conf new file mode 100644 index 0000000..eb0c9d1 --- /dev/null +++ b/configs/nginx/fabrica.conf @@ -0,0 +1,5 @@ +location /fabrica/ { + alias %%ROOT%%/images/; + autoindex on; + allow all; +} \ No newline at end of file diff --git a/configs/nginx/factory.conf b/configs/nginx/factory.conf deleted file mode 100644 index 1670c92..0000000 --- a/configs/nginx/factory.conf +++ /dev/null @@ -1,17 +0,0 @@ -location /factory/ { - alias /srv/factory/images/; - autoindex on; - allow 192.168.10.0/24; - allow 192.168.5.0/24; - deny all; -} -location /factory/images/cadoles/ { - alias /srv/factory/images/cadoles/; - autoindex on; - allow 192.168.5.10; - allow 192.168.5.11; - allow 192.168.5.12; - allow 192.168.5.13; - allow 192.168.10.177/24; - deny all; -} diff --git a/fabrica b/fabrica index 348d4c3..3e56589 100755 --- a/fabrica +++ b/fabrica @@ -4,6 +4,8 @@ WORKDIR="./terraform" TERRA="terraform" PACKER_ROOT="./packer" +[ -f /etc/lsb-release ] && source /etc/lsb-release + # FIXME function needGitPull() { @@ -12,6 +14,85 @@ function needGitPull() return ${diff} } +## Install & Uninstall ## +function install_hashicorp_tools() +{ + if [[ ${DISTRIB_ID} == "ManjaroLinux" ]] + then + pacman -Syq terraform packer --noconfirm + return ${?} + elif [[ ${DISTRIB_ID} == "Ubuntu" ]] + then + 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 terraform packer + 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} + 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 @@ -37,24 +118,28 @@ function trun() 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 @@ -62,33 +147,38 @@ function taint() cd ${WORKDIR} - $TERRA taint module.${resource}.opennebula_template.vm_template - $TERRA taint module.${resource}.opennebula_virtual_machine.vm - $TERRA taint module.${resource}.opennebula_image.system + $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.${resource}.opennebula_template.vm_template - $TERRA taint module.${resource}.opennebula_virtual_machine.vm - $TERRA taint module.${resource}.opennebula_image.system - $TERRA taint module.${resource}.opennebula_image.data + $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 @@ -98,11 +188,6 @@ function build() return ${?} } -function publish() -{ - target=$2 - echo "FIXME for publish" -} $1 $@ exit ${?} diff --git a/terraform/04-virtual-machines.tfvars b/terraform/04-virtual-machines.tfvars index fb70100..9b1c64a 100644 --- a/terraform/04-virtual-machines.tfvars +++ b/terraform/04-virtual-machines.tfvars @@ -16,7 +16,7 @@ virtual_machines = { disks = { system = { name = "prod-amon-static", - source = "http://localhost/fabrica/infra/production/amon.fabrica.local/system/amon.fabrica.local" + source = "http://localhost/fabrica/infra/production/amon.fabrica.local/system/amon.fabrica.local.img" } } depends = [ "internet" ] @@ -37,7 +37,7 @@ virtual_machines = { disks = { system = { name = "prod-scribe-static", - source = "http://localhost/fabrica/infra/production/scribe.fabrica.local/system/scribe.fabrica.local" + source = "http://localhost/fabrica/infra/production/scribe.fabrica.local/system/scribe.fabrica.local.img" } } depends = [ "internet" ]