Skip to content

Instantly share code, notes, and snippets.

@elarib
Last active November 10, 2023 17:16
Show Gist options
  • Save elarib/5193d6a6f973245cc3cc95b5aac683c9 to your computer and use it in GitHub Desktop.
Save elarib/5193d6a6f973245cc3cc95b5aac683c9 to your computer and use it in GitHub Desktop.
Stalake oneline install script
#!/bin/bash
set -e
print_starlake_ascii_art() {
cat <<EOF
_____ _______ _____ _ _ ________
/ ____|__ __|/\ | __ \| | /\ | |/ / ____|
| (___ | | / \ | |__) | | / \ | ' /| |__
\___ \ | | / /\ \ | _ /| | / /\ \ | < | __|
____) | | |/ ____ \| | \ \| |____ / ____ \| . \| |____
|_____/ |_/_/ \_\_| \_\______/_/ \_\_|\_\______|
EOF
}
get_installation_directory() {
read -p "Where do you want to install Starlake? [$HOME/starlake]: " INSTALL_DIR
INSTALL_DIR=${INSTALL_DIR:-$HOME/starlake}
mkdir -p "$INSTALL_DIR"
}
get_from_url() {
local url=$1
echo "Curling $url"
local response=$(curl -s -w "%{http_code}" "$url")
local status_code=${response: -3}
if [[ ! $status_code =~ ^(2|3)[0-9][0-9]$ ]]; then
echo "Error: Failed to retrieve data from $url. HTTP status code: $status_code"
exit 1
fi
# Print the content excluding the status code
local content_length=${#response}
local content="${response:0:content_length-3}"
echo "$content"
}
get_version_to_install() {
# Extract the version number from command-line arguments
for arg in "$@"; do
if [[ $arg == "--version="* ]]; then
VERSION="${arg#*=}"
fi
done
ALL_SNAPSHOT_VERSIONS=$(get_from_url https://s01.oss.sonatype.org/service/local/repositories/snapshots/content/ai/starlake/starlake-spark3_2.12/ | awk -F'<|>' '/<text>/{print $3}' | grep -oE '^[0-9]+\.[0-9]+\.[0-9]+-SNAPSHOT$' | sort -rV)
ALL_RELEASE_VERSIONS=$(get_from_url https://s01.oss.sonatype.org/service/local/repositories/releases/content/ai/starlake/starlake-spark3_2.12/ | awk -F'<|>' '/<text>/{print $3}' | grep -oE '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -rV)
SNAPSHOT_VERSION=$(echo "$ALL_SNAPSHOT_VERSIONS" | head -n 1)
LATEST_RELEASE_VERSIONS=$(echo "$ALL_RELEASE_VERSIONS" | head -n 5)
VERSIONS=("$SNAPSHOT_VERSION" $LATEST_RELEASE_VERSIONS)
VERSIONS=$VERSIONS
while [[ ! "$VERSIONS" =~ (^|[[:space:]])"$VERSION"($|[[:space:]]) ]]; do
echo "Invalid version $VERSION. Please choose from the available versions."
echo "Last 5 available versions:"
for version in "${VERSIONS[@]}"; do
echo "$version"
done
read -p "Which version do you want to install? [$(echo "$VERSIONS" | head -n 1)]: " VERSION
VERSION=${VERSION:-$(echo "$VERSIONS" | head -n 1)}
done
}
install_starlake() {
echo "installing $VERSION"
if [[ $VERSION == *"SNAPSHOT"* ]]; then
local url=https://raw.githubusercontent.com/starlake-ai/starlake/master/distrib/starlake.sh
else
local url=https://raw.githubusercontent.com/starlake-ai/starlake/v$VERSION/distrib/starlake.sh
fi
get_from_url $url > "$INSTALL_DIR/starlake"
chmod +x "$INSTALL_DIR/starlake"
}
add_starlake_to_path() {
if [ -f "$HOME/.zshrc" ] || [ -f "$HOME/.bashrc" ]; then
if [ -f "$HOME/.zshrc" ]; then
if ! grep -q "$INSTALL_DIR" ~/.zshrc; then
echo -e "\nexport PATH=$INSTALL_DIR:\$PATH" >> ~/.zshrc
fi
zsh ~/.zshrc
fi
if [ -f "$HOME/.bashrc" ]; then
if ! grep -q "$INSTALL_DIR" ~/.bashrc; then
echo -e "\nexport PATH=$INSTALL_DIR:\$PATH" >> ~/.bashrc
fi
source ~/.bashrc
fi
echo "Starlake has been added to your PATH."
else
echo "Could not find .zshrc or .bashrc file. Please add the following line to your shell configuration file manually:"
echo "export PATH=$INSTALL_DIR:\$PATH"
fi
}
run_installation_command() {
SL_VERSION=$VERSION "$INSTALL_DIR/starlake" install
}
print_success_message() {
echo "Starlake has been successfully installed!"
}
main() {
print_starlake_ascii_art
get_installation_directory
get_version_to_install "$@"
install_starlake
add_starlake_to_path
run_installation_command
print_success_message
}
# Run the main function
main "$@"
# Convert this script so it can run on Windows
function print_starlake_ascii_art {
Write-Host " _____ _______ _____ _ _ ________"
Write-Host " / ____|__ __|/\ | __ \| | /\ | |/ / ____|"
Write-Host " | (___ | | / \ | |__) | | / \ | ' /| |__"
Write-Host " \___ \ | | / /\ \ | _ /| | / /\ \ | < | __|"
Write-Host " ____) | | |/ ____ \| | \ \| |____ / ____ \| . \| |____"
Write-Host " |_____/ |_/_/ \_\_| \_\______/_/ \_\_|\_\______|"
}
function get_installation_directory {
$INSTALL_DIR = Read-Host "Where do you want to install Starlake? [$HOME\starlake]"
if ($INSTALL_DIR -eq "") {
$INSTALL_DIR = "$HOME\starlake"
}
New-Item -ItemType Directory -Path $INSTALL_DIR -Force | Out-Null
$INSTALL_DIR
}
function get_versions_from_url {
param (
[string]$url
)
# Download the content from the URL
$response = Invoke-RestMethod -Uri $url
# Extract text content within <text> tags
$textContents = $response.SelectNodes("//text").InnerText
# Define the version pattern regex
$versionPattern = '(\d+\.\d+\.\d+(-SNAPSHOT)?)'
# Filter and extract only the matching versions
$matchingVersions = $textContents | Where-Object { $_ -match $versionPattern }
# Sort the matching versions in descending order
$matchingVersions | Sort-Object -Descending
}
function get_version_to_install {
# Extract the version number from command-line arguments
for ($i=0; $i -lt $args.Length; $i++) {
$arg = $args[$i]
if ($arg.StartsWith("--version=")) {
$VERSION=$arg.Substring(10)
}
}
$ALL_SNAPSHOT_VERSIONS = (get_versions_from_url https://s01.oss.sonatype.org/service/local/repositories/snapshots/content/ai/starlake/starlake-spark3_2.12/)
$ALL_RELEASE_VERSIONS = (get_versions_from_url https://s01.oss.sonatype.org/service/local/repositories/releases/content/ai/starlake/starlake-spark3_2.12/)
$SNAPSHOT_VERSION = $ALL_SNAPSHOT_VERSIONS[0]
$LATEST_RELEASE_VERSIONS = $ALL_RELEASE_VERSIONS[0..4]
$VERSIONS = @($SNAPSHOT_VERSION) + $LATEST_RELEASE_VERSIONS
do {
Write-Host "Invalid version $VERSION. Please choose from the available versions."
Write-Host "Last 5 available versions:"
foreach ($version in $VERSIONS) {
Write-Host $version
}
$VERSION = Read-Host "Which version do you want to install? [$($VERSIONS[0])]"
if ($VERSION -eq "") {
$VERSION = $VERSIONS[0]
}
} while ($VERSION -notin $VERSIONS)
$VERSION
}
function install_starlake {
param (
[string]$INSTALL_DIR,
[string]$VERSION
)
Write-Host "installing $VERSION"
if ($VERSION -like "*SNAPSHOT*") {
$url = "https://raw.githubusercontent.com/starlake-ai/starlake/master/distrib/starlake.cmd"
} else {
$url = "https://raw.githubusercontent.com/starlake-ai/starlake/v$VERSION/distrib/starlake.cmd"
}
Write-Host "Downloading $url to $INSTALL_DIR"
wget $url -OutFile $INSTALL_DIR/starlake.cmd
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
}
function add_starlake_to_path {
# Implement adding to PATH if needed
}
function run_installation_command {
$env:SL_VERSION = $VERSION
Start-Process -FilePath "$INSTALL_DIR\starlake.cmd" -ArgumentList 'install' -Wait
}
function print_success_message {
Write-Host "Starlake has been successfully installed!"
}
function main {
print_starlake_ascii_art
$INSTALL_DIR=get_installation_directory
$VERSION=get_version_to_install
install_starlake $INSTALL_DIR $VERSION
add_starlake_to_path $INSTALL_DIR
run_installation_command $INSTALL_DIR
print_success_message
}
# Run the main function
main $args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment