File size: 5,083 Bytes
f2f92fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Script to update .streamlit directory and configuration files on all EC2 instances
#
# Usage:
#   .\update-streamlit-config.ps1    # Deploys entire .streamlit directory

Write-Host "Updating .streamlit configuration on all instances..." -ForegroundColor Green

# Check if .streamlit directory exists locally
if (-not (Test-Path ".\.streamlit")) {
    Write-Host "Error: .streamlit directory not found!" -ForegroundColor Red
    Write-Host "Please ensure you have a .streamlit directory in the current folder." -ForegroundColor Yellow
    exit 1
}

# Get instance IPs and IDs
. .\get-ips.ps1
$instanceIds = aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names portfolio-manager-asg --query 'AutoScalingGroups[0].Instances[?HealthStatus==`Healthy`].InstanceId' --output text

if ($global:instances.Count -eq 0) {
    Write-Host "No instances found" -ForegroundColor Red
    exit 1
}

Write-Host "Found $($global:instances.Count) instances: $($global:instances -join ', ')" -ForegroundColor Green
Write-Host "Local .streamlit directory contents:" -ForegroundColor Cyan
Get-ChildItem ".\.streamlit" | ForEach-Object { Write-Host "  $($_.Name)" -ForegroundColor White }

# Generate a temporary SSH key for this session
Write-Host "Generating temporary SSH key..." -ForegroundColor Cyan
ssh-keygen -t rsa -f temp_streamlit_key -N '""' -q

$instanceIdArray = $instanceIds -split "`t"

# Update each instance one at a time
for ($i = 0; $i -lt $global:instances.Count; $i++) {
    $ip = $global:instances[$i]
    $instanceId = $instanceIdArray[$i]
    
    Write-Host "Updating .streamlit config on instance $instanceId ($ip)..." -ForegroundColor Yellow
    
    # Send SSH public key via EC2 Instance Connect
    Write-Host "Sending SSH key to $instanceId..."
    aws ec2-instance-connect send-ssh-public-key --instance-id $instanceId --instance-os-user ec2-user --ssh-public-key file://temp_streamlit_key.pub
    
    if ($LASTEXITCODE -eq 0) {
        # Create .streamlit directory on remote instance with proper ownership
        Write-Host "Creating .streamlit directory on $ip..."
        ssh -i temp_streamlit_key -o StrictHostKeyChecking=no ec2-user@$ip "mkdir -p /home/ec2-user/AWS_Portfolio_Manager/.streamlit && chmod 755 /home/ec2-user/AWS_Portfolio_Manager/.streamlit"
        
        if ($LASTEXITCODE -eq 0) {
            # Copy each file to temp location first, then move to final destination
            Write-Host "Copying .streamlit directory contents to $ip..."
            $copySuccess = $true
            Get-ChildItem ".\.streamlit" | ForEach-Object {
                Write-Host "  Copying $($_.Name)..."
                # Copy to temp location first
                scp -i temp_streamlit_key -o StrictHostKeyChecking=no "$($_.FullName)" ec2-user@${ip}:/tmp/
                if ($LASTEXITCODE -eq 0) {
                    # Move from temp to final location with proper permissions
                    ssh -i temp_streamlit_key -o StrictHostKeyChecking=no ec2-user@$ip "mv /tmp/$($_.Name) /home/ec2-user/AWS_Portfolio_Manager/.streamlit/ && chmod 644 /home/ec2-user/AWS_Portfolio_Manager/.streamlit/$($_.Name)"
                    if ($LASTEXITCODE -ne 0) {
                        $copySuccess = $false
                        Write-Host "  Failed to move $($_.Name) to final location" -ForegroundColor Red
                    }
                } else {
                    $copySuccess = $false
                    Write-Host "  Failed to copy $($_.Name) to temp location" -ForegroundColor Red
                }
            }
            
            if ($copySuccess) {
                Write-Host ".streamlit directory updated successfully on $ip" -ForegroundColor Green
                
                # Restart Streamlit service to pick up new configuration
                Write-Host "Restarting Streamlit service on $ip..."
                ssh -i temp_streamlit_key -o StrictHostKeyChecking=no ec2-user@$ip "pkill -f 'streamlit run' && cd /home/ec2-user/AWS_Portfolio_Manager && nohup ./venv/bin/streamlit run application.py --server.port 5000 --server.address 0.0.0.0 > /dev/null 2>&1 &"
                
                if ($LASTEXITCODE -eq 0) {
                    Write-Host "Service restarted successfully on $ip" -ForegroundColor Green
                } else {
                    Write-Host "Failed to restart service on $ip" -ForegroundColor Red
                }
            } else {
                Write-Host "Failed to copy .streamlit directory to $ip" -ForegroundColor Red
            }
        } else {
            Write-Host "Failed to create .streamlit directory on $ip" -ForegroundColor Red
        }
    } else {
        Write-Host "Failed to send SSH key to $instanceId" -ForegroundColor Red
    }
    
    Write-Host "Waiting 3 seconds before next instance..." -ForegroundColor Cyan
    Start-Sleep -Seconds 3
}

# Clean up temporary key files
Remove-Item temp_streamlit_key, temp_streamlit_key.pub -ErrorAction SilentlyContinue

Write-Host "All instances updated with .streamlit configuration!" -ForegroundColor Green