|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +PROJECT_NAME="eev4" |
| 4 | + |
| 5 | +# runs the given command as root (detects if we are root already) |
| 6 | +runAsRoot() { |
| 7 | + local CMD="$*" |
| 8 | + |
| 9 | + if [ $EUID -ne 0 ]; then |
| 10 | + CMD="sudo $CMD" |
| 11 | + fi |
| 12 | + |
| 13 | + $CMD |
| 14 | +} |
| 15 | + |
| 16 | +# fail_trap is executed if an error occurs. |
| 17 | +fail_trap() { |
| 18 | + result=$? |
| 19 | + if [ "$result" != "0" ]; then |
| 20 | + echo "Failed to install $PROJECT_NAME" |
| 21 | + echo -e "\tFor support, go to https://github.com/EasyEngine/docker-compose-wordpress." |
| 22 | + fi |
| 23 | + cleanup |
| 24 | + exit $result |
| 25 | +} |
| 26 | + |
| 27 | +# cleanup temporary files |
| 28 | +cleanup() { |
| 29 | + rm -rf "$EE_TMP_ROOT" |
| 30 | +} |
| 31 | + |
| 32 | +# checks dependencies requried to run this setup |
| 33 | +checkDependencies() { |
| 34 | + dependencies=( |
| 35 | + "git^git - https://git-scm.com/downloads" |
| 36 | + "docker^docker - https://docs.docker.com/engine/installation" |
| 37 | + "docker-compose^docker-compose - https://docs.docker.com/compose/install" ) |
| 38 | + |
| 39 | + for dependency in "${dependencies[@]}" ; do |
| 40 | + dep="${dependency%%^*}" |
| 41 | + msg="${dependency##*^}" |
| 42 | + if ! type "$dep" > /dev/null 2>&1; then |
| 43 | + echo "Please install $msg" |
| 44 | + fi |
| 45 | + done |
| 46 | +} |
| 47 | + |
| 48 | +# cloneRepo clones the repository to a temporary location |
| 49 | +cloneRepo() { |
| 50 | + REPO_URL="https://github.com/EasyEngine/docker-compose-wordpress" |
| 51 | + EE_TMP_ROOT="$(mktemp -dt ee-installer-XXXXXX)" |
| 52 | + EE_TMP_REPO="$EE_TMP_ROOT/docker-compose-wordpress" |
| 53 | + echo "Clone repo $REPO_URL" |
| 54 | + git clone "$REPO_URL" "$EE_TMP_REPO" |
| 55 | +} |
| 56 | + |
| 57 | +# installScript installs the main script. |
| 58 | +installScript() { |
| 59 | + EE_INSTALL_DIR="/opt/$PROJECT_NAME" |
| 60 | + EE_SCRIPT="scripts/eev4" |
| 61 | + EE_SCRIPT_INSTALL_PATH="/usr/local/bin/eev4" |
| 62 | + |
| 63 | + mkdir -p "$EE_INSTALL_DIR" |
| 64 | + echo "Preparing to install into $EE_INSTALL_DIR" |
| 65 | + runAsRoot cp -r "$EE_TMP_REPO" "$EE_INSTALL_DIR" |
| 66 | + runAsRoot ln -s "$EE_INSTALL_DIR/$EE_SCRIPT" "$EE_SCRIPT_INSTALL_PATH" |
| 67 | +} |
| 68 | + |
| 69 | +# testVersion tests the installed script to make sure it is working. |
| 70 | +testInstalled() { |
| 71 | + EE_INSTALLED="$(which $PROJECT_NAME)" |
| 72 | + if [ "$?" = "1" ]; then |
| 73 | + return 1 |
| 74 | + else |
| 75 | + echo "$PROJECT_NAME already installed into $EE_INSTALLED" |
| 76 | + return 0 |
| 77 | + fi |
| 78 | +} |
| 79 | + |
| 80 | +# Execution |
| 81 | + |
| 82 | +#Stop execution on any error |
| 83 | +trap "fail_trap" EXIT |
| 84 | +set -e |
| 85 | + |
| 86 | +if ! testInstalled; then |
| 87 | + checkDependencies |
| 88 | + cloneRepo |
| 89 | + installScript |
| 90 | +fi |
| 91 | +cleanup |
0 commit comments