File size: 2,425 Bytes
c3e002b |
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 |
# Update Launch Template to pull from Git
# Define the user data bash script
$userData = @'
#!/bin/bash
cd /home/ec2-user
# Clone repo if it doesn't exist, otherwise pull latest
if [ ! -d "AWS_Portfolio_Manager" ]; then
git clone https://huggingface.co/spaces/Multichem-PD/DFS_Portfolio_Manager AWS_Portfolio_Manager
cd AWS_Portfolio_Manager
python3 -m venv venv
mkdir -p .streamlit
else
cd AWS_Portfolio_Manager
git pull origin main
fi
# Install/update packages
source venv/bin/activate
pip install -r requirements.txt
# Restart Streamlit
sudo supervisorctl restart streamlit
'@
# Encode to base64 for AWS
$userDataBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($userData))
Write-Host "Creating new launch template version..." -ForegroundColor Cyan
# Create properly formatted JSON file with ASCII encoding (no BOM)
$jsonContent = "{`"UserData`": `"$userDataBase64`"}"
[System.IO.File]::WriteAllText("$PWD\launch-template-data.json", $jsonContent, [System.Text.Encoding]::ASCII)
# Create new launch template version
aws ec2 create-launch-template-version `
--launch-template-name portfolio-manager-template `
--source-version '$Latest' `
--launch-template-data file://launch-template-data.json
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to create launch template version" -ForegroundColor Red
Remove-Item "launch-template-data.json" -ErrorAction SilentlyContinue
exit 1
}
# Get the latest version number
$latestVersion = aws ec2 describe-launch-template-versions `
--launch-template-name portfolio-manager-template `
--query 'LaunchTemplateVersions[0].VersionNumber' `
--output text
Write-Host "Setting version $latestVersion as default..." -ForegroundColor Cyan
# Set the new version as default
aws ec2 modify-launch-template `
--launch-template-name portfolio-manager-template `
--default-version $latestVersion
# Clean up temp file
Remove-Item "launch-template-data.json" -ErrorAction SilentlyContinue
if ($LASTEXITCODE -eq 0) {
Write-Host "Success! Launch template updated to version $latestVersion" -ForegroundColor Green
Write-Host "New instances will automatically pull code from Git" -ForegroundColor Green
Write-Host "Don't forget to add secrets.toml to new instances manually!" -ForegroundColor Yellow
} else {
Write-Host "Failed to set default version" -ForegroundColor Red
exit 1
} |