diff --git a/.gitignore b/.gitignore index 4f607e2..62a2f15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,11 @@ /.idea /.trash -/bin/postscripts -/data +/bin/postscripts/commands/* +/bin/postscripts/plugins/* +/data/* /etc/config -/etc/projects \ No newline at end of file +/etc/projects +/etc/customers +/etc/shortnames + +!/**/.keep \ No newline at end of file diff --git a/bin/commands/create-project b/bin/commands/create-project index 791313a..ef26c02 100644 --- a/bin/commands/create-project +++ b/bin/commands/create-project @@ -2,8 +2,7 @@ ### DO NOT EDIT THIS FILE -source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" -source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/bash_header" function usage { echo @@ -21,150 +20,59 @@ function usage { echo " - data_dir/customer/project/etc/config" echo echo "--help Prints this message" - echo + echo " " } -help="$(getParameter "--help" false "$*")" -if [ "$help" == true ] || [ "$1" == "" ] -then - usage - exit -fi - -customer=$(getArgument "$1" "$(usage)" true) -project=$(getArgument "$2" "$(usage)" true) -shortname=$(getArgument "$3" "$(usage)" true) - -for p in "${projects[@]}" -do - if [ "$p" == "$project" ] - then - echoError "Project already exists" - echo - exit - fi -done - -for s in "${shortnames[@]}" -do - if [ "$s" == "$shortname" ] - then - echoError "Shortname already exists" - echo - exit - fi -done - -echo -echoMainTitle "Create project" - -echo -echoSubTitle "Please verify data" -echo -echo "Project path: $workspaces_dir/$customer/$project" -echo "Project data path: $project_manager_dir/data/$customer/$project" -echo -confirm +customer="$(getArgument "$1" "$(usage)")" +project="$(getArgument "$2" "$(usage)")" +shortname="$(getArgument "$3" "$(usage)")" +has_project="$(hasProject "$project")" +has_shortname="$(hasShortname "$shortname")" -if [ -d "$project_manager_dir/data/$customer/$project" ] || [ -d "$project_manager_dir/data/$customer/$project" ] +if [ "$has_project" == true ] then - echo - echoError "Data path already exists" + echoError "Project already exists" echo exit fi -if [ -d "$workspaces_dir/$customer/$project" ] || [ -d "$workspaces_dir/$customer/$project" ] +if [ "$has_shortname" == true ] then - echo - echoError "Path already exists" + echoError "Shortname already exists" echo exit fi -path="$project_manager_dir/data/$customer/$project" - -if [ ! -d "$path" ]; then mkdir -p "$path"; fi -if [ ! -d "$path/.ssh" ]; then mkdir "$path/.ssh"; fi -if [ ! -d "$path/backup" ]; then mkdir "$path/backup"; fi -if [ ! -d "$path/backup/database" ]; then mkdir "$path/backup/database"; fi -if [ ! -d "$path/bin" ]; then mkdir "$path/bin"; fi -if [ ! -d "$path/bin/postscripts" ]; then mkdir "$path/bin/postscripts"; fi -if [ ! -d "$path/bin/postscripts/plugins" ]; then mkdir "$path/bin/postscripts/plugins"; fi -if [ ! -d "$path/bin/includes" ]; then mkdir "$path/bin/includes"; fi -if [ ! -d "$path/bin/includes/plugins" ]; then mkdir "$path/bin/includes/plugins"; fi -if [ ! -d "$path/etc" ]; then mkdir "$path/etc"; fi -if [ ! -d "$path/etc/plugins" ]; then mkdir "$path/etc/plugins"; fi -if [ ! -d "$path/shared" ]; then mkdir "$path/shared"; fi -if [ ! -d "$path/shared/live" ]; then mkdir "$path/shared/live"; fi -if [ ! -d "$path/shared/stage" ]; then mkdir "$path/shared/stage"; fi -if [ ! -d "$path/var" ]; then mkdir "$path/var"; fi -if [ ! -d "$path/var/tmp" ]; then mkdir "$path/var/tmp"; fi -if [ ! -d "$path/var/latest" ]; then mkdir "$path/var/latest"; fi - -if [ ! -f "$path/etc/config" ] -then - quoted="$(sedEscape "$project_manager_dir")" - cat "$project_manager_dir/etc/.config_template" | \ - sed "s/app_customer=''/app_customer='$customer'/" | \ - sed "s/app_project=''/app_project='$project'/" | \ - sed "s/app_project_manager_dir=''/app_project_manager_dir='$quoted'/" \ - > "$path/etc/config" -fi - -plugin_path="$project_manager_dir/plugins" -plugins=$(ls -t "$plugin_path") -for plugin in ${plugins[*]} -do - "$project_manager_dir/bin/commands/install-plugin" "$shortname" "$plugin" -done - -if [ ! -d "$workspaces_dir/$customer/$project" ]; then mkdir -p "$workspaces_dir/$customer/$project"; fi - -projects_string=''; -for i in "${!projects[@]}" -do - ((nr=i+1)) - projects_string+="$(echo -e "\n\t'${projects[$i]}' # $nr")" -done -((nr=nr+1)) -projects_string+="$(echo -e "\n\t'$project' # $nr")" - -shortnames_string=''; -for i in "${!shortnames[@]}" -do - ((nr=i+1)) - shortnames_string+="$(echo -e "\n\t'${shortnames[$i]}' # $nr")" -done -((nr=nr+1)) -shortnames_string+="$(echo -e "\n\t'$shortname' # $nr")" +echo +echoMainTitle "Create project" -customers_string=''; -for i in "${!customers[@]}" -do - ((nr=i+1)) - customers_string+="$(echo -e "\n\t'${customers[$i]}' # $nr")" -done -((nr=nr+1)) -customers_string+="$(echo -e "\n\t'$customer' # $nr")" +project_dir="$project_manager_dir/data/$customer/$project" +workspace_dir="$project_manager_workspaces_dir/$customer/$project" -cat <<- EOF > "$project_manager_dir/etc/projects" -#!/bin/bash +echo +echoSubTitle "Please verify data" +echo +echo "Project path: $workspace_dir" +echo "Project data path: $project_dir" +echo +confirm -projects=($projects_string -) -shortnames=($shortnames_string -) -customers=($customers_string -) +makeProjectManagerDirectories "$shortname" +makeWorkspaceDirectories "$shortname" +addProjectConfig "$customer" "$project" "$shortname" -EOF +#if [ ! -f "$path/etc/config" ] +#then +# quoted="$(sedEscape "$project_manager_dir")" +# cat "$project_manager_dir/etc/.config_template" | \ +# sed "s/app_customer=''/app_customer='$customer'/" | \ +# sed "s/app_project=''/app_project='$project'/" | \ +# sed "s/app_project_manager_dir=''/app_project_manager_dir='$quoted'/" \ +# > "$path/etc/config" +#fi -echo -echoSubTitle "Running postscripts ..." -postScript "$path/bin/postscripts/commands/create-project" -postScript "$workspaces_dir/$customer/$project/bin/postscripts/commands/create-project" +postScript "$project_manager_dir/bin/postscripts/commands/create-project" echo -echoSuccess "Project has been created, please configure: $path/etc/*" +echoSuccess "Project has been created, please configure $project_dir/etc" echo \ No newline at end of file diff --git a/bin/commands/install-plugin b/bin/commands/install-plugin index 85d7678..69fe63a 100644 --- a/bin/commands/install-plugin +++ b/bin/commands/install-plugin @@ -2,8 +2,7 @@ ### DO NOT EDIT THIS FILE -source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" -source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/includes/bash_header" function usage { echo @@ -17,13 +16,6 @@ function usage { echo " " } -help="$(getParameter "--help" false "$*")" -if [ "$help" == true ] || [ "$1" == "" ] -then - usage - exit -fi - shortname=$(getArgument "$1" "$(usage)" true) plugin=$(getArgument "$2" "$(usage)" true) customer=$(getCustomerFromShortname "$shortname") @@ -31,22 +23,6 @@ project=$(getProjectFromShortname "$shortname") project_dir="$project_manager_dir/data/$customer/$project" plugin_dir="$project_manager_dir/plugins/$plugin" -if [ ! -d "$project_dir" ] -then - echo - echoError "Could not find project dir" - echo - exit -fi - -if [ ! -d "$plugin_dir" ] -then - echo - echoError "Could not find plugin dir" - echo - exit -fi - echo echoMainTitle "Install plugin" @@ -58,11 +34,9 @@ fi etc_files=$(list "$plugin/etc") for etc_file in "${etc_files[@]}" do - cp "$etc_file" "$project_dir/etc/plugins/$plugin/config" + cp "$etc_file" "$project_dir/etc/plugins/$plugin/." done -echo -echoSubTitle "Running postscripts ..." postScript "$project_manager_dir/bin/postscripts/commands/install-plugin" postScript "$project_dir/bin/postscripts/commands/install-plugin" diff --git a/bin/commands/install-project-manager b/bin/commands/install-project-manager index 222c374..bd0ed29 100644 --- a/bin/commands/install-project-manager +++ b/bin/commands/install-project-manager @@ -2,11 +2,11 @@ ### DO NOT EDIT THIS FILE -source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/includes" +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/bash_header" function usage { echo - echoMainTitle "Installs Sumedia Project Manager" + echoMainTitle "Install Sumedia Project Manager" echo echoSubTitle "Usage:" echo @@ -29,14 +29,16 @@ echoMainTitle "Install Sumedia Project Manager" echo echoSubTitle "Please configure Sumedia Project Manager" echo -workspaces_dir="$(readConsole "Workspaces Dir" "Invalid selection" true "$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd)")" -project_manager_dir="$(readConsole "Project Manager Dir" "Invalid selection" true "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)")" +workspaces_dir="$(readConsole "Workspaces dir" "Invalid selection" true "$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd)")" +local_apache_httpdocs="$(readConsole "Local Apache httpdocs dir (optional)" "Invalid selection" true)" +sudo_password="$(readConsole "sudo password (optional)" "Invalid selection" true)" if [ ! -f "$project_manager_dir/etc/config" ] then cat "$project_manager_dir/etc/.project_manager_config_template" | \ - sed "s/workspaces_dir=''/workspaces_dir='$(sedEscape "$workspaces_dir")'/" | \ - sed "s/project_manager_dir=''/project_manager_dir='$(sedEscape "$project_manager_dir")'/" \ + sed "s/project_manager_workspaces_dir=''/project_manager_workspaces_dir='$(sedEscape "$workspaces_dir")'/" | \ + sed "s/project_manager_local_apache_httpdocs=''/project_manager_local_apache_httpdocs='$(sedEscape "$local_apache_httpdocs")'/" \ + sed "s/project_manager_sudo_password=''/project_manager_sudo_password='$(sedEscape "$sudo_password")'/" \ > "$project_manager_dir/etc/config" fi @@ -45,11 +47,19 @@ then cp "$project_manager_dir/etc/.projects_template" "$project_manager_dir/etc/projects" fi -echo -echoSubTitle "Running postscripts ..." -postScript "$path/bin/postscripts/commands/install-project-manager" +if [ ! -f "$project_manager_dir/etc/customers" ] +then + cp "$project_manager_dir/etc/.customers_template" "$project_manager_dir/etc/customers" +fi + +if [ ! -f "$project_manager_dir/etc/shortnames" ] +then + cp "$project_manager_dir/etc/.shortnames_template" "$project_manager_dir/etc/shortnames" +fi + +postScript "$project_manager_dir/bin/postscripts/commands/install-project-manager" postScript "$workspaces_dir/$customer/$project/bin/postscripts/commands/install-project-manager" echo -echoSuccess "Sumedia Project Manager has been installed" +echoSuccess "Project Manager has been installed." echo \ No newline at end of file diff --git a/bin/commands/list-projects b/bin/commands/list-projects index b7b1a53..d0064f0 100644 --- a/bin/commands/list-projects +++ b/bin/commands/list-projects @@ -2,8 +2,7 @@ ### DO NOT EDIT THIS FILE -source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" -source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/includes/bash_header" function usage { echo @@ -33,13 +32,6 @@ function listProjects { echo echoMainTitle "Listing projects" -help="$(getParameter "--help" false "$*")" -if [ "$help" == true ] -then - usage - exit -fi - if [ "${#projects[@]}" -eq 0 ] then echo @@ -48,5 +40,3 @@ then else listProjects fi - -echoSuccess "Listing done" \ No newline at end of file diff --git a/bin/commands/remove-project b/bin/commands/remove-project index e6c6cfe..e5d1841 100644 --- a/bin/commands/remove-project +++ b/bin/commands/remove-project @@ -2,8 +2,7 @@ ### DO NOT EDIT THIS FILE -source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" -source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/includes/bash_header" function usage { echo @@ -19,33 +18,12 @@ function usage { echo } -help="$(getParameter "--help" false "$*")" -if [ "$help" == true ] || [ "$1" == "" ] -then - usage - exit -fi - shortname="$(getArgument "$1" "$(usage)" true)" customer="$(getCustomerFromShortname "$shortname")" project="$(getProjectFromShortname "$shortname")" - -index=false -for i in "${!shortnames[@]}" -do - if [ "${shortnames[$i]}" == "$shortname" ] - then - index="$i" - fi -done - -if [ "$index" == false ] -then - echo - echoError "Could not find project: $shortname" - echo - exit -fi +pos="$(getProjectConfigPosition "$shortname")" +workspace_dir="$project_manager_workspaces_dir/$customer/$project" +project_dir="$project_manager_dir/data/$customer/$project" echo echoMainTitle "Remove project" @@ -53,17 +31,14 @@ echoMainTitle "Remove project" echo echoSubTitle "Please verify data" echo -echo "Project Path: $workspaces_dir/$customer/$project" -echo "Project Data Path: $project_manager_dir/data/$customer/$project" +echo "Project Path: $workspace_dir" +echo "Project Data Path: $project_dir" echo confirm echo echo "Moving files to trash" -customer_dir_list=($(ls -t "$project_manager_dir/data/$customer")) -has_more_dirs=$(if [ "${#customer_dir_list[@]}" -gt 1 ]; then echo true; else echo false; fi) - rand=$(cat /proc/sys/kernel/random/uuid) trash_path="$project_manager_dir/.trash/$rand" mkdir "$trash_path" @@ -71,65 +46,13 @@ mkdir "$trash_path/data" mkdir "$trash_path/workspace" mkdir "$trash_path/data/$customer" mkdir "$trash_path/workspace/$customer" -mv "$project_manager_dir/data/$customer/$project" "$trash_path/data/$customer/$project" -mv "$workspaces_dir/$customer/$project" "$trash_path/workspace/$customer/$project" - -if [ "$has_more_dirs" != true ] -then - rmdir "$project_manager_dir/data/$customer" - rmdir "$workspaces_dir/$customer" -fi +mv "$project_dir" "$trash_path/data/$customer/$project" +mv "$workspace_dir" "$trash_path/workspace/$customer/$project" -nr=0 -projects_string=''; -for i in "${!projects[@]}" -do - if [ "$i" != "$index" ] - then - ((nr=nr+1)) - projects_string+="$(echo -e "\n\t'${projects[$i]}' # $nr")" - fi -done +removeProjectConfig "$shortname" -nr=0 -shortnames_string=''; -for i in "${!shortnames[@]}" -do - if [ "$i" != "$index" ] - then - ((nr=nr+1)) - shortnames_string+="$(echo -e "\n\t'${shortnames[$i]}' # $nr")" - fi -done - -nr=0 -customers_string=''; -for i in "${!customers[@]}" -do - if [ "$i" != "$index" ] - then - ((nr=nr+1)) - customers_string+="$(echo -e "\n\t'${customers[$i]}' # $nr")" - fi -done - -cat <<- EOF > "$project_manager_dir/etc/projects" -#!/bin/bash - -projects=($projects_string -) -shortnames=($shortnames_string -) -customers=($customers_string -) - -EOF - -echo -echoSubTitle "Running postscripts ..." -postScript "$path/bin/postscripts/commands/remove-project" -postScript "$workspaces_dir/$customer/$project/bin/postscripts/commands/remove-project" +postScript "$project_manager_dir/bin/postscripts/commands/remove-project" echo -echoSuccess "Project has been remove, (moved to ./.trash)" +echoSuccess "Project has been removed" echo \ No newline at end of file diff --git a/bin/includes/bash_header b/bin/includes/bash_header new file mode 100644 index 0000000..60a1438 --- /dev/null +++ b/bin/includes/bash_header @@ -0,0 +1,23 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)/includes" + +help="$(getParameter "--help" false "$*")" +if [ "$help" == true ] || [ "$1" == "" ] +then + usage + exit +fi + +project_manager_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../" &> /dev/null && pwd)" + +config_file="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" +if [ -f "$config" ]; then source "$config_file"; fi +projects_file="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" +if [ -f "$projects_file" ]; then source "$projects_file"; fi +customers_file="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/customers" +if [ -f "$customers_file" ]; then source "$customers_file"; fi +shortnames_file="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/shortnames" +if [ -f "$shortnames_file" ]; then source "$shortnames_file"; fi \ No newline at end of file diff --git a/bin/includes/includes b/bin/includes/includes index f392cb6..2431f45 100644 --- a/bin/includes/includes +++ b/bin/includes/includes @@ -2,215 +2,5 @@ ### DO NOT EDIT THIS FILE -if [ true != ${includes_included:-false} ] -then - includes_included=true - - function echoMainTitle { - echo -e "\033[38;5;214m$1\033[0m" - } - - function echoSubTitle { - echo -e "\033[38;5;142m$1\033[0m" - } - - function echoSmall { - echo -e "\033[38;5;240m$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 echoWarning { - echo -e "\033[0;33m$1\033[0m" - } - - function echoSuccess { - echo -e "\033[0;32m$1\033[0m" - } - - function getParameter { - local name=$1 - local has=$2 - local parameters=($3) - - for i in "${!parameters[@]}" - do - if [ "${parameters[$i]}" == "$name" ] && [ "$has" == true ] - then - ((j=i+1)) - echo "${parameters[$j]}" - elif [ "${parameters[$i]}" == "$name" ] - then - echo true - fi - done - } - - function getArgument { - local errmsg=$2 - local allow=${3:-false} - local var=$1 - - 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 - read -p "$message [default:$default]: " var - else - read -p "$message: " var - fi - - if [ "$var" == "" ] && [ "$default" != "" ] - then - var="$default" - fi - - if [ "$(isAllowed "$var" "$allow")" == false ] - then - echo "$errmsg" >&2 - var=$(readConsole "$message" "$errmsg" "$allow" "$default") - fi - - echo "$var" - } - - function getProjectFromShortname { - local shortname="$(getArgument "$1" "Invalid Argument" true)" - for i in "${!shortnames[@]}" - do - if [ "${shortnames[$i]}" == "$shortname" ] - then - echo "${projects[$i]}" - fi - done - } - - function getCustomerFromShortname { - local shortname="$(getArgument "$1" "Invalid Argument" true)" - for i in "${!shortnames[@]}" - do - if [ "${shortnames[$i]}" == "$shortname" ] - then - echo "${customers[$i]}" - fi - done - } - - function loadProjectConfig - { - local shortname="$(getArgument "$1" "Invalid Argument" true)" - local customer="$(getCustomerFromShortname "$shortname")" - local project="$(getProjectFromShortname "$shortname")" - - if [ "$customer" == "" ] || [ "$project" == "" ] - then - echo - echoError "Could not load project with name: $shortname" - echo - exit - fi - - local path="$project_manager_dir/data/$customer/$project" - local plugin_path="$project_manager_dir/plugins" - local plugins="$(ls -t "$plugin_path")" - - source "$path/etc/config" - for plugin in ${plugins[*]} - do - if [ -f "$path/etc/plugins/$plugin/config" ] - then - source "$path/etc/plugins/$plugin/config" - fi - done - } - - function postScript { - local script=$(getArgument "$1" "Usage: postScript \"post_script.sh\"" true) - if [ -f "$script" ] - then - source "$script" - fi - } - - function getConfig { - local env=$(getArgument "$1" "Usage getEnvVar [live|stage|local_live|local_stage] var" "live stage local_live local_stage") - local suffix=$(getArgument "$2" "Usage getEnvVar [live|stage|local_live|local_stage] var" true) - echo "$(eval "echo \"\$${env}_$suffix\"")" - } - - function sedEscape { - echo "$1" | sed -r 's/([\$\.\*\/\[\\^])/\\\1/g' | sed 's/[]]/\[]]/g' - } - - function confirm { - confirm="$(readConsole "Should i execute? [y,n]" "Invalid selection" "y n")" - - if [ "$confirm" == "n" ] - then - echo - echoError "Aborting" - echo - exit - fi - } - - plugin_dir="$project_manager_dir/plugins" - plugins="$(ls -t "$plugin_dir")" - for plugin in ${plugins[*]} - do - if [ -f "$plugin_dir/$plugin/includes/includes" ] - then - source "$plugin_dir/$plugin/includes/includes" - fi - done - -fi +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)/main_functions" +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)/project_manager" diff --git a/bin/includes/main_functions b/bin/includes/main_functions new file mode 100644 index 0000000..ca58de9 --- /dev/null +++ b/bin/includes/main_functions @@ -0,0 +1,165 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +if [ true != ${includes_main_functions:-false} ] +then + includes_main_functions=true + + function echoMainTitle { + echo -e "\033[38;5;214m$1\033[0m" + } + + function echoSubTitle { + echo -e "\033[38;5;142m$1\033[0m" + } + + function echoSmall { + echo -e "\033[38;5;240m$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 echoWarning { + echo -e "\033[0;33m$1\033[0m" + } + + function echoSuccess { + echo -e "\033[0;32m$1\033[0m" + } + + function getParameter { + local name=$1 + local has=$2 + local parameters=($3) + + for i in "${!parameters[@]}" + do + if [ "${parameters[$i]}" == "$name" ] && [ "$has" == true ] + then + ((j=i+1)) + echo "${parameters[$j]}" + elif [ "${parameters[$i]}" == "$name" ] + then + echo true + fi + done + } + + function getArgument { + local var=$1 + local errmsg=${2:-"Invalid Argument"} + local allow=${3:-true} + + 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 + read -p "$message [default:$default]: " var + else + read -p "$message: " var + fi + + if [ "$var" == "" ] && [ "$default" != "" ] + then + var="$default" + fi + + if [ "$(isAllowed "$var" "$allow")" == false ] + then + echo "$errmsg" >&2 + var=$(readConsole "$message" "$errmsg" "$allow" "$default") + fi + + echo "$var" + } + + function postScript { + local script=$(getArgument "$1" "Usage: postScript \"post_script.sh\"" true) + if [ -f "$script" ] + then + source "$script" + fi + } + + function getConfig { + local env=$(getArgument "$1" "Usage getEnvVar [live|stage|local_live|local_stage] var" "live stage local_live local_stage") + local suffix=$(getArgument "$2" "Usage getEnvVar [live|stage|local_live|local_stage] var" true) + echo "$(eval "echo \"\$${env}_$suffix\"")" + } + + function sedEscape { + echo "$1" | sed -r 's/([\$\.\*\/\[\\^])/\\\1/g' | sed 's/[]]/\[]]/g' + } + + function confirm { + confirm="$(readConsole "Should i execute? [y,n]" "Invalid selection" "y n")" + + if [ "$confirm" == "n" ] + then + echo + echoError "Aborting" + echo + exit + fi + } + + function getSudoPassword { + if [ "${project_manager_sudo_password}" == "" ] + then + echo -n "Sudo Password:" + read -s project_manager_sudo_password + fi + echo project_manager_sudo_password + } + +fi \ No newline at end of file diff --git a/bin/includes/project_header b/bin/includes/project_header deleted file mode 100644 index d3fa74b..0000000 --- a/bin/includes/project_header +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -### DO NOT EDIT THIS FILE - -source "$project_manager_dir/bin/includes/includes" - -help="$(getParameter "--help" false "$*")" -if [ "$help" == true ] || [ "$1" == "" ] -then - usage - exit -fi - -shortname="$(getArgument "$1" "$(usage)" true)" -project="$(getProjectFromShortname "$shortname")" -customer="$(getCustomerFromShortname "$shortname")" -loadProjectConfig "$shortname" -app_dir="$project_manager_dir/data/$customer/$project" diff --git a/bin/includes/project_manager b/bin/includes/project_manager new file mode 100644 index 0000000..ca51e92 --- /dev/null +++ b/bin/includes/project_manager @@ -0,0 +1,301 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +if [ true != ${includes_project_manager:-false} ] +then + includes_project_manager=true + + function getProjectFromShortname { + local shortname="$(getArgument "$1")" + for i in "${!project_manager_shortnames[@]}" + do + if [ "${project_manager_shortnames[$i]}" == "$shortname" ] + then + echo "${project_manager_projects[$i]}" + fi + done + } + + function getCustomerFromShortname { + local shortname="$(getArgument "$1")" + for i in "${!project_manager_shortnames[@]}" + do + if [ "${project_manager_shortnames[$i]}" == "$shortname" ] + then + echo "${project_manager_customers[$i]}" + fi + done + } + + function loadProjectConfig + { + local shortname="$(getArgument "$1")" + local customer="$(getCustomerFromShortname "$shortname")" + local project="$(getProjectFromShortname "$shortname")" + + if [ "$customer" == "" ] || [ "$project" == "" ] + then + echo + echoError "Could not load project with name: $shortname" + echo + exit + fi + + local path="$project_manager_dir/data/$customer/$project" + source "$path/etc/config" + } + + function loadPluginConfig + { + local shortname="$(getArgument "$1")" + local customer="$(getCustomerFromShortname "$shortname")" + local project="$(getProjectFromShortname "$shortname")" + + if [ "$customer" == "" ] || [ "$project" == "" ] + then + echo + echoError "Could not load project with name: $shortname" + echo + exit + fi + + local path="$project_manager_dir/data/$customer/$project" + local plugin_path="$project_manager_dir/plugins" + local plugins="$(ls -t "$plugin_path")" + + for plugin in ${plugins[*]} + do + if [ -f "$path/etc/plugins/$plugin/config" ] + then + source "$path/etc/plugins/$plugin/config" + fi + done + } + + function makeProjectManagerDirectories { + local shortname="$(getArgument "$1")" + local customer="$(getArgument "$2")" + local project="$(getArgument "$3")" + + path="$project_manager_dir/data/$customer/$project" + + if [ ! -d "$path" ]; then mkdir -p "$path"; fi + if [ ! -d "$path/.ssh" ]; then mkdir "$path/.ssh"; fi + if [ ! -d "$path/backup" ]; then mkdir "$path/backup"; fi + if [ ! -d "$path/backup/database" ]; then mkdir "$path/backup/database"; fi + if [ ! -d "$path/bin" ]; then mkdir "$path/bin"; fi + if [ ! -d "$path/bin/postscripts" ]; then mkdir "$path/bin/postscripts"; fi + if [ ! -d "$path/bin/postscripts/plugins" ]; then mkdir "$path/bin/postscripts/plugins"; fi + if [ ! -d "$path/bin/includes" ]; then mkdir "$path/bin/includes"; fi + if [ ! -d "$path/bin/includes/plugins" ]; then mkdir "$path/bin/includes/plugins"; fi + if [ ! -d "$path/etc" ]; then mkdir "$path/etc"; fi + if [ ! -d "$path/etc/plugins" ]; then mkdir "$path/etc/plugins"; fi + if [ ! -d "$path/shared" ]; then mkdir "$path/shared"; fi + if [ ! -d "$path/shared/live" ]; then mkdir "$path/shared/live"; fi + if [ ! -d "$path/shared/stage" ]; then mkdir "$path/shared/stage"; fi + if [ ! -d "$path/var" ]; then mkdir "$path/var"; fi + if [ ! -d "$path/var/tmp" ]; then mkdir "$path/var/tmp"; fi + if [ ! -d "$path/var/latest" ]; then mkdir "$path/var/latest"; fi + } + + function makeWorkspaceDirectories { + local shortname="$(getArgument "$1")" + local customer="$(getArgument "$2")" + local project="$(getArgument "$3")" + + if [ ! -d "$project_manager_workspaces_dir/$customer/$project" ]; then mkdir -p "$project_manager_workspaces_dir/$customer/$project"; fi + } + + function addProjectConfig { + local shortname="$(getArgument "$1")" + local customer="$(getArgument "$2")" + local project="$(getArgument "$3")" + + _addProject "$project" + _addCustomer "$customer" + _addShortname "$shortname" + } + + function hasShortname { + local shortname="$(getArgument "$1")" + + for i in "${!project_manager_shortnames[@]}" + do + if [ "${project_manager_shortname[$i]}" == "$shortname" ] + then + echo true + fi + done + echo false + } + + function hasProject { + local project="$(getArgument "$1")" + + for i in "${!project_manager_projects[@]}" + do + if [ "${project_manager_projects[$i]}" == "$project" ] + then + echo true + fi + done + echo false + } + + function _addShortname { + local shortname="$(getArgument "$1")" + local hasShortname="$(hasShortname "$shortname")" + + if [ "$hasShortname" == true ] + then + echo + echoError "Shortname already exists" + echo + exit + fi + + local string=''; + for i in "${!project_manager_shortnames[@]}" + do + string+="$(echo -e "\n\t'${project_manager_shortnames[$i]}'")" + done + string+="$(echo -e "\n\t'$shortname'")" + _writeShortnames "$string" + } + + function _addProject { + local project="$(getArgument "$1")" + local hasProject="$(hasProject "$project")" + + if [ "$hasProject" == true ] + then + echo + echoError "Project already exists" + echo + exit + fi + + local string=''; + for i in "${!project_manager_projects[@]}" + do + string+="$(echo -e "\n\t'${project_manager_projects[$i]}'")" + done + string+="$(echo -e "\n\t'$project'")" + _writeProjects "$string" + } + + function _addCustomer { + local customer="$(getArgument "$1")" + + local string=''; + for i in "${!project_manager_customers[@]}" + do + string+="$(echo -e "\n\t'${project_manager_customers[$i]}'")" + done + string+="$(echo -e "\n\t'$customer'")" + _writeCustomers "$string" + } + + function _writeShortnames { + local string="$(getArgument "$1")" +cat <<- EOF > "$project_manager_dir/etc/shortnames" +#!/bin/bash + +project_manager_shortnames=($string +) +EOF + } + + function _writeCustomers { + local string="$(getArgument "$1")" +cat <<- EOF > "$project_manager_dir/etc/customers" +#!/bin/bash + +project_manager_customers=($string +) +EOF + } + + function _writeProjects { + local string="$(getArgument "$1")" +cat <<- EOF > "$project_manager_dir/etc/projects" +#!/bin/bash + +project_manager_projects=($string +) +EOF + } + + function removeProjectConfig { + local shortname="$(getArgument "$1")" + local pos="$(getProjectConfigPosition "$shortname")" + + _removeShortname "$pos" + _removeCustomer "$pos" + _removeProject "$pos" + } + + function getProjectConfigPosition { + local shortname="$(getArgument "$1")" + + for i in "${!project_manager_shortnames[@]}" + do + if [ "${$project_manager_shortnames[$i]}" == "$shortname" ] + then + ((ret=i+1)) + return "$ret" + fi + done + return 0 + } + + function _removeShortname { + local pos="$(getArgument "$1")" + + local string=''; + for i in "${!project_manager_shortnames[@]}" + do + ((nr=i+1)) + if [ "$pos" == "$nr" ] + then + continue + fi + string+="$(echo -e "\n\t'${project_manager_shortnames[$i]}'")" + done + _writeShortnames "$string" + } + + function _removeProjects { + local pos="$(getArgument "$1")" + + local string=''; + for i in "${!project_manager_projects[@]}" + do + ((nr=i+1)) + if [ "$pos" == "$nr" ] + then + continue + fi + string+="$(echo -e "\n\t'${project_manager_projects[$i]}'")" + done + _writeProjects "$string" + } + + function _removeCustomer { + local pos="$(getArgument "$1")" + + local string=''; + for i in "${!project_manager_customers[@]}" + do + ((nr=i+1)) + if [ "$pos" == "$nr" ] + then + continue + fi + string+="$(echo -e "\n\t'${project_manager_customers[$i]}'")" + done + _writeCustomers "$string" + } + +fi \ No newline at end of file diff --git a/bin/postscripts/commands/.keep b/bin/postscripts/commands/.keep new file mode 100644 index 0000000..e69de29 diff --git a/etc/.config_template b/etc/.config_template index ce7c2bd..31a4cba 100644 --- a/etc/.config_template +++ b/etc/.config_template @@ -1,41 +1,24 @@ #!/bin/bash -### GENERAL - -app_customer='' -app_project='' -app_project_manager_dir='' - -### STAGE - -# stage domain -stage_domain='' +### LOCAL STAGE ENV # url to stage -stage_url='' +local_stage_url='' # The httpdocs folder on your server -stage_httpdocs_path='' +local_stage_httpdocs_path='' -### LIVE - -# live domain -live_domain='' +### LOCAL LIVE ENV # url to live -live_url='' +local_live_url='' # The httpdocs folder on your server -live_httpdocs_path='' - -### LOCAL - -# url to your project on the local machine -local_url='' +local_live_httpdocs_path='' -### POST SCRIPT +### POSTSCRIPTS -app_dir='$app_project_manager_dir/data/$customer/$project' +app_dir= if [ -f '$app_dir/etc/post_config.sh' ] then source '$app_dir/etc/post_config.sh' diff --git a/etc/.customers_template b/etc/.customers_template new file mode 100644 index 0000000..d9d7928 --- /dev/null +++ b/etc/.customers_template @@ -0,0 +1,3 @@ +#!/bin/bash + +project_manager_customers=() \ No newline at end of file diff --git a/etc/.project_manager_config_template b/etc/.project_manager_config_template index da6c71b..519edd2 100644 --- a/etc/.project_manager_config_template +++ b/etc/.project_manager_config_template @@ -1,5 +1,5 @@ #!/bin/bash -workspaces_dir='' -project_manager_dir='' - +project_manager_workspaces_dir='' +project_manager_local_apache_httpdocs='' # leave empty if you don't want to use +project_manager_sudo_password='' # leave empty to fetch on call diff --git a/etc/.projects_template b/etc/.projects_template index f26c7fa..1da510f 100644 --- a/etc/.projects_template +++ b/etc/.projects_template @@ -1,5 +1,3 @@ #!/bin/bash -projects=() -shortnames=() -customers=() \ No newline at end of file +project_manager_projects=() diff --git a/etc/.shortnames_template b/etc/.shortnames_template new file mode 100644 index 0000000..e376c7d --- /dev/null +++ b/etc/.shortnames_template @@ -0,0 +1,3 @@ +#!/bin/bash + +project_manager_shortnames=()