Skip to content

Instantly share code, notes, and snippets.

@ayenisholah
Last active March 1, 2025 16:56
Show Gist options
  • Select an option

  • Save ayenisholah/c6ba2867cf1be6e7ba8be5bbc2894445 to your computer and use it in GitHub Desktop.

Select an option

Save ayenisholah/c6ba2867cf1be6e7ba8be5bbc2894445 to your computer and use it in GitHub Desktop.
Automated installation script for the NFT Bidding Bot. It checks and installs Docker and Docker Compose, sets up the project directory, downloads necessary files, creates a .env file, pulls Docker images, and starts services. Includes health checks and useful commands.
#!/bin/bash
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Repository and version information
REGISTRY="ayenisholah"
VERSION="latest"
echo -e "${GREEN}NFT Bidding Bot Installation Script${NC}"
echo "----------------------------------------"
# Check OS
OS=$(uname)
echo -e "${YELLOW}Detected OS: $OS${NC}"
# Check Docker installation
if ! [ -x "$(command -v docker)" ]; then
echo -e "${YELLOW}Docker not found. Installing Docker...${NC}"
if [ "$OS" = "Linux" ]; then
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER
rm get-docker.sh
elif [ "$OS" = "Darwin" ]; then
echo -e "${RED}Please install Docker Desktop manually from: https://www.docker.com/products/docker-desktop${NC}"
exit 1
fi
fi
# Fix Docker socket permissions
echo -e "${YELLOW}Setting Docker socket permissions...${NC}"
if [ "$OS" = "Linux" ]; then
# Ensure the docker group exists
if ! getent group docker > /dev/null; then
sudo groupadd docker
fi
# Add current user to docker group
sudo usermod -aG docker $USER
# Set permissions for Docker socket
sudo chmod 666 /var/run/docker.sock
fi
# Check Docker Compose installation
if ! [ -x "$(command -v docker-compose)" ]; then
echo -e "${YELLOW}Docker Compose not found.${NC}"
if [ "$OS" = "Darwin" ]; then
# Get Docker version for macOS
DOCKER_VERSION=$(docker version --format '{{.Server.Version}}' 2>/dev/null)
MAJOR_VERSION=$(echo $DOCKER_VERSION | cut -d. -f1)
if [ "$MAJOR_VERSION" -ge 2 ]; then
echo -e "${GREEN}Docker version >= 2.0.0 detected. Docker Compose is already included.${NC}"
else
echo -e "${YELLOW}Installing Docker Compose...${NC}"
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
fi
else
echo -e "${YELLOW}Installing Docker Compose...${NC}"
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
fi
fi
# Stop and remove existing containers if they exist
echo -e "${YELLOW}Checking for existing containers...${NC}"
CONTAINER_PREFIX="nft-bidding-bot"
EXISTING_CONTAINERS=$(docker ps -aq --filter "name=${CONTAINER_PREFIX}")
if [ ! -z "$EXISTING_CONTAINERS" ]; then
echo -e "${YELLOW}Stopping existing containers...${NC}"
docker stop $EXISTING_CONTAINERS
echo -e "${YELLOW}Removing existing containers...${NC}"
docker rm $EXISTING_CONTAINERS
fi
# Make sure all containers using Redis volumes are stopped
echo -e "${YELLOW}Ensuring all Redis containers are stopped...${NC}"
REDIS_CONTAINERS=$(docker ps -a --filter "ancestor=redis" -q)
if [ ! -z "$REDIS_CONTAINERS" ]; then
echo -e "${YELLOW}Stopping Redis containers...${NC}"
docker stop $REDIS_CONTAINERS
echo -e "${YELLOW}Removing Redis containers...${NC}"
docker rm $REDIS_CONTAINERS
fi
# Make sure all MongoDB containers are stopped
echo -e "${YELLOW}Ensuring all MongoDB containers are stopped...${NC}"
MONGO_CONTAINERS=$(docker ps -a --filter "ancestor=mongo" -q)
if [ ! -z "$MONGO_CONTAINERS" ]; then
echo -e "${YELLOW}Stopping MongoDB containers...${NC}"
docker stop $MONGO_CONTAINERS
echo -e "${YELLOW}Removing MongoDB containers...${NC}"
docker rm $MONGO_CONTAINERS
fi
# Stop all containers that might be using our volumes
echo -e "${YELLOW}Stopping all containers to ensure volumes can be removed...${NC}"
ALL_CONTAINERS=$(docker ps -q)
if [ ! -z "$ALL_CONTAINERS" ]; then
docker stop $ALL_CONTAINERS
fi
# Clear Redis and MongoDB volumes
echo -e "${YELLOW}Clearing data volumes...${NC}"
# Clear Redis volumes
REDIS_VOLUMES=$(docker volume ls -q | grep "redis_data_")
if [ ! -z "$REDIS_VOLUMES" ]; then
echo -e "${YELLOW}Removing Redis volumes: $REDIS_VOLUMES${NC}"
docker volume rm $REDIS_VOLUMES || {
echo -e "${RED}Failed to remove some Redis volumes. Forcing removal...${NC}"
for vol in $REDIS_VOLUMES; do
docker volume rm -f $vol || echo -e "${RED}Could not remove volume $vol${NC}"
done
}
fi
# Clear MongoDB volume
MONGODB_VOLUME=$(docker volume ls -q | grep "mongodb_data")
if [ ! -z "$MONGODB_VOLUME" ]; then
echo -e "${YELLOW}Removing MongoDB volume: $MONGODB_VOLUME${NC}"
docker volume rm $MONGODB_VOLUME || {
echo -e "${RED}Failed to remove MongoDB volume. Trying alternative approach...${NC}"
# Find containers using the volume
CONTAINERS_USING_VOLUME=$(docker ps -a --filter "volume=$MONGODB_VOLUME" -q)
if [ ! -z "$CONTAINERS_USING_VOLUME" ]; then
echo -e "${YELLOW}Found containers using MongoDB volume. Stopping them...${NC}"
docker stop $CONTAINERS_USING_VOLUME
docker rm $CONTAINERS_USING_VOLUME
fi
# Try removing again
docker volume rm $MONGODB_VOLUME || {
echo -e "${RED}Still could not remove MongoDB volume. Skipping...${NC}"
echo -e "${YELLOW}You may need to manually remove it later with: docker volume rm $MONGODB_VOLUME${NC}"
}
}
fi
# Create project directory
PROJECT_DIR="nft-bidding-bot"
mkdir -p $PROJECT_DIR
cd $PROJECT_DIR
# Create data directory
mkdir -p data
# Download necessary files
echo -e "${YELLOW}Downloading configuration files...${NC}"
curl -s "https://gist.githubusercontent.com/ayenisholah/316e1bcf625a86e35b532e1b0fd0b368/raw/904db25147f79dae673d6e517cb70f7a83b8c344/compose.yaml?_=$(uuidgen)" -o compose.yaml
curl -s "https://gist.githubusercontent.com/ayenisholah/ad163622797f8be7b3731a1795c71e21/raw/19caddbcb33d2acb200a1660ea024a5be493a8ee/compose.update.yaml?_=$(uuidgen)" -o compose.update.yaml
# Create .env file
cat > .env << EOL
MONGODB_URI=mongodb://mongodb:27017/BIDDING_BOT
PORT_SERVER=3003
PORT_CLIENT=3001
DOCKER_GROUP_ID=998
EOL
# Start services
echo -e "${YELLOW}Starting services...${NC}"
docker compose pull && docker compose build && docker compose up -d
# Check health
echo -e "${YELLOW}Checking service health...${NC}"
sleep 10
if curl -s http://localhost:3003/health > /dev/null; then
echo -e "${GREEN}Server is healthy!${NC}"
else
echo -e "${RED}Server health check failed${NC}"
fi
if curl -s http://localhost:3001 > /dev/null; then
echo -e "${GREEN}Client is accessible!${NC}"
else
echo -e "${RED}Client health check failed${NC}"
fi
echo -e "\n${GREEN}Installation complete!${NC}"
echo -e "Server running at: http://localhost:3003"
echo -e "Client running at: http://localhost:3001"
echo -e "\nUseful commands:"
echo -e "${YELLOW}cd $PROJECT_DIR${NC}"
echo -e "${YELLOW}docker compose ps${NC} - Check service status"
echo -e "${YELLOW}docker compose logs${NC} - View logs"
echo -e "${YELLOW}docker compose down${NC} - Stop services"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment