429 lines
9.8 KiB
Bash
429 lines
9.8 KiB
Bash
#!/bin/bash
|
|
|
|
# TG Autoposter - Quick Commands Reference
|
|
# Copy this file and keep it handy while developing/operating
|
|
|
|
## =====================================
|
|
## 🚀 QUICK START
|
|
## =====================================
|
|
|
|
# Start everything
|
|
docker-compose up -d
|
|
|
|
# Start specific service
|
|
docker-compose up -d postgres redis bot celery_beat
|
|
|
|
# Stop everything
|
|
docker-compose down
|
|
|
|
# Stop specific service
|
|
docker-compose stop bot
|
|
|
|
# Restart services
|
|
docker-compose restart
|
|
|
|
## =====================================
|
|
## 📊 MONITORING & LOGS
|
|
## =====================================
|
|
|
|
# View all logs
|
|
docker-compose logs -f
|
|
|
|
# View specific service logs (last 50 lines)
|
|
docker-compose logs bot --tail 50
|
|
docker-compose logs celery_worker_send --tail 50
|
|
docker-compose logs postgres --tail 50
|
|
|
|
# Real-time resource usage
|
|
docker stats
|
|
|
|
# Check service status
|
|
docker-compose ps
|
|
|
|
# Check specific service status
|
|
docker ps | grep tg_autoposter
|
|
|
|
## =====================================
|
|
## 🗄️ DATABASE OPERATIONS
|
|
## =====================================
|
|
|
|
# Connect to PostgreSQL
|
|
docker-compose exec postgres psql -U bot -d tg_autoposter
|
|
|
|
# Run SQL commands
|
|
docker-compose exec postgres psql -U bot -d tg_autoposter -c "SELECT * FROM groups;"
|
|
|
|
# Backup database
|
|
docker-compose exec -T postgres pg_dump -U bot tg_autoposter > backup_$(date +%Y%m%d_%H%M%S).sql
|
|
|
|
# Restore database
|
|
gunzip < backup.sql.gz | docker-compose exec -T postgres psql -U bot tg_autoposter
|
|
|
|
# Database migrations
|
|
docker-compose exec bot alembic upgrade head # Apply latest
|
|
docker-compose exec bot alembic downgrade -1 # Rollback one step
|
|
docker-compose exec bot alembic current # Show current version
|
|
docker-compose exec bot alembic history # Show all versions
|
|
|
|
## =====================================
|
|
## 🔴 CACHE & CELERY
|
|
## =====================================
|
|
|
|
# Connect to Redis
|
|
docker-compose exec redis redis-cli
|
|
|
|
# Flush Redis cache
|
|
docker-compose exec redis redis-cli FLUSHDB
|
|
|
|
# Check Redis info
|
|
docker-compose exec redis redis-cli INFO
|
|
|
|
# Check Celery active tasks
|
|
docker-compose logs celery_worker_send | grep -i active
|
|
|
|
# Inspect Celery tasks (in another terminal with flower running)
|
|
# Open: http://localhost:5555
|
|
|
|
# Clear Celery queue
|
|
docker-compose exec redis redis-cli
|
|
# In redis-cli:
|
|
# FLUSHDB
|
|
|
|
# Revoke a specific task
|
|
celery -A app.celery_config revoke TASK_ID
|
|
|
|
## =====================================
|
|
## 🧪 TESTING & CODE QUALITY
|
|
## =====================================
|
|
|
|
# Run all tests
|
|
docker-compose exec bot pytest
|
|
|
|
# Run tests with coverage
|
|
docker-compose exec bot pytest --cov=app
|
|
|
|
# Run specific test file
|
|
docker-compose exec bot pytest tests/test_handlers.py
|
|
|
|
# Run tests in watch mode (local only)
|
|
pytest-watch
|
|
|
|
# Lint code
|
|
docker-compose exec bot flake8 app/
|
|
docker-compose exec bot mypy app/
|
|
docker-compose exec bot black --check app/
|
|
|
|
# Format code
|
|
docker-compose exec bot black app/
|
|
docker-compose exec bot isort app/
|
|
|
|
# Lint + format in one command
|
|
make lint && make fmt
|
|
|
|
## =====================================
|
|
## 🔧 CONFIGURATION & SETUP
|
|
## =====================================
|
|
|
|
# Create .env file from template
|
|
cp .env.example .env
|
|
|
|
# Edit environment variables
|
|
nano .env
|
|
|
|
# Validate .env
|
|
grep -E "^[A-Z_]+" .env
|
|
|
|
# Check specific variable
|
|
echo $TELEGRAM_BOT_TOKEN
|
|
|
|
# Update variable at runtime
|
|
export NEW_VARIABLE=value
|
|
|
|
## =====================================
|
|
## 📦 DEPENDENCY MANAGEMENT
|
|
## =====================================
|
|
|
|
# Install Python dependencies
|
|
docker-compose exec bot pip install -r requirements.txt
|
|
|
|
# Install dev dependencies
|
|
docker-compose exec bot pip install -r requirements-dev.txt
|
|
|
|
# List installed packages
|
|
docker-compose exec bot pip list
|
|
|
|
# Upgrade pip, setuptools, wheel
|
|
docker-compose exec bot pip install --upgrade pip setuptools wheel
|
|
|
|
# Check for outdated packages
|
|
docker-compose exec bot pip list --outdated
|
|
|
|
## =====================================
|
|
## 🐳 DOCKER MANAGEMENT
|
|
## =====================================
|
|
|
|
# Build images
|
|
docker-compose build
|
|
|
|
# Rebuild specific service
|
|
docker-compose build bot
|
|
|
|
# View images
|
|
docker images | grep tg_autoposter
|
|
|
|
# Remove unused images
|
|
docker image prune
|
|
|
|
# View containers
|
|
docker ps -a
|
|
|
|
# Remove container
|
|
docker rm container_id
|
|
|
|
# View volumes
|
|
docker volume ls
|
|
|
|
# Inspect container
|
|
docker inspect container_id
|
|
|
|
# Copy file from container
|
|
docker cp container_id:/app/file.txt ./file.txt
|
|
|
|
# Copy file to container
|
|
docker cp ./file.txt container_id:/app/file.txt
|
|
|
|
## =====================================
|
|
## 🤖 BOT OPERATIONS
|
|
## =====================================
|
|
|
|
# Connect to bot shell
|
|
docker-compose exec bot bash
|
|
# or Python shell
|
|
docker-compose exec bot python
|
|
|
|
# Check bot token is set
|
|
docker-compose exec bot echo $TELEGRAM_BOT_TOKEN
|
|
|
|
# Test bot is running
|
|
docker-compose exec bot curl http://localhost:8000
|
|
|
|
# View bot logs
|
|
docker-compose logs bot -f
|
|
|
|
# Restart bot service
|
|
docker-compose restart bot
|
|
|
|
# Stop bot (without stopping other services)
|
|
docker-compose stop bot
|
|
|
|
# Start bot (after stopping)
|
|
docker-compose start bot
|
|
|
|
## =====================================
|
|
## 🌐 WEB INTERFACES
|
|
## =====================================
|
|
|
|
# Flower (Task Monitoring)
|
|
# Open in browser: http://localhost:5555
|
|
# Default: admin / password (from FLOWER_PASSWORD env var)
|
|
|
|
# PostgreSQL Admin (if pgAdmin is running)
|
|
# Open in browser: http://localhost:5050
|
|
# Default: admin@admin.com / admin
|
|
|
|
# Redis Commander (if running)
|
|
# Open in browser: http://localhost:8081
|
|
|
|
## =====================================
|
|
## 🔍 DEBUGGING
|
|
## =====================================
|
|
|
|
# Get container ID
|
|
docker-compose ps | grep bot
|
|
|
|
# Inspect container
|
|
docker inspect [CONTAINER_ID]
|
|
|
|
# Check container logs for errors
|
|
docker-compose logs bot | grep -i error
|
|
|
|
# Check resource limits
|
|
docker stats --no-stream
|
|
|
|
# Monitor specific metric
|
|
watch -n 1 'docker stats --no-stream | grep tg_autoposter'
|
|
|
|
# Check network
|
|
docker network ls | grep tg_autoposter
|
|
|
|
# Test connectivity
|
|
docker-compose exec bot ping redis
|
|
docker-compose exec bot ping postgres
|
|
|
|
## =====================================
|
|
## 🚨 TROUBLESHOOTING
|
|
## =====================================
|
|
|
|
# Service won't start - check logs
|
|
docker-compose logs [service] --tail 50
|
|
|
|
# Fix: Restart service
|
|
docker-compose restart [service]
|
|
|
|
# Port already in use
|
|
# Change port in docker-compose.yml or:
|
|
sudo lsof -i :5555
|
|
sudo kill -9 PID
|
|
|
|
# Database won't connect
|
|
docker-compose exec postgres psql -U bot -d tg_autoposter -c "\dt"
|
|
|
|
# Fix: Run migrations
|
|
docker-compose exec bot alembic upgrade head
|
|
|
|
# Out of disk space
|
|
docker system prune -a
|
|
docker image prune
|
|
docker volume prune
|
|
|
|
# Memory issues
|
|
docker stats
|
|
docker-compose restart [service]
|
|
|
|
# Permission denied
|
|
sudo chown -R $USER:$USER .
|
|
chmod +x docker.sh quickstart.sh
|
|
|
|
## =====================================
|
|
## 🔐 SECURITY
|
|
## =====================================
|
|
|
|
# Generate secure password
|
|
openssl rand -base64 32
|
|
|
|
# Check for exposed secrets
|
|
git log --all --oneline --grep="password\|secret\|key" | head -20
|
|
|
|
# Scan for security issues
|
|
pip install bandit
|
|
docker-compose exec bot bandit -r app/
|
|
|
|
# Check for vulnerable dependencies
|
|
pip install safety
|
|
docker-compose exec bot safety check
|
|
|
|
# Rotate secrets
|
|
# 1. Generate new values
|
|
# 2. Update .env and secrets
|
|
# 3. Restart services
|
|
docker-compose restart
|
|
|
|
## =====================================
|
|
## 📈 PERFORMANCE TUNING
|
|
## =====================================
|
|
|
|
# Check slow queries (in PostgreSQL)
|
|
# Enable slow query log, then:
|
|
docker-compose exec postgres tail -f /var/log/postgresql/slowquery.log
|
|
|
|
# Check database size
|
|
docker-compose exec postgres psql -U bot -d tg_autoposter -c "
|
|
SELECT schemaname, tablename,
|
|
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename))
|
|
FROM pg_tables
|
|
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;"
|
|
|
|
# Analyze query performance
|
|
docker-compose exec postgres psql -U bot -d tg_autoposter -c "EXPLAIN ANALYZE SELECT * FROM messages LIMIT 10;"
|
|
|
|
# Vacuum and analyze database
|
|
docker-compose exec postgres psql -U bot -d tg_autoposter -c "VACUUM ANALYZE;"
|
|
|
|
## =====================================
|
|
## 🔄 COMMON WORKFLOWS
|
|
## =====================================
|
|
|
|
# Full restart (nuclear option)
|
|
docker-compose down -v
|
|
docker-compose up -d
|
|
docker-compose exec bot alembic upgrade head
|
|
|
|
# Backup and cleanup
|
|
docker-compose exec -T postgres pg_dump -U bot tg_autoposter > backup.sql
|
|
docker-compose down -v
|
|
docker image prune -a
|
|
docker volume prune
|
|
|
|
# Deploy new version
|
|
git pull origin main
|
|
docker-compose build
|
|
docker-compose up -d
|
|
docker-compose exec bot alembic upgrade head
|
|
docker-compose logs -f
|
|
|
|
# Roll back to previous version
|
|
git checkout previous-tag
|
|
docker-compose build
|
|
docker-compose up -d
|
|
docker-compose exec bot alembic downgrade -1
|
|
|
|
## =====================================
|
|
## 📚 HELP & REFERENCES
|
|
## =====================================
|
|
|
|
# Show this file
|
|
cat QUICK_COMMANDS.md
|
|
|
|
# Docker Compose help
|
|
docker-compose help
|
|
|
|
# Docker help
|
|
docker help
|
|
|
|
# Check version
|
|
docker --version
|
|
docker-compose --version
|
|
python --version
|
|
|
|
# Read documentation
|
|
# - README.md
|
|
# - DEVELOPMENT.md
|
|
# - PRODUCTION_DEPLOYMENT.md
|
|
# - docs/DOCKER_CELERY.md
|
|
|
|
## =====================================
|
|
## 💡 QUICK TIPS
|
|
## =====================================
|
|
|
|
# Use alias for faster commands (add to ~/.bashrc)
|
|
# alias dc='docker-compose'
|
|
# alias dcb='docker-compose build'
|
|
# alias dcd='docker-compose down'
|
|
# alias dcu='docker-compose up -d'
|
|
# alias dcl='docker-compose logs -f'
|
|
# alias dcp='docker-compose ps'
|
|
|
|
# Then use:
|
|
# dc ps
|
|
# dcl bot
|
|
# dcu
|
|
|
|
# Use Make for predefined targets
|
|
make help # Show all make targets
|
|
make up # Start services
|
|
make down # Stop services
|
|
make logs # View logs
|
|
|
|
# Use docker.sh for comprehensive management
|
|
./docker.sh up
|
|
./docker.sh logs
|
|
./docker.sh ps
|
|
|
|
---
|
|
|
|
**Quick Reference Version**: 1.0
|
|
**Last Updated**: 2024-01-01
|
|
**Usefulness**: ⭐⭐⭐⭐⭐
|
|
|
|
Keep this file open while working on the project!
|