-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
268 lines (216 loc) · 8.05 KB
/
install.sh
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/bin/bash
# ASCII Art for "OctaByte"
cat <<'EOF'
___ _ ____ _
/ _ \ ___| |_ __ _| __ ) _ _| |_ ___
| | | |/ __| __/ _` | _ \| | | | __/ _ \
| |_| | (__| || (_| | |_) | |_| | || __/
\___/ \___|\__\__,_|____/ \__, |\__\___|
|___/
EOF
echo "VERSION 1.0"
# Set environment variables
DOMAIN="vm.octabyte.io"
CDN="https://github.com/octabytes/app-templates/blob/main"
# Get user input for service configuration
read -p "Enter Service Name: " SERVICE_NAME
read -p "Enter Service Domain (prefix for $DOMAIN): " SERVICE_SUBDOMAIN
read -p "Enter Service Admin Email: " SERVICE_EMAIL
# Create the full service domain
SERVICE_DOMAIN="$SERVICE_SUBDOMAIN.$DOMAIN"
# Detect the OS (Ubuntu or Debian)
OS=$(lsb_release -is 2>/dev/null || grep '^ID=' /etc/os-release | cut -d= -f2 | tr -d '"')
# Docker installation function for Ubuntu
install_docker_ubuntu() {
echo "Detected OS: Ubuntu. Installing Docker for Ubuntu..."
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" \
| sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
}
# Docker installation function for Debian
install_docker_debian() {
echo "Detected OS: Debian. Installing Docker for Debian..."
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" \
| sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-buildx-plugin
}
# Check for Docker installation and install based on OS
if ! command -v docker &> /dev/null
then
echo "Docker not found. Installing Docker..."
if [[ "$OS" == "Ubuntu" ]]; then
install_docker_ubuntu
elif [[ "$OS" == "Debian" ]]; then
install_docker_debian
else
echo "Unsupported OS: $OS. Exiting."
exit 1
fi
# Configure Docker to limit log size
echo "Configuring Docker log limits..."
sudo mkdir -p /etc/docker
# cat <<EOF | sudo tee /etc/docker/daemon.json
# {
# "log-driver": "json-file",
# "log-opts": {
# "max-size": "10m",
# "max-file": "3"
# },
# "storage-driver": "overlay2"
# }
# EOF
# Restart Docker to apply configurations
sudo systemctl restart docker
echo "Docker setup complete."
else
echo "Docker is already installed."
fi
# Check if Git is installed, if not, install it
if ! command -v git &> /dev/null; then
echo "Git not found. Installing Git..."
sudo apt update
sudo apt install -y git
else
echo "Git is already installed. Skipping installation."
fi
# Clone the repository
echo "Cloning repository..."
git clone https://github.com/octabytes/app-templates.git /tmp/app-templates
# Copy the selected service folder to /opt/app
SERVICE_FOLDER="/tmp/app-templates/$SERVICE_NAME"
if [ ! -d "$SERVICE_FOLDER" ]; then
echo "Error: Service folder for $SERVICE_NAME not found!"
exit 1
fi
echo "Setting up service..."
sudo mkdir -p /opt/app
sudo cp -r "$SERVICE_FOLDER/." /opt/app/
rm -rf /tmp/app-templates # Clean up the cloned repo
echo "Creating .env file from env.txt..."
# Define env.txt and .env file paths
ENV_FILE="/opt/app/.env"
ENV_TXT_FILE="/opt/app/env.txt"
# Function to generate random password
generate_random_password() {
echo $(openssl rand -base64 12)
}
SOFTWARE_PASSWORD=$(generate_random_password)
# Function to create .env from env.txt
create_env_file() {
# Check if env.txt exists
if [[ ! -f "$ENV_TXT_FILE" ]]; then
echo "Error: $ENV_TXT_FILE not found!"
return 1
fi
# Create or clear the .env file
echo "" | sudo tee "$ENV_FILE" > /dev/null
if [[ $? -ne 0 ]]; then
echo "Failed to create .env file. Check permissions."
return 1
fi
# Read from env.txt and process each line
while IFS= read -r line; do
# Skip blank lines
if [[ -z "$line" ]]; then
continue
fi
# Handle comments directly
if [[ "$line" =~ ^# ]]; then
echo "$line" | sudo tee -a "$ENV_FILE" > /dev/null
continue
fi
# Replace RANDOM_PASSWORD with a generated password
if [[ "$line" == *"RANDOM_PASSWORD"* ]]; then
password=$(generate_random_password)
line=${line//RANDOM_PASSWORD/$password}
fi
# Check if line contains special characters (<, >, &, etc.)
if [[ "$line" =~ [\<\>\&\'\"\`] ]]; then
# Append the line without using eval
echo "$line" | sudo tee -a "$ENV_FILE" > /dev/null
continue
fi
# Replace placeholders with actual values using eval only for safe lines
line=$(eval echo "$line")
# Append to .env file with error handling
echo "$line" | sudo tee -a "$ENV_FILE" > /dev/null
if [[ $? -ne 0 ]]; then
echo "Failed to write to .env file. Check permissions."
return 1
fi
done < "$ENV_TXT_FILE"
# Attempt to remove env.txt, handle errors
if ! sudo rm -f "$ENV_TXT_FILE"; then
echo "Warning: Unable to remove $ENV_TXT_FILE. Check permissions."
fi
echo ".env file created successfully."
}
# Call the function to create the .env file
create_env_file
# Move to app directory
cd /opt/app
# Execute the pre-install script
PRE_INSTALL_SCRIPT="/opt/app/scripts/preInstall.sh"
if [ -f "$PRE_INSTALL_SCRIPT" ]; then
echo "Running pre-install script..."
sudo bash "$PRE_INSTALL_SCRIPT"
else
echo "Warning: preInstall.sh script not found!"
fi
# Run docker-compose.yml
DOCKER_COMPOSE_FILE="/opt/app/docker-compose.yml"
if [ -f "$DOCKER_COMPOSE_FILE" ]; then
echo "Running Docker Compose..."
if command -v docker-compose &> /dev/null; then
# Run with standalone docker-compose
sudo docker-compose -f "$DOCKER_COMPOSE_FILE" up -d
elif command -v docker &> /dev/null && docker compose version &> /dev/null; then
# Run with docker compose (plugin)
sudo docker compose -f "$DOCKER_COMPOSE_FILE" up -d
else
echo "Error: Docker Compose not found. Please install it."
exit 1
fi
else
echo "Warning: docker-compose.yml not found!"
fi
# Define the location of your web.txt file
WEB_FILE="/opt/app/web.txt"
# Function to display the information in a table format
display_web_info() {
# Load environment variables from the .env file
if [[ -f $ENV_FILE ]]; then
export $(grep -v '^#' "$ENV_FILE" | xargs)
else
echo "Error: .env file not found!"
exit 1
fi
echo "---------------------------------------------"
while IFS= read -r line; do
# Use eval to expand the variables
eval "echo \"$line\"" | while IFS='=' read -r key value; do
printf "| %-15s | %s\n" "$key" "$value"
done
done < "$WEB_FILE"
echo "---------------------------------------------"
}
display_web_info
# ASCII Art for "Congratulations"
cat <<'EOF'
____ _ _ _ _
/ ___|___ _ __ __ _ _ __ __ _| |_ _ _| | __ _| |_(_) ___ _ __ ___
| | / _ \| '_ \ / _` | '__/ _` | __| | | | |/ _` | __| |/ _ \| '_ \/ __|
| |__| (_) | | | | (_| | | | (_| | |_| |_| | | (_| | |_| | (_) | | | \__ \
\____\___/|_| |_|\__, |_| \__,_|\__|\__,_|_|\__,_|\__|_|\___/|_| |_|___/
|___/
EOF
# End of script