Initial commit: AutonetSellCar platform with deployment system

- Frontend: Next.js 14 with TypeScript
- Backend: FastAPI with SQLAlchemy
- Agent: Carmodoo sync agent
- Deployment: Docker Compose based staging/production setup
- Scripts: Automated deployment with rollback support

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
AutonetSellCar Deploy
2025-12-30 13:24:39 +09:00
commit 1f0dcb1ddb
224 changed files with 55119 additions and 0 deletions

70
deploy/deploy-staging.sh Normal file
View File

@@ -0,0 +1,70 @@
#!/bin/bash
# AutonetSellCar Staging Deployment Script
# This script is executed automatically by git post-receive hook
set -e
STAGING_DIR="/opt/autonet/staging"
LOG_FILE="/opt/autonet/logs/staging-deploy.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
mkdir -p /opt/autonet/logs
log "=========================================="
log "Starting Staging Deployment"
log "=========================================="
cd $STAGING_DIR
log "[1/4] Stopping existing containers..."
docker compose -f docker-compose.staging.yml down 2>/dev/null || true
log "[2/4] Building containers (this may take a while)..."
docker compose -f docker-compose.staging.yml build --no-cache
log "[3/4] Starting containers..."
docker compose -f docker-compose.staging.yml up -d
log "[4/4] Waiting for services to be ready..."
sleep 15
# Health check
BACKEND_OK=false
FRONTEND_OK=false
for i in {1..5}; do
if curl -sf http://localhost:8001/docs > /dev/null 2>&1; then
BACKEND_OK=true
break
fi
log "Backend not ready yet, retry $i/5..."
sleep 5
done
for i in {1..5}; do
if curl -sf http://localhost:3001 > /dev/null 2>&1; then
FRONTEND_OK=true
break
fi
log "Frontend not ready yet, retry $i/5..."
sleep 5
done
if $BACKEND_OK && $FRONTEND_OK; then
log "=========================================="
log "Staging Deployment SUCCESSFUL!"
log "Frontend: http://192.168.0.202:3001"
log "Backend: http://192.168.0.202:8001/docs"
log "=========================================="
exit 0
else
log "=========================================="
log "Staging Deployment FAILED!"
log "Backend: $BACKEND_OK, Frontend: $FRONTEND_OK"
log "Check logs: docker compose -f docker-compose.staging.yml logs"
log "=========================================="
exit 1
fi

256
deploy/deploy.sh Normal file
View File

