From 73db6160b878a5a77681becb1dff67b8fcf85f2b Mon Sep 17 00:00:00 2001 From: Michel Tricot Date: Sun, 5 May 2024 00:10:23 -0700 Subject: [PATCH 01/11] add install script for abctl --- abctl_install.sh | 232 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100755 abctl_install.sh diff --git a/abctl_install.sh b/abctl_install.sh new file mode 100755 index 0000000..03aca0d --- /dev/null +++ b/abctl_install.sh @@ -0,0 +1,232 @@ +#!/bin/bash + +set -Eeu + +VERSION=0.1 + +# Debug +DEBUG=${DEBUG:-""} +if ! [ -z "$DEBUG" ]; then + echo "Running in debug mode" + set -x +fi + +# Trap config +TRAP_MESSAGE= +_trap() { + local rv=$? + if [ "$rv" -ne 0 ]; then + _event abctl_install failed "$TRAP_MESSAGE" + else + _event abctl_install succeeded + fi + exit "$rv" +} +trap "_trap" EXIT + +# Dev +FORCE_OS=${FORCE_OS:-""} +FORCE_FUN=${FORCE_FUN:-""} + +# Defaults +TELEMETRY_ENABLED=${TELEMETRY_ENABLED:-1} +TELEMETRY_STORE=${TELEMETRY_STORE:-~/.airbyte/analytics.yml} +TELEMETRY_KEY=${TELEMETRY_KEY:-"kpYsVGLgxEqD5OuSZAQ9zWmdgBlyiaej"} +TELEMETRY_INSTANCE_ID=${TELEMETRY_INSTANCE_ID:-""} +TELEMETRY_SESSION_ID=${TELEMETRY_SESSION_ID:-""} +TELEMETRY_LOG="" + +RELEASE_TAG=${RELEASE_TAG:-""} + +DIR_TMP=${DIR_TMP:-$(mktemp -d -t "abctl_install.XXXX")} +DIR_INSTALL=${DIR_INSTALL:-/usr/local/bin} + +# Consts + +# Helpers +_error() { + local rv=$? + + TRAP_MESSAGE="$1" + echo "$@" 1>&2 + + exit "$rv" +} + +_sudo() { + if [ "$(whoami)" = "root" ]; then + "$@" + elif _exists sudo; then + sudo -E "$@" + else + _error "Neither root or sudo" ; + fi +} + +_exists() { + which "$1" 2>&1 > /dev/null +} + +_curl() { + local url=$1 + local data=${2:-""} + + if _exists curl; then + if [ -z "$data" ]; then + curl -Lsf1 --output - "$url" + else + curl -Lsf1 --output - "$url" -d "$data" -X POST -H "content-type: application/json" + fi + elif _exists wget; then + if [ -z "$data" ]; then + wget -q -O - "$url" + else + wget -q -O - -H "$url" --post-data "$data" --header "content-type: application/json" + fi + else + _error "Neither curl or wget available." ; + fi +} + +_extract_value() { + grep "$1" | cut -d : -f2- | tr -d '"[:space:],' +} + +_unique_id() { + # does it need to be ulid? + LC_ALL=C tr -dc A-Za-z0-9 "$TELEMETRY_STORE" + echo "# For more information or to opt out, please see" >> "$TELEMETRY_STORE" + echo "# - https://docs.airbyte.com/operator-guides/telemetry" >> "$TELEMETRY_STORE" + echo "anonymous_user_id: $TELEMETRY_INSTANCE_ID" >> "$TELEMETRY_STORE" + fi +} + +_event() { + local event=$1 + local state=$2 + local message=${3:-""} + + [ -z "${TELEMETRY_ENABLED}" ] && return 0 + + # ensure we don't log the same event twice + echo "$TELEMETRY_LOG" | grep -q "$event-$state" && return 0 + + echo "Sending $event ($state, instance: '$TELEMETRY_INSTANCE_ID', session: '$TELEMETRY_SESSION_ID' msg: '$message')" + + local telemetry_request=$(cat < /dev/null + TELEMETRY_LOG="$event-$state-$TELEMETRY_LOG" +} + +_install_binary() { + local os="$1" + local arch="$2" + + local url_fragment=releases/latest + [ -z "$RELEASE_TAG" ] || url_fragment="releases/tags/$RELEASE_TAG" + + local release_data=$(_curl "https://api.github.com/repos/airbytehq/abctl/$url_fragment") + local release_tag=$(echo "$release_data" | _extract_value tag_name) + local release_url=$(echo "$release_data" | grep "$os-$arch" | _extract_value browser_download_url) + local release_filename=$(echo "$release_data" | grep "$os-$arch" | _extract_value name) + + _curl "$release_url" > "${DIR_TMP}/$release_filename" + + ( + cd "${DIR_TMP}" + mkdir -p release + tar zxf "${release_filename}" -C release + local binary=$(ls -1 release/*/abctl | head -n 1) + echo "Installing '$binary' to ${DIR_INSTALL}" + _sudo cp "$binary" "${DIR_INSTALL}" + ) +} + +_get_os() { + if ! [ -z "${FORCE_OS}" ]; then + echo "${FORCE_OS}" + elif [ "$(uname)" = "Linux" ]; then + echo linux + elif [ "$(uname)" = "Darwin" ]; then + echo darwin + elif uname -r | grep -c Microsoft; then + echo "windows" + else + _error "Unknown system." + fi +} + +_get_arch() { + if uname -m | grep -q "arm"; then + echo arm64 + else + echo amd64 + fi +} + +# System installs +_install_linux() { + echo "Installing for Linux..." + + _install_binary linux "$(_get_arch)" +} + +_install_darwin() { + echo "Installing for Darwin..." + + if ! _exists abrew; then + _install_binary darwin "$(_get_arch)" + elif brew ls --version abctl > /dev/null; then + brew upgrade abctl + else + brew tap airbytehq/tap + brew install abctl + fi +} + +_install_windows() { + echo "Installing for Windows..." + echo "Unsupported" +} + +main() { + [ -z "${FORCE_FUN}" ] || { "$@"; exit 0; } + + _init_telemetry + + _event abctl_install started + + "_install_$(_get_os)" "$@" +} + +main "$@" From bd146198d543ae7c79b35427ecd3505023af44f6 Mon Sep 17 00:00:00 2001 From: Michel Tricot Date: Sun, 5 May 2024 00:21:31 -0700 Subject: [PATCH 02/11] better uniq --- abctl_install.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/abctl_install.sh b/abctl_install.sh index 03aca0d..9eae83f 100755 --- a/abctl_install.sh +++ b/abctl_install.sh @@ -94,7 +94,9 @@ _extract_value() { _unique_id() { # does it need to be ulid? - LC_ALL=C tr -dc A-Za-z0-9 /dev/null; then brew upgrade abctl From b0f7d4e8cb7e931f1bf59d23b06b0b28f62e672b Mon Sep 17 00:00:00 2001 From: Michel Tricot Date: Sun, 5 May 2024 00:49:13 -0700 Subject: [PATCH 03/11] better error reason detection --- abctl_install.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/abctl_install.sh b/abctl_install.sh index 9eae83f..f2bdfb9 100755 --- a/abctl_install.sh +++ b/abctl_install.sh @@ -4,6 +4,12 @@ set -Eeu VERSION=0.1 +# Logging +# Automatically logs everything that goes to stdout into a file +# at the same time +DIR_TMP=${DIR_TMP:-$(mktemp -d -t "abctl_install.XXXX")} +exec > >(tee "$DIR_TMP/output.log") 2>&1 + # Debug DEBUG=${DEBUG:-""} if ! [ -z "$DEBUG" ]; then @@ -12,11 +18,10 @@ if ! [ -z "$DEBUG" ]; then fi # Trap config -TRAP_MESSAGE= _trap() { local rv=$? if [ "$rv" -ne 0 ]; then - _event abctl_install failed "$TRAP_MESSAGE" + _event abctl_install failed "$(tail -n 1 "$DIR_TMP/output.log")" else _event abctl_install succeeded fi @@ -38,7 +43,6 @@ TELEMETRY_LOG="" RELEASE_TAG=${RELEASE_TAG:-""} -DIR_TMP=${DIR_TMP:-$(mktemp -d -t "abctl_install.XXXX")} DIR_INSTALL=${DIR_INSTALL:-/usr/local/bin} # Consts From 1688c150a29250ce1589879c6d47eb1dd29aadd2 Mon Sep 17 00:00:00 2001 From: Michel Tricot Date: Sun, 5 May 2024 00:50:42 -0700 Subject: [PATCH 04/11] remove debug --- abctl_install.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/abctl_install.sh b/abctl_install.sh index f2bdfb9..9230ba4 100755 --- a/abctl_install.sh +++ b/abctl_install.sh @@ -131,8 +131,6 @@ _event() { # ensure we don't log the same event twice echo "$TELEMETRY_LOG" | grep -q "$event-$state" && return 0 - echo "Sending $event ($state, instance: '$TELEMETRY_INSTANCE_ID', session: '$TELEMETRY_SESSION_ID' msg: '$message')" - local telemetry_request=$(cat < Date: Sun, 5 May 2024 00:54:31 -0700 Subject: [PATCH 05/11] add comment --- abctl_install.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/abctl_install.sh b/abctl_install.sh index 9230ba4..283a64c 100755 --- a/abctl_install.sh +++ b/abctl_install.sh @@ -203,6 +203,7 @@ _get_arch() { _install_linux() { echo "Installing for Linux..." + # We should probably handle yum & deb packages instead of installing the binary directly _install_binary linux "$(_get_arch)" } From 2ebe47e6c11fbc7a6e83bc0d789e036ad8268a2a Mon Sep 17 00:00:00 2001 From: Michel Tricot Date: Mon, 6 May 2024 15:35:06 -0700 Subject: [PATCH 06/11] fix ulid + add binary test --- abctl_install.sh | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/abctl_install.sh b/abctl_install.sh index 283a64c..50cfbe2 100755 --- a/abctl_install.sh +++ b/abctl_install.sh @@ -21,9 +21,10 @@ fi _trap() { local rv=$? if [ "$rv" -ne 0 ]; then - _event abctl_install failed "$(tail -n 1 "$DIR_TMP/output.log")" + [ -z "$TELEMETRY_MSG" ] && TELEMETRY_MSG=$(tail -n 1 "$DIR_TMP/output.log") + _event abctl_install failed "$TELEMETRY_MSG" else - _event abctl_install succeeded + _event abctl_install succeeded "$TELEMETRY_MSG" fi exit "$rv" } @@ -39,7 +40,8 @@ TELEMETRY_STORE=${TELEMETRY_STORE:-~/.airbyte/analytics.yml} TELEMETRY_KEY=${TELEMETRY_KEY:-"kpYsVGLgxEqD5OuSZAQ9zWmdgBlyiaej"} TELEMETRY_INSTANCE_ID=${TELEMETRY_INSTANCE_ID:-""} TELEMETRY_SESSION_ID=${TELEMETRY_SESSION_ID:-""} -TELEMETRY_LOG="" +TELEMETRY_LOG= +TELEMETRY_MSG= RELEASE_TAG=${RELEASE_TAG:-""} @@ -51,7 +53,7 @@ DIR_INSTALL=${DIR_INSTALL:-/usr/local/bin} _error() { local rv=$? - TRAP_MESSAGE="$1" + TELEMETRY_MSG="$1" echo "$@" 1>&2 exit "$rv" @@ -97,10 +99,10 @@ _extract_value() { } _unique_id() { - # does it need to be ulid? - local time="$(date +"%s")" - local rand="$(LC_ALL=C tr -dc A-Za-z0-9 /dev/null +} + main() { [ -z "${FORCE_FUN}" ] || { "$@"; exit 0; } @@ -233,6 +239,8 @@ main() { _event abctl_install started "_install_$(_get_os)" "$@" + + _test_binary } main "$@" From 66ed0eb7c1a43e2452f733459e0380a014c1c8aa Mon Sep 17 00:00:00 2001 From: Michel Tricot Date: Tue, 7 May 2024 09:10:48 -0700 Subject: [PATCH 07/11] only use ulid charset --- abctl_install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/abctl_install.sh b/abctl_install.sh index 50cfbe2..5ed25a5 100755 --- a/abctl_install.sh +++ b/abctl_install.sh @@ -101,7 +101,7 @@ _extract_value() { _unique_id() { # looks like a ulid but the time piece is in ascii instead of 48 bits encoded local time="$(date +"%s000" | head -c 10)" - local rand="$(LC_ALL=C tr -dc A-Za-z0-9 Date: Thu, 9 May 2024 12:29:20 -0700 Subject: [PATCH 08/11] more visibility --- abctl_install.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/abctl_install.sh b/abctl_install.sh index 5ed25a5..0e67262 100755 --- a/abctl_install.sh +++ b/abctl_install.sh @@ -166,6 +166,8 @@ _install_binary() { local release_url=$(echo "$release_data" | grep "$os-$arch" | _extract_value browser_download_url) local release_filename=$(echo "$release_data" | grep "$os-$arch" | _extract_value name) + echo "Downloading binary from $release_url" + _curl "$release_url" > "${DIR_TMP}/$release_filename" ( From 81c2645137d4c30c89f3e39090a7209c29c5dce1 Mon Sep 17 00:00:00 2001 From: Michel Tricot Date: Thu, 9 May 2024 12:31:42 -0700 Subject: [PATCH 09/11] nicer debug --- abctl_install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/abctl_install.sh b/abctl_install.sh index 0e67262..f27d0a4 100755 --- a/abctl_install.sh +++ b/abctl_install.sh @@ -166,7 +166,7 @@ _install_binary() { local release_url=$(echo "$release_data" | grep "$os-$arch" | _extract_value browser_download_url) local release_filename=$(echo "$release_data" | grep "$os-$arch" | _extract_value name) - echo "Downloading binary from $release_url" + echo "Downloading abctl from $release_url" _curl "$release_url" > "${DIR_TMP}/$release_filename" From 55b9221acfa9975e603d52d1c5950711439a106d Mon Sep 17 00:00:00 2001 From: Michel Tricot Date: Fri, 10 May 2024 15:38:18 -0700 Subject: [PATCH 10/11] help --- abctl_install.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/abctl_install.sh b/abctl_install.sh index f27d0a4..751f21d 100755 --- a/abctl_install.sh +++ b/abctl_install.sh @@ -23,8 +23,16 @@ _trap() { if [ "$rv" -ne 0 ]; then [ -z "$TELEMETRY_MSG" ] && TELEMETRY_MSG=$(tail -n 1 "$DIR_TMP/output.log") _event abctl_install failed "$TELEMETRY_MSG" + echo + echo "abctl install failed: $TELEMETRY_MSG" else _event abctl_install succeeded "$TELEMETRY_MSG" + echo + echo "abctl install succeeded. Run:" + echo + echo " abctl --help" + echo + echo "to get started! (You might need to sudo, depending on your docker setup)" fi exit "$rv" } From c94c87b73182dfe3ec4d167e75f2abce36e18c67 Mon Sep 17 00:00:00 2001 From: Michel Tricot Date: Mon, 20 May 2024 16:22:23 -0700 Subject: [PATCH 11/11] cosmetic --- abctl_install.sh | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/abctl_install.sh b/abctl_install.sh index 751f21d..bf3b152 100755 --- a/abctl_install.sh +++ b/abctl_install.sh @@ -22,11 +22,13 @@ _trap() { local rv=$? if [ "$rv" -ne 0 ]; then [ -z "$TELEMETRY_MSG" ] && TELEMETRY_MSG=$(tail -n 1 "$DIR_TMP/output.log") - _event abctl_install failed "$TELEMETRY_MSG" + _telemetry_event abctl_install failed "$TELEMETRY_MSG" + echo echo "abctl install failed: $TELEMETRY_MSG" else - _event abctl_install succeeded "$TELEMETRY_MSG" + _telemetry_event abctl_install succeeded "$TELEMETRY_MSG" + echo echo "abctl install succeeded. Run:" echo @@ -55,8 +57,6 @@ RELEASE_TAG=${RELEASE_TAG:-""} DIR_INSTALL=${DIR_INSTALL:-/usr/local/bin} -# Consts - # Helpers _error() { local rv=$? @@ -113,7 +113,7 @@ _unique_id() { echo "${time}${rand}" | head -c 26 } -_init_telemetry() { +_telemetry_init() { [ "$TELEMETRY_ENABLED" -eq 1 ] || return 0 [ -z "$TELEMETRY_SESSION_ID" ] && TELEMETRY_SESSION_ID=$(_unique_id) @@ -131,12 +131,12 @@ _init_telemetry() { fi } -_event() { +_telemetry_event() { + [ "$TELEMETRY_ENABLED" -eq 1 ] || return 0 + local event=$1 local state=$2 local message=${3:-""} - - [ -z "${TELEMETRY_ENABLED}" ] && return 0 # ensure we don't log the same event twice echo "$TELEMETRY_LOG" | grep -q "$event-$state" && return 0 @@ -234,7 +234,7 @@ _install_darwin() { _install_windows() { echo "Installing for Windows..." - echo "Unsupported" + echo "Unsupported, use WSL2" } _test_binary() { @@ -244,9 +244,9 @@ _test_binary() { main() { [ -z "${FORCE_FUN}" ] || { "$@"; exit 0; } - _init_telemetry + _telemetry_init - _event abctl_install started + _telemetry_event abctl_install started "_install_$(_get_os)" "$@"