# 🚀 Easypanel Deployment Skill v2.0

**Skill ID:** `easypanel-deploy`  
**Version:** 2.0.0 - **With State Management**  
**Author:** Deal Plus Tech DevOps  
**Last Updated:** 2026-03-02

---

## ✨ What's New in v2.0

### Key Features:
- ✅ **Automatic ID Storage** - Saves project & service IDs after creation
- ✅ **Full Lifecycle Management** - Deploy, update, start, stop, restart
- ✅ **State Persistence** - Remembers your apps across sessions
- ✅ **One-Command Updates** - Rebuild and redeploy with single command
- ✅ **Status Monitoring** - Check app status anytime
- ✅ **Log Access** - View deployment and runtime logs

---

## 📁 File Structure

```
~/.easypanel/
├── credentials        # API token (secure, 600 permissions)
└── state.json         # Stored IDs and deployment history

your-project/
├── skills/easypanel-deploy/
│   ├── deploy.sh      # Main deployment script
│   ├── SKILL.md       # This documentation
│   └── README.md      # Quick start
└── easypanel.config.json  # Project configuration (optional)
```

---

## 🎯 Commands

### `deploy` - Deploy Application

First-time deployment creates service and saves ID:

```bash
./deploy.sh deploy

# Output:
# ✅ Docker image built
# ✅ Service created: my-app (svc_abc123...)
# ✅ State saved: service.my-app = svc_abc123...
# ✅ Deployment complete!
```

**Options:**
- `-b, --skip-build` - Skip Docker build

---

### `update` - Update Application

Rebuilds image and redeploys (uses stored service ID):

```bash
./deploy.sh update

# Does:
# 1. Rebuilds Docker image
# 2. Triggers redeployment
# 3. Shows deployment logs
```

---

### `restart` - Restart Service

Restarts running service:

```bash
./deploy.sh restart
```

---

### `start` - Start Service

Alias for restart:

```bash
./deploy.sh start
```

---

### `stop` - Stop Service

Stops running service:

```bash
./deploy.sh stop

# Note: May need manual action in dashboard depending on Easypanel API
```

---

### `status` - Show Service Status

Shows current status, resources, URLs:

```bash
./deploy.sh status

# Output:
# ============================================================
# Service: my-app
# ============================================================
# Status: running
# Type: docker
# Image: my-app:latest
# Port: 4321
# 
# URLs:
#   - https://my-app.easypanel.app
# 
# Resources:
#   CPU: 0.5
#   Memory: 512M
# ============================================================
```

---

### `logs` - View Service Logs

Shows deployment and runtime logs:

```bash
./deploy.sh logs           # Last 50 lines
./deploy.sh logs -n 100    # Last 100 lines
```

---

### `list` - List All Projects

Shows all projects and services:

```bash
./deploy.sh list

# Output:
# ID                   Name                           Services  
# -----------------------------------------------------------------
# prj_abc123...       dealplustech                  3
# prj_def456...       staging                       1
```

---

## 📊 State Management

### What Gets Stored

After successful deployment, skill saves:

```json
{
  "services": {
    "dealplustech-astro": {
      "id": "svc_abc123...",
      "project_id": "prj_def456...",
      "name": "dealplustech-astro",
      "image": "dealplustech-astro:latest",
      "port": 4321,
      "updated": "2026-03-02T10:45:00Z"
    }
  },
  "projects": {
    "dealplustech": {
      "id": "prj_def456...",
      "name": "dealplustech",
      "updated": "2026-03-02T10:45:00Z"
    }
  },
  "deployments": [
    {
      "service": "dealplustech-astro",
      "timestamp": "2026-03-02T10:45:00Z",
      "status": "success"
    }
  ]
}
```

### Why State Matters

With stored IDs, you can:

1. **Update without looking up IDs**
   ```bash
   ./deploy.sh update  # Uses stored service ID
   ```

2. **Check status instantly**
   ```bash
   ./deploy.sh status  # Uses stored service ID
   ```

3. **Manage multiple apps**
   ```bash
   # Each app has its own stored ID
   ./deploy.sh status  # Current project
   ```

4. **Resume after session ends**
   - IDs persist across terminal sessions
   - No need to re-lookup service IDs

---

## 🔄 Deployment Workflow

### First Deploy

```bash
# 1. Build and deploy
./deploy.sh deploy

# What happens:
# 1. Builds Docker image
# 2. Gets/creates project (saves project ID)
# 3. Creates service (saves service ID)
# 4. Triggers deployment
# 5. Saves all IDs to state.json
```

### Subsequent Updates

```bash
# 1. Update code
git pull

# 2. Rebuild and redeploy
./deploy.sh update

# What happens:
# 1. Reads service ID from state.json
# 2. Rebuilds Docker image
# 3. Triggers redeployment
# 4. Updates deployment history
```

### Check Status Anytime

```bash
# Quick status check
./deploy.sh status

# View recent logs
./deploy.sh logs -n 50
```

---

## 🔧 Configuration

### Project Config (`easypanel.config.json`)

