Scripts switching prod and dev service endpoints

A script that switches the prod and dev endpoints for the auth & relay services.
This commit is contained in:
Arnab Chakraborty 2025-01-01 13:57:01 +03:00
parent 26a9f30b0d
commit bcf82614e6
2 changed files with 276 additions and 0 deletions

163
scripts/switch_servers.ps1 Normal file
View File

@ -0,0 +1,163 @@
# Usage
# .\script.ps1 dev # Will prompt for relay server modification
# .\script.ps1 prod # Will prompt for relay server modification
# .\script.ps1 dev -r # Will automatically modify relay servers
# .\script.ps1 prod -r # Will automatically modify relay servers
# .\script.ps1 dev -s # Will skip relay server modification without prompting
# .\script.ps1 prod -s # Will skip relay server modification without prompting
# File paths
$rustFile = "core/crates/cloud-services/src/lib.rs"
$tsxFile = "interface/util/index.tsx"
$coreFile = "core/src/lib.rs"
# Function to prompt for relay servers change
function Prompt-RelayServers {
while ($true) {
$response = Read-Host "Do you want to modify relay servers as well? (y/n)"
switch ($response.ToLower()) {
'y' { return $true }
'n' { return $false }
default { Write-Host "Please answer y or n." }
}
}
}
# Check arguments
if ($args.Count -lt 1 -or $args.Count -gt 2) {
Write-Host "Usage: <script> <dev|prod> [-r|-s]"
Write-Host " -r: Automatically modify relay servers without prompting"
Write-Host " -s: Skip relay servers modification without prompting"
exit 1
}
# Check environment argument
$env = $args[0]
if ($env -notmatch '^(dev|prod)$') {
Write-Host "Invalid argument. Use 'dev' or 'prod'"
exit 1
}
# Check flags for relay server handling
$modifyRelay = $false
if ($args.Count -eq 2) {
switch ($args[1]) {
'-r' { $modifyRelay = $true }
'-s' { $modifyRelay = $false }
default {
Write-Host "Invalid flag. Use -r or -s"
exit 1
}
}
} else {
$modifyRelay = Prompt-RelayServers
}
# Function to update file content with regex
function Update-FileContent {
param (
[string]$FilePath,
[string]$Pattern,
[string]$Replacement
)
$content = Get-Content $FilePath -Raw
$content = [regex]::Replace($content, $Pattern, $Replacement)
Set-Content -Path $FilePath -Value $content -NoNewline
}
switch ($env) {
'dev' {
# Update Rust file
Update-FileContent $rustFile `
'^pub const AUTH_SERVER_URL.*' `
'// pub const AUTH_SERVER_URL: &str = "https://auth.spacedrive.com";'
Update-FileContent $rustFile `
'^// pub const AUTH_SERVER_URL.*localhost.*' `
'pub const AUTH_SERVER_URL: &str = "http://localhost:9420";'
# Update TypeScript file
Update-FileContent $tsxFile `
"^export const AUTH_SERVER_URL.*" `
"// export const AUTH_SERVER_URL = 'https://auth.spacedrive.com';"
Update-FileContent $tsxFile `
"^// export const AUTH_SERVER_URL.*localhost.*" `
"export const AUTH_SERVER_URL = 'http://localhost:9420';"
if ($modifyRelay) {
# Comment out production relay
Update-FileContent $coreFile `
'^\s*\.unwrap_or_else\(\|_\| "https://relay\.spacedrive\.com:4433/"\.to_string\(\)\)' `
'$0 // .unwrap_or_else(|_| "https://relay.spacedrive.com:4433/".to_string())'
# Uncomment development relay
Update-FileContent $coreFile `
'^\s*// \.unwrap_or_else\(\|_\| "http://localhost:8081/"\.to_string\(\)\)' `
'.unwrap_or_else(|_| "http://localhost:8081/".to_string())'
# Comment out production pkarr
Update-FileContent $coreFile `
'^\s*\.unwrap_or_else\(\|_\| "https://irohdns\.spacedrive\.com/pkarr"\.to_string\(\)\)' `
'$0 // .unwrap_or_else(|_| "https://irohdns.spacedrive.com/pkarr".to_string())'
# Uncomment development pkarr
Update-FileContent $coreFile `
'^\s*// \.unwrap_or_else\(\|_\| "http://localhost:8080/pkarr"\.to_string\(\)\)' `
'.unwrap_or_else(|_| "http://localhost:8080/pkarr".to_string())'
# Comment out production cloud domain
Update-FileContent $coreFile `
'^\s*\.unwrap_or_else\(\|_\| "cloud\.spacedrive\.com"\.to_string\(\)\)' `
'$0 // .unwrap_or_else(|_| "cloud.spacedrive.com".to_string())'
# Uncomment development cloud domain
Update-FileContent $coreFile `
'^\s*// \.unwrap_or_else\(\|_\| "localhost"\.to_string\(\)\)' `
'.unwrap_or_else(|_| "localhost".to_string())'
}
}
'prod' {
# Update Rust file
Update-FileContent $rustFile `
'^// pub const AUTH_SERVER_URL.*spacedrive.*' `
'pub const AUTH_SERVER_URL: &str = "https://auth.spacedrive.com";'
Update-FileContent $rustFile `
'^pub const AUTH_SERVER_URL.*localhost.*' `
'// pub const AUTH_SERVER_URL: &str = "http://localhost:9420";'
# Update TypeScript file
Update-FileContent $tsxFile `
"^// export const AUTH_SERVER_URL.*spacedrive.*" `
"export const AUTH_SERVER_URL = 'https://auth.spacedrive.com';"
Update-FileContent $tsxFile `
"^export const AUTH_SERVER_URL.*localhost.*" `
"// export const AUTH_SERVER_URL = 'http://localhost:9420';"
if ($modifyRelay) {
# Uncomment production relay
Update-FileContent $coreFile `
'^\s*// \.unwrap_or_else\(\|_\| "https://relay\.spacedrive\.com:4433/"\.to_string\(\)\)' `
'.unwrap_or_else(|_| "https://relay.spacedrive.com:4433/".to_string())'
# Comment out development relay
Update-FileContent $coreFile `
'^\s*\.unwrap_or_else\(\|_\| "http://localhost:8081/"\.to_string\(\)\)' `
'// .unwrap_or_else(|_| "http://localhost:8081/".to_string())'
# Uncomment production pkarr
Update-FileContent $coreFile `
'^\s*// \.unwrap_or_else\(\|_\| "https://irohdns\.spacedrive\.com/pkarr"\.to_string\(\)\)' `
'.unwrap_or_else(|_| "https://irohdns.spacedrive.com/pkarr".to_string())'
# Comment out development pkarr
Update-FileContent $coreFile `
'^\s*\.unwrap_or_else\(\|_\| "http://localhost:8080/pkarr"\.to_string\(\)\)' `
'// .unwrap_or_else(|_| "http://localhost:8080/pkarr".to_string())'
# Uncomment production cloud domain
Update-FileContent $coreFile `
'^\s*// \.unwrap_or_else\(\|_\| "cloud\.spacedrive\.com"\.to_string\(\)\)' `
'.unwrap_or_else(|_| "cloud.spacedrive.com".to_string())'
# Comment out development cloud domain
Update-FileContent $coreFile `
'^\s*\.unwrap_or_else\(\|_\| "localhost"\.to_string\(\)\)' `
'// .unwrap_or_else(|_| "localhost".to_string())'
}
}
}

