-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
96 lines (73 loc) · 2.15 KB
/
Copy pathsetup.sh
File metadata and controls
96 lines (73 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
set -e
APP_DIR="/opt/csye6225"
APP_ZIP="/tmp/webapp-fork.zip"
APP_GROUP="webapp_group"
APP_USER="webapp_user"
APP_DEST="$APP_DIR/webapp-fork"
ENV_FILE="$APP_DEST/.env"
# Check for Windows line endings
if grep -q $'\r' "$0"; then
echo "Error: Convert script to Linux line endings (LF)"
exit 1
fi
read -sp "Enter Database Password: " DB_PASSWORD
echo "password has been input"
if [ -z "$DB_PASSWORD" ]; then
echo "Password is empty, retry"
exit 1
fi
read -sp "Enter Database user: " DB_NAME
echo "dbname has been input"
if [ -z "$DB_NAME" ]; then
echo "dbname is empty, retry"
exit 1
fi
echo "Updating packages"
sudo apt-get update -y
echo "Upgrading system"
sudo apt-get upgrade -y
echo "Installing dependencies"
sudo apt-get install -y unzip postgresql postgresql-contrib nodejs npm
echo "Verifying installations"
unzip -v
node -v
echo "installing postgres"
sudo apt-get install postgresql postgresql-contrib
echo "Configuring PostgreSQL"
sudo systemctl enable postgresql
sudo systemctl start postgresql
if ! getent group "$APP_GROUP" >/dev/null; then
echo "Creating group: $APP_GROUP"
sudo groupadd "$APP_GROUP"
fi
if ! id -u "$APP_USER" >/dev/null; then
echo "Creating user: $APP_USER"
sudo useradd -m -g "$APP_GROUP" -s /bin/bash "$APP_USER"
fi
echo "Setting up app directory"
sudo mkdir -p "$APP_DIR"
sudo chown "$APP_USER:$APP_GROUP" "$APP_DIR"
echo "Deploying application"
sudo unzip -o "$APP_ZIP" -d "$APP_DIR"
sudo chown -R "$APP_USER:$APP_GROUP" "$APP_DIR"
sudo chmod -R 750 "$APP_DIR"
if [ ! -f "$ENV_FILE" ]; then
echo "Error: Existing .env file not found at $ENV_FILE"
exit 1
fi
# password update in env
sudo sed -i "s/^DB_PASSWORD=.*/DB_PASSWORD='${DB_PASSWORD}'/" "$ENV_FILE"
# set permissions
sudo chown "$APP_USER:$APP_GROUP" "$ENV_FILE"
sudo chmod 600 "$ENV_FILE"
echo "Updating database password"
sudo -u postgres psql -c "ALTER USER ${DB_NAME} WITH PASSWORD '${DB_PASSWORD}';"
echo "Installing dependencies"
cd "$APP_DEST"
sudo -u "$APP_USER" npm install
sudo -u "$APP_USER" npm install dotenv --save
echo "Starting application"
sudo -u "$APP_USER" npm start
unset DB_PASSWORD
echo "Deployment completed successfully"