Skip to content

Commit fdd5b11

Browse files
committed
Fix array spreading
1 parent 1cb5b31 commit fdd5b11

1 file changed

Lines changed: 59 additions & 48 deletions

File tree

deploy.sh

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,27 @@ for i in "$@"; do
7878
echo -e "${BRed}Error: Invalid setup option '$setup'. Please use 'full' or 'only'. ${Color_Off}"
7979
exit 1
8080
else
81-
echo -e "${BCyan}You included the setup option '$setup'.${Color_Off} (Please be careful with this option, as it can cause issues if not used correctly.)"
81+
echo -e "\nYou included the setup option '$setup'. (Please be careful with this option, as it can cause issues if not used correctly.)"
8282
printf "${BCyan}How many commands do you want to run on the remote server(s)?: ${Color_Off}"
8383
read -r NUMBER
84-
SETUP_COMMAND="echo -e \"${Cyan}Executing commands... ${Color_Off}\""
84+
SETUP_COMMAND="echo -e '${Cyan}Executing commands... ${Color_Off}\n'"
8585
if ! [[ "$NUMBER" =~ ^[0-9]+$ ]]; then
8686
echo -e "${BRed}Error: Invalid number of commands '$NUMBER'. Please provide a number.${Color_Off}"
87-
exit 1O
87+
exit 1
8888
fi
89-
for ((i=1; i<=NUMBER; i++)); do
90-
printf "${BCyan}Enter command $i: ${Color_Off}"
89+
for ((j=1; j<=NUMBER; j++)); do
90+
printf "${BCyan}Enter command $j: ${Color_Off}"
9191
read -r command
9292
if [[ -z "$command" ]]; then
9393
echo -e "${BRed}Error: Command cannot be empty. Please provide a valid command.${Color_Off}"
9494
exit 1
9595
fi
9696
SETUP_COMMAND="$SETUP_COMMAND && $command"
97-
echo -e "${Green}Command $i added: $command${Color_Off}"
97+
echo -e "${Green}Command $j added: $command${Color_Off}"
9898
done
9999
fi
100100
elif [[ $i =~ ^--project= ]]; then
101101
PROJECT_NAME="${i#*=}"
102-
export PROJECT_NAME="$PROJECT_NAME"
103102
if [ ! -d "$PROJECT_NAME" ]; then
104103
echo -e "${BRed} Could not find $PROJECT_NAME in $(pwd) ${Color_Off}"
105104
exit 1
@@ -177,53 +176,66 @@ fi
177176
restart_services=""
178177
for i in "${SERVICES[@]}"; do
179178
if [[ "$i" =~ ^[a-zA-Z0-9_]+$ ]]; then
180-
restart_services="$restart_services $i"
179+
restart_services="${restart_services} $i"
181180
else
182181
echo -e "${BRed}Error:${Color_Off} ${Red}Invalid service name '$i'. Only alphanumeric characters and underscores are allowed.${Color_Off}"
183182
exit 1
184183
fi
185184
done
186185

187186
if [ -n "$ROLLBACK" ]; then
188-
if [[ -z "$SERVERS" || -z "$SERVICES" ]]; then
187+
if [[ -n "${SERVERS[*]}" && -n "${SERVICES[*]}" ]]; then
188+
for i in "${SERVERS[@]}"; do
189+
echo -e "${BBlue}Rolling back on server: ${BYellow}\t$i\t...\n${Color_Off}"
190+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "bash -c '
191+
cd $DEPLOY_DIR/
192+
rm -rf ../current
193+
f=\$(ls -ut | grep $PROJECT_NAME | head -n +$((ROLLBACK)) | tail -n +$((ROLLBACK)))
194+
echo -e \"${Cyan}Rolling back to version: ${BCyan}\$f${Color_Off}\n\"
195+
ln -s \$(readlink -f \$f) ../current
196+
ls -l .. | grep current
197+
sudo systemctl restart ${restart_services[*]}
198+
echo -e \"${Cyan}Service statuses after restart:${Color_Off}\n\"
199+
sudo systemctl status ${restart_services[*]} --no-pager
200+
echo -e \"${Green}Rollback completed on server name: $i.${Color_Off}\n\"
201+
'"
202+
done
203+
echo -e "${BCyan}Done. Bye!${Color_Off}"
204+
exit 0
205+
else
189206
echo -e "${BRed}Error: Please provide servers for rollback and services to restart ${Color_Off}"
190207
exit 1
191208
fi
192-
for i in "${SERVERS[@]}"; do
193-
echo -e "${BBlue}Rolling back on server: ${BYellow}\t$i\t...\n${Color_Off}"
194-
ssh -i "$SSH_KEY" "$SSH_USER@$i" "bash -c '
195-
cd $DEPLOY_DIR/
196-
rm -rf ../current
197-
f=\$(ls -ut | grep "$PROJECT_NAME" | head -n +$((ROLLBACK)) | tail -n +$((ROLLBACK)))
198-
echo -e \"${Cyan}Rolling back to version: ${BCyan}\$f${Color_Off}\n\"
199-
ln -s \$(readlink -f \$f) ../current
200-
ls -l .. | grep current
201-
sudo systemctl restart $restart_services
202-
echo -e \"${Cyan}Service statuses after restart:${Color_Off}\n\"
203-
sudo systemctl status $restart_services --no-pager
204-
echo -e \"${Green}Rollback completed on server name: $i.${Color_Off}\n\"
205-
'"
206-
done
207-
echo -e "${BCyan}Done. Bye!${Color_Off}"
208-
exit 0
209209
fi
210210

