← Back
Editing: libdeploy.sh
check_dependencies() { for dependency in "$@"; do if ! command -v "$dependency" 1>/dev/null 2>&1; then echo "Erreur : la commande $dependency est introuvable." exit 1 fi done } check_files() { for file in "$@"; do if [ ! -f "$file" ]; then echo "Erreur : le fichier $file n’existe pas." exit 1 fi done } check_variables() { for name in "$@"; do # shellcheck disable=SC2086 if [ -z "$(eval printf -- '%b' \"\$$name\")" ]; then echo "Erreur : la variable $name n’est pas définie." exit 1 fi done } get_project_tech() { if [ -z "$PROJECT_TECH" ]; then check_variables 'PROJECT_PATH' YWD_MONITOR_DIR="$SCRIPT_PATH/ywd_monitor" if [ ! -e "$YWD_MONITOR_DIR/Makefile" ]; then ( cd "$SCRIPT_PATH" || { printf 'Critical error:\n' >&2 printf 'Couldnʼt enter the following directory: %s\n' "$SCRIPT_PATH" >&2 printf 'Please report this issue on our issues tracker: %s\n' \ 'https://framagit.org/yeswedev/ywd_deploy/issues' >&2 return 1 } git submodule update --init --recursive ywd_monitor >/dev/null ) fi if [ ! -e "$YWD_MONITOR_DIR/libmonitor.sh" ]; then ( cd "$YWD_MONITOR_DIR" || { printf 'Critical error:\n' >&2 printf 'Couldnʼt enter the following directory: %s\n' "$YWD_MONITOR_DIR" >&2 printf 'Please report this issue on our issues tracker: %s\n' \ 'https://framagit.org/yeswedev/ywd_deploy/issues' >&2 return 1 } make >/dev/null ) fi PROJECT_TECH=$("$YWD_MONITOR_DIR/get-project-type.sh" "$PROJECT_PATH") if [ "$DEBUG" = 'true' ]; then # shellcheck disable=SC2016 printf '$PROJECT_TECH set to "%s" using informations from YWD Monitor.\n' "$PROJECT_TECH" >&2 fi fi printf '%s' "$PROJECT_TECH" } get_project_environment() { case "$(get_project_tech)" in ('laravel') SERVER_ENVIRONMENT=$(laravel_get_environment) ;; ('wordpress') get_environment_from_wordpress ;; (*) # Nothing to do here true ;; esac if [ -z "$SERVER_ENVIRONMENT" ]; then SERVER_ENVIRONMENT='production' fi printf '%s' "$SERVER_ENVIRONMENT" return 0 } composer_install() { case "$(get_project_tech)" in ('drupal_composer') drupal_composer_install ;; (*) check_files 'composer.json' check_dependencies 'composer' composer_options='--no-interaction' if [ "$(get_project_environment)" = 'production' ]; then composer_options="$composer_options --no-dev" fi # shellcheck disable=SC2086 composer install $composer_options ;; esac } database_backup() { printf 'Sauvegarde de la base de données, cette étape peut prendre plusieurs minutes…\n' # shellcheck disable=SC2039 local db_backup_path # shellcheck disable=SC2153 db_backup_path="$BACKUP_PATH/database" YWD_AUTOSAVE_DIR="$SCRIPT_PATH/ywd_autosave" if [ ! -e "$YWD_AUTOSAVE_DIR/Makefile" ]; then ( cd "$SCRIPT_PATH" || { printf 'Critical error:\n' >&2 printf 'Couldnʼt enter the following directory: %s\n' "$SCRIPT_PATH" >&2 printf 'Please report this issue on our issues tracker: %s\n' \ 'https://framagit.org/yeswedev/ywd_deploy/issues' >&2 return 1 } git submodule update --init --recursive ywd_monitor >/dev/null ) fi if [ ! -e "$YWD_AUTOSAVE_DIR/libautosave.sh" ]; then ( cd "$YWD_AUTOSAVE_DIR" || { printf 'Critical error:\n' >&2 printf 'Couldnʼt enter the following directory: %s\n' "$YWD_AUTOSAVE_DIR" >&2 printf 'Please report this issue on our issues tracker: %s\n' \ 'https://framagit.org/yeswedev/ywd_deploy/issues' >&2 return 1 } make ) fi export BACKUPS_PATH="$db_backup_path" export MAX_DUMPS="$DB_BACKUP_CLEANUP_MAX" "$SCRIPT_PATH/ywd_autosave/quicksave.sh" "$PROJECT_PATH" printf '\033[1;32mOK\033[0m\n' return 0 } check_directories() { for directory in "$@"; do if [ ! -d "$directory" ]; then echo "Erreur : le répertoire $directory n’existe pas." exit 1 fi done } get_environment() { SERVER_ENVIRONMENT=$(get_project_environment) if [ "$DEBUG" = 'true' ]; then printf 'SERVER_ENVIRONMENT=%s\n' "$SERVER_ENVIRONMENT" fi export SERVER_ENVIRONMENT return 0 } git_update() { check_directories '.git' check_variables 'BACKUP_PATH' [ -d "$BACKUP_PATH" ] || mkdir --parents "$BACKUP_PATH" check_dependencies 'git' git_update_log="$BACKUP_PATH/git.log" date="$(date +%F-%H:%M)" commit="$(git show --format=format:%H --no-patch HEAD)" printf '%s - HEAD before update: %s\n' "$date" "$commit" >> "$git_update_log" git checkout -- . git pull find . -maxdepth 1 -iname 'readme.md' -delete commit="$(git show --format=format:%H --no-patch HEAD)" printf '%s - HEAD after update: %s\n' "$date" "$commit" >> "$git_update_log" printf -- '%s\n' '-----' >> "$git_update_log" } git_submodule_update() { check_directories '.git/modules' check_dependencies 'git' unset options if [ "$GIT_SUBMODULE_UPDATE_FROM_REMOTE" = 'true' ]; then options='--remote' fi git submodule update $options } gulp_build() { if [ -d './node_modules/.bin' ]; then export PATH="./node_modules/.bin:$PATH" fi check_dependencies 'gulp' [ "$DEBUG" = 'true' ] && echo "Utilisation de $(command -v gulp)" gulp build } pm2_restart() { check_dependencies 'pm2' pm2 restart "$PROJECT_NAME" } pre_deploy_actions() { check_variables 'PROJECT_TECH' 'PRE_DEPLOY_CUSTOM_ACTIONS' 'SCRIPT_PATH' case "$PROJECT_TECH" in ('laravel') laravel_pre_deploy_actions ;; ('drupal_composer') drupal_pre_deploy_actions ;; (*) # Nothing to do here true ;; esac if [ "$PRE_DEPLOY_CUSTOM_ACTIONS" = 'true' ] && [ -f "$SCRIPT_PATH/pre-deploy-custom-actions.sh" ]; then if [ -x "$SCRIPT_PATH/pre-deploy-custom-actions.sh" ]; then "$SCRIPT_PATH/pre-deploy-custom-actions.sh" else sh "$SCRIPT_PATH/pre-deploy-custom-actions.sh" fi fi return 0 } post_deploy_actions() { check_variables 'PROJECT_TECH' 'POST_DEPLOY_CUSTOM_ACTIONS' 'SCRIPT_PATH' if [ "$POST_DEPLOY_CUSTOM_ACTIONS" = 'true' ] && [ -f "$SCRIPT_PATH/post-deploy-custom-actions.sh" ]; then if [ -x "$SCRIPT_PATH/post-deploy-custom-actions.sh" ]; then "$SCRIPT_PATH/post-deploy-custom-actions.sh" else sh "$SCRIPT_PATH/post-deploy-custom-actions.sh" fi fi case "$PROJECT_TECH" in ('laravel') laravel_post_deploy_actions ;; ('drupal_composer') drupal_post_deploy_actions ;; (*) # Nothing to do here true ;; esac return 0 } drush_action() { if ! PATH="vendor/bin:$PATH" command -v drush >/dev/null; then composer_install fi # shellcheck disable=SC2039 local action uri action="$1" if drupal_has_multiples_sites; then for uri in $(drupal_get_uris_list); do # shellcheck disable=SC2086 PATH="vendor/bin:$PATH" drush --uri="$uri" $action done else # shellcheck disable=SC2086 PATH="vendor/bin:$PATH" drush $action fi return 0 } drush_maintenance_on() { drush_action 'state-set system.maintenance_mode 1' return 0 } drush_maintenance_off() { drush_action 'state-set system.maintenance_mode 0' return 0 } drush_cache_rebuild() { drush_action 'cache-rebuild' return 0 } drupal_pre_deploy_actions() { drush_maintenance_on return 0 } drupal_post_deploy_actions() { drush_cache_rebuild drush_maintenance_off return 0 } drupal_composer_install() { check_dependencies 'find' 'composer' if [ -f 'composer.json' ]; then composer_options='--no-interaction' if [ "$(get_project_environment)" = 'production' ]; then composer_options="$composer_options --no-dev" fi # shellcheck disable=SC2086 composer install $composer_options fi # shellcheck disable=SC2039 local file directory themes_path for themes_path in 'web/themes' 'web/themes/contrib' 'web/themes/custom'; do if [ -e "$themes_path" ]; then while read -r file; do if [ ! -f "$file" ]; then continue fi ( directory="$(dirname "$file")" # shellcheck disable=SC2164 cd "$directory" check_files 'composer.json' # shellcheck disable=SC2046 composer install --no-interaction --no-dev ) done <<- EOL $(find "$themes_path" -mindepth 2 -maxdepth 2 -type f -name 'composer.json') EOL fi done } drupal_npm_install() { check_dependencies 'find' 'npm' # shellcheck disable=SC2039 local file directory themes_path for themes_path in 'web/themes' 'web/themes/contrib' 'web/themes/custom'; do if [ -e "$themes_path" ]; then while read -r file; do if [ ! -f "$file" ]; then continue fi ( directory="$(dirname "$file")" # shellcheck disable=SC2164 cd "$directory" check_files 'package.json' # shellcheck disable=SC2046 npm $(npm_install_command) ) done <<- EOL $(find "$themes_path" -mindepth 2 -maxdepth 2 -type f -name 'package.json') EOL fi done } drupal_npm_build() { check_dependencies 'find' 'npm' # shellcheck disable=SC2039 local file directory themes_path for themes_path in 'web/themes' 'web/themes/contrib' 'web/themes/custom'; do if [ -e "$themes_path" ]; then while read -r file; do if [ ! -f "$file" ]; then continue fi ( directory="$(dirname "$file")" # shellcheck disable=SC2164 cd "$directory" check_directories 'node_modules' npm run gulp ) done <<- EOL $(find "$themes_path" -mindepth 2 -maxdepth 2 -type f -name 'package.json') EOL fi done } drupal_has_multiples_sites() { if [ ! -e "$PROJECT_PATH/web/sites/sites.php" ]; then return 1 fi test "$(grep --count "^\\s*\$sites\\['" "$PROJECT_PATH/web/sites/sites.php")" -gt 1 } drupal_get_uris_list() { # shellcheck disable=SC2039 local sites sites=$(sed --quiet "/^\\s*\$sites\\['/s/^\\s*\$sites\\['\\(.*\\)'\\] = .*/\\1/p" "$PROJECT_PATH/web/sites/sites.php") printf '%s' "$sites" return 0 } compass_install() { check_dependencies 'gem' check_variables 'PROJECT_TECH' case "$PROJECT_TECH" in ('prestashop1.6') prestashop16_compass_install ;; (*) gem install --user-install compass ;; esac return 0 } compass_build() { check_variables 'PROJECT_TECH' case "$PROJECT_TECH" in ('prestashop1.6') prestashop16_compass_build ;; (*) if ! command -v compass >/dev/null 2>&1; then compass_install fi check_dependencies 'compass' check_files 'config.rb' compass compile ;; esac return 0 } laravel_get_environment() { check_files '.env' # shellcheck disable=SC2039 local server_environment server_environment="$(grep '^APP_ENV=' .env | sed 's/^APP_ENV=\(.\+\)/\1/')" printf '%s' "$server_environment" return 0 } laravel_npm_install() { npm_action "$(npm_install_command)" return 0 } laravel_npm_build() { check_directories 'node_modules' get_environment # shellcheck disable=SC2153 if [ "$SERVER_ENVIRONMENT" = 'production' ]; then npm_environment='prod' else npm_environment='dev' fi npm_action "run $npm_environment" } laravel_pre_deploy_actions() { artisan_maintenance_on return 0 } laravel_post_deploy_actions() { artisan_maintenance_off return 0 } artisan_action() { check_files 'artisan' check_dependencies 'php' # shellcheck disable=SC2039 local action action="$1" # shellcheck disable=SC2086 php artisan $action return 0 } artisan_migration() { artisan_action 'migrate --force' return 0 } artisan_refresh() { artisan_action 'migrate:refresh --force' return 0 } artisan_seed() { artisan_action 'db:seed --force' return 0 } artisan_maintenance_on() { artisan_action 'down' return 0 } artisan_maintenance_off() { artisan_action 'up' return 0 } npm_action() { check_files 'package.json' check_dependencies 'npm' # shellcheck disable=SC2039 local action action="$1" # shellcheck disable=SC2086 npm $action return 0 } npm_install() { printf 'Installation des paquets npm, cette étape peut prendre plusieurs minutes…\n' case "$PROJECT_TECH" in ('drupal_composer') drupal_npm_install ;; ('laravel') laravel_npm_install ;; ('prestashop') prestashop_npm_install ;; ('wordpress'*) npm_install_wordpress ;; (*) check_files 'package.json' check_dependencies 'npm' # shellcheck disable=SC2046 npm $(npm_install_command) ;; esac printf '\033[1;32mOK\033[0m\n' } npm_install_command() { # shellcheck disable=SC2039 local version version_major version_minor install_command version=$(npm --version) version_major=$(printf '%s' "$version" | cut --delimiter='.' --fields=1) version_minor=$(printf '%s' "$version" | cut --delimiter='.' --fields=2) # shellcheck disable=SC2086 if { [ $version_major -gt 5 ] ||\ [ $version_major -eq 5 ] && [ $version_minor -ge 7 ] } && { [ -f 'package-lock.json' ] ||\ [ -f 'npm-shrinkwrap.json' ] } then install_command='ci' else install_command='install' fi printf '%s' "$install_command" } npm_build() { case "$PROJECT_TECH" in ('drupal_composer') drupal_npm_build ;; ('laravel') laravel_npm_build ;; ('prestashop') prestashop_npm_build ;; ('wordpress'*) npm_build_wordpress ;; (*) check_dependencies 'npm' get_environment if [ "$SERVER_ENVIRONMENT" = 'production' ]; then npm_environment='prod' else npm_environment='dev' fi npm run $npm_environment ;; esac } prestashop_npm_install() { check_dependencies 'find' 'npm' find 'themes' -mindepth 3 -maxdepth 3 -type f -name 'package.json' | while read -r file; do ( cd "$(dirname "$file")" || return 1 check_files 'package.json' # shellcheck disable=SC2046 npm $(npm_install_command) ) done } prestashop_npm_build() { check_dependencies 'find' 'npm' find 'themes' -mindepth 3 -maxdepth 3 -type f -name 'package.json' | while read -r file; do ( cd "$(dirname "$file")" || return 1 check_directories 'node_modules' npm run build ) done } prestashop16_compass_install() { check_dependencies 'gem' find 'themes' -mindepth 2 -maxdepth 2 -type f -name 'config.rb' | while read -r file; do ( cd "$(dirname "$file")" || return 1 check_files 'config.rb' gem install --user-install compass ) done return 0 } prestashop16_compass_build() { find 'themes' -mindepth 2 -maxdepth 2 -type f -name 'config.rb' | while read -r file; do ( cd "$(dirname "$file")" || return 1 check_files 'config.rb' if ! command -v compass >/dev/null 2>&1; then ( cd "$PROJECT_PATH" || return 1 compass_install ) fi check_dependencies 'compass' check_files 'config.rb' compass compile ) done return 0 } get_environment_from_wordpress() { check_files '.env' check_variables 'PROJECT_PATH' SERVER_ENVIRONMENT="$(grep '^WP_ENV=' "$PROJECT_PATH/.env" | sed 's/^WP_ENV=\(.\+\)/\1/')" export SERVER_ENVIRONMENT return 0 } npm_install_wordpress() { check_variables 'PROJECT_TECH' check_dependencies 'find' 'npm' # shellcheck disable=SC2039 local file directory themes_path case "$PROJECT_TECH" in ('wordpress_vanilla') themes_path='wp-content/themes' ;; (*) themes_path='web/app/themes' ;; esac find "$themes_path" -mindepth 2 -maxdepth 2 -type f -name 'package.json' | while read -r file; do ( directory="$(dirname "$file")" # shellcheck disable=SC2164 cd "$directory" check_files 'package.json' # shellcheck disable=SC2046 npm $(npm_install_command) ) done return 0 } npm_build_wordpress() { check_variables 'PROJECT_TECH' check_dependencies 'find' 'npm' # shellcheck disable=SC2039 local file directory themes_path npm_action if [ "$(get_project_environment)" = 'production' ]; then npm_action='build:production' else npm_action='build' fi case "$PROJECT_TECH" in ('wordpress_vanilla') themes_path='wp-content/themes' ;; (*) themes_path='web/app/themes' ;; esac find "$themes_path" -mindepth 2 -maxdepth 2 -type f -name 'package.json' | while read -r file; do ( directory="$(dirname "$file")" # shellcheck disable=SC2164 cd "$directory" check_directories 'node_modules' npm run $npm_action ) done return 0 }
Save File
Cancel