|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# debops-install: install DebOps playbooks and roles |
| 4 | +# Copyright (C) 2014 Maciej Delmanowski <drybjed@gmail.com> |
| 5 | +# Part of the DebOps project - http://debops.org/ |
| 6 | + |
| 7 | + |
| 8 | +# This program is free software; you can redistribute |
| 9 | +# it and/or modify it under the terms of the |
| 10 | +# GNU General Public License as published by the Free |
| 11 | +# Software Foundation; either version 2 of the License, |
| 12 | +# or (at your option) any later version. |
| 13 | +# |
| 14 | +# This program is distributed in the hope that it will |
| 15 | +# be useful, but WITHOUT ANY WARRANTY; without even the |
| 16 | +# implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 17 | +# PARTICULAR PURPOSE. See the GNU General Public |
| 18 | +# License for more details. |
| 19 | +# |
| 20 | +# You should have received a copy of the GNU General |
| 21 | +# Public License along with this program; if not, |
| 22 | +# write to the Free Software Foundation, Inc., 59 |
| 23 | +# Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | +# |
| 25 | +# An on-line copy of the GNU General Public License can |
| 26 | +# be downloaded from the FSF web page at: |
| 27 | +# http://www.gnu.org/copyleft/gpl.html |
| 28 | + |
| 29 | + |
| 30 | +# This is an installer script which should download DebOps playbooks and roles |
| 31 | +# to user $HOME directory if they are not found either in directory relative to |
| 32 | +# $PWD or DebOps is not installed system-wide. |
| 33 | + |
| 34 | + |
| 35 | +set -e |
| 36 | + |
| 37 | +# ---- Variable definitions ---- |
| 38 | + |
| 39 | +# Set some global constants |
| 40 | +declare -r DEBOPS_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}/debops" |
| 41 | +declare -r DEBOPS_CONFIG=".debops.cfg" |
| 42 | +declare -r SCRIPT_NAME="$(basename ${0})" |
| 43 | + |
| 44 | +# Paths to look through if 'debops.cfg' is found in local directory |
| 45 | +DEBOPS_PLAYBOOKS_PWD_PATHS=( |
| 46 | + "${PWD}/debops-playbooks/playbooks" |
| 47 | +) |
| 48 | + |
| 49 | +# Paths to look through if local install is not found |
| 50 | +DEBOPS_PLAYBOOKS_INSTALL_PATHS=( |
| 51 | + "${DEBOPS_DATA_HOME}/debops-playbooks/playbooks" |
| 52 | + "/usr/local/share/debops/debops-playbooks/playbooks" |
| 53 | + "/usr/share/debops/debops-playbooks/playbooks" |
| 54 | +) |
| 55 | + |
| 56 | +# Default installation directory |
| 57 | +DEBOPS_DEFAULT_INSTALL_PATH="${DEBOPS_DATA_HOME}/debops-playbooks" |
| 58 | + |
| 59 | +# Default git sources for debops-playbooks |
| 60 | +DEBOPS_GIT_URI="https://github.com/debops/debops-playbooks" |
| 61 | + |
| 62 | +# Ansible Galaxy requirements file to use by default to download or update |
| 63 | +# Ansible roles, relative to debops-playbooks repository |
| 64 | +DEBOPS_GALAXY_REQUIREMENTS="galaxy/requirements.txt" |
| 65 | + |
| 66 | +# Path to install roles, relative to debops-playbooks repository |
| 67 | +DEBOPS_GALAXY_ROLES="playbooks/roles/" |
| 68 | + |
| 69 | + |
| 70 | +# ---- Main installer script ---- |
| 71 | + |
| 72 | +# Check if playbooks are installed in local directory |
| 73 | +if [ -f ${PWD}/${DEBOPS_CONFIG} ] ; then |
| 74 | + for playbook_path in "${DEBOPS_PLAYBOOKS_PWD_PATHS[@]}" ; do |
| 75 | + if [ -f ${playbook_path}/site.yml ] ; then |
| 76 | + debops_playbooks="${playbook_path}" |
| 77 | + break |
| 78 | + fi |
| 79 | + done |
| 80 | +fi |
| 81 | + |
| 82 | +# If playbooks have not been found in local directory, look for them in known |
| 83 | +# locations |
| 84 | +if [ -z "${debops_playbooks}" ] ; then |
| 85 | + for playbook_path in "${DEBOPS_PLAYBOOKS_INSTALL_PATHS[@]}" ; do |
| 86 | + if [ -f ${playbook_path}/site.yml ] ; then |
| 87 | + debops_playbooks="${playbook_path}" |
| 88 | + break |
| 89 | + fi |
| 90 | + done |
| 91 | +fi |
| 92 | + |
| 93 | +# Playbooks have not been found, at this point assume playbooks are not |
| 94 | +# installed. Install them in user home directory |
| 95 | +if [ -z "${debops_playbooks}" ] ; then |
| 96 | + echo "DebOps playbooks have not been found, installing in ${DEBOPS_DEFAULT_INSTALL_PATH}" |
| 97 | + |
| 98 | + if ! type git > /dev/null 2>&1 ; then |
| 99 | + echo >&2 "${SCRIPT_NAME}: Error: git command not found" ; exit 1 |
| 100 | + fi |
| 101 | + |
| 102 | + # Download main debops-playbooks repository |
| 103 | + git clone --quiet ${DEBOPS_GIT_URI} ${DEBOPS_DEFAULT_INSTALL_PATH} |
| 104 | + |
| 105 | + if ! type ansible-galaxy > /dev/null 2>&1 ; then |
| 106 | + echo >&2 "${SCRIPT_NAME}: Error: ansible-galaxy command not found" ; exit 1 |
| 107 | + fi |
| 108 | + |
| 109 | + pushd ${DEBOPS_DEFAULT_INSTALL_PATH} |
| 110 | + ansible-galaxy --roles-path=${DEBOPS_GALAXY_ROLES} install --role-file=${DEBOPS_GALAXY_REQUIREMENTS} |
| 111 | +else |
| 112 | + echo "DebOps playbooks have been found in ${debops_playbooks}" |
| 113 | +fi |
| 114 | + |
0 commit comments