James McCool commited on
Commit
f2f92fc
·
1 Parent(s): e8c6244

adding some pointless scripts

Browse files
update-pm-files.ps1 CHANGED
@@ -1,16 +1,20 @@
1
  # Simple script to update files on all instances using EC2 Instance Connect
2
  #
3
  # Usage Examples:
4
- # .\update-exposure-spread.ps1 # Updates exposure_spread.py (default)
5
- # .\update-exposure-spread.ps1 -FileName "predict_dupes.py" # Updates predict_dupes.py
6
- # .\update-exposure-spread.ps1 -FileName "app.py" -TargetPath "/home/ec2-user/AWS_Portfolio_Manager/" # Updates app.py in root directory
 
7
  #
8
  param(
9
  [Parameter(Mandatory=$false)]
10
  [string]$FileName = "exposure_spread.py",
11
 
12
  [Parameter(Mandatory=$false)]
13
- [string]$TargetPath = "/home/ec2-user/AWS_Portfolio_Manager/global_func/"
 
 
 
14
  )
15
 
16
  # Handle special case: app.py maps to application.py on the remote instances
@@ -82,8 +86,19 @@ for ($i = 0; $i -lt $global:instances.Count; $i++) {
82
  if ($LASTEXITCODE -eq 0) {
83
  Write-Host "$remoteFileName copied successfully to $ip" -ForegroundColor Green
84
 
85
- # Only restart Streamlit if we're updating a Python file
86
- if ($remoteFileName -like "*.py") {
 
 
 
 
 
 
 
 
 
 
 
87
  Write-Host "Restarting Streamlit service on $ip..."
88
  ssh -i temp_update_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 &"
89
 
@@ -93,7 +108,7 @@ for ($i = 0; $i -lt $global:instances.Count; $i++) {
93
  Write-Host "Failed to restart service on $ip" -ForegroundColor Red
94
  }
95
  } else {
96
- Write-Host "Non-Python file - skipping service restart" -ForegroundColor Yellow
97
  }
98
  } else {
99
  Write-Host "Failed to copy $remoteFileName to $ip" -ForegroundColor Red
@@ -102,8 +117,8 @@ for ($i = 0; $i -lt $global:instances.Count; $i++) {
102
  Write-Host "Failed to send SSH key to $instanceId" -ForegroundColor Red
103
  }
104
 
105
- Write-Host "Waiting 15 seconds before next instance..." -ForegroundColor Cyan
106
- Start-Sleep -Seconds 15
107
  }
108
 
109
  # Clean up temporary key files
 
1
  # Simple script to update files on all instances using EC2 Instance Connect
2
  #
3
  # Usage Examples:
4
+ # .\update-pm-files.ps1 # Updates exposure_spread.py (default)
5
+ # .\update-pm-files.ps1 -FileName "predict_dupes.py" # Updates predict_dupes.py
6
+ # .\update-pm-files.ps1 -FileName "app.py" # Updates app.py and auto-restarts Streamlit
7
+ # .\update-pm-files.ps1 -FileName "predict_dupes.py" -RestartStreamlit # Updates predict_dupes.py and forces Streamlit restart
8
  #
9
  param(
10
  [Parameter(Mandatory=$false)]
11
  [string]$FileName = "exposure_spread.py",
12
 
13
  [Parameter(Mandatory=$false)]
14
+ [string]$TargetPath = "/home/ec2-user/AWS_Portfolio_Manager/global_func/",
15
+
16
+ [Parameter(Mandatory=$false)]
17
+ [switch]$RestartStreamlit
18
  )
19
 
20
  # Handle special case: app.py maps to application.py on the remote instances
 
86
  if ($LASTEXITCODE -eq 0) {
87
  Write-Host "$remoteFileName copied successfully to $ip" -ForegroundColor Green
88
 
89
+ # Determine if we should restart Streamlit
90
+ $shouldRestart = $false
91
+ if ($FileName -eq "app.py") {
92
+ # Always restart when updating app.py
93
+ $shouldRestart = $true
94
+ Write-Host "app.py detected - Streamlit will be restarted" -ForegroundColor Magenta
95
+ } elseif ($RestartStreamlit) {
96
+ # Restart if explicitly requested via parameter
97
+ $shouldRestart = $true
98
+ Write-Host "RestartStreamlit parameter specified - Streamlit will be restarted" -ForegroundColor Magenta
99
+ }
100
+
101
+ if ($shouldRestart) {
102
  Write-Host "Restarting Streamlit service on $ip..."
103
  ssh -i temp_update_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 &"
104
 
 
108
  Write-Host "Failed to restart service on $ip" -ForegroundColor Red
109
  }
110
  } else {
111
+ Write-Host "Skipping Streamlit restart (use -RestartStreamlit to force)" -ForegroundColor Yellow
112
  }
113
  } else {
114
  Write-Host "Failed to copy $remoteFileName to $ip" -ForegroundColor Red
 
117
  Write-Host "Failed to send SSH key to $instanceId" -ForegroundColor Red
118
  }
