# 🚀 COPILOT CLI+ Complete Bootstrap Script

Complete automated setup of the entire Copilot CLI+ environment with DeepSeek integration.

## 📋 Overview

This script will **rebuild the entire setup from scratch** on any Ubuntu/Debian system.

Includes:
- ✅ System dependencies
- ✅ Python packages
- ✅ API Server (FastAPI)
- ✅ DeepSeek integration
- ✅ MCP Server bridge
- ✅ Systemd services
- ✅ SSH configuration
- ✅ Sudo safety rules
- ✅ Full automated tests

## 🚀 Quick Start

### On Target System:

```bash
# Download bootstrap script
wget https://your-host.com/bootstrap.sh -O bootstrap.sh
chmod +x bootstrap.sh

# Run with DeepSeek API key
./bootstrap.sh "sk-your-deepseek-api-key-here"

# Optional flags
./bootstrap.sh "sk-..." --verbose        # Detailed output
./bootstrap.sh "sk-..." --skip-tests     # Skip integration tests
```

## 📥 Restore from Backup

If you need to restore on a new system:

```bash
# 1. Copy bootstrap script
scp bootstrap.sh user@newhost:/tmp/

# 2. Run on new host
ssh user@newhost "cd /tmp && bash bootstrap.sh 'sk-your-key'"

# 3. Verify
ssh user@newhost "curl http://localhost:8888/health"
```

## 🔧 What Gets Installed

### System-level:
- `python3`, `pip`, `curl`, `wget`, `git`
- `ansible`, `sudo`
- `openssh-server`, `openssh-client`
- `systemd`

### Python packages:
- `fastapi`, `uvicorn`, `pydantic`, `requests`

### Local components:
- `/opt/local-agent/bin/api-server.py` - REST API
- `/opt/local-agent/bin/deepseek-api.py` - DeepSeek wrapper
- `/opt/local-agent/bin/mcp-server.py` - Copilot CLI bridge
- `/opt/local-agent/config/` - Configuration files
- Systemd services for automation

### Services started:
- `local-agent-api` (port 8888)
- `ollama` (port 11434)
- `local-agent` (daemon)
- `ssh` (port 22)

## ✅ Features

### Safety:
- ✅ Dangerous command blocking
- ✅ Sudo restrictions
- ✅ Command timeouts
- ✅ Comprehensive logging

### Integration:
- ✅ DeepSeek API (Polish/English)
- ✅ Shell command execution
- ✅ Ansible automation
- ✅ Service monitoring

### Automation:
- ✅ Automatic service restart
- ✅ Health checks
- ✅ Audit logging
- ✅ Recovery scripts

## 📊 API Endpoints (After Setup)

```bash
# Health check
curl http://localhost:8888/health

# Execute shell
curl -X POST http://localhost:8888/execute \
  -H "Content-Type: application/json" \
  -d '{"command":"whoami"}'

# Query DeepSeek
curl -X POST http://localhost:8888/deepseek \
  -H "Content-Type: application/json" \
  -d '{"query":"Cześć!"}'

# Service status
curl http://localhost:8888/services

# API docs
curl http://localhost:8888/docs
```

## 🔐 API Key Management

The bootstrap script:
1. Takes DeepSeek API key as argument
2. Stores it securely in systemd service
3. Never logs the key value
4. Makes it available to API server

To rotate the key:

```bash
# Edit systemd service
sudo nano /etc/systemd/system/local-agent-api.service

# Update DEEPSEEK_API_KEY variable
# Then reload and restart
sudo systemctl daemon-reload
sudo systemctl restart local-agent-api
```

## 📝 Logs

After setup, check logs in:

```bash
tail -f /opt/local-agent/logs/api.log      # API activity
tail -f /opt/local-agent/logs/agent.log    # Command execution
```

## 🧪 Testing

Bootstrap automatically runs tests:

```bash
✓ API health check
✓ Services endpoint
✓ DeepSeek integration
```