@@ -0,0 +1,256 @@
#!/bin/bash
# AutonetSellCar Main Deployment Script
# Usage: ./deploy.sh {promote|rollback|rollback-to <timestamp>|status|cleanup}
set -e
STAGING_DIR="/opt/autonet/staging"
PROD_DIR="/opt/autonet/production"
RELEASES_DIR="/opt/autonet/releases"
LOG_FILE="/opt/autonet/logs/deploy.log"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
mkdir -p /opt/autonet/logs
health_check() {
local port=$1
local name=$2
for i in {1..5}; do
if curl -sf "http://localhost:$port" > /dev/null 2>&1; then
return 0
fi
log "$name not ready yet, retry $i/5..."
sleep 5
done
return 1
}
case "$1" in
promote)
log "=========================================="
log "Promoting Staging -> Production"
log "=========================================="
# 1. Backup current production
if [ -d "$PROD_DIR/frontend" ]; then
log "[1/6] Backing up current production to releases/$TIMESTAMP..."
mkdir -p "$RELEASES_DIR/$TIMESTAMP"
rsync -a --exclude='*.db' --exclude='uploads/' "$PROD_DIR/" "$RELEASES_DIR/$TIMESTAMP/"
echo "$TIMESTAMP" > "$PROD_DIR/.current_release"
echo "$TIMESTAMP" > "$PROD_DIR/.previous_release"
else
log "[1/6] No existing production to backup (first deployment)"
fi
# 2. Stop production containers
log "[2/6] Stopping production containers..."
cd "$PROD_DIR" 2>/dev/null && docker compose -f docker-compose.production.yml down 2>/dev/null || true
# 3. Copy staging to production (exclude DB and uploads)
log "[3/6] Copying staging code to production..."
mkdir -p "$PROD_DIR/frontend" "$PROD_DIR/backend" "$PROD_DIR/agent"
rsync -av --delete --exclude='*.db' --exclude='uploads/' --exclude='node_modules/' --exclude='__pycache__/' \
"$STAGING_DIR/frontend/" "$PROD_DIR/frontend/"
rsync -av --delete --exclude='*.db' --exclude='uploads/' --exclude='__pycache__/' \
"$STAGING_DIR/backend/" "$PROD_DIR/backend/"
rsync -av --delete --exclude='__pycache__/' \
"$STAGING_DIR/agent/" "$PROD_DIR/agent/" 2>/dev/null || true
# Copy docker-compose file
cp "$STAGING_DIR/docker-compose.production.yml" "$PROD_DIR/"
# 4. Build production containers
log "[4/6] Building production containers..."
cd "$PROD_DIR"
docker compose -f docker-compose.production.yml build
# 5. Start production containers
log "[5/6] Starting production containers..."
docker compose -f docker-compose.production.yml up -d
# 6. Health check
log "[6/6] Running health checks..."
sleep 15
BACKEND_OK=false
FRONTEND_OK=false
if health_check 8000 "Backend"; then
BACKEND_OK=true
log "Backend: OK"
fi
if health_check 3000 "Frontend"; then
FRONTEND_OK=true
log "Frontend: OK"
fi
if $BACKEND_OK && $FRONTEND_OK; then
log "=========================================="
log "Production Deployment SUCCESSFUL!"
log "Release: $TIMESTAMP"
log "Frontend: http://192.168.0.202:3000"
log "Backend: http://192.168.0.202:8000/docs"
log "=========================================="
else
log "=========================================="
log "Health check FAILED! Auto-rolling back..."
log "=========================================="
$0 rollback
exit 1
fi
;;
rollback)
log "=========================================="
log "Rolling Back Production"
log "=========================================="
if [ -f "$PROD_DIR/.previous_release" ]; then
PREV_RELEASE=$(cat "$PROD_DIR/.previous_release")
elif [ -f "$PROD_DIR/.current_release" ]; then
PREV_RELEASE=$(cat "$PROD_DIR/.current_release")
else
log "ERROR: No previous release found!"
exit 1
fi
if [ ! -d "$RELEASES_DIR/$PREV_RELEASE" ]; then
log "ERROR: Release $PREV_RELEASE not found!"
log "Available releases:"
ls -1 "$RELEASES_DIR" 2>/dev/null || echo "None"
exit 1
fi
log "Rolling back to release: $PREV_RELEASE"
# Stop production
cd "$PROD_DIR" 2>/dev/null && docker compose -f docker-compose.production.yml down 2>/dev/null || true
# Restore from release
rsync -av --delete --exclude='*.db' --exclude='uploads/' \
"$RELEASES_DIR/$PREV_RELEASE/frontend/" "$PROD_DIR/frontend/"
rsync -av --delete --exclude='*.db' --exclude='uploads/' \
"$RELEASES_DIR/$PREV_RELEASE/backend/" "$PROD_DIR/backend/"
rsync -av --delete \
"$RELEASES_DIR/$PREV_RELEASE/agent/" "$PROD_DIR/agent/" 2>/dev/null || true
# Copy docker-compose
cp "$RELEASES_DIR/$PREV_RELEASE/docker-compose.production.yml" "$PROD_DIR/" 2>/dev/null || true
# Rebuild and start
cd "$PROD_DIR"
docker compose -f docker-compose.production.yml build
docker compose -f docker-compose.production.yml up -d
log "=========================================="
log "Rollback to $PREV_RELEASE COMPLETE!"
log "=========================================="
;;
rollback-to)
if [ -z "$2" ]; then
log "Usage: deploy.sh rollback-to <release_timestamp>"
log "Available releases:"
ls -1 "$RELEASES_DIR" 2>/dev/null || echo "None"
exit 1
fi
TARGET_RELEASE="$2"
if [ ! -d "$RELEASES_DIR/$TARGET_RELEASE" ]; then
log "ERROR: Release $TARGET_RELEASE not found!"
log "Available releases:"
ls -1 "$RELEASES_DIR"
exit 1
fi
log "=========================================="
log "Rolling Back to Specific Release: $TARGET_RELEASE"
log "=========================================="
# Backup current before rollback
if [ -d "$PROD_DIR/frontend" ]; then
mkdir -p "$RELEASES_DIR/$TIMESTAMP"
rsync -a --exclude='*.db' --exclude='uploads/' "$PROD_DIR/" "$RELEASES_DIR/$TIMESTAMP/"
fi
# Stop production
cd "$PROD_DIR" 2>/dev/null && docker compose -f docker-compose.production.yml down 2>/dev/null || true
# Restore from target release
rsync -av --delete --exclude='*.db' --exclude='uploads/' \
"$RELEASES_DIR/$TARGET_RELEASE/frontend/" "$PROD_DIR/frontend/"
rsync -av --delete --exclude='*.db' --exclude='uploads/' \
"$RELEASES_DIR/$TARGET_RELEASE/backend/" "$PROD_DIR/backend/"
rsync -av --delete \
"$RELEASES_DIR/$TARGET_RELEASE/agent/" "$PROD_DIR/agent/" 2>/dev/null || true
cp "$RELEASES_DIR/$TARGET_RELEASE/docker-compose.production.yml" "$PROD_DIR/" 2>/dev/null || true
echo "$TARGET_RELEASE" > "$PROD_DIR/.current_release"
# Rebuild and start
cd "$PROD_DIR"
docker compose -f docker-compose.production.yml build
docker compose -f docker-compose.production.yml up -d
log "=========================================="
log "Rollback to $TARGET_RELEASE COMPLETE!"
log "=========================================="
;;
status)
echo ""
echo "=== Staging Containers ==="
docker compose -f "$STAGING_DIR/docker-compose.staging.yml" ps 2>/dev/null || echo "Not running"
echo ""
echo "=== Production Containers ==="
docker compose -f "$PROD_DIR/docker-compose.production.yml" ps 2>/dev/null || echo "Not running"
echo ""
echo "=== Current Release ==="
if [ -f "$PROD_DIR/.current_release" ]; then
cat "$PROD_DIR/.current_release"
else
echo "None"
fi
echo ""
echo "=== Available Releases ==="
ls -1 "$RELEASES_DIR" 2>/dev/null | tail -10 || echo "None"
echo ""
;;
cleanup)
log "Cleaning up old releases (keeping last 10)..."
cd "$RELEASES_DIR" 2>/dev/null || exit 0
ls -1 | head -n -10 | xargs -r rm -rf
log "Cleanup complete!"
;;
logs-staging)
docker compose -f "$STAGING_DIR/docker-compose.staging.yml" logs -f --tail=100
;;
logs-prod)
docker compose -f "$PROD_DIR/docker-compose.production.yml" logs -f --tail=100
;;
*)
echo "AutonetSellCar Deployment Script"
echo ""
echo "Usage: ./deploy.sh <command>"
echo ""
echo "Commands:"
echo " promote Promote staging to production"
echo " rollback Rollback to previous release"
echo " rollback-to <ts> Rollback to specific release"
echo " status Show container and release status"
echo " cleanup Remove old releases (keep last 10)"
echo " logs-staging Show staging logs"
echo " logs-prod Show production logs"
echo ""
;;
esac

