Skip to content

Commit 062ae45

Browse files
committed
Fix remote-deploy-script.sh: remove set -euo pipefail, handle read failures, expand brace mkdir
1 parent 48f57ae commit 062ae45

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

scripts/remote-deploy-script.sh

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# Prompts for remote user and root password before deploying.
44
# Usage: bash scripts/remote-deploy-script.sh
55

6-
set -euo pipefail
7-
86
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
97
OUT="$ROOT/out"
108
CONFIG="$ROOT/configuration/nwe-config.xml"
@@ -22,25 +20,34 @@ echo "=============================================="
2220
echo ""
2321

2422
# ── Prompt: Remote user ───────────────────────────────────────────────────────
25-
read -rp "Remote user [$DEFAULT_USER]: " REMOTE_USER
23+
printf "Remote user [%s]: " "$DEFAULT_USER"
24+
read -r REMOTE_USER || true
2625
REMOTE_USER="${REMOTE_USER:-$DEFAULT_USER}"
2726

2827
# ── Prompt: Remote host ───────────────────────────────────────────────────────
29-
read -rp "Remote host [$DEFAULT_HOST]: " REMOTE_HOST
28+
printf "Remote host [%s]: " "$DEFAULT_HOST"
29+
read -r REMOTE_HOST || true
3030
REMOTE_HOST="${REMOTE_HOST:-$DEFAULT_HOST}"
3131

3232
# ── Prompt: Remote directory ──────────────────────────────────────────────────
33-
read -rp "Remote deploy directory [$DEFAULT_REMOTE_DIR]: " REMOTE_DIR
33+
printf "Remote deploy directory [%s]: " "$DEFAULT_REMOTE_DIR"
34+
read -r REMOTE_DIR || true
3435
REMOTE_DIR="${REMOTE_DIR:-$DEFAULT_REMOTE_DIR}"
3536

3637
# ── Prompt: Root password ─────────────────────────────────────────────────────
3738
echo ""
38-
read -rsp "Enter root password for $REMOTE_USER@$REMOTE_HOST: " ROOT_PASSWORD
39+
printf "Enter root password for %s@%s: " "$REMOTE_USER" "$REMOTE_HOST"
40+
read -rs ROOT_PASSWORD || true
3941
echo ""
4042
echo ""
4143

44+
if [ -z "$ROOT_PASSWORD" ]; then
45+
echo "[ERROR] No password provided. Exiting."
46+
exit 1
47+
fi
48+
4249
# ── Validate local build ──────────────────────────────────────────────────────
43-
if [[ ! -d "$OUT" ]] || [[ -z "$(ls -A "$OUT" 2>/dev/null)" ]]; then
50+
if [ ! -d "$OUT" ] || [ -z "$(ls -A "$OUT" 2>/dev/null)" ]; then
4451
echo "[ERROR] No compiled classes found in $OUT"
4552
echo " Run 'bash bash/NWE.install.sh' first to compile."
4653
exit 1
@@ -51,19 +58,19 @@ echo ""
5158

5259
# ── Helper: run remote command via sshpass ────────────────────────────────────
5360
remote_exec() {
54-
sshpass -p "$ROOT_PASSWORD" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "$@"
61+
sshpass -p "$ROOT_PASSWORD" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 "$REMOTE_USER@$REMOTE_HOST" "$@"
5562
}
5663

5764
remote_copy() {
58-
sshpass -p "$ROOT_PASSWORD" scp -o StrictHostKeyChecking=no -r "$@"
65+
sshpass -p "$ROOT_PASSWORD" scp -o StrictHostKeyChecking=no -o ConnectTimeout=10 -r "$@"
5966
}
6067

6168
# ── Check sshpass is available ────────────────────────────────────────────────
62-
if ! command -v sshpass &>/dev/null; then
63-
echo "[WARN] sshpass not found. Installing..."
64-
if command -v apt-get &>/dev/null; then
69+
if ! command -v sshpass >/dev/null 2>&1; then
70+
echo "[WARN] sshpass not found. Attempting to install..."
71+
if command -v apt-get >/dev/null 2>&1; then
6572
sudo apt-get install -y sshpass
66-
elif command -v yum &>/dev/null; then
73+
elif command -v yum >/dev/null 2>&1; then
6774
sudo yum install -y sshpass
6875
else
6976
echo "[ERROR] Cannot install sshpass automatically."
@@ -74,12 +81,19 @@ fi
7481

7582
# ── 2. Create remote directory structure ──────────────────────────────────────
7683
echo "[2/5] Creating remote directory structure..."
77-
remote_exec "mkdir -p $REMOTE_DIR/{out,configuration,scripts,bash,jars,logging}"
84+
if ! remote_exec "mkdir -p $REMOTE_DIR/out $REMOTE_DIR/configuration $REMOTE_DIR/scripts $REMOTE_DIR/bash $REMOTE_DIR/jars $REMOTE_DIR/logging"; then
85+
echo "[ERROR] Failed to connect to $REMOTE_USER@$REMOTE_HOST"
86+
echo " Check your password and that the server is reachable."
87+
exit 1
88+
fi
7889
echo " Done."
7990

8091
# ── 3. Upload compiled classes ────────────────────────────────────────────────
8192
echo "[3/5] Uploading compiled classes (out/)..."
82-
remote_copy "$OUT/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/out/"
93+
if ! remote_copy "$OUT/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/out/"; then
94+
echo "[ERROR] Failed to upload classes."
95+
exit 1
96+
fi
8397
echo " Done."
8498

8599
# ── 4. Upload configuration, scripts, jars ───────────────────────────────────
@@ -89,15 +103,14 @@ remote_copy "$SCRIPTS/startup.sh" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/scripts
89103
remote_copy "$BASH_DIR/NWE.install.sh" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/bash/NWE.install.sh"
90104

91105
# Upload jars (MySQL connector, Lanterna)
92-
if [[ -d "$ROOT/jars" ]]; then
106+
if [ -d "$ROOT/jars" ]; then
93107
remote_copy "$ROOT/jars/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/jars/"
94108
fi
95109
echo " Done."
96110

97111
# ── 5. Set permissions and verify ────────────────────────────────────────────
98112
echo "[5/5] Setting permissions and verifying deployment..."
99-
remote_exec "chmod +x $REMOTE_DIR/scripts/*.sh $REMOTE_DIR/bash/*.sh 2>/dev/null || true"
100-
remote_exec "ls -la $REMOTE_DIR/out/ | head -20"
113+
remote_exec "chmod +x $REMOTE_DIR/scripts/*.sh $REMOTE_DIR/bash/*.sh 2>/dev/null; ls -la $REMOTE_DIR/out/ | head -20"
101114
echo ""
102115

103116
# ── Summary ───────────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)