File size: 5,567 Bytes
34dcea4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<# 
    deploy.ps1 β€” One-command Faraday Cloud deployment.
    
    Usage:
        .\deploy.ps1 push     # Upload data to Supabase only
        .\deploy.ps1 deploy   # Push data + deploy to Cloud Run
        .\deploy.ps1 full     # Push + build + deploy + test
    
    Prerequisites:
        - Python venv activated with supabase installed
        - gcloud CLI authenticated (for Cloud Run deployment)
#>

param(
    [Parameter(Position=0)]
    [ValidateSet("push", "deploy", "full")]
    [string]$Action = "full"
)

$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $PSScriptRoot
$AiMemoryDir = $PSScriptRoot

# ─────────────────────────────────────────────────────
# Config
# ─────────────────────────────────────────────────────
$GCP_PROJECT = "faraday-memory-cloud"
$GCP_REGION = "asia-south1"  # Mumbai β€” closest to India
$SERVICE_NAME = "faraday-mcp"
$FARADAY_API_KEY = "frdy_" + [System.Guid]::NewGuid().ToString("N").Substring(0, 24)

# ─────────────────────────────────────────────────────
# Step 1: Push data to Supabase
# ─────────────────────────────────────────────────────
function Push-Data {
    Write-Host "`nπŸ“¦ Pushing data to Supabase Storage..." -ForegroundColor Cyan
    
    $venvPython = Join-Path $ProjectRoot ".venv\Scripts\python.exe"
    if (Test-Path $venvPython) {
        & $venvPython (Join-Path $AiMemoryDir "sync.py") push
    } else {
        python (Join-Path $AiMemoryDir "sync.py") push
    }
    
    if ($LASTEXITCODE -ne 0) {
        Write-Host "❌ Data push failed!" -ForegroundColor Red
        exit 1
    }
    Write-Host "βœ… Data pushed to Supabase." -ForegroundColor Green
}

# ─────────────────────────────────────────────────────
# Step 2: Deploy to Cloud Run
# ─────────────────────────────────────────────────────
function Deploy-CloudRun {
    Write-Host "`nπŸš€ Deploying to Google Cloud Run..." -ForegroundColor Cyan
    
    # Check gcloud auth
    $account = gcloud auth list --filter="status=ACTIVE" --format="value(account)" 2>$null
    if (-not $account) {
        Write-Host "⚠️ Not authenticated with gcloud. Running 'gcloud auth login'..." -ForegroundColor Yellow
        gcloud auth login
    }
    
    # Set project
    gcloud config set project $GCP_PROJECT 2>$null
    
    # Enable required APIs
    Write-Host "  Enabling Cloud Run API..." -ForegroundColor Gray
    gcloud services enable run.googleapis.com artifactregistry.googleapis.com cloudbuild.googleapis.com 2>$null
    
    # Deploy from source (Cloud Build handles Dockerfile)
    Write-Host "  Building and deploying container..." -ForegroundColor Gray
    gcloud run deploy $SERVICE_NAME `
        --source $AiMemoryDir `
        --region $GCP_REGION `
        --platform managed `
        --allow-unauthenticated `
        --memory 2Gi `
        --cpu 1 `
        --min-instances 0 `
        --max-instances 2 `
        --timeout 300 `
        --set-env-vars "SUPABASE_URL=https://qwxagrmoryojholseclm.supabase.co" `
        --set-env-vars "SUPABASE_KEY=$env:SUPABASE_KEY" `
        --set-env-vars "FARADAY_API_KEY=$FARADAY_API_KEY" `
        --set-env-vars "SUPABASE_BUCKET=faraday-memory"
    
    if ($LASTEXITCODE -ne 0) {
        Write-Host "❌ Cloud Run deployment failed!" -ForegroundColor Red
        exit 1
    }
    
    # Get service URL
    $serviceUrl = gcloud run services describe $SERVICE_NAME --region $GCP_REGION --format="value(status.url)"
    
    Write-Host "`n" -NoNewline
    Write-Host "═══════════════════════════════════════════════════" -ForegroundColor Green
    Write-Host "  βœ… DEPLOYMENT COMPLETE!" -ForegroundColor Green
    Write-Host "═══════════════════════════════════════════════════" -ForegroundColor Green
    Write-Host "`n  Service URL:  $serviceUrl" -ForegroundColor White
    Write-Host "  SSE Endpoint: $serviceUrl/sse" -ForegroundColor White
    Write-Host "  API Key:      $FARADAY_API_KEY" -ForegroundColor Yellow
    Write-Host "`n  πŸ“± Claude Phone Config:" -ForegroundColor Cyan
    Write-Host @"
  {
    "mcpServers": {
      "faraday": {
        "command": "npx",
        "args": [
          "-y", "mcp-remote",
          "$serviceUrl/sse"
        ]
      }
    }
  }
"@ -ForegroundColor Gray
    Write-Host ""
}

# ─────────────────────────────────────────────────────
# Execute
# ─────────────────────────────────────────────────────
switch ($Action) {
    "push" {
        Push-Data
    }
    "deploy" {
        Deploy-CloudRun
    }
    "full" {
        Push-Data
        Deploy-CloudRun
    }
}