53
deploy/post-receive Normal file
View File

@@ -0,0 +1,53 @@
#!/bin/bash
# Git post-receive hook for AutonetSellCar
# This hook is triggered when code is pushed to the bare repository
# It automatically deploys to staging environment
STAGING_DIR="/opt/autonet/staging"
GIT_DIR="/opt/autonet/git/autonet.git"
SCRIPTS_DIR="/opt/autonet/scripts"
echo ""
echo "========================================"
echo " AutonetSellCar - Git Push Received"
echo "========================================"
echo ""
# Read the pushed ref
while read oldrev newrev refname; do
branch=$(echo $refname | sed 's/refs\/heads\///')
echo "Branch pushed: $branch"
# Only deploy main branch
if [ "$branch" = "main" ] || [ "$branch" = "master" ]; then
echo "Deploying to staging..."
echo ""
# Checkout code to staging directory
git --work-tree="$STAGING_DIR" --git-dir="$GIT_DIR" checkout -f "$branch"
# Copy docker-compose files
cp "$STAGING_DIR/docker-compose.staging.yml" "$STAGING_DIR/"
cp "$STAGING_DIR/docker-compose.production.yml" "$STAGING_DIR/"
# Run staging deployment script
"$SCRIPTS_DIR/deploy-staging.sh"
echo ""
echo "========================================"
echo " Staging deployment complete!"
echo "========================================"
echo ""
echo " Test your changes at:"
echo " - Frontend: http://192.168.0.202:3001"
echo " - Backend: http://192.168.0.202:8001/docs"
echo ""
echo " When ready, run:"
echo " ssh server2 '/opt/autonet/scripts/deploy.sh promote'"
echo ""
echo "========================================"
else
echo "Skipping deployment for branch: $branch"
echo "Only main/master branch triggers deployment"
fi
done