119
 
120
+ Write-Host "Waiting 3 seconds before next instance..." -ForegroundColor Cyan
121
+ Start-Sleep -Seconds 3
122
  }
123
 
124
  # Clean up temporary key files
update-requirements.ps1 ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Script to update Python requirements on all EC2 instances
2
+ #
3
+ # Usage:
4
+ # .\update-requirements.ps1 # Updates requirements.txt and installs packages
5
+
6
+ Write-Host "Updating Python requirements on all instances..." -ForegroundColor Green
7
+
8
+ # Get instance IPs and IDs
9
+ . .\get-ips.ps1
10
+ $instanceIds = aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names portfolio-manager-asg --query 'AutoScalingGroups[0].Instances[?HealthStatus==`Healthy`].InstanceId' --output text
11
+
12
+ if ($global:instances.Count -eq 0) {
13
+ Write-Host "No instances found" -ForegroundColor Red
14
+ exit 1
15
+ }
16
+
17
+ Write-Host "Found $($global:instances.Count) instances: $($global:instances -join ', ')" -ForegroundColor Green
18
+
19
+ # Generate a temporary SSH key for this session
20
+ Write-Host "Generating temporary SSH key..." -ForegroundColor Cyan
21
+ ssh-keygen -t rsa -f temp_req_key -N '""' -q
22
+
23
+ $instanceIdArray = $instanceIds -split "`t"
24
+
25
+ # Update each instance one at a time
26
+ for ($i = 0; $i -lt $global:instances.Count; $i++) {
27
+ $ip = $global:instances[$i]
28
+ $instanceId = $instanceIdArray[$i]
29
+
30
+ Write-Host "Updating requirements on instance $instanceId ($ip)..." -ForegroundColor Yellow
31
+
32
+ # Send SSH public key via EC2 Instance Connect
33
+ Write-Host "Sending SSH key to $instanceId..."
34
+ aws ec2-instance-connect send-ssh-public-key --instance-id $instanceId --instance-os-user ec2-user --ssh-public-key file://temp_req_key.pub
35
+
36
+ if ($LASTEXITCODE -eq 0) {
37
+ # Copy requirements.txt
38
+ Write-Host "Copying requirements.txt to $ip..."
39
+ scp -i temp_req_key -o StrictHostKeyChecking=no ".\requirements.txt" ec2-user@${ip}:/home/ec2-user/AWS_Portfolio_Manager/requirements.txt
40
+
41
+ if ($LASTEXITCODE -eq 0) {
42
+ Write-Host "Installing Python packages on $ip..."
43
+ 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"
44
+
45
+ if ($LASTEXITCODE -eq 0) {
46
+ Write-Host "Requirements updated successfully on $ip" -ForegroundColor Green
47
+
48
+ # Restart Streamlit service
49
+ Write-Host "Restarting Streamlit service on $ip..."
50
+ 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 &"
51
+
52
+ if ($LASTEXITCODE -eq 0) {
53
+ Write-Host "Service restarted successfully on $ip" -ForegroundColor Green
54
+ } else {
55
+ Write-Host "Failed to restart service on $ip" -ForegroundColor Red
56
+ }
57
+ } else {
58
+ Write-Host "Failed to install packages on $ip" -ForegroundColor Red
59
+ }
60
+ } else {
61
+ Write-Host "Failed to copy requirements.txt to $ip" -ForegroundColor Red
62
+ }
63
+ } else {
64
+ Write-Host "Failed to send SSH key to $instanceId" -ForegroundColor Red
65
+ }
66
+
67
+ Write-Host "Waiting 3 seconds before next instance..." -ForegroundColor Cyan
68
+ Start-Sleep -Seconds 3
69
+ }
70
+
71
+ # Clean up temporary key files
72
+ Remove-Item temp_req_key, temp_req_key.pub -ErrorAction SilentlyContinue
73
+
74
+ Write-Host "All instances updated with new requirements!" -ForegroundColor Green
update-streamlit-config.ps1 ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Script to update .streamlit directory and configuration files on all EC2 instances
2
+ #
3
+ # Usage:
4
+ # .\update-streamlit-config.ps1 # Deploys entire .streamlit directory
5
+
6
+ Write-Host "Updating .streamlit configuration on all instances..." -ForegroundColor Green
7
+
8
+ # Check if .streamlit directory exists locally
9
+ if (-not (Test-Path ".\.streamlit")) {
10
+ Write-Host "Error: .streamlit directory not found!" -ForegroundColor Red
11
+ Write-Host "Please ensure you have a .streamlit directory in the current folder." -ForegroundColor Yellow
12
+ exit 1
13
+ }
14
+
15
+ # Get instance IPs and IDs
16
+ . .\get-ips.ps1
17
+ $instanceIds = aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names portfolio-manager-asg --query 'AutoScalingGroups[0].Instances[?HealthStatus==`Healthy`].InstanceId' --output text
18
+
19
+ if ($global:instances.Count -eq 0) {
20
+ Write-Host "No instances found" -ForegroundColor Red
21
+ exit 1
22
+ }
23
+
24
+ Write-Host "Found $($global:instances.Count) instances: $($global:instances -join ', ')" -ForegroundColor Green
25
+ Write-Host "Local .streamlit directory contents:" -ForegroundColor Cyan
26
+ Get-ChildItem ".\.streamlit" | ForEach-Object { Write-Host " $($_.Name)" -ForegroundColor White }
27
+
28
+ # Generate a temporary SSH key for this session
29
+ Write-Host "Generating temporary SSH key..." -ForegroundColor Cyan
30
+ ssh-keygen -t rsa -f temp_streamlit_key -N '""' -q
31
+
32
+ $instanceIdArray = $instanceIds -split "`t"
33
+
34
+ # Update each instance one at a time
35
+ for ($i = 0; $i -lt $global:instances.Count; $i++) {
36
+ $ip = $global:instances[$i]
37
+ $instanceId = $instanceIdArray[$i]
38
+
39
+ Write-Host "Updating .streamlit config on instance $instanceId ($ip)..." -ForegroundColor Yellow
40
+
41
+ # Send SSH public key via EC2 Instance Connect
42
+ Write-Host "Sending SSH key to $instanceId..."
43
+ aws ec2-instance-connect send-ssh-public-key --instance-id $instanceId --instance-os-user ec2-user --ssh-public-key file://temp_streamlit_key.pub
44
+
45
+ if ($LASTEXITCODE -eq 0) {
46
+ # Create .streamlit directory on remote instance with proper ownership
47
+ Write-Host "Creating .streamlit directory on $ip..."
48
+ 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"
49
+
50
+ if ($LASTEXITCODE -eq 0) {
51
+ # Copy each file to temp location first, then move to final destination
52
+ Write-Host "Copying .streamlit directory contents to $ip..."
53
+ $copySuccess = $true
54
+ Get-ChildItem ".\.streamlit" | ForEach-Object {
55
+ Write-Host " Copying $($_.Name)..."
56
+ # Copy to temp location first
57
+ scp -i temp_streamlit_key -o StrictHostKeyChecking=no "$($_.FullName)" ec2-user@${ip}:/tmp/
58
+ if ($LASTEXITCODE -eq 0) {
59
+ # Move from temp to final location with proper permissions
60
+ 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)"
61
+ if ($LASTEXITCODE -ne 0) {
62
+ $copySuccess = $false
63
+ Write-Host " Failed to move $($_.Name) to final location" -ForegroundColor Red
64
+ }
65
+ } else {
66
+ $copySuccess = $false
67
+ Write-Host " Failed to copy $($_.Name) to temp location" -ForegroundColor Red
68
+ }
69
+ }
70
+
71
+ if ($copySuccess) {
72
+ Write-Host ".streamlit directory updated successfully on $ip" -ForegroundColor Green
73
+
74
+ # Restart Streamlit service to pick up new configuration
75
+ Write-Host "Restarting Streamlit service on $ip..."
76
+ 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 &"
77
+
78
+ if ($LASTEXITCODE -eq 0) {
79
+ Write-Host "Service restarted successfully on $ip" -ForegroundColor Green
80
+ } else {
81
+ Write-Host "Failed to restart service on $ip" -ForegroundColor Red
82
+ }
83
+ } else {
84
+ Write-Host "Failed to copy .streamlit directory to $ip" -ForegroundColor Red
85
+ }
86
+ } else {
87
+ Write-Host "Failed to create .streamlit directory on $ip" -ForegroundColor Red
88
+ }
89
+ } else {
90
+ Write-Host "Failed to send SSH key to $instanceId" -ForegroundColor Red
91
+ }
92
+
93
+ Write-Host "Waiting 3 seconds before next instance..." -ForegroundColor Cyan
94
+ Start-Sleep -Seconds 3
95
+ }
96
+
97
+ # Clean up temporary key files
98
+ Remove-Item temp_streamlit_key, temp_streamlit_key.pub -ErrorAction SilentlyContinue
99
+
100
+ Write-Host "All instances updated with .streamlit configuration!" -ForegroundColor Green