To skip tests during bootstrap:

```bash
./bootstrap.sh "sk-..." --skip-tests
```

## 🆘 Troubleshooting

### Script fails with "Command not found"
- Ensure running on Ubuntu/Debian
- Check internet connection
- May need root/sudo access

### API not responding after setup
```bash
# Check service status
systemctl status local-agent-api

# Check logs
journalctl -u local-agent-api -f

# Restart
systemctl restart local-agent-api
```

### DeepSeek queries fail
```bash
# Verify API key
echo $DEEPSEEK_API_KEY

# Check service environment
systemctl show local-agent-api -p Environment

# Test directly
python3 /opt/local-agent/bin/deepseek-api.py "Test"
```

### MCP not connecting in Copilot CLI
```bash
# Verify MCP config exists
cat ~/.copilot/mcp-config.json

# Test MCP server manually
python3 /opt/local-agent/bin/mcp-server.py < /dev/null

# Restart Copilot CLI
copilot --restart
```

## 🔄 Disaster Recovery

If you lose the host, to fully restore:

1. **Get the bootstrap script**
   ```bash
   # From backup/git
   git clone <your-backup> && cd repo
   ```

2. **Run on new system**
   ```bash
   chmod +x bootstrap.sh
   ./bootstrap.sh "sk-your-api-key"
   ```

3. **Verify**
   ```bash
   curl http://localhost:8888/health
   copilot
   /mcp
   ```

4. **(Optional) Copy custom playbooks**
   ```bash
   scp user@oldhost:/opt/local-agent/config/playbook.yml \
       /opt/local-agent/config/
   ```

## 📦 Backup Recommendations

To prepare for disaster recovery:

```bash
# 1. Backup bootstrap script
cp /opt/local-agent/bootstrap.sh ~/bootstrap-backup.sh

# 2. Backup custom configs
tar -czf ~/agent-config-backup.tar.gz /opt/local-agent/config/

# 3. Backup documentation
cp /opt/local-agent/*.md ~/agent-docs/

# 4. Store API key securely (NOT in repo)
echo "DEEPSEEK_API_KEY=sk-..." > ~/.deepseek-key-backup

# 5. Upload to secure location (git, S3, etc)
```

To restore from backup:

```bash
# 1. Extract bootstrap
cp bootstrap-backup.sh /tmp/ && chmod +x /tmp/bootstrap-backup.sh

# 2. Run bootstrap (API key from secure storage)
/tmp/bootstrap-backup.sh "$(cat ~/.deepseek-key-backup | grep -oP 'sk-.*')"

# 3. Restore custom configs
tar -xzf agent-config-backup.tar.gz -C /

# 4. Restart services
systemctl restart local-agent-api
```

## 🎯 Next Steps After Bootstrap

1. Start Copilot CLI
   ```bash
   copilot
   ```

2. Enable MCP integration
   ```
   /mcp
   ```
   Select: `local-agent`

3. Test integration
   ```
   "Tell me what services are running"
   "Execute: whoami"
   "Ask DeepSeek: Cześć!"
   ```

4. Deploy custom automation
   ```
   # Edit playbook
   nano /opt/local-agent/config/playbook.yml
   
   # Use in Copilot
   "Deploy using Ansible"
   ```

## 📞 Support

All configuration and logs in:
- `/opt/local-agent/` - Main agent directory
- `/opt/local-agent/logs/` - All activity logs
- `~/.copilot/mcp-config.json` - MCP configuration
- `/etc/systemd/system/local-agent*.service` - Service configs

Check logs for detailed diagnostics:
```bash
journalctl -u local-agent-api -n 50
journalctl -u ollama -n 50
tail -50 /opt/local-agent/logs/*.log
```

---

**Script Version:** 1.0  
**Last Updated:** 2026-03-25  
**Compatible:** Ubuntu 20.04 LTS+, Debian 11+
