Spaces:
Sleeping
Sleeping
James McCool
commited on
Commit
·
ef5050a
1
Parent(s):
c3e002b
Add in config file.
Browse files
.streamlit/config.toml
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[server]
|
| 2 |
+
maxUploadSize = 500
|
| 3 |
+
|
| 4 |
+
[browser]
|
| 5 |
+
gatherUsageStats = false
|
setup-supervisor-all-instances.ps1
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Fixed script for Amazon Linux 2023
|
| 2 |
+
$setupScript = @'
|
| 3 |
+
#!/bin/bash
|
| 4 |
+
cd /home/ec2-user/AWS_Portfolio_Manager
|
| 5 |
+
|
| 6 |
+
# Install supervisor via pip
|
| 7 |
+
sudo pip3 install supervisor --break-system-packages
|
| 8 |
+
|
| 9 |
+
# Create supervisor directories
|
| 10 |
+
sudo mkdir -p /etc/supervisor/conf.d
|
| 11 |
+
sudo mkdir -p /var/log/supervisor
|
| 12 |
+
|
| 13 |
+
# Create main supervisor config
|
| 14 |
+
sudo tee /etc/supervisord.conf > /dev/null <<'MAINEOF'
|
| 15 |
+
[unix_http_server]
|
| 16 |
+
file=/tmp/supervisor.sock
|
| 17 |
+
|
| 18 |
+
[supervisord]
|
| 19 |
+
logfile=/var/log/supervisor/supervisord.log
|
| 20 |
+
logfile_maxbytes=50MB
|
| 21 |
+
logfile_backups=10
|
| 22 |
+
loglevel=info
|
| 23 |
+
pidfile=/tmp/supervisord.pid
|
| 24 |
+
nodaemon=false
|
| 25 |
+
minfds=1024
|
| 26 |
+
minprocs=200
|
| 27 |
+
|
| 28 |
+
[rpcinterface:supervisor]
|
| 29 |
+
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
|
| 30 |
+
|
| 31 |
+
[supervisorctl]
|
| 32 |
+
serverurl=unix:///tmp/supervisor.sock
|
| 33 |
+
|
| 34 |
+
[include]
|
| 35 |
+
files = /etc/supervisor/conf.d/*.conf
|
| 36 |
+
MAINEOF
|
| 37 |
+
|
| 38 |
+
# Create systemd service for supervisor
|
| 39 |
+
sudo tee /etc/systemd/system/supervisord.service > /dev/null <<'SYSEOF'
|
| 40 |
+
[Unit]
|
| 41 |
+
Description=Supervisor process control system
|
| 42 |
+
After=network.target
|
| 43 |
+
|
| 44 |
+
[Service]
|
| 45 |
+
Type=forking
|
| 46 |
+
ExecStart=/usr/local/bin/supervisord -c /etc/supervisord.conf
|
| 47 |
+
ExecReload=/usr/local/bin/supervisorctl reload
|
| 48 |
+
ExecStop=/usr/local/bin/supervisorctl shutdown
|
| 49 |
+
Restart=on-failure
|
| 50 |
+
|
| 51 |
+
[Install]
|
| 52 |
+
WantedBy=multi-user.target
|
| 53 |
+
SYSEOF
|
| 54 |
+
|
| 55 |
+
# Enable and start supervisord
|
| 56 |
+
sudo systemctl daemon-reload
|
| 57 |
+
sudo systemctl enable supervisord
|
| 58 |
+
sudo systemctl start supervisord
|
| 59 |
+
|
| 60 |
+
# Kill any running streamlit
|
| 61 |
+
pkill -f 'streamlit run' || true
|
| 62 |
+
|
| 63 |
+
# Create supervisor config for streamlit
|
| 64 |
+
sudo tee /etc/supervisor/conf.d/streamlit.conf > /dev/null <<'EOF'
|
| 65 |
+
[program:streamlit]
|
| 66 |
+
directory=/home/ec2-user/AWS_Portfolio_Manager
|
| 67 |
+
command=/home/ec2-user/AWS_Portfolio_Manager/venv/bin/streamlit run application.py --server.port 5000 --server.address 0.0.0.0
|
| 68 |
+
user=ec2-user
|
| 69 |
+
autostart=true
|
| 70 |
+
autorestart=true
|
| 71 |
+
stderr_logfile=/var/log/streamlit.err.log
|
| 72 |
+
stdout_logfile=/var/log/streamlit.out.log
|
| 73 |
+
environment=HOME="/home/ec2-user",USER="ec2-user"
|
| 74 |
+
EOF
|
| 75 |
+
|
| 76 |
+
# Reload and start
|
| 77 |
+
sudo supervisorctl reread
|
| 78 |
+
sudo supervisorctl update
|
| 79 |
+
sudo supervisorctl start streamlit
|
| 80 |
+
'@
|
| 81 |
+
|
| 82 |
+
# Write with Unix line endings
|
| 83 |
+
[System.IO.File]::WriteAllText("$PWD\setup_supervisor.sh", $setupScript.Replace("`r`n", "`n"), [System.Text.Encoding]::ASCII)
|
| 84 |
+
|
| 85 |
+
# Now run on all instances
|
| 86 |
+
. .\get-ips.ps1
|
| 87 |
+
|
| 88 |
+
$instanceIds = aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names portfolio-manager-asg --query 'AutoScalingGroups[0].Instances[?HealthStatus==`Healthy`].InstanceId' --output text
|
| 89 |
+
$instanceIdArray = $instanceIds -split "`t"
|
| 90 |
+
|
| 91 |
+
ssh-keygen -t rsa -f temp_supervisor_key -N '""' -q
|
| 92 |
+
|
| 93 |
+
for ($i = 0; $i -lt $global:instances.Count; $i++) {
|
| 94 |
+
$ip = $global:instances[$i]
|
| 95 |
+
$instanceId = $instanceIdArray[$i]
|
| 96 |
+
|
| 97 |
+
Write-Host "Setting up supervisor on $ip..." -ForegroundColor Yellow
|
| 98 |
+
aws ec2-instance-connect send-ssh-public-key --instance-id $instanceId --instance-os-user ec2-user --ssh-public-key file://temp_supervisor_key.pub | Out-Null
|
| 99 |
+
|
| 100 |
+
# Copy script and execute
|
| 101 |
+
scp -i temp_supervisor_key -o StrictHostKeyChecking=no setup_supervisor.sh ec2-user@${ip}:/tmp/
|
| 102 |
+
ssh -i temp_supervisor_key -o StrictHostKeyChecking=no ec2-user@$ip "bash /tmp/setup_supervisor.sh"
|
| 103 |
+
|
| 104 |
+
if ($LASTEXITCODE -eq 0) {
|
| 105 |
+
Write-Host "Supervisor configured on $ip" -ForegroundColor Green
|
| 106 |
+
} else {
|
| 107 |
+
Write-Host "Check errors on $ip" -ForegroundColor Yellow
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
Remove-Item temp_supervisor_key, temp_supervisor_key.pub, setup_supervisor.sh -ErrorAction SilentlyContinue
|
| 112 |
+
|
| 113 |
+
Write-Host "`nSetup complete! Verifying..." -ForegroundColor Green
|
| 114 |
+
|
| 115 |
+
# Verify
|
| 116 |
+
foreach ($ip in $global:instances) {
|
| 117 |
+
Write-Host "`nChecking $ip..." -ForegroundColor Cyan
|
| 118 |
+
ssh -i "C:\Users\mccoo\.ssh\aws-eb" ec2-user@$ip "sudo supervisorctl status" 2>$null
|
| 119 |
+
}
|
update-launch-template-with-supervisor.ps1
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Updated user data with supervisor setup
|
| 2 |
+
$userData = @'
|
| 3 |
+
#!/bin/bash
|
| 4 |
+
cd /home/ec2-user
|
| 5 |
+
|
| 6 |
+
# Install supervisor if not already installed
|
| 7 |
+
if ! command -v supervisorctl &> /dev/null; then
|
| 8 |
+
sudo dnf install -y supervisor
|
| 9 |
+
sudo systemctl enable supervisord
|
| 10 |
+
sudo systemctl start supervisord
|
| 11 |
+
fi
|
| 12 |
+
|
| 13 |
+
# Remove old directory if it exists but isn't a git repo
|
| 14 |
+
if [ -d "AWS_Portfolio_Manager" ] && [ ! -d "AWS_Portfolio_Manager/.git" ]; then
|
| 15 |
+
sudo rm -rf AWS_Portfolio_Manager
|
| 16 |
+
fi
|
| 17 |
+
|
| 18 |
+
# Clone or pull
|
| 19 |
+
if [ ! -d "AWS_Portfolio_Manager" ]; then
|
| 20 |
+
git clone https://huggingface.co/spaces/Multichem-PD/DFS_Portfolio_Manager AWS_Portfolio_Manager
|
| 21 |
+
cd AWS_Portfolio_Manager
|
| 22 |
+
python3 -m venv venv
|
| 23 |
+
cp app.py application.py
|
| 24 |
+
mkdir -p .streamlit
|
| 25 |
+
else
|
| 26 |
+
cd AWS_Portfolio_Manager
|
| 27 |
+
git pull origin main
|
| 28 |
+
cp app.py application.py
|
| 29 |
+
fi
|
| 30 |
+
|
| 31 |
+
# Install/update packages
|
| 32 |
+
source venv/bin/activate
|
| 33 |
+
pip install -r requirements.txt
|
| 34 |
+
|
| 35 |
+
# Create supervisor config for streamlit
|
| 36 |
+
sudo tee /etc/supervisor/conf.d/streamlit.conf > /dev/null <<EOF
|
| 37 |
+
[program:streamlit]
|
| 38 |
+
directory=/home/ec2-user/AWS_Portfolio_Manager
|
| 39 |
+
command=/home/ec2-user/AWS_Portfolio_Manager/venv/bin/streamlit run application.py --server.port 5000 --server.address 0.0.0.0
|
| 40 |
+
user=ec2-user
|
| 41 |
+
autostart=true
|
| 42 |
+
autorestart=true
|
| 43 |
+
stderr_logfile=/var/log/streamlit.err.log
|
| 44 |
+
stdout_logfile=/var/log/streamlit.out.log
|
| 45 |
+
environment=HOME="/home/ec2-user",USER="ec2-user"
|
| 46 |
+
EOF
|
| 47 |
+
|
| 48 |
+
# Reload supervisor and restart streamlit
|
| 49 |
+
sudo supervisorctl reread
|
| 50 |
+
sudo supervisorctl update
|
| 51 |
+
sudo supervisorctl restart streamlit
|
| 52 |
+
'@
|
| 53 |
+
|
| 54 |
+
# Encode to base64
|
| 55 |
+
$userDataBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($userData))
|
| 56 |
+
|
| 57 |
+
# Create JSON file
|
| 58 |
+
$jsonContent = "{`"UserData`": `"$userDataBase64`"}"
|
| 59 |
+
[System.IO.File]::WriteAllText("$PWD\launch-template-data.json", $jsonContent, [System.Text.Encoding]::ASCII)
|
| 60 |
+
|
| 61 |
+
# Create new launch template version
|
| 62 |
+
aws ec2 create-launch-template-version `
|
| 63 |
+
--launch-template-name portfolio-manager-template `
|
| 64 |
+
--source-version '$Latest' `
|
| 65 |
+
--launch-template-data file://launch-template-data.json
|
| 66 |
+
|
| 67 |
+
$latestVersion = aws ec2 describe-launch-template-versions `
|
| 68 |
+
--launch-template-name portfolio-manager-template `
|
| 69 |
+
--query 'LaunchTemplateVersions[0].VersionNumber' `
|
| 70 |
+
--output text
|
| 71 |
+
|
| 72 |
+
aws ec2 modify-launch-template `
|
| 73 |
+
--launch-template-name portfolio-manager-template `
|
| 74 |
+
--default-version $latestVersion
|
| 75 |
+
|
| 76 |
+
Remove-Item "launch-template-data.json" -ErrorAction SilentlyContinue
|
| 77 |
+
|
| 78 |
+
Write-Host "Launch template updated to version $latestVersion with supervisor!" -ForegroundColor Green
|