296
deploy/setup-server2.sh Normal file
View File

@@ -0,0 +1,296 @@
#!/bin/bash
# AutonetSellCar Server2 Setup Script
# Run this script on server2 (192.168.0.202) to set up the deployment infrastructure
#
# Usage:
# scp setup-server2.sh user@192.168.0.202:/tmp/
# ssh user@192.168.0.202 'chmod +x /tmp/setup-server2.sh && /tmp/setup-server2.sh'
set -e
echo "========================================"
echo " AutonetSellCar Server2 Setup"
echo "========================================"
echo ""
# Check if running as appropriate user
if [ "$(id -u)" = "0" ]; then
echo "WARNING: Running as root. Will change ownership to: $SUDO_USER"
DEPLOY_USER="${SUDO_USER:-root}"
else
DEPLOY_USER="$(whoami)"
fi
echo "Deploy user: $DEPLOY_USER"
echo ""
# 1. Create directory structure
echo "[1/6] Creating directory structure..."
sudo mkdir -p /opt/autonet/{git,staging,production,releases,scripts,logs}
sudo chown -R "$DEPLOY_USER:$DEPLOY_USER" /opt/autonet
echo " Created /opt/autonet/{git,staging,production,releases,scripts,logs}"
# 2. Initialize bare git repository
echo ""
echo "[2/6] Initializing bare Git repository..."
cd /opt/autonet/git
if [ ! -d "autonet.git" ]; then
git init --bare autonet.git
echo " Created /opt/autonet/git/autonet.git"
else
echo " Repository already exists, skipping..."
fi
# 3. Copy deployment scripts
echo ""
echo "[3/6] Setting up deployment scripts..."
# Create deploy-staging.sh
cat > /opt/autonet/scripts/deploy-staging.sh << 'STAGING_SCRIPT'
#!/bin/bash
set -e
STAGING_DIR="/opt/autonet/staging"
LOG_FILE="/opt/autonet/logs/staging-deploy.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
mkdir -p /opt/autonet/logs
log "=========================================="
log "Starting Staging Deployment"
log "=========================================="
cd $STAGING_DIR
log "[1/4] Stopping existing containers..."
docker compose -f docker-compose.staging.yml down 2>/dev/null || true
log "[2/4] Building containers..."
docker compose -f docker-compose.staging.yml build --no-cache
log "[3/4] Starting containers..."
docker compose -f docker-compose.staging.yml up -d
log "[4/4] Waiting for services..."
sleep 15
# Health check
BACKEND_OK=false
FRONTEND_OK=false
for i in {1..5}; do
if curl -sf http://localhost:8001/docs > /dev/null 2>&1; then
BACKEND_OK=true
break
fi
sleep 5
done
for i in {1..5}; do
if curl -sf http://localhost:3001 > /dev/null 2>&1; then
FRONTEND_OK=true
break
fi
sleep 5
done
if $BACKEND_OK && $FRONTEND_OK; then
log "Staging Deployment SUCCESSFUL!"
log "Frontend: http://192.168.0.202:3001"
log "Backend: http://192.168.0.202:8001/docs"
else
log "Staging Deployment FAILED!"
exit 1
fi
STAGING_SCRIPT
chmod +x /opt/autonet/scripts/deploy-staging.sh
echo " Created deploy-staging.sh"
# Create main deploy.sh (simplified version for setup)
cat > /opt/autonet/scripts/deploy.sh << 'DEPLOY_SCRIPT'
#!/bin/bash
set -e
STAGING_DIR="/opt/autonet/staging"
PROD_DIR="/opt/autonet/production"
RELEASES_DIR="/opt/autonet/releases"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
case "$1" in
promote)
echo "Promoting Staging -> Production..."
# Backup current
if [ -d "$PROD_DIR/frontend" ]; then
mkdir -p "$RELEASES_DIR/$TIMESTAMP"
rsync -a --exclude='*.db' --exclude='uploads/' "$PROD_DIR/" "$RELEASES_DIR/$TIMESTAMP/"
echo "$TIMESTAMP" > "$PROD_DIR/.previous_release"
fi
# Stop production
cd "$PROD_DIR" 2>/dev/null && docker compose -f docker-compose.production.yml down 2>/dev/null || true
# Copy staging to production
mkdir -p "$PROD_DIR"/{frontend,backend,agent}
rsync -av --delete --exclude='*.db' --exclude='uploads/' "$STAGING_DIR/frontend/" "$PROD_DIR/frontend/"
rsync -av --delete --exclude='*.db' --exclude='uploads/' "$STAGING_DIR/backend/" "$PROD_DIR/backend/"
rsync -av --delete "$STAGING_DIR/agent/" "$PROD_DIR/agent/" 2>/dev/null || true
cp "$STAGING_DIR/docker-compose.production.yml" "$PROD_DIR/"
# Build and start
cd "$PROD_DIR"
docker compose -f docker-compose.production.yml build
docker compose -f docker-compose.production.yml up -d
echo "$TIMESTAMP" > "$PROD_DIR/.current_release"
echo "Production deployed! Release: $TIMESTAMP"
;;
rollback)
PREV=$(cat "$PROD_DIR/.previous_release" 2>/dev/null || cat "$PROD_DIR/.current_release" 2>/dev/null)
[ -d "$RELEASES_DIR/$PREV" ] || { echo "No release to rollback"; exit 1; }
cd "$PROD_DIR" && docker compose -f docker-compose.production.yml down 2>/dev/null || true
rsync -av --delete --exclude='*.db' --exclude='uploads/' "$RELEASES_DIR/$PREV/frontend/" "$PROD_DIR/frontend/"
rsync -av --delete --exclude='*.db' --exclude='uploads/' "$RELEASES_DIR/$PREV/backend/" "$PROD_DIR/backend/"
docker compose -f docker-compose.production.yml build
docker compose -f docker-compose.production.yml up -d
echo "Rolled back to: $PREV"
;;
rollback-to)
[ -d "$RELEASES_DIR/$2" ] || { echo "Release not found"; ls "$RELEASES_DIR"; exit 1; }
cd "$PROD_DIR" && docker compose -f docker-compose.production.yml down 2>/dev/null || true
rsync -av --delete --exclude='*.db' --exclude='uploads/' "$RELEASES_DIR/$2/frontend/" "$PROD_DIR/frontend/"
rsync -av --delete --exclude='*.db' --exclude='uploads/' "$RELEASES_DIR/$2/backend/" "$PROD_DIR/backend/"
docker compose -f docker-compose.production.yml build
docker compose -f docker-compose.production.yml up -d
echo "$2" > "$PROD_DIR/.current_release"
echo "Rolled back to: $2"
;;
status)
echo "=== Staging ===" && docker compose -f "$STAGING_DIR/docker-compose.staging.yml" ps 2>/dev/null || echo "Not running"
echo "=== Production ===" && docker compose -f "$PROD_DIR/docker-compose.production.yml" ps 2>/dev/null || echo "Not running"
echo "=== Releases ===" && ls -1 "$RELEASES_DIR" 2>/dev/null | tail -5 || echo "None"
;;
cleanup)
cd "$RELEASES_DIR" && ls -1 | head -n -10 | xargs -r rm -rf
echo "Cleaned up old releases"
;;
*) echo "Usage: deploy.sh {promote|rollback|rollback-to <ts>|status|cleanup}" ;;
esac
DEPLOY_SCRIPT
chmod +x /opt/autonet/scripts/deploy.sh
echo " Created deploy.sh"
# 4. Setup post-receive hook
echo ""
echo "[4/6] Setting up Git post-receive hook..."
cat > /opt/autonet/git/autonet.git/hooks/post-receive << 'HOOK_SCRIPT'
#!/bin/bash
STAGING_DIR="/opt/autonet/staging"
GIT_DIR="/opt/autonet/git/autonet.git"
SCRIPTS_DIR="/opt/autonet/scripts"
echo ""
echo "========================================"
echo " AutonetSellCar - Deploying to Staging"
echo "========================================"
while read oldrev newrev refname; do
branch=$(echo $refname | sed 's/refs\/heads\///')
if [ "$branch" = "main" ] || [ "$branch" = "master" ]; then
git --work-tree="$STAGING_DIR" --git-dir="$GIT_DIR" checkout -f "$branch"
"$SCRIPTS_DIR/deploy-staging.sh"
echo ""
echo "Staging ready at:"
echo " Frontend: http://192.168.0.202:3001"
echo " Backend: http://192.168.0.202:8001/docs"
echo ""
echo "To deploy to production:"
echo " /opt/autonet/scripts/deploy.sh promote"
fi
done
HOOK_SCRIPT
chmod +x /opt/autonet/git/autonet.git/hooks/post-receive
echo " Created post-receive hook"
# 5. Check dependencies
echo ""
echo "[5/6] Checking dependencies..."
if command -v docker &> /dev/null; then
echo " Docker: $(docker --version)"
else
echo " WARNING: Docker not found! Please install Docker."
fi
if command -v docker compose &> /dev/null; then
echo " Docker Compose: $(docker compose version)"
else
echo " WARNING: Docker Compose not found! Please install Docker Compose."
fi
if command -v git &> /dev/null; then
echo " Git: $(git --version)"
else
echo " WARNING: Git not found! Please install Git."
fi
if command -v curl &> /dev/null; then
echo " curl: installed"
else
echo " WARNING: curl not found! Please install curl."
fi
if command -v rsync &> /dev/null; then
echo " rsync: installed"
else
echo " WARNING: rsync not found! Installing..."
sudo apt-get update && sudo apt-get install -y rsync
fi
# 6. Print summary
echo ""
echo "[6/6] Setup complete!"
echo ""
echo "========================================"
echo " Setup Summary"
echo "========================================"
echo ""
echo "Directory structure:"
echo " /opt/autonet/"
echo " ├── git/autonet.git/ <- Push code here"
echo " ├── staging/ <- Staging environment"
echo " ├── production/ <- Production environment"
echo " ├── releases/ <- Rollback archives"
echo " ├── scripts/ <- Deployment scripts"
echo " └── logs/ <- Deployment logs"
echo ""
echo "Next steps on development server (server4):"
echo ""
echo " 1. Add remote:"
echo " git remote add staging ssh://$DEPLOY_USER@192.168.0.202/opt/autonet/git/autonet.git"
echo ""
echo " 2. Push code to staging:"
echo " git push staging main"
echo ""
echo " 3. Test at http://192.168.0.202:3001"
echo ""
echo " 4. Promote to production:"
echo " ssh $DEPLOY_USER@192.168.0.202 '/opt/autonet/scripts/deploy.sh promote'"
echo ""
echo "========================================"