fuck this shit
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
IMAGE_NAME="markdown-blog"
|
||||
CONTAINER_NAME="markdown-blog-container"
|
||||
PORT=8080
|
||||
# Automatically set MARKDOWN_DIR to the directory where this script lives
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
MARKDOWN_DIR="$SCRIPT_DIR" # Use the script's directory as the default markdown directory
|
||||
|
||||
@@ -12,12 +11,13 @@ MARKDOWN_DIR="$SCRIPT_DIR" # Use the script's directory as the default markdown
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Function to check if Docker is running
|
||||
check_docker() {
|
||||
if ! docker info > /dev/null 2>&1; then
|
||||
echo -e "${RED}Error: Docker is not running${NC}"
|
||||
echo -e "${RED}Error: Docker is not running. Please start Docker and try again.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
@@ -35,7 +35,7 @@ container_running() {
|
||||
# Function to check container health
|
||||
check_container_health() {
|
||||
local health_status
|
||||
health_status=$(docker inspect --format='{{.State.Health.Status}}' $CONTAINER_NAME 2>/dev/null)
|
||||
health_status=$(docker inspect --format='{{.State.Health.Status}}' "$CONTAINER_NAME" 2>/dev/null)
|
||||
if [ "$health_status" = "healthy" ]; then
|
||||
return 0
|
||||
else
|
||||
@@ -54,17 +54,11 @@ npm_spinner_right() {
|
||||
"JavaScript: Where '1' + 1 = '11'!"
|
||||
"NPM: Installing 1000 dependencies for 'hello world'..."
|
||||
"TypeScript: Making JavaScript developers feel safe (sometimes)."
|
||||
"NPM: 5 minutes left... just kidding!"
|
||||
"JavaScript: Because who needs types anyway?"
|
||||
"NPM: One does not simply 'npm install' without warnings."
|
||||
"TypeScript: The language that makes you write more code to do less."
|
||||
"NPM: 0 vulnerabilities (in your dreams)."
|
||||
)
|
||||
local colors=(31 33 32 36 34 35 91 92 93 94 95 96)
|
||||
local joke_count=${#jokes[@]}
|
||||
local colors=(31 33 32 36 34 35) # Red, Yellow, Green, Cyan, Blue, Magenta
|
||||
local joke_index=0
|
||||
tput civis
|
||||
while kill -0 $pid 2>/dev/null; do
|
||||
tput civis # Hide cursor
|
||||
while kill -0 "$pid" 2>/dev/null; do
|
||||
local temp="${spinstr#?}"
|
||||
local cols=$(tput cols)
|
||||
local msg="[%c] %s"
|
||||
@@ -86,102 +80,84 @@ npm_spinner_right() {
|
||||
printf "\r%*s%s" $pad "" "$rainbow"
|
||||
spinstr=$temp${spinstr%$temp}
|
||||
sleep $delay
|
||||
joke_index=$(( (joke_index + 1) % joke_count ))
|
||||
joke_index=$(( (joke_index + 1) % ${#jokes[@]} ))
|
||||
done
|
||||
printf "\r"
|
||||
tput cnorm
|
||||
tput cnorm # Restore cursor
|
||||
}
|
||||
|
||||
# Function to build the Docker image (show spinner/jokes only during npm ci)
|
||||
build_image() {
|
||||
echo -e "${YELLOW}Building Docker image...${NC}"
|
||||
# Use a named pipe to capture docker build output
|
||||
local pipe=$(mktemp -u)
|
||||
mkfifo "$pipe"
|
||||
# Start docker build, redirect output to pipe
|
||||
(docker build -t $IMAGE_NAME . | tee "$pipe") &
|
||||
(docker build -t "$IMAGE_NAME" . | tee "$pipe") &
|
||||
build_pid=$!
|
||||
# Watch the pipe for the npm ci step
|
||||
while read -r line; do
|
||||
echo "$line"
|
||||
if [[ "$line" == *"RUN npm ci"* ]]; then
|
||||
# Wait for the npm ci process to start in the background
|
||||
sleep 1
|
||||
npm_spinner_right $build_pid &
|
||||
npm_spinner_right "$build_pid" &
|
||||
spinner_pid=$!
|
||||
wait $build_pid
|
||||
kill $spinner_pid 2>/dev/null
|
||||
wait "$build_pid"
|
||||
kill "$spinner_pid" 2>/dev/null
|
||||
break
|
||||
fi
|
||||
done < "$pipe"
|
||||
wait $build_pid
|
||||
wait "$build_pid"
|
||||
rm -f "$pipe"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}Error: Failed to build Docker image${NC}"
|
||||
echo -e "${RED}Error: Failed to build Docker image. Check logs above.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${GREEN}Docker image built successfully${NC}"
|
||||
echo -e "${GREEN}Docker image built successfully!${NC}"
|
||||
}
|
||||
|
||||
# Function to start the container
|
||||
start_container() {
|
||||
echo -e "${YELLOW}Starting container...${NC}"
|
||||
|
||||
# Check if container already exists
|
||||
if container_exists; then
|
||||
if container_running; then
|
||||
echo -e "${YELLOW}Container is already running${NC}"
|
||||
echo -e "${YELLOW}Container is already running.${NC}"
|
||||
return
|
||||
else
|
||||
echo -e "${YELLOW}Container exists but is not running. Starting it...${NC}"
|
||||
if ! docker start $CONTAINER_NAME; then
|
||||
echo -e "${RED}Error: Failed to start existing container${NC}"
|
||||
echo -e "${YELLOW}Restarting existing container...${NC}"
|
||||
docker start "$CONTAINER_NAME" || {
|
||||
echo -e "${RED}Error: Failed to start container.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
fi
|
||||
else
|
||||
# Create and start new container
|
||||
if ! docker run -d \
|
||||
--name $CONTAINER_NAME \
|
||||
-p $PORT:8080 \
|
||||
echo -e "${BLUE}Creating and starting new container...${NC}"
|
||||
docker run -d \
|
||||
--name "$CONTAINER_NAME" \
|
||||
-p "$PORT:8080" \
|
||||
-v "$MARKDOWN_DIR:/markdown" \
|
||||
--restart unless-stopped \
|
||||
--health-cmd="wget --no-verbose --tries=1 --spider http://localhost:8080/ || exit 1" \
|
||||
--health-cmd="curl -f http://localhost:8080/ || exit 1" \
|
||||
--health-interval=30s \
|
||||
--health-timeout=30s \
|
||||
--health-timeout=10s \
|
||||
--health-retries=3 \
|
||||
--health-start-period=5s \
|
||||
$IMAGE_NAME; then
|
||||
echo -e "${RED}Error: Failed to create and start container${NC}"
|
||||
"$IMAGE_NAME" || {
|
||||
echo -e "${RED}Error: Failed to create container.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
fi
|
||||
|
||||
# Wait for container to be ready
|
||||
|
||||
# Wait for container to be healthy
|
||||
echo -e "${YELLOW}Waiting for container to be ready...${NC}"
|
||||
local max_attempts=60
|
||||
local max_attempts=30
|
||||
local attempt=1
|
||||
|
||||
while [ $attempt -le $max_attempts ]; do
|
||||
if check_container_health; then
|
||||
echo -e "${GREEN}Container is healthy and accessible at http://localhost:$PORT${NC}"
|
||||
echo -e "${GREEN}Container is healthy! Access it at: http://localhost:$PORT${NC}"
|
||||
return
|
||||
fi
|
||||
|
||||
# Check if container is still running
|
||||
if ! container_running; then
|
||||
echo -e "${RED}Error: Container stopped unexpectedly${NC}"
|
||||
docker logs $CONTAINER_NAME
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -n "."
|
||||
printf "."
|
||||
sleep 1
|
||||
attempt=$((attempt + 1))
|
||||
done
|
||||
|
||||
echo -e "${RED}Error: Container did not become healthy in time${NC}"
|
||||
docker logs $CONTAINER_NAME
|
||||
echo -e "${RED}Error: Container did not become healthy. Check logs with './manage_container.sh logs'.${NC}"
|
||||
exit 1
|
||||
}
|
||||
|
||||
@@ -189,77 +165,71 @@ start_container() {
|
||||
stop_container() {
|
||||
echo -e "${YELLOW}Stopping container...${NC}"
|
||||
if ! container_exists; then
|
||||
echo -e "${YELLOW}Container does not exist${NC}"
|
||||
echo -e "${YELLOW}Container does not exist.${NC}"
|
||||
return
|
||||
fi
|
||||
|
||||
if ! docker stop $CONTAINER_NAME; then
|
||||
echo -e "${RED}Error: Failed to stop container${NC}"
|
||||
docker stop "$CONTAINER_NAME" || {
|
||||
echo -e "${RED}Error: Failed to stop container.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${GREEN}Container stopped successfully${NC}"
|
||||
}
|
||||
echo -e "${GREEN}Container stopped successfully.${NC}"
|
||||
}
|
||||
|
||||
# Function to remove the container
|
||||
remove_container() {
|
||||
echo -e "${YELLOW}Removing container...${NC}"
|
||||
if ! container_exists; then
|
||||
echo -e "${YELLOW}Container does not exist${NC}"
|
||||
echo -e "${YELLOW}Container does not exist.${NC}"
|
||||
return
|
||||
fi
|
||||
|
||||
if container_running; then
|
||||
stop_container
|
||||
fi
|
||||
|
||||
if ! docker rm $CONTAINER_NAME; then
|
||||
echo -e "${RED}Error: Failed to remove container${NC}"
|
||||
docker rm "$CONTAINER_NAME" || {
|
||||
echo -e "${RED}Error: Failed to remove container.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${GREEN}Container removed successfully${NC}"
|
||||
}
|
||||
echo -e "${GREEN}Container removed successfully.${NC}"
|
||||
}
|
||||
|
||||
# Function to restart the container
|
||||
restart_container() {
|
||||
echo -e "${YELLOW}Restarting container...${NC}"
|
||||
if ! container_exists; then
|
||||
echo -e "${YELLOW}Container does not exist. Starting new container...${NC}"
|
||||
echo -e "${YELLOW}Container does not exist. Starting new one...${NC}"
|
||||
start_container
|
||||
return
|
||||
fi
|
||||
|
||||
if ! docker restart $CONTAINER_NAME; then
|
||||
echo -e "${RED}Error: Failed to restart container${NC}"
|
||||
docker restart "$CONTAINER_NAME" || {
|
||||
echo -e "${RED}Error: Failed to restart container.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${GREEN}Container restarted successfully${NC}"
|
||||
}
|
||||
echo -e "${GREEN}Container restarted successfully.${NC}"
|
||||
}
|
||||
|
||||
# Function to view logs
|
||||
view_logs() {
|
||||
echo -e "${YELLOW}Viewing logs...${NC}"
|
||||
if ! container_exists; then
|
||||
echo -e "${RED}Error: Container does not exist${NC}"
|
||||
echo -e "${RED}Error: Container does not exist.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
docker logs -f $CONTAINER_NAME
|
||||
docker logs -f "$CONTAINER_NAME"
|
||||
}
|
||||
|
||||
# Function to show container status
|
||||
show_status() {
|
||||
echo -e "${YELLOW}Container Status:${NC}"
|
||||
if ! container_exists; then
|
||||
echo -e "${RED}Container does not exist${NC}"
|
||||
echo -e "${RED}Container does not exist.${NC}"
|
||||
return
|
||||
fi
|
||||
|
||||
if container_running; then
|
||||
local health_status
|
||||
health_status=$(docker inspect --format='{{.State.Health.Status}}' $CONTAINER_NAME 2>/dev/null)
|
||||
echo -e "${GREEN}Container is running${NC}"
|
||||
echo "Health status: $health_status"
|
||||
echo "Access the application at: http://localhost:$PORT"
|
||||
health_status=$(docker inspect --format='{{.State.Health.Status}}' "$CONTAINER_NAME" 2>/dev/null)
|
||||
echo -e "${GREEN}Container is running.${NC}"
|
||||
echo -e "Health: ${health_status}"
|
||||
echo -e
|
||||
else
|
||||
echo -e "${YELLOW}Container exists but is not running${NC}"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user