#!/usr/bin/env bash # --------------------------------------------------------------------------------------------------------------------- # SCRIPT EXEC LOCATION: /opt/update # --------------------------------------------------------------------------------------------------------------------- # Usage: ./export_project.sh -project=maisonalexandra.lv -branch="release/legacy-system-php74"= git_repository="" www_project_dir="" git_clone_dir="" project_title="" export_branch="" backup_cfg_file_location_check="" Red='\033[31m' Green='\033[0;32m' Color_Off='\033[0m' script_dir="/opt/update/project_updates" for i in "$@"; do case $i in -project=*|--update_project=*) project_title="${i#*=}" ;; -branch=*|--export_branch=*) export_branch="${i#*=}" ;; *) echo "Unknown parameter passed: $i"; exit 1 ;; esac done # Override Script dir for testing for maisonalexandra.local.test if [[ "$project_title" == "maisonalexandra.local.test" ]]; then script_dir="/var/www/html/development/scripts/temp" script_dir="/Users/marekskeiss/Projects/brsystems/git/MAISON ALEXANDRA/development/scripts/temp" fi statusOkMessageOutput() { local msg=$1 local status=$2 local dashes="------------------------------------------------------------" echo -e "$msg ${dashes:1:60-${#msg}}> ${Green}$status${Color_Off}" } dirOperationFailure() { echo -e "\n${Red}ABORTING: ${Color_Off}An error occured while executing cd command...${Red}\n" echo -e "${Red}$1${Color_Off}\n" exit 0 } # Check if base data provided if [[ -z "$project_title" || -z "$export_branch" ]]; then echo "Base data not provided...." exit 0 fi # Declare configuration for export declare -a repoConfiguration repoConfiguration[0]="maisonalexandra.lv;ssh://git@gitea.brsystems.lv:2020/brs/MAISON ALEXANDRA.git;MAISON ALEXANDRA;/var/www;system-brs-core/config/init.cfg.ini" repoConfiguration[1]="maisonalexandra.local.test;ssh://git@gitea.brsystems.lv:2020/brs/MAISON ALEXANDRA.git;MAISON ALEXANDRA;$script_dir/www;system-brs-core/config/init.cfg.ini" repoConfiguration[3]="domain.fr;fr;http" for page in "${repoConfiguration[@]}" do # turn e.g. 'domain.de;de;https' into # array ['domain.de', 'de', 'https'] IFS=";" read -r -a arr <<< "${page}" site="${arr[0]}" # maisonalexandra.lv if [[ "$project_title" == "$site" ]]; then git_repository="${arr[1]}" # ssh://git@gitea.brsystems.lv:2020/brs/MAISON ALEXANDRA.git git_clone_dir="${arr[2]}" # MAISON ALEXANDRA www_project_dir="${arr[3]}" # /var/www backup_cfg_file_location_check="${arr[4]}" # system-brs-core/config/init.cfg.ini break fi done # Check if base structure found in cfg if [[ -z "$git_clone_dir" || -z "$git_repository" || -z "$www_project_dir" || ! -d "$script_dir" || ! -d "$www_project_dir/$project_title" || ! -d "$www_project_dir/$project_title" ]]; then echo "Base configuration not found...." exit 0 fi statusOkMessageOutput "Base configuration load & check" "OK" # Create copy of current project state temp_dir_title=$(date +%Y.%m.%d)_${project_title}_update_$(date +%H%M%S) working_dir="$script_dir/$temp_dir_title" # if directory exists exit if [[ -d "$working_dir" ]]; then echo "Project already exists ahead of backup creation - try again a bit later..." exit 0 fi mkdir "$working_dir" cd "$working_dir" || dirOperationFailure "$working_dir" statusOkMessageOutput "Working directory configuration" "OK" # Create backup copy of original code mkdir -p "$working_dir/backup/${project_title}" cp -rip "$www_project_dir/$project_title" "$working_dir/backup" if [[ ! -d "$working_dir/backup/${project_title}" || ! -d "$working_dir/backup/${project_title}/files" || ! -f "$working_dir/backup/${project_title}/${backup_cfg_file_location_check}" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Backup copy creation failed!\n" exit 0 fi statusOkMessageOutput "Backup copy created" "OK" statusOkMessageOutput "Zipping project backup data - SLOW!!!" "Zipping" # Zip backup for easier access cd "$working_dir/backup/" || dirOperationFailure "$working_dir/backup/" zip -r "${project_title}.zip" "${project_title}" >/dev/null if [[ ! -f "${project_title}.zip" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Backup copy zip creation failed!\n" exit 0 fi echo -e "Backup .zip location: $working_dir/backup/${project_title}.zip" statusOkMessageOutput "Backup copy zipped for easier access" "OK" # Extract new code from git cd "$working_dir" || dirOperationFailure "$working_dir" git clone "$git_repository" cd "$git_clone_dir" || dirOperationFailure "$git_clone_dir" statusOkMessageOutput "Git repository cloned" "OK" # Check that the correct branch exists # -q quiet output, not to output found string # -i option, short for –ignore-case # -c option tells grep to count its total number of occurrences # by default exit status is 0 (true) if the pattern was found; otherwise blank string. if [[ $(git branch -a | grep -ic "$export_branch") -eq 0 ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Specified branch not found...${Red}\n" echo -e "${Red}$export_branch${Color_Off}\n" exit 0 fi if [[ $(git branch -a | grep -ic "$export_branch") -gt 1 ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}More than one export branch found:${Red}\n" git branch -a | grep -i "$export_branch" echo -e "\n${Color_Off}" exit 0 fi # Checkout desired branch git checkout "$export_branch" # Check that the correct branch has been exported if [[ ! $(git branch | grep -ic "$export_branch") -eq 1 ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Branch checkout failure! :${Red}\n" echo -e "$export_branch${Color_Off}" exit 0 fi statusOkMessageOutput "Switched to correct branch" "OK" # Create git archive for update git archive --format zip --output "$working_dir/export.zip" "$export_branch" if [[ ! -f "$working_dir/export.zip" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Zip archive not found!${Red}\n" exit 0 fi statusOkMessageOutput "Archive .zip created successfully" "OK" cd "$working_dir" || dirOperationFailure "$working_dir" unzip export.zip -d "$working_dir/new_version_data" >/dev/null cd "$working_dir/new_version_data" || dirOperationFailure "$working_dir/new_version_data" statusOkMessageOutput "Archive successfully unzipped" "OK" # Data / folder movement according to each specific project case $project_title in #-------------------------------------------------------------------------------------- # ZUTENIS GIT BASE DEPLOYMENT #-------------------------------------------------------------------------------------- "zutenis.lv"|"zutenis.local.test") su_user_pass='$2y$11$nM7tTu3k/rQGswhREUXlsOUJoZdbumlcoQG07YiCFbQGFTqcYY3Di' su_user_key='3HMZGRH7JEC2OBPR' # Moving & renaming WWW mv "$working_dir/new_version_data/www" "$working_dir/new_$project_title" if [[ ! -d "$working_dir/new_$project_title" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}WWW DIR movement failed!${Red}\n" exit 0 fi cd "$working_dir/new_$project_title" || dirOperationFailure "$working_dir/new_$project_title" rm -Rf "files" rm -f "system-brs-core/config/init.cfg.ini" cp "$www_project_dir/$project_title/system-brs-core/config/init.cfg.ini" "system-brs-core/config/init.cfg.ini" cp -r "$www_project_dir/$project_title/files" "files" # Validate that the directories are all in place if [[ ! -f "system-brs-core/config/init.cfg.ini" || ! -d "files" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Some files are not copied correctly!" exit 0 fi statusOkMessageOutput "File update / movement" "OK" cd "$working_dir/new_$project_title" || dirOperationFailure "$working_dir/new_$project_title" twoFactor='3HMZGRH7JEC2OBPR' password='$2y$11$nM7tTu3k\/rQGswhREUXlsOUJoZdbumlcoQG07YiCFbQGFTqcYY3Di' sed -i "s/^ CONST suUserPassword.*/ CONST suUserPassword = '$password';/" system-brs-core/app/pubUsers/inc/pubUsersController.php sed -i "s/^ CONST sSUser2FASecret.*/ CONST sSUser2FASecret = '$twoFactor';/" system-brs-core/app/pubUsers/inc/pubUsersController.php statusOkMessageOutput "Sed replacement operations" "OK" chown apache:apache -R "$working_dir/new_$project_title" cd "$working_dir/new_$project_title" || dirOperationFailure "$working_dir/new_$project_title" find . -type f -exec chmod 0644 {} \; find . -type d -exec chmod 0755 {} \; statusOkMessageOutput "Directory permission [chown/chmod] setup" "OK" chcon -t httpd_sys_content_t "$working_dir/new_$project_title" -R chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/cache" -R chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/files" -R chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/logs" -R statusOkMessageOutput "SElinux Permission [httpd_sys_rw_content_t] setup" "OK" # Check if destination already doesn't have new folder if [[ -d "$www_project_dir/new_$project_title" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Movement to base WWW dir failed!" echo -e "\n${Red}$www_project_dir/new_$project_title ${Color_Off}already exists!\n" exit 0 fi # Update COMPOSER library location - each project might contain its own copy # - so it will reside inside project folder #-------------------------------------------------------------------------------------- cd "$working_dir/new_$project_title" || dirOperationFailure "$working_dir/new_$project_title" sed -i "s#^ include './../vendor/autoload.php';# include './vendor/autoload.php';#" ./index.php # Copy composer files to directory cp "$working_dir/new_version_data/composer.json" "$working_dir/new_$project_title/composer.json" cp "$working_dir/new_version_data/composer.lock" "$working_dir/new_$project_title/composer.lock" # Modify composer file autoload paths sed -i 's#^ "BRSystems\\\\Core\\\\Cms\\\\" : "www/system-brs-core/cms/",# "BRSystems\\\\Core\\\\Cms\\\\" : "system-brs-core/cms/",#' "$working_dir/new_$project_title/composer.json" sed -i 's#^ "BRSystems\\\\Core\\\\App\\\\" : "www/system-brs-core/app/",# "BRSystems\\\\Core\\\\App\\\\" : "system-brs-core/app/",#' "$working_dir/new_$project_title/composer.json" sed -i 's#^ "BRSystems\\\\Core\\\\Sys\\\\" : "www/system-brs-core/sys/",# "BRSystems\\\\Core\\\\Sys\\\\" : "system-brs-core/sys/",#' "$working_dir/new_$project_title/composer.json" sed -i 's#^ "BRSystems\\\\Core\\\\Config\\\\" : "www/system-brs-core/config/",# "BRSystems\\\\Core\\\\Config\\\\" : "system-brs-core/config/",#' "$working_dir/new_$project_title/composer.json" sed -i 's#^ "BRSystems\\\\Core\\\\Library\\\\" : "www/system-brs-core/library/",# "BRSystems\\\\Core\\\\Library\\\\" : "system-brs-core/library/",#' "$working_dir/new_$project_title/composer.json" sed -i 's#^ "BRSystems\\\\Core\\\\Helpers\\\\" : "www/system-brs-core/functions/"# "BRSystems\\\\Core\\\\Helpers\\\\" : "system-brs-core/functions/"#' "$working_dir/new_$project_title/composer.json" # Validate copied files if [[ ! -f "composer.json" || ! -f "composer.lock" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Composer cfg files are not copied correctly!" exit 0 fi statusOkMessageOutput "Composer cfg files copied" "OK" statusOkMessageOutput "Composer install - SLOW!!!" "installing" # Override composer install for testing for maisonalexandra.local.test # TODO BRS: Be great to create a separate container with same server structure and testing ground if [[ "$project_title" == "maisonalexandra.local.test" ]]; then if [[ $(docker exec -i maisonalexandra_php bash -c "cd /var/www/html/development/scripts/temp/$temp_dir_title/new_$project_title; composer install --no-dev" 2>&1 | grep -ic "autoload") -eq 0 ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Composer installation failed...${Red}\n" exit 0 fi else # Install composer library if [[ $(COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev 2>&1 | grep -ic "autoload") -eq 0 ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Composer installation failed...${Red}\n" exit 0 fi fi # Check some dirs exists in vendor folder if [[ ! -d "vendor" || ! -d "vendor/dompdf" || ! -d "vendor/twig" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Composer package not installed correctly!" exit 0 fi statusOkMessageOutput "Composer installed into new dir" "OK" # Copy FONT to DomPdf & Install "Muli" font #-------------------------------------------------------------------------------------- cd "$working_dir/new_$project_title" || dirOperationFailure "$working_dir/new_$project_title" # Check some dirs exists in vendor folder if [[ ! -d "vendor/dompdf" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Dompdf package is missing, failed installing Muli font!" exit 0 fi # Copy font loader and data to package location cp "$working_dir/new_$project_title/system-brs-core/library/cPdfFromHTML/load_font.php" \ "$working_dir/new_$project_title/vendor/dompdf/load_font.php" mv "$working_dir/new_$project_title/system-brs-core/library/cPdfFromHTML/FONTS" \ "$working_dir/new_$project_title/vendor/dompdf/FONTS" # Navigate to font dir cd "$working_dir/new_$project_title/vendor/dompdf" || dirOperationFailure "$working_dir/new_$project_title/vendor/dompdf" if [[ ! -d "FONTS" || ! -f "load_font.php" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Dompdf font installation data are missing!" exit 0 fi # Add permissions chmod +x "$working_dir/new_$project_title/vendor/dompdf/load_font.php" chmod +x -R "$working_dir/new_$project_title/vendor/dompdf/FONTS" chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/vendor/dompdf/load_font.php" chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/vendor/dompdf/FONTS" -R # Install the font - FONT LOADER FAILS, NEED TO CALL MANUALLY !!!!! php load_font.php 'Muli' ./FONTS/Muli/static/Muli-Regular.ttf ./FONTS/Muli/static/Muli-Bold.ttf ./FONTS/Muli/static/Muli-Italic.ttf ./FONTS/Muli/static/Muli-BoldItalic.ttf >/dev/null # Check Muli font in new location cd "dompdf/lib/fonts" || dirOperationFailure "dompdf/lib/fonts" if [[ ! -f "Muli-Bold.ufm" || ! -f "Muli-Bold.ttf" || ! -f "Muli-Regular.ttf" || ! -f "Muli-Regular.ufm" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Dompdf font installation failed - new font not found!" exit 0 fi statusOkMessageOutput "Dompdf Muli font installation successful" "OK" # Final movement before DEPLOYMENT #-------------------------------------------------------------------------------------- mv "$working_dir/new_$project_title" "$www_project_dir/new_$project_title" if [[ ! -d "$www_project_dir/new_$project_title" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Movement to base WWW dir failed!" exit 0 fi statusOkMessageOutput "Pre-deployment status check" "OK" # Deploy new Version mv "$www_project_dir/$project_title" "$www_project_dir/old_$project_title" mv "$www_project_dir/new_$project_title" "$www_project_dir/$project_title" if [[ -d "$www_project_dir/new_$project_title" || ! -d "$www_project_dir/old_$project_title" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}DEPLOYMENT within WWW dir failed!!!!!!!!" exit 0 fi statusOkMessageOutput "Project $project_title deployment successful" "OK" # Cleanup rm -Rf "$working_dir/${git_clone_dir:?}" rm -Rf "$working_dir/new_version_data" rm -Rf "$www_project_dir/old_${project_title:?}" if [[ -d "$working_dir/$git_clone_dir" || -d "$working_dir/new_version_data" || -d "$www_project_dir/old_$project_title" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Data CLEANUP failed!" exit 0 fi statusOkMessageOutput "Project $project_title CLEANUP successful" "OK" # Clearing cache folder rm -Rf "$www_project_dir/$git_clone_dir/cache/*" statusOkMessageOutput "Cache clear successful" "OK" ;; #-------------------------------------------------------------------------------------- # DSDDELUXE.EU GIT BASE DEPLOYMENT #-------------------------------------------------------------------------------------- "dsddeluxe.eu") # Moving & renaming WWW mv "$working_dir/new_version_data/www" "$working_dir/new_$project_title" if [[ ! -d "$working_dir/new_$project_title" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}WWW DIR movement failed!${Red}\n" exit 0 fi cd "$working_dir/new_$project_title" || dirOperationFailure "$working_dir/new_$project_title" rm -Rf "files" rm -f "system/config/main.cfg.php" cp "$www_project_dir/$project_title/system/config/main.cfg.php" "system/config/main.cfg.php" cp -r "$www_project_dir/$project_title/files" "files" # Validate that the directories are all in place if [[ ! -f "system/config/main.cfg.php" || ! -d "files" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Some files are not copied correctly!" exit 0 fi statusOkMessageOutput "File update / movement" "OK" chown apache:apache -R "$working_dir/new_$project_title" cd "$working_dir/new_$project_title" || dirOperationFailure "$working_dir/new_$project_title" find . -type f -exec chmod 0644 {} \; find . -type d -exec chmod 0755 {} \; statusOkMessageOutput "Directory permission [chown/chmod] setup" "OK" chcon -t httpd_sys_content_t "$working_dir/new_$project_title" -R chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/cache" -R chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/files" -R chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/logs" -R chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/system/library/vendor/dompdf/dompdf/lib/fonts" -R chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/system/library/smarty/cache" -R chcon -t httpd_sys_rw_content_t "$working_dir/new_$project_title/system/library/smarty/templates_c" -R statusOkMessageOutput "SElinux Permission [httpd_sys_rw_content_t] setup" "OK" # Check if destination already doesn't have new folder if [[ -d "$www_project_dir/new_$project_title" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Movement to base WWW dir failed!" echo -e "\n${Red}$www_project_dir/new_$project_title ${Color_Off}already exists!\n" exit 0 fi # Final movement before DEPLOYMENT mv "$working_dir/new_$project_title" "$www_project_dir/new_$project_title" if [[ ! -d "$www_project_dir/new_$project_title" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Movement to base WWW dir failed!" exit 0 fi statusOkMessageOutput "Pre-deployment status check" "OK" # Deploy new Version mv "$www_project_dir/$project_title" "$www_project_dir/old_$project_title" mv "$www_project_dir/new_$project_title" "$www_project_dir/$project_title" if [[ -d "$www_project_dir/new_$project_title" || ! -d "$www_project_dir/old_$project_title" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}DEPLOYMENT within WWW dir failed!!!!!!!!" exit 0 fi statusOkMessageOutput "Project $project_title deployment successful" "OK" # Cleanup rm -Rf "$working_dir/${git_clone_dir:?}" rm -Rf "$working_dir/new_version_data" rm -Rf "$www_project_dir/old_${project_title:?}" if [[ -d "$working_dir/$git_clone_dir" || -d "$working_dir/new_version_data" || -d "$www_project_dir/old_$project_title" ]]; then echo -e "\n${Red}ABORTING: ${Color_Off}Data CLEANUP failed!" exit 0 fi statusOkMessageOutput "Project $project_title CLEANUP successful" "OK" # Clearing cache folder rm -Rf "$www_project_dir/$git_clone_dir/cache/*" rm -Rf "$www_project_dir/$git_clone_dir/system/library/smarty/cache/*" rm -Rf "$www_project_dir/$git_clone_dir/system/library/smarty/templates_c/*" statusOkMessageOutput "Cache clear successful" "OK" ;; #-------------------------------------------------------------------------------------- # OMEGAONLINE.LV GIT BASE DEPLOYMENT #-------------------------------------------------------------------------------------- "omegaonline.lv") echo -e "Romanian" ;; #-------------------------------------------------------------------------------------- # UNSUPPORTED PROJECT - EXIT ! #-------------------------------------------------------------------------------------- *) echo -e "\n${Red}ABORTING: ${Color_Off}Project folder setup not found / not defined!" exit 0 ;; esac exit 1