From 2d96ee55701c3b88007c5ed0d8e6d0392a8827f6 Mon Sep 17 00:00:00 2001 From: Sven Ullmann Date: Mon, 6 Mar 2023 22:14:45 +0100 Subject: [PATCH] make shopware manager to project manager ;) --- .etc/config.sh | 211 ----------------- .etc/live.my.cnf | 4 - .etc/local.my.cnf | 9 - .etc/stage.my.cnf | 4 - .giignore | 6 + .gitignore | 8 - {.ssh => .trash}/.keep | 0 bin/{ => bak}/commands/import_media.sh | 10 +- bin/bak/commands/install_remote_system.sh | 14 ++ bin/{ => bak}/commands/install_shopware.sh | 28 ++- bin/{ => bak}/commands/make_local_database.sh | 0 bin/bak/commands/switch_local_env.sh | 20 ++ bin/bak/functions/backup_db.sh | 104 +++++++++ bin/bak/functions/clone_repo.sh | 46 ++++ bin/bak/functions/create_database.sh | 51 +++++ bin/bak/functions/create_directories.sh | 45 ++++ bin/bak/functions/deploy.sh | 30 +++ bin/bak/functions/fetch_files.sh | 38 ++++ bin/bak/functions/import_db.sh | 58 +++++ bin/bak/functions/link_latest_db.sh | 28 +++ bin/bak/functions/write_shopware_env_file.sh | 84 +++++++ bin/commands/clone_repo.sh | 20 -- bin/commands/create-project | 158 +++++++++++++ bin/commands/create_local_env.sh | 57 ----- bin/commands/import_db.sh | 84 ------- bin/commands/import_gdpr_data.sh | 50 ---- bin/commands/install-project-manager | 53 +++++ bin/commands/list-projects | 50 ++++ bin/commands/remove-project | 122 ++++++++++ bin/commands/switch_system.sh | 33 --- bin/includes/includes | 215 ++++++++++++++++++ bin/includes/includes.sh | 166 -------------- bin/includes/project_header | 15 ++ bin/manager | 100 -------- .../postscripts/plugins}/.keep | 0 bin/project-manager | 149 ++++++++++++ {bin/postscripts => data}/.keep | 0 etc/.config_template | 35 +++ etc/.keep | 0 etc/.project_manager_config_template | 5 + etc/.projects_template | 5 + git/.keep | 0 plugins/db/etc/.config_template | 49 ++++ plugins/git/commands/clone | 60 +++++ plugins/git/etc/.config_template | 9 + plugins/git/includes/includes | 13 ++ plugins/shopware/etc/.config_template | 88 +++++++ plugins/shopware/includes/includes | 47 ++++ plugins/ssh/commands/add-key | 70 ++++++ plugins/ssh/etc/.config_template | 15 ++ plugins/ssh/includes/includes | 161 +++++++++++++ shared/.keep | 0 var/tmp/.keep | 0 53 files changed, 1867 insertions(+), 760 deletions(-) delete mode 100644 .etc/config.sh delete mode 100644 .etc/live.my.cnf delete mode 100644 .etc/local.my.cnf delete mode 100644 .etc/stage.my.cnf create mode 100644 .giignore delete mode 100644 .gitignore rename {.ssh => .trash}/.keep (100%) rename bin/{ => bak}/commands/import_media.sh (82%) create mode 100644 bin/bak/commands/install_remote_system.sh rename bin/{ => bak}/commands/install_shopware.sh (69%) rename bin/{ => bak}/commands/make_local_database.sh (100%) create mode 100755 bin/bak/commands/switch_local_env.sh create mode 100644 bin/bak/functions/backup_db.sh create mode 100755 bin/bak/functions/clone_repo.sh create mode 100644 bin/bak/functions/create_database.sh create mode 100644 bin/bak/functions/create_directories.sh create mode 100755 bin/bak/functions/deploy.sh create mode 100644 bin/bak/functions/fetch_files.sh create mode 100644 bin/bak/functions/import_db.sh create mode 100644 bin/bak/functions/link_latest_db.sh create mode 100644 bin/bak/functions/write_shopware_env_file.sh delete mode 100755 bin/commands/clone_repo.sh create mode 100644 bin/commands/create-project delete mode 100755 bin/commands/create_local_env.sh delete mode 100755 bin/commands/import_db.sh delete mode 100755 bin/commands/import_gdpr_data.sh create mode 100644 bin/commands/install-project-manager create mode 100644 bin/commands/list-projects create mode 100644 bin/commands/remove-project delete mode 100644 bin/commands/switch_system.sh create mode 100644 bin/includes/includes delete mode 100644 bin/includes/includes.sh create mode 100644 bin/includes/project_header delete mode 100755 bin/manager rename {backup/database => bin/postscripts/plugins}/.keep (100%) create mode 100755 bin/project-manager rename {bin/postscripts => data}/.keep (100%) create mode 100644 etc/.config_template delete mode 100644 etc/.keep create mode 100644 etc/.project_manager_config_template create mode 100644 etc/.projects_template delete mode 100644 git/.keep create mode 100644 plugins/db/etc/.config_template create mode 100644 plugins/git/commands/clone create mode 100644 plugins/git/etc/.config_template create mode 100644 plugins/git/includes/includes create mode 100644 plugins/shopware/etc/.config_template create mode 100644 plugins/shopware/includes/includes create mode 100644 plugins/ssh/commands/add-key create mode 100644 plugins/ssh/etc/.config_template create mode 100644 plugins/ssh/includes/includes delete mode 100644 shared/.keep delete mode 100644 var/tmp/.keep diff --git a/.etc/config.sh b/.etc/config.sh deleted file mode 100644 index 7f3b90b..0000000 --- a/.etc/config.sh +++ /dev/null @@ -1,211 +0,0 @@ -#!/bin/bash - -### DONT OVERWRITE THIS FILE - -app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd) - - -### GENERAL - - -project_name='www.domain.de' - - -### STAGE - - -stage_ssh_user='your-user' -stage_ssh_domain='your-domain' -stage_ssh_key_file='/path/to/.ssh/key' - -stage_httpdocs_path='/var/www/vhosts/domain.de/httpdocs' -stage_httpdocs_slug='/shopware' # optional slug if you install the software not directly in httpdocs - -stage_database_socket='' -stage_database_host='localhost' -stage_database_port='3306' -stage_database_name='database_name' -stage_database_user='database_user' -stage_database_password='database_password' - -# only needed if you want to create a database -stage_database_admin_user='' -stage_database_admin_password='' - - -### LIVE - - -live_ssh_user='user' -live_ssh_domain='domain.de' -live_ssh_key_file='' - -live_httpdocs_path='/var/www/vhosts/domain.de/httpdocs' -live_httpdocs_slug='' - -live_database_socket='' -live_database_host='localhost' -live_database_port='3306' -live_database_name='name' -live_database_user='user' -live_database_password='password' - -# only needed if you want to create a database -live_database_admin_user='' -live_database_admin_password='' - - -### LOCAL STAGE - - -local_stage_host='localhost' -local_stage_slug='/shopware' - -local_stage_database_socket='' -local_stage_database_host='localhost' -local_stage_database_port='3306' -local_stage_database_name='name_stage' -local_stage_database_user='user' -local_stage_database_password='password' - -# only needed if you can't create database and want to create a database -local_stage_database_admin_user='' -local_stage_database_admin_password='' - - -### LOCAL LIVE - - -local_live_host='localhost' -local_live_slug='/shopware' - -local_live_database_has_socket='n' -local_live_database_socket='' -local_live_database_host='localhost' -local_live_database_port='3306' -local_live_database_name='name_live' -local_live_database_user='user' -local_live_database_password='password' - -# only needed if you can't create database and want to create a database -local_live_database_admin_user='' -local_live_database_admin_password='' - - -### SHOPWARE - - -local_shopware_user='user' -local_shopware_email='email' -local_shopware_firstname='Name' -local_shopware_lastname='Lastname' - -install_shopware_name="Project" -install_shopware_email="$local_shopware_email" -install_shopware_locale='de_DE' -install_shopware_currency='EUR' -install_shopware_storefront_name="Storefront" -install_shopware_storefront_url='https://localhost/domain.de/www.domain.de' - - -### MAILER - - -local_mailer_url='smtp://localhost:1025' - - -### GIT - - -git_ssh_key_file='' -git_url='' -git_shopware_path='' - - -# you can update stage with data from live -live_to_stage_live_urls=("https://www.domain.de") -live_to_stage_stage_urls=("https://stage.domain.de") - -# if you copy the stage state to local -stage_to_local_stage_urls=("https://stage.domain.de") -stage_to_local_local_urls=("https://localhost/domain.de/stage.domain.de") - -# here more complete, copy the live system to local -live_to_local_live_urls=( - "https://www.domain.de" - "http://www.domain.de" -) -live_to_local_local_urls=( - "https://localhost/domain.de/www.domain.de" - "http://localhost/domain.de/www.domain.de" -) - - -### GDPR SHOPWARE 6 TABLES - - -gdpr_tables=( - acl_user_role - cart - customer - customer_address - customer_recovery - customer_tag - customer_wishlist - customer_wishlist_product - elasticsearch_index_task - import_export_log - integration - integration_role - log_entry - message_queue_stats - newsletter_recipient - newsletter_recipient_tag - order - order_address - order_customer - order_delivery - order_delivery_position - order_line_item - order_tag - order_transaction - product_export - product_review - promotion_persona_customer - refresh_token - sales_channel_api_context - state_machine_history - user - user_access_key - user_config - user_recovery - version - version_commit - version_commit_data - klarna_payment_request_log - payone_payment_card - payone_payment_mandate - payone_payment_redirect - unzer_payment_payment_device - unzer_payment_transfer_info - enqeueue - product_keyword_dictionary - product_search_keyword -) - - -### Media Files - -import_media_files=( - "/files" - "/public/media" -) - - -### POST SCRIPT - - -if [ -f "$app_dir/bin/post_config.sh" ] -then - source "$app_dir/bin/post_config.sh" -fi diff --git a/.etc/live.my.cnf b/.etc/live.my.cnf deleted file mode 100644 index 634f540..0000000 --- a/.etc/live.my.cnf +++ /dev/null @@ -1,4 +0,0 @@ -[mysqldump] -user= -password= -max_allowed_packet=500M diff --git a/.etc/local.my.cnf b/.etc/local.my.cnf deleted file mode 100644 index 95eadd5..0000000 --- a/.etc/local.my.cnf +++ /dev/null @@ -1,9 +0,0 @@ -[mysql] -user= -password= -max_allowed_packet=500M - -[mysqldump] -user= -password= -max_allowed_packet=500M diff --git a/.etc/stage.my.cnf b/.etc/stage.my.cnf deleted file mode 100644 index 634f540..0000000 --- a/.etc/stage.my.cnf +++ /dev/null @@ -1,4 +0,0 @@ -[mysqldump] -user= -password= -max_allowed_packet=500M diff --git a/.giignore b/.giignore new file mode 100644 index 0000000..4f607e2 --- /dev/null +++ b/.giignore @@ -0,0 +1,6 @@ +/.idea +/.trash +/bin/postscripts +/data +/etc/config +/etc/projects \ No newline at end of file diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 8818db1..0000000 --- a/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -.idea -.ssh -backup -bin/postscripts -etc -shared -var -!.keep \ No newline at end of file diff --git a/.ssh/.keep b/.trash/.keep similarity index 100% rename from .ssh/.keep rename to .trash/.keep diff --git a/bin/commands/import_media.sh b/bin/bak/commands/import_media.sh similarity index 82% rename from bin/commands/import_media.sh rename to bin/bak/commands/import_media.sh index 8ddbda9..f0d9c37 100755 --- a/bin/commands/import_media.sh +++ b/bin/bak/commands/import_media.sh @@ -18,20 +18,12 @@ ssh_domain="$(eval "echo \$${env}_ssh_domain")" slug="$(eval "echo \"\$local_${env}_slug\"")" source="$(eval "echo \"\$${env}_httpdocs_path\$${env}_httpdocs_slug\$filepath/.\"")" -target="$app_dir/shared/$project_name/$env$filepath" -link="$app_dir/git/${project_name}${slug}${filepath}/." +target="$app_dir/git/${project_name}${slug}${filepath}/." echo echo "Fetch Files ..." rsync -auvzqLK --delete "$ssh_user"@"$ssh_domain":"$source" "$target" -if [ -f "$link" ] -then - rm -rf "$link" -fi - -ln -s "$target" "$link" - postScript "$app_dir/bin/postscripts/import_media.sh" echo \ No newline at end of file diff --git a/bin/bak/commands/install_remote_system.sh b/bin/bak/commands/install_remote_system.sh new file mode 100644 index 0000000..0c4ab54 --- /dev/null +++ b/bin/bak/commands/install_remote_system.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +env="$(getArgument "$1" "Usage: install_remote_system.sh [live|stage]" "live stage")" + +echoMainTitle "Installing $env system" + +"$app_dir/bin/commands/single/create_directories.sh" "$env" +"$app_dir/bin/commands/single/create_database.sh" "$env" \ No newline at end of file diff --git a/bin/commands/install_shopware.sh b/bin/bak/commands/install_shopware.sh similarity index 69% rename from bin/commands/install_shopware.sh rename to bin/bak/commands/install_shopware.sh index cab7fd5..4f218d0 100755 --- a/bin/commands/install_shopware.sh +++ b/bin/bak/commands/install_shopware.sh @@ -11,7 +11,7 @@ updatemode=0 echo echoMainTitle "Shopware installer/updater" -path="$app_dir/git/$project_name$git_shopware_path" +path="$app_dir/git/$project_name" if [ -f "$path/install.lock" ] then @@ -30,6 +30,25 @@ then echoSelect "[stage]" "Local stage database" echo env=$(readConsole "Select: " "Invalid selection" "live stage") + + echo + echoSequenzeTitle "Basic configuration" + shop_name=$(requiredParameter "Shop name") + shop_email=$(requiredParameter "Shop email") + shop_locale=$(requiredParameter "Shop locale") + shop_currency=$(requiredParameter "Shop currency") + + echo + echoSequenzeTitle "Storefront configuration" + store_name=$(requiredParameter "Storefront name") + store_url=$(requiredParameter "Storefront url") + + echo + echoSequenzeTitle "Admin user" + admin_firstname=$(requiredParameter "Firstname") + admin_lastname=$(requiredParameter "Lastname") + admin_email=$(requiredParameter "Email") + admin_user=$(requiredParameter "User/Login") fi ### Fetch sources @@ -77,10 +96,9 @@ then echo "Setup Shopware ..." cp "$app_dir/etc/.env.local" "$path/.env" - bin/console system:install --shop-name="$install_shopware_name" --shop-email="$install_shopware_email" \ - --shop-locale="$install_shopware_locale" --shop-currency="$install_shopware_currency" --no-interaction --env="dev" - bin/console sales-channel:create:storefront --name="$install_shopware_storefront_name" --url="$install_install_shopware_storefront_url" --no-interaction --quiet - bin/console user:create -a --firstName="$local_shopware_user" --lastName="$local_shopware_firstname" --email="$local_shopware_email" "$local_shopware_user" + bin/console system:install --shop-name="$shop_name" --shop-email="$shop_email" --shop-locale="$shop_locale" --shop-currency="$shop_currency" --no-interaction --env="dev" + bin/console sales-channel:create:storefront --name="$store_name" --url="$store_url" --no-interaction --quiet + bin/console user:create -a --firstName="$admin_firstname" --lastName="$admin_lastname" --email="$admin_email" "$admin_user" else echo "Prepare Shopware setup ..." diff --git a/bin/commands/make_local_database.sh b/bin/bak/commands/make_local_database.sh similarity index 100% rename from bin/commands/make_local_database.sh rename to bin/bak/commands/make_local_database.sh diff --git a/bin/bak/commands/switch_local_env.sh b/bin/bak/commands/switch_local_env.sh new file mode 100755 index 0000000..35f5c4c --- /dev/null +++ b/bin/bak/commands/switch_local_env.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +env=$(getArgument $1 "Usage: switch_local_env.sh [live|stage]" "live stage") + +echoMainTitle "Switching local system to $env" + + + +echoFinal "... local environment switched" + +echo "Running postscript ..." +postScript "$app_dir/bin/postscripts/switch_local_env.sh" + + diff --git a/bin/bak/functions/backup_db.sh b/bin/bak/functions/backup_db.sh new file mode 100644 index 0000000..0733cf3 --- /dev/null +++ b/bin/bak/functions/backup_db.sh @@ -0,0 +1,104 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +usage="Usage: fetch_latest_db.sh [live|stage|local_live|local_stage] (1,0:fetchNewFlag) (1,0:withGdprFlag)" +env="$(getArgument "$1" "$usage" "live stage local_live local_stage")" +fetch_new="$(getArgument "$2" "$usage" "1 0")" +with_gdpr="$(getArgument "$3" "$usage" "1 0")" +is_remote="$(if [ "$env" == "live" ] || [ "$env" == "stage" ]; then echo 1; else echo 0; fi)" + +currentDate=$(date '+%Y-%m-%d_%H:%M:%S') + +echo +echoMainTitle "Backup $env system database" + +database_name="$(getConfig "$env" "database_name")" + +if [ "$is_remote" == "1" ] +then + ssh_user="$(getConfig "$env" "ssh_user")" + ssh_domain="$(getConfig "$env" "ssh_domain")" + addSSHKey "$env" + + ignoredTablesArguments=("$(buildIgnoredTablesArguments "$env" "${gdpr_tables[*]}")") + + echo "Backup structure ..." + + has_fetched="$(if [ -f "$app_dir/var/latest/${env}_structure.sql.gz" ]; then echo 1; else echo 0; fi)" + if [ "$fetch_new" == "1" ] || [ "$has_fetched" == "0" ] + then + scp "$app_dir/etc/$env.my.cnf" "$ssh_user"@"$ssh_domain":"~/$env.my.cnf" + ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/$env.my.cnf --no-tablespaces --no-data \ + \"$database_name\" | LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | \ + sed -e 's/,NO_AUTO_CREATE_USER//' | gzip -9 > ~/${env}_structure.sql.gz" + scp "$ssh_user"@"$ssh_domain":"~/${env}_structure.sql.gz" "$app_dir/backup/database/$currentDate-${env}_structure.sql.gz" + ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_structure.sql.gz" + fi + + echo "Backup data ..." + + has_fetched="$(if [ -f "$app_dir/var/latest/${env}_data.sql.gz" ]; then echo 1; else echo 0; fi)" + if [ "$fetch_new" == "1" ] || [ "$has_fetched" == "0" ] + then + ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/${env}.my.cnf --no-tablespaces --no-create-info \ + --skip-triggers ${ignoredTablesArguments[*]} \"$database_name\" | LANG=C LC_CTYPE=C LC_ALL=C \ + sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > ~/${env}_data.sql.gz" + scp "$ssh_user"@"$ssh_domain":"~/${env}_data.sql.gz" "$app_dir/backup/database/$currentDate-${env}_data.sql.gz" + ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_data.sql.gz" + fi + + if [ "$with_gdpr" == "1" ] + then + includedTables=("$(buildIncludedTables "${gdprTables[@]}")") + + echo "Backup GDPR ..." + + has_fetched="$(if [ -f "$app_dir/var/latest/${env}_gdpr_data.sql.gz" ]; then echo 1; else echo 0; fi)" + if [ "$fetch_new" == "1" ] || [ "$has_fetched" == "0" ] + then + ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/${env}.my.cnf --no-tablespaces --no-create-info --skip-triggers \ + \"$database_name\" ${includedTables[*]} | LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \ + ~/${env}_gdpr_data.sql.gz" + scp "$ssh_user"@"$ssh_domain":"~/${env}_gdpr_data.sql.gz" "$app_dir/backup/database/$currentDate-${env}_gdpr_data.sql.gz" + ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_gdpr_data.sql.gz" + fi + fi + + ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}.my.cnf" +else + + echo "Backup structure ..." + + mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --hex-blob --no-data "$database_name" | \ + LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | sed -e 's/,NO_AUTO_CREATE_USER//' | gzip -9 > \ + "$app_dir/backup/database/${currentDate}-${env}_structure.sql.gz" + + echo "Backup data ..." + + mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --no-create-info --skip-triggers --hex-blob \ + ${ignoredTablesArguments[*]} "$database_name" | \ + LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \ + "$app_dir/backup/database/${currentDate}-${env}_data.sql.gz" + + if [ "$with_gdpr" == "1" ] + then + includedTables=("$(buildIncludedTables "${gdprTables[@]}")") + + echo "Backup gdpr ..." + + mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --no-create-info --skip-triggers --hex-blob \ + "$database_name" ${includedTables[*]} | \ + LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \ + "$app_dir/backup/database/${currentDate}-${env}_gdpr_data.sql.gz" + fi +fi + +echoFinal "... database backed up" + +echo "Running postscript ..." +postScript "$app_dir/bin/postscripts/functions/backup_db.sh" \ No newline at end of file diff --git a/bin/bak/functions/clone_repo.sh b/bin/bak/functions/clone_repo.sh new file mode 100755 index 0000000..32bd539 --- /dev/null +++ b/bin/bak/functions/clone_repo.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +env="$(getArgument "$1" "Usage: clone_repo.sh [live|stage|local]" "live stage local")" + +echoMainTitle "Cloning $env system repository" + +is_remote=$(if [ "$env" == "live" ] || [ "$env" == "stage" ]; then echo 1; else echo 0; fi) +if [ "$is_remote" == "1" ] +then + addSSHKey "$env" + ssh_user="$(getConfig "$env" "ssh_user")" + ssh_domain="$(getConfig "$env" "ssh_domain")" + httpdocs_path="$(getConfig "$env" "httpdocs_path")" + httpdocs_git_subpath="$(getConfig "$env" "httpdocs_git_subpath")" + has_git="$(ssh "$ssh_user"@"ssh_domain" "[ -d \"$httpdocs_path$httpdocs_git_subpath/.git\" ] && echo 1 || echo 0")" + + if [ "$has_git" == "1" ] + then + echoWarning "Git allready installed, aborting" + else + ssh "$ssh_user"@"ssh_domain" "cd $httpdocs_path$httpdocs_git_subpath && git clone \"$git_url\" ." + ssh "$ssh_user"@"ssh_domain" "cd $httpdocs_path$httpdocs_git_subpath && git config credential.helper store" + fi +fi + +if [ "$is_remote" == "0" ] +then + has_git="$(if [ -d "$app_dir/git/$project_name" ]; then echo 1; else echo 0; fi)" + if [ "$has_git" == "1" ] + then + echoWarning "Git allready installed, aborting" + else + git clone "$git_url" "$app_dir/git/$project_name" + fi +fi + +echoFinal "... git repository cloned" + +echo "Running postscript ..." +postScript "$app_dir/bin/postscript/functions/clone_repo.sh" diff --git a/bin/bak/functions/create_database.sh b/bin/bak/functions/create_database.sh new file mode 100644 index 0000000..04cdb85 --- /dev/null +++ b/bin/bak/functions/create_database.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +env="$(getArgument "$1" "Usage: create_database.sh [live|stage|local_live|local_stage]" "live stage local_live local_stage")" + +echoMainTitle "Create $env system database" + +database_name="$(getConfig "$env" "database_name")" +ssh_user="$(getConfig "$env" "ssh_user")" +ssh_domain="$(getConfig "$env" "ssh_domain")" +is_remote=$(if [ "$env" == "live" ] || [ "$env" == "stage" ]; then echo 1; else echo 0; fi) + +if [ "$is_remote" == "1" ] +then + addSSHKey "$env"; + scp "$app_dir/etc/$env.my.cnf" "$ssh_user"@"$ssh_domain":"~/$env.my.cnf" + has_database=$(ssh "$ssh_user"@"ssh_domain" "mysql --defaults-extra-file=\"~/$env.my.cnf\" --defaults-group-suffix=\"admin\" -se \"SHOW DATABASES\" | grep \"$database_name\"") +fi + +if [ "$is_remote" == "0" ] +then + has_database=$(mysql --defaults-extra-file="$app_dir/etc/$env.my.cnf" --defaults-group-suffix="admin" -se "SHOW DATABASES" | grep "$database_name") +fi + +if [ "$has_database" == "" ] +then + create="$(readConsole "The Environment $env has no Database, should i creeate it? [y,n]", "Invalid selection" "y n")" + + if [ "$create" == "y" ] && [ "$is_remote" == "1" ] + then + ssh "$ssh_user"@"ssh_domain" "mysql --defaults-extra-file=\"~/$env.my.cnf\" --defaults-group-suffix=\"admin\" -se \"CREATE DATABASE \`$database_name\`\"" + elif [ "$create" == "y" ] + then + mysql --defaults-extra-file="$app_dir/etc/$env.my.cnf" --defaults-group-suffix="admin" -se "CREATE DATABASE \`$database_name\`" + fi +fi + +if [ "$is_remote" == "1" ] +then + ssh "$ssh_user"@"ssh_domain" "rm \"~/$env.my.cnf\"" +fi + +echoFinal "... database created" + +echo "Running postscript ..." +postScript "$app_dir/bin/postscript/functions/create_database.sh" diff --git a/bin/bak/functions/create_directories.sh b/bin/bak/functions/create_directories.sh new file mode 100644 index 0000000..7581ee5 --- /dev/null +++ b/bin/bak/functions/create_directories.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +env="$(getArgument "$1" "Usage: create_directories.sh [live|stage]" "live stage")" + +echoMainTitle "Installing $env system directories" + +addSSHKey "$env" + +httpdocs_path="$(getConfig "$env" "httpdocs_path")" +httpdocs_git_subpath="$(getConfig "$env" "httpdocs_git_subpath")" +ssh_user="$(getConfig "$env" "ssh_user")" +ssh_domain="$(getConfig "$env" "ssh_domain")" +database_name="$(getConfig "$env" "database_name")" + +has_httpdocs_path="$(ssh "$ssh_user"@"ssh_domain" "[ -d \"$httpdocs_path\" ] && echo 1 || echo 0")" +has_httpdocs_git_path="$(ssh "$ssh_user"@"ssh_domain" "[ -d \"$httpdocs$httpdocs_git_subpath\" ] && echo 1 || echo 0")" + +if [ "$has_httpdocs" == "0" ] +then + create_httpdocs="$(readConsole "The $env System has no httpdocs ($httpdocs_path), should i create it [y,n]" "Invalid selection" "y n")" + if [ "$create_httpdocs" == "y" ] + then + ssh "$ssh_user"@"ssh_domain" "mkdir -p \"$httpdocs_path\"" + fi +fi + +if [ "$has_httpdocs_git_path" == "0" ] +then + create_git="$(readConsole "The $env System has no subpath to git directory ($httpdocs_path$httpdocs_git_path), should i create it [y,n]" "Invalid selection" "y n")" + if [ "$create_git" == "y" ] + then + ssh "$ssh_user"@"ssh_domain" "mkdir -p \"$httpdocs_path$httpdocs_git_subpath\"" + fi +fi + +echoFinal "... directories created" + +echo "Running postscript ..." +postScript "$app_dir/bin/postscript//create_directories.sh" \ No newline at end of file diff --git a/bin/bak/functions/deploy.sh b/bin/bak/functions/deploy.sh new file mode 100755 index 0000000..3d36a70 --- /dev/null +++ b/bin/bak/functions/deploy.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +env=$(getArgument $1 "Usage: deploy.sh [live|stage]" "live stage") + +echoMainTitle "Deploying $env system" + +httpdocs_path="$(getConfig "$env" "httpdocs_path")" +httpdocs_git_subpath="$(getConfig "$env" "httpdocs_git_subpath")" +ssh_user="$(getConfig "$env" "ssh_user")" +ssh_domain="$(getConfig "$env" "ssh_domain")" +path="$httpdocs_path$httpdocs_git_subpath" + +addSSHKey "$env" + +ssh "$ssh_user"@"$ssh_domain" "cd $path && git stash" +ssh "$ssh_user"@"$ssh_domain" "cd $path && git ls-files -m | rm" +ssh "$ssh_user"@"$ssh_domain" "cd $path && git pull" +ssh "$ssh_user"@"$ssh_domain" "$path/bin/console cache:clear" +ssh "$ssh_user"@"$ssh_domain" "$path/bin/console theme:compile" + +echo "... system deployed" + +echo "Running postscript ..." +postScript "$app_dir/bin/postscripts/functions/deploy.sh" \ No newline at end of file diff --git a/bin/bak/functions/fetch_files.sh b/bin/bak/functions/fetch_files.sh new file mode 100644 index 0000000..d3f66af --- /dev/null +++ b/bin/bak/functions/fetch_files.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +env="$(getArgument "$1" "Usage: fetch_files.sh [live|stage]" "live stage")" + +ssh_user="$(getConfig "$env" "ssh_user")" +ssh_domain="$(getConfig "$env" "ssh_domain")" +httpdocs_path="$(getConfig "$env" "httpdocs_path")" +httpdocs_git_subpath="$(getConfig "$env" "httpdocs_git_subpath")" +httpdocs_shopware_subpath="$(getConfig "$env" "httpdocs_shopware_subpath")" +remote_path="$httpdocs_path$httpdocs_git_subpath$httpdocs_shopware_subpath" +local_path="$app_dir/git/$project_name/$local_httpdocs_shopware_subpath" +shared_path="$app_dir/shared/$project_name/$env" + +echoMainTitle "Fetching $env system files" + +for file in "${shared_files[@]}" +do + echo "Fetching files $file" + addSSHKey "$env" + scp "$ssh_user"@"$ssh_domain":"$remote_path/$file/." "$shared_path/$file/." + if [ -f "$local_path/$file" ] + then + rm -rf "$local_path/$file" + fi + ln -s "$shared_path/$file" "$local_path/$file" +done + +echoFinal "... files fetched" + +echo "Running postscript ..." +postScript "$app_dir/bin/postscripts/function/fetch_files.sh" + diff --git a/bin/bak/functions/import_db.sh b/bin/bak/functions/import_db.sh new file mode 100644 index 0000000..bec041d --- /dev/null +++ b/bin/bak/functions/import_db.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +usage="Usage: import_db.sh [live|stage] (1,0:withGdprData)" +env="$(getArgument "$1" "$usage" "live stage")" +with_gdpr="$(getArgument "$2" "$usage" "1 0")" + +echoMainTitle "Importing $env database to local installation" + +structure_file="$app_dir/var/latest/${env}_structure.sql.gz" +data_file="$app_dir/var/latest/${env}_data.sql.gz" +gdpr_file="$app_dir/var/latest/${env}_gdpr_data.sql.gz" + +database_name=$(getConfig "local_$env" "database_name") + +if [ ! -f "$structure_file" ]; then echoError "Missing structure file - aborting"; fi +if [ ! -f "$data_file" ]; then echoError "Missing data file - aborting"; fi +if [ ! -f "$gdpr_file" ] && [ "$with_gdpr" == "1" ]; then echoError "Missing GDPR file - aborting"; fi + +if [ -f "$structure_file" ] && [ -f "$data_file" ] && ([ "$with_gdpr" == "1" ] && [ -f "$gdpr_file" ] || [ "$with_gdpr" == "0" ]) +then + + echo "Uncompress Database ..." + + gzip -fdc "$app_dir/var/latest/${env}_structure.sql.gz" > "$app_dir/var/tmp/${env}_structure.sql" + gzip -dfc "$app_dir/var/latest/${env}_data.sql.gz" > "$app_dir/var/tmp/${env}_data.sql" + if [ "$with_gdpr" == "1" ]; then gzip -dfc "$app_dir/var/latest/${env}_gdpr_data.sql.gz" > "$app_dir/var/tmp/${env}_gdpr_data.sql"; fi + + echo "Drop local tables ..." + + mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --add-drop-table --add-drop-trigger --no-data "$database_name" | \ + grep -e '[DROP TABLE|DROP TRIGGER]' >> "$app_dir/var/tmp/${env}_drop_local.sql" + echo "SET FOREIGN_KEY_CHECKS = 1;" >> "$app_dir/var/tmp/${env}_drop_local.sql" + mysql --defaults-extra-file="$app_dir/etc/local.my.cnf" -D "$database_name" < "$app_dir/var/tmp/${env}_drop_local.sql" + rm "$app_dir/var/tmp/${env}_drop_local.sql" + + echo "Import structure ..." + mysql --defaults-extra-file="$app_dir/etc/local.my.cnf" -D "$database_name" < "$app_dir/var/tmp/${env}_structure.sql" + + echo "Import data ..." + mysql --defaults-extra-file="$app_dir/etc/local.my.cnf" -D "$database_name" < "$app_dir/var/tmp/${env}_data.sql" + + if [ "$with_gdpr" == "1" ] + then + echo "Import GDPR ..." + mysql --defaults-extra-file="$app_dir/etc/local.my.cnf" -D "$database_name" < "$app_dir/var/tmp/${env}_gdpr_data.sql" + fi + + echo "Cleanup ..." + rm "$structure_file" + rm "$data_file" + rm "$gdpr_file" +fi diff --git a/bin/bak/functions/link_latest_db.sh b/bin/bak/functions/link_latest_db.sh new file mode 100644 index 0000000..7ebbb08 --- /dev/null +++ b/bin/bak/functions/link_latest_db.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +env=$(getArgument $1 "Usage: link_latest_db.sh [live|stage]" "live stage") + +echo +echoMainTitle "Set $env system latest databases links" + +files=($(ls -t | head -1)) +date=$("${files[0]}" | sed -e "s/(\d{4}-\d{2}-\d{2}_\d{2}:\d{2}:\d{2}).*/\1/") + +struct_file="${date}_${env}_structure.sql.gz" +data_file="${date}_${env}_data.sql.gz" +gdpr_file="${date}_${env}_gdpr_data.sql.gz" + +if [ -f "$struct_file" ]; then ln -s "$struct_file" "$app_dir/var/latest/${env}_structure.sql.gz"; fi +if [ -f "$data_file" ]; then ln -s "$data_file" "$app_dir/var/latest/${env}_data.sql.gz"; fi +if [ -f "$gdpr_data_file" ]; then ln -s "$gdpr_data_file" "$app_dir/var/latest/${env}_gdpr_data.sql.gz"; fi + +echoFinal "... latest databases linked" + +echo "Running postscript ..." +postScript "$app_dir/bin/postscripts/functions/set_latest_db.sh" \ No newline at end of file diff --git a/bin/bak/functions/write_shopware_env_file.sh b/bin/bak/functions/write_shopware_env_file.sh new file mode 100644 index 0000000..d5958cd --- /dev/null +++ b/bin/bak/functions/write_shopware_env_file.sh @@ -0,0 +1,84 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +source "$(dirname "${BASH_SOURCE[0]}")/../../includes/includes.sh" +app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd) +source "$app_dir/etc/config.sh" + +env="$(getArgument "$1" "Usage: write_shopware_env_file.sh [live|stage|local_live|local_stage]" "live stage local_live local_stage")" +is_remote=$(if [ "$env" == "live" ] || [ "$env" == "stage" ]; then echo 1; else echo 0; fi) + +echoMainTitle "Create $env system .env file" + +currentDate=$(date '+%Y-%m-%d_%H:%M:%S') + +database_socket="$(getConfig "$env" "database_socket")" +database_host="$(getConfig "$env" "database_host")" +database_port="$(getConfig "$env" "database_port")" +database_name="$(getConfig "$env" "database_name")" +database_user="$(getConfig "$env" "database_user")" +database_password="$(getConfig "$env" "database_password")" + +if [ "$database_socket" != "" ] +then + db_connection="mysql://$database_host?unix_socket=$database_socket&dbname=$database_name&user=$database_user&password=$database_password" +else + db_connection="mysql://$database_user:$database_password@$database_host:$database_port/$database_name" +fi + +app_url=${"$(getConfig "$env" "urls")"[0]} + +app_secret="$(getSecret "APP_SECRET")" +echo "$app_secret" > "$app_dir/etc/app_secret" +instance_id="$(getSecret "INSTANCE_ID")" +echo "$instance_id" > "$app_dir/etc/instance_id" + +short="$(echo "$env" | sed -e "s/[_live|_stage]//")" +host="$(eval "echo \"\${${short}_urls[0]}\"")" + +cat <<- EOF > "var/tmp/$env.env" +APP_ENV=dev +APP_SECRET=$app_secret +APP_URL=$app_url +TRUSTED_HOSTS=$host +MAILER_URL=$local_mailer_url +INSTANCE_ID=$instance_id +DATABASE_URL=$db_connection +COMPOSER_HOME=vendor +BLUE_GREEN_DEPLOYMENT=1 +SHOPWARE_HTTP_CACHE_ENABLED=0 +SHOPWARE_HTTP_DEFAULT_TTL=0 +EOF + +if [ "$is_remote" == "1" ] +then + ssh_user="$(eval "echo \"\$${env}_ssh_user\"")" + ssh_domain="$(eval "echo \"\$${env}_ssh_domain\"")" + httpdocs_path="$(getConfig "$env" "httpdocs_path")" + httpdocs_git_subpath="$(getConfig "$env" "httpdocs_git_subpath")" + httpdocs_shopware_subpath="$(getConfig "$env" "httpdocs_shopware_subpath")" + path="$httpdocs_path$httpdocs_git_subpath$httpdocs_shopware_subpath" + addSSHKey "$env" + has_env_file="$(ssh "ssh_user"@"ssh_domain" "[ -f \""$path/.env"\" ] && echo 1 || echo 2")" + if [ "$has_env_file" == "1" ] + then + scp "$ssh_user"@"$ssh_domain":"$path/.env" "$app_dir/backup/$currentDate.$short.env}" + fi + scp "var/tmp/$env.env" "$ssh_user"@"$ssh_domain":"$path/.env" +else + path="$app_dir/git/$project_name/$local_httpdocs_shopware_subpath" + has_env_file="$(ssh "[ -f \""$path/.env"\" ] && echo 1 || echo 2")" + if [ "$has_env_file" == "1" ] + then + cp "$path/.env" "$app_dir/backup/$currentDate.$short.env}" + fi + cp "var/tmp/$env.env" "$path/.env" +fi + +rm "var/tmp/$env.env" + +echoFinal "... .env file created" + +echo "Running postscript ..." +postScript "$app_dir/bin/postscripts/function/write_shopware_env_file.sh" \ No newline at end of file diff --git a/bin/commands/clone_repo.sh b/bin/commands/clone_repo.sh deleted file mode 100755 index bd0f108..0000000 --- a/bin/commands/clone_repo.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -### DO NOT EDIT THIS FILE - -source "$(dirname "${BASH_SOURCE[0]}")/../includes/includes.sh" -app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd) -source "$app_dir/etc/config.sh" - -echo -echoMainTitle "Cloning Repository" - -repo_path="$app_dir/git/$project_name" - -if [ ! -d "$repo_path" ] && [ "$git_url" != "" ] -then - addSSHKey "git" - git clone "$git_url" "$repo_path" -fi - -postScript "$app_dir/bin/postscript/clone_repo.sh" diff --git a/bin/commands/create-project b/bin/commands/create-project new file mode 100644 index 0000000..30775d3 --- /dev/null +++ b/bin/commands/create-project @@ -0,0 +1,158 @@ +#!/bin/bash + +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" + +function usage { + echo + echoMainTitle "Creates a new project" + echo + echoSubTitle "Usage:" + echo + echo "create-project [customer] [project] [shortname]" + echo + echo "The given customer and project will result in the file directory as you have setted" + echo "it in project_manager/etc/projects configuration file:" + echo " - workspace_dir/customer/project" + echo " - data_dir/customer/project" + echo "It will set a configuration file for your project to:" + echo " - data_dir/customer/project/etc/config" + echo + echo "--help Prints this message" + 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 + +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")" + +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")" + +cat <<- EOF > "$project_manager_dir/etc/projects" +#!/bin/bash + +projects=($projects_string +) +shortnames=($shortnames_string +) +customers=($customers_string +) + +EOF + +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/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 + etc_file="$plugin_path/$plugin/etc/.config_template" + if [ -f "$etc_file" ] && [ ! -f "$path/etc/plugins/$plugin/config" ] + then + if [ ! -d "$path/etc/plugins/$plugin" ] + then + mkdir "$path/etc/plugins/$plugin" + fi + cp "$etc_file" "$path/etc/plugins/$plugin/config" + fi +done + +if [ ! -d "$workspaces_dir/$customer/$project" ]; then mkdir -p "$workspaces_dir/$customer/$project"; fi + +echo +echoSubTitle "Running postscripts ..." +postScript "$path/bin/postscripts/commands/create-project" +postScript "$workspaces_dir/$customer/$project/bin/postscripts/commands/create-project" + +echo +echoSuccess "Project has been created, please configure: $path/etc/*" +echo \ No newline at end of file diff --git a/bin/commands/create_local_env.sh b/bin/commands/create_local_env.sh deleted file mode 100755 index 2378c56..0000000 --- a/bin/commands/create_local_env.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash - -### DO NOT EDIT THIS FILE - -source "$(dirname "${BASH_SOURCE[0]}")/../includes/includes.sh" -app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd) -source "$app_dir/etc/config.sh" - -env=$(getArgument "1" "Usage: create_local_env.sh [local_live|local_stage]" "local_live local_stage" | sed -e 's/local_//') - -echo -echoMainTitle "Create local .env file" -echo - -local_database_socket=$(eval "echo \"\$local_${env}_database_socket\"") -local_database_host=$(eval "echo \"\$local_${env}_database_host\"") -local_database_port=$(eval "echo \"\$local_${env}_database_port\"") -local_database_name=$(eval "echo \"\$local_${env}_database_name\"") -local_database_user=$(eval "echo \"\$local_${env}_database_user\"") -local_database_password=$(eval "echo \"\$local_${env}_database_password\"") -if [ "$local_database_socket" != "" ] -then - db_connection="mysql://$local_database_host?unix_socket=$local_database_socket&dbname=$local_database_name&user=$local_database_user&password=$local_database_password" -else - db_connection="mysql://$local_database_user:$local_database_password@$local_database_host:$local_database_port/$local_database_name" -fi - -app_url=$(eval "echo \"\$${env}_to_local_local_urls[0]\"") - -ssh_user=$(eval "echo \"\$${env}_ssh_user\"") -ssh_domain=$(eval "echo \"\$${env}_ssh_domain\"") -ssh_httpdocs_path=$(eval "echo \"\$${env}_ssh_httpdocs_path\"") -ssh_httpdocs_slug=$(eval "echo \"\$${env}_ssh_httpdocs_slug\"") - -addSSHKey "$env" -app_secret=$(ssh "$ssh_user"@"$ssh_domain" "cat \"$ssh_httpdocs_path$ssh_httpdocs_slug/.env\"" | sed -i -r 's/^APP_SECRET=\(.*\)\$/\\1/') -instance_id=$(ssh "$ssh_user"@"$ssh_domain" "cat \"$ssh_httpdocs_path$ssh_httpdocs_slug/.env\"" | sed -i -r 's/^INSTANCE_ID=\(.*\)\$/\\1/') - -host=$(eval "echo \"\$local_${env}_host}\"") - -path="$app_dir/git/$project_name" - -cat <<- EOF > "$path/.env" -APP_ENV=dev -APP_SECRET=$app_secret -APP_URL=$app_url -TRUSTED_HOSTS=$host -MAILER_URL=$local_mailer_url -INSTANCE_ID=$instance_id -DATABASE_URL=$db_connection -COMPOSER_HOME=vendor -BLUE_GREEN_DEPLOYMENT=1 -SHOPWARE_HTTP_CACHE_ENABLED=0 -SHOPWARE_HTTP_DEFAULT_TTL=0 -EOF - -postScript "$app_dir/bin/postscripts/create_local_env.sh" \ No newline at end of file diff --git a/bin/commands/import_db.sh b/bin/commands/import_db.sh deleted file mode 100755 index 4678ba0..0000000 --- a/bin/commands/import_db.sh +++ /dev/null @@ -1,84 +0,0 @@ -#!/bin/bash - -### DO NOT EDIT THIS FILE - -source "$(dirname "${BASH_SOURCE[0]}")/../includes/includes.sh" -app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd) -source "$app_dir/etc/config.sh" - -env=$(getArgument "$1" "Usage: import_db.sh [live|stage]" "live stage") - -echo -echoMainTitle "Import Database from $env to local ($env)" - -currentDate=$(date '+%Y-%m-%d_%H:%M:%S') - -addSSHKey "$env" -ssh_user="$(eval "echo \$${env}_ssh_user")" -ssh_domain="$(eval "echo \$${env}_ssh_domain")" - -ignoredTablesArguments=$(buildIgnoredTablesArguments "$env" "${gdpr_tables[*]}") - -echo "Fetching Database ..." - -database_name=$(eval "echo \$${env}_database_name") -local_database_name=$(eval "echo \"\$local_${env}_database_name\"") - -scp "$app_dir/etc/$env.my.cnf" "$ssh_user"@"$ssh_domain":"~/$env.my.cnf" -ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/$env.my.cnf --no-tablespaces --no-data \ - \"$database_name\" | LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | \ - sed -e 's/,NO_AUTO_CREATE_USER//' | gzip -9 > ~/${env}_structure.sql.gz" -scp "$ssh_user"@"$ssh_domain":"~/${env}_structure.sql.gz" "$app_dir/var/tmp/${env}_structure.sql.gz" -ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_structure.sql.gz" -ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/${env}.my.cnf --no-tablespaces --no-create-info \ - --skip-triggers $ignoredTablesArguments \"$database_name\" | LANG=C LC_CTYPE=C LC_ALL=C \ - sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > ~/${env}_data.sql.gz" -scp "$ssh_user"@"$ssh_domain":"~/${env}_data.sql.gz" "$app_dir/var/tmp/${env}_data.sql.gz" -ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_data.sql.gz" -ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}.my.cnf" - -echo "Backup Database ..." - -mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --hex-blob --no-data "$local_database_name" | \ - LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | sed -e 's/,NO_AUTO_CREATE_USER//' | gzip -9 > \ - "$app_dir/backup/database/${currentDate}_local_${env}_structure.sql.gz" - -mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --no-create-info --skip-triggers --hex-blob "$local_database_name" | \ - LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \ - "$app_dir/backup/database/${currentDate}_local_${env}_data.sql.gz" - -cp "$app_dir/var/tmp/${env}_structure.sql.gz" "$app_dir/backup/database/${currentDate}_${env}_structure.sql.gz" -cp "$app_dir/var/tmp/${env}_data.sql.gz" "$app_dir/backup/database/${currentDate}_${env}_data.sql.gz" - -echo "Uncompress Database ..." -gzip --force -d "$app_dir/var/tmp/${env}_structure.sql.gz" -gzip --force -d "$app_dir/var/tmp/${env}_data.sql.gz" - -echo "Install Database ..." - -mysql --defaults-extra-file="$app_dir/etc/local.my.cnf" -e "CREATE DATABASE IF NOT EXISTS \`$local_database_name\`;" - -echo "Remove Tables ..." -mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --add-drop-table --add-drop-trigger --no-data "$local_database_name" | \ - grep -e '[DROP TABLE|DROP TRIGGER]' >> "$app_dir/var/tmp/drop_local_$env.sql" -echo "SET FOREIGN_KEY_CHECKS = 1;" >> "$app_dir/var/tmp/drop_local_$env.sql" -mysql --defaults-extra-file="$app_dir/etc/local.my.cnf" -D "$local_database_name" < "$app_dir/var/tmp/drop_local_$env.sql" - -echo "Import Structure ..." -mysql --defaults-extra-file="$app_dir/etc/local.my.cnf" -D "$local_database_name" < "$app_dir/var/tmp/${env}_structure.sql" - -echo "Import Data ..." -mysql --defaults-extra-file="$app_dir/etc/local.my.cnf" -D "$local_database_name" < "$app_dir/var/tmp/${env}_data.sql" - -echo "Post Scripts ..." -postScript "$app_dir/bin/postscripts/import_db.sh" - -"$app_dir/bin/commands/make_local_database.sh" "$env" -"$app_dir/bin/commands/create_local_env.sh" "local_$env" - -echo "Cleanup ..." - -rm "$app_dir/var/tmp/drop_local_$env.sql" -rm "$app_dir/var/tmp/${env}_structure.sql" -rm "$app_dir/var/tmp/${env}_data.sql" - diff --git a/bin/commands/import_gdpr_data.sh b/bin/commands/import_gdpr_data.sh deleted file mode 100755 index 6284b41..0000000 --- a/bin/commands/import_gdpr_data.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash - -### DO NOT EDIT THIS FILE - -source "$(dirname "${BASH_SOURCE[0]}")/../includes/includes.sh" -app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd) -source "$app_dir/etc/config.sh" - -env=$(getArgument "1" "Usage: import_gdpr_data.sh [live|stage]" "live stage") - -echo -echoMainTitle "Fetch GDPR Data from $env" - -currentDate=$(date '+%Y-%m-%d_%H:%M:%S') - -addSSHKey "$env" -ssh_user="$(eval "echo \$${env}_ssh_user")" -ssh_domain="$(eval "echo \$${env}_ssh_domain")" -includedTables=$(buildIncludedTables "${gdprTables[@]}" -database_name=$(eval "echo \$${env}_database_name") -local_database_name=$(eval "echo \$local_${env}_database_name") - -echo "Fetching Data ..." -scp "$app_dir/etc/${env}.my.cnf" "$ssh_user"@"$ssh_domain":"~/${env}.my.cnf" -ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/${env}.my.cnf --no-tablespaces --no-create-info --skip-triggers \ - "$database_name" "${includedTables[@]}" | LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \ - ~/${env}_gdpr_data.sql.gz" -scp "$ssh_user"@"$ssh_domain":"~/${env}_gdpr_data.sql.gz" "$app_dir/var/tmp/${env}_gdpr_data.sql.gz" -ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_gdpr_data.sql.gz" -ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}.my.cnf" - -echo "Backup Data ..." - -cp "$app_dir/var/tmp/${env}_gdpr_data.sql.gz" "$app_dir/backup/database/${env}/${currentDate}_${env}_gdpr_data.sql.gz" - -echo "Uncompress Data ..." - -gzip -d "$app_dir/var/tmp/${env}_gdpr_data.sql.gz" - -echo "Import Data ..." - -mysql --defaults-extra-file="$app_dir/etc/local.my.cnf" -D "$local_database_name" < "$app_dir/var/tmp/${env}_gdpr_data.sql" - -echo "Post Scripts ..." - -postScript "$app_dir/bin/postscripts/import_gdpr_data.sh" ] - -echo "Cleanup ..." - -rm "$app_dir/var/tmp/${env}_gdpr_data.sql" diff --git a/bin/commands/install-project-manager b/bin/commands/install-project-manager new file mode 100644 index 0000000..5b613a4 --- /dev/null +++ b/bin/commands/install-project-manager @@ -0,0 +1,53 @@ +#!/bin/bash + +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/includes" + +function usage { + echo + echoMainTitle "Installs Sumedia Project Manager" + echo + echoSubTitle "Usage:" + echo + echo "install-project-manager" + echo + echo "--help Prints this message" + echo +} + +help="$(getParameter "--help" false "$@")" +if [ "$help" == true ] +then + usage + exit +fi + +echo +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)")" + +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")\"/" \ + > "$project_manager_dir/etc/config" +fi + +if [ ! -f "$project_manager_dir/etc/projects" ] +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" +postScript "$workspaces_dir/$customer/$project/bin/postscripts/commands/install-project-manager" + +echo +echoSuccess "Sumedia Project Manager has been installed" +echo \ No newline at end of file diff --git a/bin/commands/list-projects b/bin/commands/list-projects new file mode 100644 index 0000000..90d07b1 --- /dev/null +++ b/bin/commands/list-projects @@ -0,0 +1,50 @@ +#!/bin/bash + +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" + +function usage { + echo + echoMainTitle "List all projects" + echo + echoSubTitle "Usage:" + echo + echo "list-projects" + echo + echo "--help Prints this message" + echo +} + +function listProjects { + echo >&2 + echoSubTitle "Projects list" >&2 + echo >&2 + for i in "${!projects[@]}" + do + echo "- Shortname: ${shortnames[$i]} + Customer: ${customers[$i]} + Project: ${projects[$i]}" >&2 + echo + done +} + +echo +echoMainTitle "Listing projects" + +help="$(getParameter "--help" false "$*")" +if [ "$help" == true ] +then + usage + exit +else + if [ "${#projects[@]}" -eq 0 ] + then + echo + echo "There are currently no projects setted up" + echo + else + listProjects + fi +fi + +echoSuccess "Listing done" \ No newline at end of file diff --git a/bin/commands/remove-project b/bin/commands/remove-project new file mode 100644 index 0000000..15bbbc5 --- /dev/null +++ b/bin/commands/remove-project @@ -0,0 +1,122 @@ +#!/bin/bash + +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" +source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" + +function usage { + echo + echoMainTitle "Removes a project" + echo + echoSubTitle "Usage:" + echo + echo "remove-project [shortname]" + echo + echo "This will remove the project and move the project files to ./.trash" + echo + echo "--help Prints this message" + 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 + +echo +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 +confirm + +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 + +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 +echo "Moving files to trash" + +rand=$(cat /proc/sys/kernel/random/uuid) +trash_path="$project_manager_dir/.trash/$rand" +mkdir "$trash_path" +mkdir "$trash_path/data" +mkdir "$trash_path/workspace" +mv "$project_manager_dir/data/$customer" "$trash_path/data/$customer" +mv "$workspaces_dir/$customer" "$trash_path/workspace/$customer" + +echo +echoSubTitle "Running postscripts ..." +postScript "$path/bin/postscripts/commands/remove-project" +postScript "$workspaces_dir/$customer/$project/bin/postscripts/commands/remove-project" + +echo +echoSuccess "Project has been remove, (moved to ./.trash)" +echo \ No newline at end of file diff --git a/bin/commands/switch_system.sh b/bin/commands/switch_system.sh deleted file mode 100644 index cb0bf61..0000000 --- a/bin/commands/switch_system.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -### DO NOT EDIT THIS FILE - -source "$(dirname "${BASH_SOURCE[0]}")/../includes/includes.sh" -app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd) -source "$app_dir/etc/config.sh" - -env=$(getArgument "$1" "Usage: switch_system.sh [live|stage] [true|false: UpdateDatabase]" "live stage") -update_database_flag=$(getArgument "$2" "Usage: switch_system.sh [live|stage] [true|false: UpdateDatabase]" "true false") - -echoMainTitle "Switch to local_$env System" - -shopware_check_install="$app_dir/git/$project_name$git_shopware_path/src" -if [ ! -d "$shopware_check_install" ] -then - echo - echoError "No Shopware installed, try bin/commands/install_shopware.sh" - exit -fi - -if [ "$update_database_flag" == true ] -then - "$app_dir/bin/commands/import_db.sh" "$env" - "$app_dir/bin/commands/create_local_env.sh" "local_$env" - "$app_dir/bin/commands/make_local_database.sh" "$env" -fi - -for path in "${import_media_files[@]}" -do - "$app_dir/bin/commands/import_media.sh" "$env" "$path" -done - diff --git a/bin/includes/includes b/bin/includes/includes new file mode 100644 index 0000000..9314bfd --- /dev/null +++ b/bin/includes/includes @@ -0,0 +1,215 @@ +#!/bin/bash + +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 diff --git a/bin/includes/includes.sh b/bin/includes/includes.sh deleted file mode 100644 index 221611d..0000000 --- a/bin/includes/includes.sh +++ /dev/null @@ -1,166 +0,0 @@ -#!/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 diff --git a/bin/includes/project_header b/bin/includes/project_header new file mode 100644 index 0000000..a296464 --- /dev/null +++ b/bin/includes/project_header @@ -0,0 +1,15 @@ +#!/bin/bash + +source "$project_manager_dir/bin/includes/includes" +shortname="$(getArgument "$1" "$(usage)" true)" +project="$(getProjectFromShortname "$shortname")" +customer="$(getCustomerFromShortname "$shortname")" +loadProjectConfig "$shortname" +app_dir="$project_manager_dir/data/$customer/$project" + +help="$(getParameter "--help" false "$*")" +if [ "$help" == true ] || [ "$1" == "" ] +then + usage + exit +fi \ No newline at end of file diff --git a/bin/manager b/bin/manager deleted file mode 100755 index c39b831..0000000 --- a/bin/manager +++ /dev/null @@ -1,100 +0,0 @@ -#!/bin/bash - -set -e - -source "$(dirname "${BASH_SOURCE[0]}")/includes/includes.sh" -app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd) -source "$app_dir/etc/config.sh" - -echo -echoMainTitle "What do you want to do today?" - -echo -echoSelect "[1]" "Databaseupdate" -echoSelect "[2]" "Fetching Files" -echo -main_selection=$(readConsole "Select: " "Invalid selection" "1 2") -words=( - 'databaseupdate' - 'files' -) - -for i in "${!words[@]}" -do - ((cur=i+1)) - if [ $cur == "$main_selection" ] - then - main_selection=${words[$i]} - fi -done - -echo $main_selection - -if [ "$main_selection" == "databaseupdate" ] -then - echo - echoSubTitle "Databaseupdate" - echo - echo "From Environment:" - echo - echoSelect "[live] " "Live" - echoSelect "[stage] " "Stage" - echo - env=$(readConsole "Select: " "Invalid selection" "live stage") - - echo - confirm=$(readConsole "You want to copy the database from $env to local ($env)? [y,n]: " "Invalid selection" "y n") - - if [ "$confirm" == "y" ] - then - source "$app_dir/bin/commands/import_db.sh" "$env" - else - echo "Aborted" - fi -fi - -if [ "$main_selection" == "files" ] -then - echo - echoSubTitle "Fetching Files" - echo - echo "From Environment:" - echo - echoSelect "[live] " "Live" - echoSelect "[stage] " "Stage" - env=$(readConsole "Select: " "Invalid selection" "live stage") - - echoSubTitle "Select a directory:" - echo - echoSelect "[1] " "/files" - echoSelect "[2] " "/public/media" - echoSelect "[3] " ".. custom .." - files_selection=$(readConsole "Select: " "Invalid selection" "1 2 3") - values=( - "/files" - "/public/media" - ) - if [ "$files_selection" -gt "${#values[@]}" ] - then - files_selection=$(readConsole "Please give a subpath to the shopware root on $env: ", "Invalid selection", true) - else - for i in "${!values[@]}" - do - ((cur=i+1)) - if [ "$cur" == "$files_selection" ] - then - files_selection="${values[$i]}" - fi - done - fi - - echo - confirm=$(readConsole "You want to copy the /files from $env to local ($env)? [y,n]: " "Invalid selection" "y n") - - if [ "$confirm" == "y" ] - then - source "$app_dir/bin/commands/import_media.sh" "$env" "$files_selection" - else - echo "Aborted" - fi -fi \ No newline at end of file diff --git a/backup/database/.keep b/bin/postscripts/plugins/.keep similarity index 100% rename from backup/database/.keep rename to bin/postscripts/plugins/.keep diff --git a/bin/project-manager b/bin/project-manager new file mode 100755 index 0000000..5a036e0 --- /dev/null +++ b/bin/project-manager @@ -0,0 +1,149 @@ +#!/bin/bash +set -e + +project_manager_dir="$(readlink -f "$(dirname "$(readlink -f ${BASH_SOURCE[0]})")/..")" +source "$project_manager_dir/bin/includes/includes" +if [ -f "$project_manager_dir/etc/config" ] +then + source "$project_manager_dir/etc/config" +fi + +if [ -f "$project_manager_dir/etc/projects" ] +then + source "$project_manager_dir/etc/projects" +fi + +function use { + echo + echoMainTitle "Sumedia Project Manager" + echoSmall "Manager: $project_manager_dir" + echoSmall "Workspaces: $workspaces_dir" + echo + echoSubTitle "Usage:" + echo + echo "project-manager [command] [command-args]" + echo "project-manager [plugin:command] [command-args]" + echo + echo "--help Prints this message" + echo "--help [command] Prints the help message for a command" + echo "--help [plugin:command] Prints the help message for a command" + echo "--list-commands List all commands" + echo "--list-commands [plugin] List all plugin commands" + echo "--list-plugins List all plugins" + echo +} + +function list { + local dir=$1 + + echo >&2 + echoSubTitle "List $dir" >&2 + echo >&2 + + dir=($(ls -t "$project_manager_dir/$dir")) + for script in "${dir[@]}" + do + echo "- $script" >&2 + done + echo >&2 +} + +if [ "$1" == "" ] +then + use + exit +fi + +help="$(getParameter "--help" false "$*")" +list_commands="$(getParameter "--list-commands" false "$*")" +list_plugins="$(getParameter "--list-plugins" false "$*")" + +if [[ "$1" == *"--"* ]]; then key=2; else key=1; fi +param="$(eval "echo \"\$${key}\"")" +if [[ "$param" == *":"* ]] +then + command="$(echo "$param" | cut -f2 -d':')" + plugin="$(echo "$param" | cut -f1 -d':')" +else + command=$param + plugin="" +fi + +if [ ! -f "$project_manager_dir/etc/config" ] || [ ! -f "$project_manager_dir/etc/projects" ] +then + command="install-project-manager" + plugin="" +fi + +if [ "$help" == true ] || [ "$list_commands" == true ] || [ "$list_plugins" == "true" ] +then + + if [ "$command" == "" ] + then + use + exit + elif [ "$command" != "" ] && [ "$plugin" != "" ] && [ "$help" == true ] + then + file="$project_manager_dir/plugins/$plugin/commands/$command" + if [ ! -f "$file" ] + then + echo + echoError "No such file or directory: $file" + echo + exit + else + source "$file" --help + exit + fi + elif [ "$command" != "" ] && [ "$help" == true ] + then + file="$project_manager_dir/bin/commands/$command" + if [ ! -f "$file" ] + then + echo + echoError "No such file or directory: $file" + echo + exit + else + source "$file" --help + exit + fi + elif [ "$command" != "" ] && [ "$plugin" != "" ] && [ "$list_commands" == true ] + then + list "plugins/$plugin/commands" + elif [ "$command" != "" ] && [ "$list_commands" == true ] + then + list "bin/commands" + elif [ "$list_plugins" == true ] + then + list "plugins" + fi + +else + + if [ "$plugin" != "" ] + then + file="$project_manager_dir/plugins/$plugin/commands/$command" + else + file="$project_manager_dir/bin/commands/$command" + fi + + if [ ! -f "$file" ] + then + echo + echoError "No such file or directory: $file" + echo + exit + else + source "$file" "${@:2}" + fi + + echo + echoSubTitle "Running postscripts ..." + postScript "$project_manager_dir/bin/postscripts/project-manager" + + echo + echoSuccess "Project Manager done" + echo + +fi \ No newline at end of file diff --git a/bin/postscripts/.keep b/data/.keep similarity index 100% rename from bin/postscripts/.keep rename to data/.keep diff --git a/etc/.config_template b/etc/.config_template new file mode 100644 index 0000000..869e70a --- /dev/null +++ b/etc/.config_template @@ -0,0 +1,35 @@ +#!/bin/bash + +### GENERAL + +app_customer="" +app_project="" +app_project_manager_dir="" + +### STAGE + +stage_httpdocs_path="" +stage_httpdocs_git_subpath="" +stage_httpdocs_shopware_subpath="" + +### LIVE + +live_httpdocs_path="" +live_httpdocs_git_subpath="" +live_httpdocs_shopware_subpath="" + +### LOCAL STAGE + +local_stage_host="" + +### LOCAL LIVE + +local_live_host="" + +### POST SCRIPT + +app_dir="$app_project_manager_dir/data/$customer/$project" +if [ -f "$app_dir/etc/post_config.sh" ] +then + source "$app_dir/etc/post_config.sh" +fi diff --git a/etc/.keep b/etc/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/etc/.project_manager_config_template b/etc/.project_manager_config_template new file mode 100644 index 0000000..6a1c6ec --- /dev/null +++ b/etc/.project_manager_config_template @@ -0,0 +1,5 @@ +#!/bin/bash + +workspaces_dir="" +project_manager_dir="" + diff --git a/etc/.projects_template b/etc/.projects_template new file mode 100644 index 0000000..f26c7fa --- /dev/null +++ b/etc/.projects_template @@ -0,0 +1,5 @@ +#!/bin/bash + +projects=() +shortnames=() +customers=() \ No newline at end of file diff --git a/git/.keep b/git/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/plugins/db/etc/.config_template b/plugins/db/etc/.config_template new file mode 100644 index 0000000..267f994 --- /dev/null +++ b/plugins/db/etc/.config_template @@ -0,0 +1,49 @@ +#!/bin/bash + +### STAGE + +db_stage_socket="" +db_stage_host="" +db_stage_port="" +db_stage_name="" +db_stage_user="" +db_stage_password="" + +db_stage_admin_user="" +db_stage_admin_password="" + +### LIVE + +db_live_socket="" +db_live_host="" +db_live_port="" +db_live_name="" +db_live_user="" +db_live_password="" + +db_live_admin_user="" +db_live_admin_password="" + +### LOCAL_STAGE + +db_local_stage_socket="" +db_local_stage_host="" +db_local_stage_port="" +db_local_stage_name="" +db_local_stage_user="" +db_local_stage_password="" + +db_local_stage_admin_user="" +db_local_stage_admin_password="" + +### LOCAL_LIVE + +db_local_live_socket="" +db_local_live_host="" +db_local_live_port="" +db_local_live_name="" +db_local_live_user="" +db_local_live_password="" + +db_local_live_admin_user="" +db_local_live_admin_password="" \ No newline at end of file diff --git a/plugins/git/commands/clone b/plugins/git/commands/clone new file mode 100644 index 0000000..d94b876 --- /dev/null +++ b/plugins/git/commands/clone @@ -0,0 +1,60 @@ +#!/bin/bash + +function usage { + echo + echoMainTitle "Clone repository" + echo + echoSubTitle "Usage:" + echo + echo "project-manager git:clone [project-shortname]" + echo + echo "--help Prints this message" + echo +} + +source "$project_manager_dir/bin/includes/project_header" +gitValidate + +echo +echoMainTitle "Cloning Repository" + +if [ "$git_url" == "" ] +then + echo + echoError "There is no git url configured" + echo + exit +fi + +echo +echoSubTitle "Please confirm following data" +echo +if [ "$git_ssh_private_key" != "" ] +then + echo "SSH Private Key File: $git_ssh_public_key" +fi +if [ "$git_ssh_public_key" != "" ] +then + echo "SSH Public Key File: $git_ssh_public_key" +fi +echo "GIT Url: $git_url" +echo "GIT User: $git_ssh_user" +echo "GIT Domain: $git_ssh_domain" +echo "Install Location: $workspaces_dir/$customer/$project" +echo +confirm="$(readConsole "Should i execute [y,n]:" "Invalid selection" "y n")" + +if [ "$confirm" == "n" ] +then + echo + echoError "Aborting" + echo + exit; +fi + +sshAddKey "git" +git clone "$git_url" "$workspaces_dir/$customer/$project" + +echo +echoSuccess "GIT cloned" +echo \ No newline at end of file diff --git a/plugins/git/etc/.config_template b/plugins/git/etc/.config_template new file mode 100644 index 0000000..e4d73e4 --- /dev/null +++ b/plugins/git/etc/.config_template @@ -0,0 +1,9 @@ +#!/bin/bash + +### GIT + +git_url="" +git_ssh_user="" +git_ssh_domain="" +git_ssh_private_key="" +git_ssh_public_key="" \ No newline at end of file diff --git a/plugins/git/includes/includes b/plugins/git/includes/includes new file mode 100644 index 0000000..83a0254 --- /dev/null +++ b/plugins/git/includes/includes @@ -0,0 +1,13 @@ +#!/bin/bash + +git_included=true + +function gitValidate { + if [ "$git_url" == "" ] + then + echo >&2 + echoError "Please configure $project_manager_dir/data/$customer/$project/etc/plugins/git/config" >&2 + echo >&2 + exit + fi +} diff --git a/plugins/shopware/etc/.config_template b/plugins/shopware/etc/.config_template new file mode 100644 index 0000000..ffb3566 --- /dev/null +++ b/plugins/shopware/etc/.config_template @@ -0,0 +1,88 @@ +#!/bin/bash + +### GENERAL + +shopware_version="" + +shopware6_httpdocs_subpath="" + +shopware6_local_user="" +shopware6_local_email="" +shopware6_local_firstname="" +shopware6_local_lastname="" + +shopware6_install_name="" +shopware6_install_email="" +shopware6_install_locale="" +shopware6_install_currency="" +shopware6_install_storefront_name="" +shopware6_install_storefront_url="" + +### URL MAPPING + +shopware6_live_urls=() +shopware6_stage_urls=() +shopware6_local_urls=() + +### GDPR SHOPWARE 6 TABLES + +shopware6_gdpr_tables=( + acl_user_role + cart + customer + customer_address + customer_recovery + customer_tag + customer_wishlist + customer_wishlist_product + elasticsearch_index_task + import_export_log + integration + integration_role + log_entry + message_queue_stats + newsletter_recipient + newsletter_recipient_tag + order + order_address + order_customer + order_delivery + order_delivery_position + order_line_item + order_tag + order_transaction + product_export + product_review + promotion_persona_customer + refresh_token + sales_channel_api_context + state_machine_history + user + user_access_key + user_config + user_recovery + version + version_commit + version_commit_data + klarna_payment_request_log + payone_payment_card + payone_payment_mandate + payone_payment_redirect + unzer_payment_payment_device + unzer_payment_transfer_info + enqeueue + product_keyword_dictionary + product_search_keyword +) + +### Media Files + +shopware6_shared_files=( + "/files" + "/public/media" + "/public/thumbnails" +) + +### MAILER + +shopware6_local_mailer_url="" \ No newline at end of file diff --git a/plugins/shopware/includes/includes b/plugins/shopware/includes/includes new file mode 100644 index 0000000..f83d684 --- /dev/null +++ b/plugins/shopware/includes/includes @@ -0,0 +1,47 @@ +#!/bin/bash + +function shopware6BuildIgnoredTablesArguments { + 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 shopware6GetSecret { + local prop="$(getArgument "$1" "Usage: getSecret [APP_SECRET|INSTANCE_ID]" "APP_SECRET INSTANCE_ID")" + local httpdocs_shopware_subpath="$(getConfig "local" "httpdocs_shopware_subpath")" + local path="$app_dir/git/$project_name$httpdocs_shopware_subpath" + + app_secret="$(cat "$app_dir/etc/app_secret")" + app_secret="$(if [ "$app_secret" == "" ]; then echo "$(_shopware6GetRemoteSecret "live" "$prop")"; else echo "$app_secret"; fi)" + app_secret="$(if [ "$app_secret" == "" ]; then echo "$(_shopware6GetRemoteSecret "stage" "$prop")"; else echo "$app_secret"; fi)" + app_secret="$(if [ "$app_secret" == "" ]; then echo "$(cat \""$path/.env"\" | sed -i -r "s/^$prop=\(.*\)\$/\\1/")"; else echo "$app_secret"; fi)" + app_secret="$(if [ "$app_secret" == "" ]; then echo "$(_shopware6GenerateSecret)"; else echo "$app_secret"; fi)" +} + +function _shopware6GetRemoteSecret { + local env="$(getArgument "$1" "Usage: _getRemoteSecret [live|stage] [APP_SECRET|INSTANCE_ID]" "live stage")" + local prop="$(getArgument "$2" "Usage: _getRemoteSecret [live|stage] [APP_SECRET|INSTANCE_ID]" "APP_SECRET INSTANCE_ID")" + local ssh_user="$(getConfig "$env" "ssh_user")" + local ssh_domain="$(getConfig "$env" "ssh_domain")" + local httpdocs_path="$(getConfig "$env" "httpdocs_path")" + local httpdocs_git_subpath="$(getConfig "$env" "httpdocs_git_subpath")" + local httpdocs_shopware_subpath="$(getConfig "$env" "httpdocs_shopware_subpath")" + local path="$httpdocs_path$httpdocs_git_subpath$httpdocs_shopware_subpath" + + addSSHKey "$env" + echo "$(ssh "$ssh_user"@"$ssh_domain" "cat \"$path/.env"\" | sed -i -r "s/^$prop=\(.*\)\$/\\1/")" +} + +function _shopware6GenerateSecret { + echo openssl rand -hex 32 +} diff --git a/plugins/ssh/commands/add-key b/plugins/ssh/commands/add-key new file mode 100644 index 0000000..e59bca6 --- /dev/null +++ b/plugins/ssh/commands/add-key @@ -0,0 +1,70 @@ +#!/bin/bash + +function usage { + echo + echoMainTitle "Create, move the ssh keys and install them to server" + echo + echoSubTitle "Usage:" + echo + echo "project-manager ssh:add-key [project-shortname] [env]" + echo + echo " [env] could be live, stage or git" + echo + echo "--help Prints this message" + echo +} + +source "$project_manager_dir/bin/includes/project_header" +env="$(getArgument "$2" "$(usage)" "live stage git")" +sshValidate + +ssh_private_key="$(sshGetConfig "$env" "private_key")" +ssh_public_key="$(sshGetConfig "$env" "public_key")" +ssh_user="$(sshGetConfig "$env" "user")" +ssh_domain="$(sshGetConfig "$env" "domain")" + +echo "$ssh_private_key" +echo "$ssh_public_key" +echo "$ssh_user" +echo "$ssh_domain" + +echo +echoMainTitle "Adding SSH Keys" +echo +echoSubTitle "Please verify data" +echo +echo "-- $env" +echo "Private key: $ssh_private_key" +echo "Public key: $ssh_public_key" +echo +confirm + +if [ "$ssh_private_key" != "" ] +then + target="$app_dir/.ssh/$env" + if [ -f "$source" ] && [ ! -f "$target" ] + then + cp "$ssh_private_key" "target" + chmod 0600 "$target" + fi +fi + +if [ "$ssh_public_key" != "" ] +then + target="$app_dir/.ssh/$env.pub" + if [ -f "$source" ] && [ ! -f "$target" ] + then + cp "$ssh_public_key" "target" + chmod 0600 "$target" + fi +fi + +if [ "$ssh_private_key" == "" ] && [ "$ssh_public_key" == "" ] && [ ! -f "$app_dir/.ssh/$env" ] && [ ! -f "$app_dir/.ssh/$env.pub" ] +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" + ssh-copy-id -i "$app_dir/.ssh/$env.pub" "$ssh_user"@"$ssh_domain" +fi + +ssh-add "$app_dir/.ssh/$env" \ No newline at end of file diff --git a/plugins/ssh/etc/.config_template b/plugins/ssh/etc/.config_template new file mode 100644 index 0000000..18d1197 --- /dev/null +++ b/plugins/ssh/etc/.config_template @@ -0,0 +1,15 @@ +#!/bin/bash + +### STAGE + +ssh_stage_user="" +ssh_stage_domain="" +ssh_stage_private_key="" +ssh_stage_public_key="" + +### LIVE + +ssh_live_user="" +ssh_live_domain="" +ssh_live_private_key="" +ssh_live_public_key="" diff --git a/plugins/ssh/includes/includes b/plugins/ssh/includes/includes new file mode 100644 index 0000000..41e4de5 --- /dev/null +++ b/plugins/ssh/includes/includes @@ -0,0 +1,161 @@ +#!/bin/bash + +ssh_included=true + +function sshValidate { + if [ "$ssh_stage_user" == "" ] || [ "$ssh_stage_domain" == "" ] || [ "$ssh_live_user" == "" ] || [ "$ssh_live_domain" == "" ] + then + echo >&2 + echoError "Please configure $project_manager_dir/data/$customer/$project/etc/plugins/ssh/config" >&2 + echo >&2 + exit + fi + + if [ "$git_included" == "" ] + then + echo >&2 + echoError "Plugin \"git\" has to be included" >&2 + echo >&2 + exit + fi + + if [ "$git_ssh_user" == "" ] || [ "$git_ssh_domain" == "" ] + then + echo >&2 + echoError "Please configure $project_manager_dir/data/$customer/$project/etc/plugins/git/config" >&2 + echo >&2 + exit + fi +} + + +function sshGetConfig { + local env=$(getArgument "$1" "Usage getEnvVar [live|stage|git] var" "live stage git") + local suffix=$(getArgument "$2" "Usage getEnvVar [live|stage|git] var" true) + + if [ "$env" == "live" ] || [ "$env" == "stage" ] + then + echo "$(eval "echo \"\$ssh_${env}_$suffix\"")" + else + echo "$(eval "echo \"\$${env}_ssh_$suffix\"")" + fi +} + +function sshGetPrivateKey +{ + local env=$(getArgument "$1" "Usage: sshGetPrivateKey [live|stage|git]" "live stage git") + + if [ "$env" == "live" ] || [ "$env" == "stage" ] + then + echo "$(sshGetConfig "$env" "private_key")" + else + echo "$ssh_private_key" + fi +} + +function sshGetPublicKey +{ + local env=$(getArgument "$1" "Usage: addSSHPublic [live|stage|git]" "live stage git") + + if [ "$env" == "live" ] || [ "$env" == "stage" ] + then + echo "$(sshGetConfig "$env" "private_key")" + else + echo "$ssh_private_key" + fi +} + +function sshAddKey { + local env="$(getArgument "$1" "Usage: sshAddKey [live|stage|git]" "live stage git")" + local ssh_private_key="$(sshGetPrivateKey "$env")" + local ssh_public_key="$(sshGetPublicKey "$env")" + + if [ "$ssh_private_key" != "" ] && [ "$ssh_public_key" != "" ] + then + if [ ! -f "$app_dir/.ssh/$env" ] && [ ! -f "$app_dir/.ssh/$env.pub" ] + then + sshCopyKeys "$env" + fi + else + if [ ! -f "$app_dir/.ssh/$env" ] && [ ! -f "$app_dir/.ssh/$env.pub" ] + then + sshInstallKeys "$env" + fi + fi + ssh-add "$app_dir/.ssh/$env" +} + +function sshCopyKeys { + local env=$(getArgument "$1" "Usage: sshCopyKeys [live|stage|git]" "live stage git") + local ssh_private_key="$(sshGetPrivateKey "$env")" + local ssh_public_key="$(sshGetPublicKey "$env")" + + if [ ! -f "$ssh_private_key" ] && [ ! -f "$ssh_public_key" ] + then + echo + echoError "Configureg $env key files don't exists" >&2 + echo + exit + fi + + if [ -f "$app_dir/.ssh/$env" ] + then + echo + echoError "SSH $env private key already exists" >&2 + echo + exit + fi + + if [ -f "$app_dir/.ssh/$env.pub" ] + then + echo + echoError "SSH $env public key already exists" >&2 + echo + exit + fi + + cp "$ssh_private_key" "$app_dir/.ssh/$env" + chmod 0600 "$app_dir/.ssh/$env" + + cp "$ssh_public_key" "$app_dir/.ssh/$env.pub" + chmod 0600 "$app_dir/.ssh/$env.pub" +} + +function sshInstallKeys { + local env=$(getArgument "$1" "Usage: sshInstallKeys [live|stage|git]" "live stage git") + local ssh_private_key="$(sshGetPrivateKey "$env")" + local ssh_public_key="$(sshGetPublicKey "$env")" + local user="$(sshGetConfig "$env" "user")" + local domain="$(sshGetConfig "$env" "domain")" + + if [ -f "$ssh_private_key" ] && [ -f "$ssh_public_key" ] + then + echo + echoError "Can't create $env key files, there are already some configured" >&2 + echo + exit + fi + + if [ -f "$app_dir/.ssh/$env" ] + then + echo + echoError "SSH $env private key already exists" >&2 + echo + exit + fi + + if [ -f "$app_dir/.ssh/$env.pub" ] + then + echo + echoError "SSH $env public key already exists" >&2 + echo + exit + fi + + 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" +} diff --git a/shared/.keep b/shared/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/var/tmp/.keep b/var/tmp/.keep deleted file mode 100644 index e69de29..0000000