|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 |
|
3 |
| -set -e |
| 3 | +set -e # Exit on error |
4 | 4 |
|
5 |
| -pre_run() { |
6 |
| - php artisan down |
7 |
| - git pull |
8 |
| - composer install --no-interaction --no-dev --prefer-dist |
9 |
| -} |
| 5 | +# Colors for fancy output |
| 6 | +GREEN="\033[1;32m" |
| 7 | +CYAN="\033[1;36m" |
| 8 | +YELLOW="\033[1;33m" |
| 9 | +RED="\033[1;31m" |
| 10 | +RESET="\033[0m" |
| 11 | +TRWL_RED="\033[38;2;255;0;0m" |
10 | 12 |
|
11 |
| -update_ui() { |
12 |
| - npm ci --no-audit --no-progress && npm run build |
13 |
| -} |
| 13 | +ASCII_ART="\ |
14 | 14 |
|
15 |
| -run_migrations() { |
16 |
| - php artisan migrate --force |
17 |
| -} |
18 | 15 |
|
19 |
| -post_run() { |
20 |
| - php artisan optimize |
21 | 16 |
|
22 |
| - php artisan db:seed --class=Database\\Seeders\\Constants\\PermissionSeeder --force |
23 |
| - php artisan up |
24 |
| -} |
| 17 | + _______ _ _ _ |
| 18 | + |__ __| | | (_) |
| 19 | + | |_ __ __ _ _____ _____| | |_ _ __ __ _ |
| 20 | + | | '__/ _\` \|/ _ \ \ /\ / / _ \ | | | '_ \ / _\` | |
| 21 | + | | | | (_| | __/\ V V / __/ | | | | | | (_| | |
| 22 | + |_|_| \__,_|\___| \_/\_/ \___|_|_|_|_| |_|\__, | |
| 23 | + __/ | |
| 24 | + |___/ |
25 | 25 |
|
26 |
| -restart_queue() { |
27 |
| - if [ -f /etc/systemd/system/traewelling-queue.service ]; then |
28 |
| - sudo systemctl restart traewelling-queue |
29 |
| - fi |
30 | 26 |
|
31 |
| - if [ -f /etc/systemd/system/traewelling-queue-webhook.service ]; then |
32 |
| - sudo systemctl restart traewelling-queue-webhook |
| 27 | + " |
| 28 | + |
| 29 | +echo -e "${TRWL_RED}${ASCII_ART}${RESET}" |
| 30 | + |
| 31 | +echo -e "${CYAN}Starting update script...${RESET}" |
| 32 | + |
| 33 | +# Get the repository root directory |
| 34 | +REPO_ROOT=$(git rev-parse --show-toplevel) |
| 35 | +echo -e "${YELLOW}Repository root determined: ${GREEN}$REPO_ROOT${RESET}" |
| 36 | +cd "$REPO_ROOT" |
| 37 | + |
| 38 | +echo -e "${CYAN}Running pre-update tasks...${RESET}" |
| 39 | + |
| 40 | +echo -e "${YELLOW}Enabling maintenance mode...${RESET}" |
| 41 | +php artisan down | sed "s/^/[AS-DOWN] /" |
| 42 | + |
| 43 | +echo -e "${YELLOW}Pulling latest changes...${RESET}" |
| 44 | +git pull | sed "s/^/[GIT-PULL] /" |
| 45 | + |
| 46 | +echo -e "${YELLOW}Installing composer dependencies...${RESET}" |
| 47 | +#composer install --no-interaction --no-dev --prefer-dist --optimize-autoloader | sed "s/^/[COMPOSER] /" |
| 48 | + |
| 49 | +echo -e "${YELLOW}Installing npm dependencies...${RESET}" |
| 50 | +#npm ci --no-audit --no-progress && npm run build | sed "s/^/[NPM] /" |
| 51 | + |
| 52 | +echo -e "${YELLOW}Running migrations...${RESET}" |
| 53 | +php artisan migrate --force | sed "s/^/[MIGRATE] /" |
| 54 | + |
| 55 | +echo -e "${YELLOW}Optimizing application...${RESET}" |
| 56 | +php artisan optimize | sed "s/^/[OPTIMIZE] /" |
| 57 | + |
| 58 | +echo -e "${YELLOW}Seeding constants to database...${RESET}" |
| 59 | +php artisan db:seed --class=Database\\Seeders\\Constants\\PermissionSeeder --force | sed "s/^/[SEED] /" |
| 60 | + |
| 61 | +echo -e "${YELLOW}Disabling maintenance mode...${RESET}" |
| 62 | +php artisan up | sed "s/^/[AS-UP] /" |
| 63 | + |
| 64 | +echo -e "\n\n${GREEN}Application updated successfully!${RESET}" |
| 65 | + |
| 66 | +restart_services() { |
| 67 | + echo -e "\n\n${CYAN}Restarting services...${RESET}" |
| 68 | + echo -e "${YELLOW}You may be asked to enter your password here if you're not running as root.${RESET}\n\n" |
| 69 | + |
| 70 | + services=( |
| 71 | + "traewelling-queue" |
| 72 | + "traewelling-queue-webhook" |
| 73 | + "traewelling-queue-export" |
| 74 | + ) |
| 75 | + |
| 76 | + for service in "${services[@]}"; do |
| 77 | + service_path="/etc/systemd/system/${service}.service" |
| 78 | + if [ -f "$service_path" ]; then |
| 79 | + echo -e "${YELLOW}Restarting ${service} service...${RESET}" |
| 80 | + sudo systemctl restart "$service" |
| 81 | + echo -e "${GREEN}${service} service restarted successfully!${RESET}" |
| 82 | + else |
| 83 | + ask_to_install_service "$service" |
33 | 84 | fi
|
| 85 | + done |
| 86 | +} |
| 87 | + |
| 88 | +ask_to_install_service() { |
| 89 | + service_name="$1" |
| 90 | + |
| 91 | + source_file="${REPO_ROOT}scripts/services/${service_name}.service" |
| 92 | + destination_file="/etc/systemd/system/${service_name}.service" |
| 93 | + |
| 94 | + if [ -f "$destination_file" ]; then |
| 95 | + echo -e "${YELLOW}Service ${service_name} is already installed.${RESET}" |
| 96 | + return 1 |
| 97 | + fi |
| 98 | + |
| 99 | + echo -e "${YELLOW}Service ${service_name} is not installed.${RESET}" |
| 100 | + echo -e "${YELLOW}Do you want to install it? [Y/n] (default: yes)${RESET}" |
| 101 | + |
| 102 | + read -r answer |
| 103 | + |
| 104 | + case "$answer" in |
| 105 | + y|Y|yes|Yes|"") |
| 106 | + echo -e "${YELLOW}Installing ${service_name} service...${RESET}" |
| 107 | + sudo cp "$source_file" "$destination_file" |
| 108 | + sudo systemctl enable "$service_name" |
| 109 | + sudo systemctl start "$service_name" |
| 110 | + echo -e "${GREEN}${service_name} service installed successfully!${RESET}" |
| 111 | + return 0 |
| 112 | + ;; |
| 113 | + n|N|no|No) |
| 114 | + echo -e "${YELLOW}Skipping ${service_name} service installation...${RESET}" |
| 115 | + return 1 |
| 116 | + ;; |
| 117 | + *) |
| 118 | + echo -e "${RED}Invalid answer. Please answer with 'y' or 'n'.${RESET}" |
| 119 | + ask_to_install_service "$service_name" |
| 120 | + ;; |
| 121 | + esac |
34 | 122 | }
|
35 | 123 |
|
36 |
| -pre_run |
37 |
| -run_migrations |
38 |
| -update_ui |
39 |
| -post_run |
40 |
| -restart_queue |
| 124 | +restart_services |
0 commit comments