Skip to content

Commit

Permalink
add Curl installer
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboyd committed Oct 7, 2022
1 parent f702703 commit 5589aa1
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 2 deletions.
213 changes: 213 additions & 0 deletions plugins/platform-backend/cli/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
executables_url() {
echo "http://localhost:3000/api/idp/executables"
}
download_url() {
echo "http://localhost:7007/api/idp/executables/dist"
}

info() {
local action="$1"
local details="$2"
command printf '\033[1;32m%12s\033[0m %s\n' "$action" "$details" 1>&2
}

error() {
command printf '\033[1;31mError\033[0m: %s\n\n' "$1" 1>&2
}

warning() {
command printf '\033[1;33mWarning\033[0m: %s\n\n' "$1" 1>&2
}

request() {
command printf '\033[1m%s\033[0m\n' "$1" 1>&2
}

eprintf() {
command printf '%s\n' "$1" 1>&2
}

bold() {
command printf '\033[1m%s\033[0m' "$1"
}

usage() {
cat >&2 <<END_USAGE
install.sh: Install idp executables
USAGE:
install.sh [FLAGS] [OPTIONS]
FLAGS:
-h, --help Prints help information
OPTIONS:
--skip-setup Do not modify startup scripts
END_USAGE
}

idp_home_is_ok() {
if [ -n "${IDP_HOME-}" ] && [ -e "$IDP_HOME" ] && ! [ -d "$IDP_HOME" ]; then
error "\$IDP_HOME is set but is not a directory ($IDP_HOME)."
eprintf "Please check your profile scripts and environment."
return 1
fi
return 0
}

parse_os_info() {
case "$(uname)" in
Linux)
echo "x86_64-unknown-linux-gnu"
;;
Darwin)
if [[ "$(uname -m)" == "arm64" ]]; then
echo "aarch64-apple-darwin"
else
echo "x86_64-apple-darwin"
fi
;;
*)
error "$(uname) is not supported"
exit 1
esac
}

parse_os_pretty() {
local uname_str="$1"

case "$uname_str" in
Linux)
echo "Linux"
;;
Darwin)
echo "macOS"
;;
*)
echo "$uname_str"
esac
}

download_executable_from_backstage() {
local os_info="$1"
local tmpdir="$2"

local filename="my-idp-$os_info"
local download_file="$tmpdir/$filename"
local archive_url="$(download_url)/$filename"
curl --progress-bar --show-error --location "$archive_url" --output "$download_file" --write-out '%{filename_effective}'
}

download_executable() {
local uname_str="$(uname -s)"
local os_info="$(parse_os_info)"
local pretty_os_name="$(parse_os_pretty `uname`)"

info 'Fetching' "archive for $pretty_os_name"
# store the downloaded archive in a temporary directory
local download_dir="$(mktemp -d)"
download_executable_from_backstage "$os_info" "$download_dir"
}

create_tree() {
local install_dir="$1"

info 'Creating' "directory layout"

# .idp/
# bin/

mkdir -p "$install_dir" && mkdir -p "$install_dir"/bin
if [ "$?" != 0 ]
then
error "Could not create directory layout. Please make sure the target directory is writeable: $install_dir"
exit 1
fi
}

install_from_file() {
local binfile="$1"
local install_dir="$2"

create_tree "$install_dir"

info 'Extracting' "IDP binaries"
# extract the files to the specified directory
cp -c "$binfile" "$install_dir"/bin/idp
}

install_remote() {
local install_dir="$1"

info 'Checking' "for existing IDP installation"

download_archive="$(download_executable; exit "$?")"
exit_status="$?"
if [ "$exit_status" != 0 ]
then
error "Could not download IDP executable. See $(executables_url) for a list of available executables"
return "$exit_status"
fi

install_from_file "$download_archive" "$install_dir"

}

install() {
local install_dir="$1"
local should_run_setup="$2"

if ! idp_home_is_ok; then
exit 1
fi

if [ -e "$install_dir/bin/idp" ]; then
info "Upgrade" "new executable will overwrite existing version"
fi

install_remote "$install_dir"

if [ "$?" == 0 ]
then
if [ "$should_run_setup" == "true" ]; then
info 'Finished' "installation. Updating user profile settings."
#"$install_dir"/bin/idp setup
else
info 'Finished' "installation. No changes were made to user profile settings."
fi
fi
}


# return if sourced (for testing the functions above)
return 0 2>/dev/null

# default to running setup after installing
should_run_setup="true"

# install to IDP_HOME, defaulting to ~/.volta
install_dir="${IDP_HOME:-"$HOME/.idp"}"

# parse command line options
while [ $# -gt 0 ]
do
arg="$1"

case "$arg" in
-h|--help)
usage
exit 0
;;
--skip-setup)
shift # shift off the argument
should_run_setup="false"
;;
*)
error "unknown option: '$arg'"
usage
exit 1
;;
esac
done

install "$install_dir" "$should_run_setup"
9 changes: 8 additions & 1 deletion plugins/platform-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import express from 'express';
import Router from 'express-promise-router';
import { Logger } from 'winston';
import { findOrCreateExecutables } from '../executables';
import { readFile } from 'fs/promises';

export interface RouterOptions {
logger: Logger;
Expand All @@ -37,7 +38,7 @@ export async function createRouter(
logger,
distDir: 'dist-bin',
baseURL: `${baseURL}/executables/dist`,
executableName: 'my-ldp',
executableName: 'my-idp',
entrypoint: resolvePackagePath("@frontside/backstage-plugin-platform-backend", "cli", "main.ts"),
})

Expand All @@ -49,6 +50,12 @@ export async function createRouter(
response.send({ status: 'ok' });
});

router.get('/install.sh', async (_, response) => {
response.setHeader('Content-Type', 'text/plain');
let installerBytes = await readFile(resolvePackagePath("@frontside/backstage-plugin-platform-backend", "cli", "install.sh"));
response.send(String(installerBytes));
});

router.get('/executables', (_, response)=> {
response.send(executables);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ExampleComponent = () => (
<Grid item>
<InfoCard title="Information card">
<Typography variant="body1">
All content should be wrapped in a card like this.
curl -sSL http://localhost:7007/api/idp/install.sh | sh
</Typography>
</InfoCard>
</Grid>
Expand Down

0 comments on commit 5589aa1

Please sign in to comment.