|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Write-Host "Updating .streamlit configuration on all instances..." -ForegroundColor Green |
|
|
|
|
|
|
|
|
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-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 } |
|
|
|
|
|
|
|
|
Write-Host "Generating temporary SSH key..." -ForegroundColor Cyan |
|
|
ssh-keygen -t rsa -f temp_streamlit_key -N '""' -q |
|
|
|
|
|
$instanceIdArray = $instanceIds -split "`t" |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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) { |
|
|
|
|
|
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) { |
|
|
|
|
|
Write-Host "Copying .streamlit directory contents to $ip..." |
|
|
$copySuccess = $true |
|
|
Get-ChildItem ".\.streamlit" | ForEach-Object { |
|
|
Write-Host " Copying $($_.Name)..." |
|
|
|
|
|
scp -i temp_streamlit_key -o StrictHostKeyChecking=no "$($_.FullName)" ec2-user@${ip}:/tmp/ |
|
|
if ($LASTEXITCODE -eq 0) { |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
} |
|
|
|
|
|
|
|
|
Remove-Item temp_streamlit_key, temp_streamlit_key.pub -ErrorAction SilentlyContinue |
|
|
|
|
|
Write-Host "All instances updated with .streamlit configuration!" -ForegroundColor Green |
|
|
|