Spaces:
Build error
Build error
| # Script to update Python requirements on all EC2 instances | |
| # | |
| # Usage: | |
| # .\update-requirements.ps1 # Updates requirements.txt and installs packages | |
| Write-Host "Updating Python requirements on all instances..." -ForegroundColor Green | |
| # 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 | |
| # Generate a temporary SSH key for this session | |
| Write-Host "Generating temporary SSH key..." -ForegroundColor Cyan | |
| ssh-keygen -t rsa -f temp_req_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 requirements 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_req_key.pub | |
| if ($LASTEXITCODE -eq 0) { | |
| # Copy requirements.txt | |
| Write-Host "Copying requirements.txt to $ip..." | |
| scp -i temp_req_key -o StrictHostKeyChecking=no ".\requirements.txt" ec2-user@${ip}:/home/ec2-user/AWS_Portfolio_Manager/requirements.txt | |
| if ($LASTEXITCODE -eq 0) { | |
| Write-Host "Installing Python packages on $ip..." | |
| ssh -i temp_req_key -o StrictHostKeyChecking=no ec2-user@$ip "cd /home/ec2-user/AWS_Portfolio_Manager && ./venv/bin/pip install -r requirements.txt" | |
| if ($LASTEXITCODE -eq 0) { | |
| Write-Host "Requirements updated successfully on $ip" -ForegroundColor Green | |
| # Restart Streamlit service | |
| Write-Host "Restarting Streamlit service on $ip..." | |
| ssh -i temp_req_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 install packages on $ip" -ForegroundColor Red | |
| } | |
| } else { | |
| Write-Host "Failed to copy requirements.txt to $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_req_key, temp_req_key.pub -ErrorAction SilentlyContinue | |
| Write-Host "All instances updated with new requirements!" -ForegroundColor Green | |