#!/bin/bash if [ true != ${includes_included:-false} ] then includes_included=true function echoMainTitle { echo -e "\033[1;34m$1\033[0m" } function echoSubTitle { echo -e "\033[0;32m$1\033[0m" } function echoSelect { local select="$1" local message="$2" echo -e "\033[0;33m$select\033[0m $message" } function echoError { echo -e "\033[0;91m$1\033[0m" } function getArgument { local var=$1 local errmsg=$2 local allow=${3:-false} if [ "$allow" != false ] && [ "$(isAllowed "$var" "$allow")" == false ] then echo "$errmsg" >&2 exit 1 fi echo "$var" } function isAllowed { local var="$1" local allow="$2" local valid=true if [ "$allow" == true ] && [ "$var" == "" ] then valid=false elif [ "$allow" != false ] && [ "$allow" != true ] then allow=($allow) local found=false for i in "${!allow[@]}" do if [ "${allow[$i]}" == "$var" ] then found=true fi done if [ "$found" != true ] then valid=false fi fi echo $valid } function readConsole { local message="$1" local errmsg="$2" local allow=${3:-false} local default=${4:-""} if [ "$default" != "" ] then message="$message [d:$default]" fi read -p "$message" var var=${var:-$default} if [ "$(isAllowed "$var" "$allow")" == false ] then echo "$errmsg" >&2 var=$(readConsole "$message" "$errmsg" "$allow") fi echo "$var" } function addSSHKey { local env=$(getArgument "$1" "Usage: addSSHKey [live|stage|git]" "live stage git") local ssh_key_file=$(eval "echo \"\$${env}_has_ssh_key_file\"") if [ "$ssh_key_file" != "" ] then copySSHKeys "$env" else installSSHKey "$env" fi ssh-add "$app_dir/.ssh/$env" } function copySSHKeys { local env=$(getArgument "$1" "Usage: copySSHKeys [live|stage|git]" "live stage git") local source=$(eval "echo \$${env}_ssh_key_file") local target="$app_dir/.ssh/$env" if [ -f "$source" ] && [ ! -f "$target" ] then cp "$source" "$target" chmod 0600 "$target" else echoError "Could not copy ssh key file" >&2 exit 1 fi if [ -f "$source.pub" ] && [ ! -f "$target.pub" ] then cp "$source.pub" "$target.pub" chmod 0600 "$target.pub" else echoError "Could not copy ssh key file" >&2 exit 1 fi } function installSSHKey { local env=$(getArgument "$1" "Usage: installSSHKey [live|stage|git]" "live stage git") local file=$(eval "echo \"\$${env}_ssh_key_file\"") local user=$(eval "echo \"\$${env}_ssh_user\"") local domain=$(eval "echo \"\$${env}_ssh_domain\"") if [ "$file" == "" ] && [ ! -f "$app_dir/.ssh/$env" ] then ssh-keygen -b 4096 -t rsa -f "$app_dir/.ssh/$env" -q -N "" chmod 0600 "$app_dir/.ssh/$env" chmod 0600 "$app_dir/.ssh/$env.pub" echo "Please enter SSH $env system password:" ssh-copy-id -i "$app_dir/.ssh/$env.pub" "$user"@"$domain" fi } function buildIgnoredTablesArguments { local env=$(getArgument "$1" "Usage: buildIgnoredTables [live|stage|local] [ignoredTablesArray]" "live stage local") local ignoredTables=$(getArgument "$2" "Usage: buildIgnoredTables [live|stage|local] [ignoredTablesArray]" true) local database_name=$(eval "echo \"\$${env}_database_name\"") ignoredTables=($ignoredTables) local ignoredTablesArguments="" for table in "${ignoredTables[@]}" do ignoredTablesArguments="$ignoredTablesArguments --ignore-table=\"${database_name}.${table}\"" done echo "$ignoredTablesArguments" } function postScript { local script=$(getArgument "$1" "Usage: postScript \"post_script.sh\"" true) if [ -f "$script" ] then source "$script" fi } fi