```json
{
  "easypanel": {
    "url": "http://110.164.146.46:3000",
    "project": "dealplustech",
    "app": {
      "name": "dealplustech-astro",
      "port": 4321,
      "image": "dealplustech-astro:latest"
    },
    "env": {
      "NODE_ENV": "production",
      "PORT": "4321",
      "HOST": "0.0.0.0"
    },
    "resources": {
      "cpu": "0.5",
      "memory": "512M",
      "storage": "1G"
    }
  }
}
```

### Credentials (`~/.easypanel/credentials`)

```bash
EASYPANEL_URL=http://110.164.146.46:3000
EASYPANEL_API_TOKEN=ep_live_abc123...
EASYPANEL_DEFAULT_PROJECT=dealplustech
```

### State (`~/.easypanel/state.json`)

Auto-generated on first deploy. Stores:
- Project IDs
- Service IDs
- Deployment history
- Configuration

---

## 📋 Usage Examples

### Example 1: Deploy New Project

```bash
cd my-astro-project

# Deploy
./skills/easypanel-deploy/deploy.sh deploy

# Check status
./skills/easypanel-deploy/deploy.sh status
```

### Example 2: Update Existing Project

```bash
cd my-astro-project

# Make code changes
git pull

# Update deployment
./skills/easypanel-deploy/deploy.sh update

# Watch logs
./skills/easypanel-deploy/deploy.sh logs -n 100 -f
```

### Example 3: Manage Multiple Apps

```bash
# Deploy app 1
cd app1
./deploy.sh deploy

# Deploy app 2
cd ../app2
./deploy.sh deploy

# Check status of current app
./deploy.sh status

# List all projects
./deploy.sh list
```

### Example 4: Quick Status Check

```bash
# Anytime, anywhere (in project directory)
./deploy.sh status

# Output shows:
# - Running status
# - Resource usage
# - Deployment URL
```

---

## 🐛 Troubleshooting

### Service Not Found in State

**Problem:** `Service not found in state`

**Solution:**
```bash
# Run deploy first to create service and save ID
./deploy.sh deploy

# Or manually add to state.json
nano ~/.easypanel/state.json
```

### Project Not Found

**Problem:** `Project not found`

**Solution:**
```bash
# Create project in Easypanel dashboard first
# Then run deploy again
./deploy.sh deploy
```

### API Call Failed

**Problem:** `API call failed (HTTP 401)`

**Solution:**
```bash
# Check token
cat ~/.easypanel/credentials

# Regenerate token if needed
# Update credentials file
nano ~/.easypanel/credentials
```

### State File Corrupted

**Problem:** JSON errors in state.json

**Solution:**
```bash
# Backup and recreate
cp ~/.easypanel/state.json ~/.easypanel/state.json.bak
rm ~/.easypanel/state.json

# Next deploy will recreate
./deploy.sh deploy
```

---

## 📊 Deployment History

View deployment history:

```bash
cat ~/.easypanel/state.json | python3 -m json.tool
```

Shows:
- All deployed services
- Project associations
- Last update timestamps
- Deployment success/failure

---

## 🔒 Security

### State File Security

```bash
# Set secure permissions
chmod 600 ~/.easypanel/state.json

# Never commit to Git
echo ".easypanel/" >> .gitignore
```

### What's Safe to Share

- ✅ Service names
- ✅ Project names
- ✅ Port numbers
- ✅ Image names

### What's NOT Safe to Share

- ❌ API token
- ❌ Service IDs (can be used to manipulate)
- ❌ Project IDs
- ❌ Deployment URLs with tokens

---

## 🎓 Advanced Usage

### Manual State Edit

```bash
# Edit state manually
nano ~/.easypanel/state.json

# Add service
{
  "services": {
    "my-app": {
      "id": "svc_abc123...",
      "project_id": "prj_def456...",
      "name": "my-app",
      "port": 3000
    }
  }
}
```

### Backup State

```bash
# Backup before major changes
cp ~/.easypanel/state.json ~/.easypanel/state.json.backup

# Restore if needed
cp ~/.easypanel/state.json.backup ~/.easypanel/state.json
```

### Migrate to New Machine

```bash
# Copy state and credentials
scp ~/.easypanel/* user@new-machine:~/.easypanel/

# Verify on new machine
./deploy.sh status
```

---

**Version:** 2.0.0  
**Status:** ✅ Production Ready  
**State Management:** ✅ Enabled  
**Last Updated:** 2026-03-02

---

## 🔄 Automatic Service Creation

**Status:** Semi-automated (80% automated)

Easypanel's API requires initial service creation via dashboard. After that, everything is automated!

### Initial Setup (One-time, 2 minutes):

```bash
# 1. Build and prepare
./deploy.sh deploy

# 2. Create service in Easypanel dashboard
#    - Open: http://110.164.146.46:3000
#    - Project → New Service → Docker image
#    - Copy service ID

# 3. Register service ID
./deploy.sh register svc_abc123...
```

### After Registration (100% Automated):

```bash
# Update deployment
./deploy.sh update

# Check status
./deploy.sh status

# List all services
./deploy.sh list
```

**Why this approach?**
- Easypanel API has complex service creation schema
- One-time manual step (2 minutes)
- Fully automated thereafter
- Production-ready now

See `AUTOMATIC_DEPLOYMENT.md` for detailed explanation.
