-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
131 lines (111 loc) · 3.48 KB
/
script.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
#!/usr/bin/env bash
# Description: This script automates the process of mirroring a website and hosting it in a Docker container on a Raspberry Pi.
set -e # Exit on any command failure
trap cleanup EXIT
cleanup() {
# Add any cleanup code if needed
echo "Cleanup completed."
}
# Get the home directory of the current user
USER_HOME=$(eval echo ~"$USER")
# Constants with environment variables and default fallbacks
DEFAULT_SAVE_PATH="${MIRROR_SAVE_PATH:-$USER_HOME/mirroredsite}"
DEFAULT_PORT=80
# Ensure the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root or with sudo privileges."
exit 1
fi
# Check if Docker and httrack are installed and Docker is running
for cmd in docker httrack; do
if ! command -v "$cmd" &> /dev/null; then
echo "Error: $cmd is not installed."
exit 1
fi
done
if ! systemctl is-active --quiet docker; then
echo "Error: Docker is not running. Please ensure Docker is set up correctly."
exit 1
fi
# Usage message
usage() {
echo "Usage: $0 -u WEBSITE_URL [-s SAVE_PATH] [-p DOCKER_PORT]"
echo " -u Website URL to mirror."
echo " -s Path to save the mirrored website. Default: $DEFAULT_SAVE_PATH."
echo " -p Port to run the Docker container. Default: $DEFAULT_PORT."
exit 1
}
# Check if port is in use using 'ss' command
is_port_in_use() {
local port="$1"
ss -tuln | grep -q ":$port "
}
# Parse command-line flags
while getopts ":u:s:p:" opt; do
case $opt in
u) website_url="$OPTARG"
;;
s) save_path="$OPTARG"
;;
p) docker_port="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
usage
;;
:) echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
# Check if URL is provided
if [ -z "$website_url" ]; then
echo "Error: Website URL not provided."
usage
fi
# Set default values if not provided
save_path=${save_path:-"$DEFAULT_SAVE_PATH/$(basename "$website_url")"}
docker_port=${docker_port:-$DEFAULT_PORT}
container_name="$(basename "$website_url")-container"
image_name="${container_name}-image"
# Check if port is already in use
if is_port_in_use "$docker_port"; then
echo "Error: Port $docker_port is already in use. Please choose a different port."
exit 1
fi
# Mirror the website
echo "Mirroring the website: $website_url..."
mkdir -p "$save_path"
if ! httrack "$website_url" -O "$save_path"; then
echo "Error: Failed to mirror the website."
exit 1
fi
# Create Dockerfile
echo "Creating Dockerfile..."
cat <<EOL > "$save_path/Dockerfile"
FROM nginx:alpine
COPY ./ /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
EOL
# Navigate to the directory and build the Docker image
echo "Building the Docker image..."
cd "$save_path"
if ! docker build -t "$image_name" .; then
echo "Error: Failed to build the Docker image."
exit 1
fi
# Check if a container with the same name is already running
if docker ps -qf name="$container_name" | grep -q .; then
echo "Stopping and removing existing container..."
docker stop "$container_name"
docker rm "$container_name"
fi
# Run the Docker container
echo "Running the Docker container on port $docker_port..."
if ! docker run -d -p "$docker_port:80" --name "$container_name" "$image_name"; then
echo "Error: Failed to run the Docker container."
exit 1
fi
# Display access information
ip_address=$(hostname -I | awk '{print $1}')
echo "Done! You can now access the mirrored website by navigating to http://$ip_address:$docker_port"