211211
echo -e "\n${BYellow}Please note that this script does not provide full graceful error handling, so that you don't live with a broken app.${Color_Off}\n"
212212
if [ "$setup" == "only" ]; then
213213
for i in "${SERVERS[@]}"; do
214+
define_api=$([ -n "$JSHOST" ] && echo "sed -i 's#undefined#\\\"$i\\\"#' $JSHOST" || echo "echo 'No JSHOST Defined. Skipping modification.'")
215+
214216
echo -e "${Green}Skipping deployment and going for server setup. This will fail if there is no deployed version.${Color_Off}\n"
215-
runcommand=$([[ "$*" =~ (^|[[:space:]])--npm($|[[:space:]]) ]] && echo "$SETUP_COMMAND && cd $DEPLOY_DIR/../current/$NODE_HOME && npm install" || echo "$SETUP_COMMAND")
216-
ssh -i "$SSH_KEY" "$SSH_USER@$i" "bash -c '
217-
$runcommand
218-
if [ -n $restart_services ]; then
219-
if sudo systemctl status $restart_services >/dev/null 2>&1; then
217+
echo -e "${Yellow}BEGIN Server MOTD: \n${Color_Off}"
218+
ssh -T -q -i "$SSH_KEY" "$SSH_USER@$i" <<EOF
219+
echo -e "${Yellow}END Server MOTD.${Color_Off}\n"
220+
$SETUP_COMMAND
221+
cd $DEPLOY_DIR/../current/$NODE_HOME
222+
if which npm >/dev/null 2>&1 && [[ "$*" =~ (^|[[:space:]])--npm($|[[:space:]]) ]]; then
223+
npm install && echo -e "${Cyan}Npm install was a success.${Color_Off}\n"
224+
elif ! which npm >/dev/null 2>&1; then
225+
echo -e "\n${BRed}Npm not found. Please install it.${Color_Off}\n"
226+
fi
227+
if [ -n "${restart_services[@]}" ]; then
228+
if sudo systemctl status ${restart_services} >/dev/null 2>&1; then
220229
echo "✅ Services are running fine. Restarting: $restart_services"
221230
sudo systemctl restart $restart_services
222-
echo -e \"${Green}Setup completed successfully on server name: $i.${Color_Off}\"
231+
echo -e "${Green}Setup completed on server name: $i.${Color_Off}"
223232
else
224233
echo "❌ Some services might be dead or unavailable. Please check your setup. Full setup failed."
225234
fi
226-
fi'"
235+
fi
236+
cd - > /dev/null && cd $DEPLOY_DIR/../current
237+
$define_api
238+
EOF
227239

228240
done
229241
echo -e "\n${BCyan}Done. Bye!${Color_Off}"
@@ -285,38 +297,37 @@ for i in "${SERVERS[@]}"; do
285297