113
scripts/switch_servers.sh Executable file
View File

@ -0,0 +1,113 @@
#!/bin/bash
## Usage:
# ./script.sh dev # Will prompt for relay server modification
# ./script.sh prod # Will prompt for relay server modification
# ./script.sh dev -r # Will automatically modify relay servers
# ./script.sh prod -r # Will automatically modify relay servers
# ./script.sh dev -s # Will skip relay server modification without prompting
# ./script.sh prod -s # Will skip relay server modification without prompting
# Create cleanup function
cleanup() {
rm -f "$rust_file-e" "$tsx_file-e" "$core_file-e"
}
# Set trap for cleanup on script exit
trap cleanup EXIT
rust_file="core/crates/cloud-services/src/lib.rs"
tsx_file="interface/util/index.tsx"
core_file="core/src/lib.rs"
# Function to prompt for relay servers change
prompt_relay() {
while true; do
read -p "Do you want to modify relay servers as well? (y/n): " yn
case $yn in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer y or n.";;
esac
done
}
if [ $# -ne 1 ] && [ $# -ne 2 ]; then
echo "Usage: $0 <dev|prod> [-r|-s]"
echo " -r: Automatically modify relay servers without prompting"
echo " -s: Skip relay servers modification without prompting"
exit 1
fi
# Check flags for relay server handling
modify_relay=false
if [ "$2" = "-r" ]; then
modify_relay=true
elif [ "$2" = "-s" ]; then
modify_relay=false
elif [ $# -eq 1 ]; then
prompt_relay && modify_relay=true
fi
case $1 in
"dev")
# Update Rust file
sed -i'' -e 's|^pub const AUTH_SERVER_URL.*|// pub const AUTH_SERVER_URL: \&str = "https:\/\/auth.spacedrive.com";|' "$rust_file"
sed -i'' -e 's|^// pub const AUTH_SERVER_URL.*localhost.*|pub const AUTH_SERVER_URL: \&str = "http:\/\/localhost:9420";|' "$rust_file"
# Update TypeScript file
sed -i'' -e "s|^export const AUTH_SERVER_URL.*|// export const AUTH_SERVER_URL = 'https:\/\/auth.spacedrive.com';|" "$tsx_file"
sed -i'' -e "s|^// export const AUTH_SERVER_URL.*localhost.*|export const AUTH_SERVER_URL = 'http:\/\/localhost:9420';|" "$tsx_file"
# Update relay servers if requested
if [ "$modify_relay" = true ]; then
# Comment out production relay
sed -i'' -e 's@^\([[:space:]]*\)\.unwrap_or_else(|_| "https://relay.spacedrive.com:4433/".to_string())@\1// .unwrap_or_else(|_| "https://relay.spacedrive.com:4433/".to_string())@' "$core_file"
# Uncomment development relay
sed -i'' -e 's@^\([[:space:]]*\)// \.unwrap_or_else(|_| "http://localhost:8081/".to_string())@\1.unwrap_or_else(|_| "http://localhost:8081/".to_string())@' "$core_file"
# Comment out production pkarr
sed -i'' -e 's@^\([[:space:]]*\)\.unwrap_or_else(|_| "https://irohdns.spacedrive.com/pkarr".to_string())@\1// .unwrap_or_else(|_| "https://irohdns.spacedrive.com/pkarr".to_string())@' "$core_file"
# Uncomment development pkarr
sed -i'' -e 's@^\([[:space:]]*\)// \.unwrap_or_else(|_| "http://localhost:8080/pkarr".to_string())@\1.unwrap_or_else(|_| "http://localhost:8080/pkarr".to_string())@' "$core_file"
# Comment out production cloud domain
sed -i'' -e 's@^\([[:space:]]*\)\.unwrap_or_else(|_| "cloud.spacedrive.com".to_string())@\1// .unwrap_or_else(|_| "cloud.spacedrive.com".to_string())@' "$core_file"
# Uncomment development cloud domain
sed -i'' -e 's@^\([[:space:]]*\)// \.unwrap_or_else(|_| "localhost".to_string())@\1.unwrap_or_else(|_| "localhost".to_string())@' "$core_file"
fi
;;
"prod")
# Update Rust file
sed -i'' -e 's|^// pub const AUTH_SERVER_URL.*spacedrive.*|pub const AUTH_SERVER_URL: \&str = "https:\/\/auth.spacedrive.com";|' "$rust_file"
sed -i'' -e 's|^pub const AUTH_SERVER_URL.*localhost.*|// pub const AUTH_SERVER_URL: \&str = "http:\/\/localhost:9420";|' "$rust_file"
# Update TypeScript file
sed -i'' -e "s|^// export const AUTH_SERVER_URL.*spacedrive.*|export const AUTH_SERVER_URL = 'https:\/\/auth.spacedrive.com';|" "$tsx_file"
sed -i'' -e "s|^export const AUTH_SERVER_URL.*localhost.*|// export const AUTH_SERVER_URL = 'http:\/\/localhost:9420';|" "$tsx_file"
# Update relay servers if requested
if [ "$modify_relay" = true ]; then
# Uncomment production relay
sed -i'' -e 's@^\([[:space:]]*\)// \.unwrap_or_else(|_| "https://relay.spacedrive.com:4433/".to_string())@\1.unwrap_or_else(|_| "https://relay.spacedrive.com:4433/".to_string())@' "$core_file"
# Comment out development relay
sed -i'' -e 's@^\([[:space:]]*\)\.unwrap_or_else(|_| "http://localhost:8081/".to_string())@\1// .unwrap_or_else(|_| "http://localhost:8081/".to_string())@' "$core_file"
# Uncomment production pkarr
sed -i'' -e 's@^\([[:space:]]*\)// \.unwrap_or_else(|_| "https://irohdns.spacedrive.com/pkarr".to_string())@\1.unwrap_or_else(|_| "https://irohdns.spacedrive.com/pkarr".to_string())@' "$core_file"
# Comment out development pkarr
sed -i'' -e 's@^\([[:space:]]*\)\.unwrap_or_else(|_| "http://localhost:8080/pkarr".to_string())@\1// .unwrap_or_else(|_| "http://localhost:8080/pkarr".to_string())@' "$core_file"
# Uncomment production cloud domain
sed -i'' -e 's@^\([[:space:]]*\)// \.unwrap_or_else(|_| "cloud.spacedrive.com".to_string())@\1.unwrap_or_else(|_| "cloud.spacedrive.com".to_string())@' "$core_file"
# Comment out development cloud domain
sed -i'' -e 's@^\([[:space:]]*\)\.unwrap_or_else(|_| "localhost".to_string())@\1// .unwrap_or_else(|_| "localhost".to_string())@' "$core_file"
fi
;;
*)
echo "Invalid argument. Use 'dev' or 'prod'"
exit 1
;;
esac