286298
ssh -i "$SSH_KEY" "$SSH_USER@$i" "sudo rm -rf $DEPLOY_DIR/new/ && ls $DEPLOY_DIR/ | sed 's/^/\t\t\t/' && sudo rm -rf $DEPLOY_DIR/../current && ln -s $DEPLOY_DIR/$file $DEPLOY_DIR/../current"
287299
echo -e "\n${Green}Finished making a symbolic link for the new release. Deleting old releases...${Color_Off}\n"
288-
ssh -i "$SSH_KEY" "$SSH_USER@$i" "cd $DEPLOY_DIR/ && ls -ut | grep "$PROJECT_NAME" | tail -n +$((KEEP + 1)) | xargs rm -rf"
289-
ls "$PROJECT_NAME/versions" -ut | grep "$PROJECT_NAME" | tail -n +$((KEEP + 1)) | xargs rm -rf
300+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "cd $DEPLOY_DIR/ && ls -ut | grep $PROJECT_NAME | tail -n +$((KEEP + 1)) | xargs rm -rf"
301+
cd "$PROJECT_NAME/versions" && ls -ut | grep "$PROJECT_NAME" | tail -n +$((KEEP + 1)) | xargs rm -rf && cd - > /dev/null
290302

291303
echo -e "${Cyan}Deleted old releases, keeping the last $KEEP versions.${Color_Off}\n"
292304
if [ "$setup" == 'full' ]; then
293-
echo -e "${Cyan}Running Setup command...${Black} ${SETUP_COMMAND}${Color_Off}"
294-
ssh -i "$SSH_KEY" -tt "$SSH_USER@$i" "$SETUP_COMMAND"
295-
echo -e "${Cyan}Setup completed successfully.${Color_Off}\n"
305+
ssh -T -i "$SSH_KEY" "$SSH_USER@$i" "$SETUP_COMMAND"
306+
echo -e "${Cyan}Setup command executed successfully.${Color_Off}\n"
296307
fi
297308

298309
if [[ "$*" =~ (^|[[:space:]])--npm($|[[:space:]]) ]]; then
299-
echo -e "${Cyan}Running npm install on the latest version...${Color_Off}"
300-
ssh -i "$SSH_KEY" "$SSH_USER@$i" "cd $DEPLOY_DIR/$file/$NODE_HOME && npm install"
301-
echo -e "${Cyan}Npm install was a success.${Color_Off}\n"
310+
echo -e "${Cyan}Running npm install on the latest version...${Color_Off}\n"
311+
ssh -i "$SSH_KEY" "$SSH_USER@$i" "cd $DEPLOY_DIR/$file/$NODE_HOME && if which npm >/dev/null 2>&1; then npm install && echo -e '${Cyan}Npm install was a success.${Color_Off}\n'; else echo -e '${Red}Npm not found. Please install it.\n${Color_Off}'; fi"
302312
fi
303313

304314
define_api=$([ -n "$JSHOST" ] && echo "sed -i 's#undefined#\\\"$i\\\"#' $JSHOST" || echo "echo 'No JSHOST Defined. Skipping modification.'")
305-
ssh -i "$SSH_KEY" "$SSH_USER@$i" <<EOF
315+
316+
ssh -T -i "$SSH_KEY" "$SSH_USER@$i" <<EOF
306317
cd $DEPLOY_DIR/$file/ && sudo rm -rf /tmp/$file.tgz
307318
$define_api
308-
if [ -n $restart_services ]; then
309-
if sudo systemctl status $restart_services >/dev/null 2>&1; then
310-
echo "\n✅ Services are running fine. Restarting: $restart_services"
311-
sudo systemctl restart $restart_services
312-
echo -e "${Green}Setup completed successfully on server name: $i.${Color_Off}"
319+
if [ -n "${restart_services[@]}" ]; then
320+
if sudo systemctl status ${restart_services} >/dev/null 2>&1; then
321+
echo "\n${Green}✅ Services are running fine.${Color_off} Restarting: ${restart_services[@]}"
322+
sudo systemctl restart ${restart_services[@]}
323+
echo -e "${Green}Setup completed successfully on server name: $i.${Color_Off}"
313324
else
314325
echo "❌ Some services might be dead or unavailable. Please check your setup. Full setup failed."
315326
fi
316327
fi
317328
EOF
318329

319-
echo -e "${Green}Your newest app release ($file) is now live on -> ($i)!${Color_Off}\nYou can visit: $i/ to use app if the services are set up correctly.\n"
330+
echo -e "${Green}Your newest app release ($file) is now live on -> ($i)!${Color_Off}\nYou can use your app if the services are set up correctly.\n"
320331
) &
321332
done
322333
wait

0 commit comments

Comments
 (0)