Skip to content

Commit

Permalink
chore: add a wrapper script for the habitat binary
Browse files Browse the repository at this point in the history
  • Loading branch information
jianliang00 committed Nov 21, 2024
1 parent 0398abb commit b7bc2a5
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 29 deletions.
31 changes: 2 additions & 29 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: ci

on:
push:
create:
branches:
- main
- "CQ-*"
pull_request:
branches:
- main
Expand All @@ -23,31 +23,4 @@ jobs:
make isort_check
make lint
make test
release:
runs-on: ubuntu-latest
needs: check
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: check if version changed
run: |
export VERSION=$(python -c "from core.__version__ import __version__; print(__version__)")
export VERSION_LINE_OID=$(git blame core/__version__.py --root -l | grep "VERSION = " | cut -c 1-40)
export HEAD_OID=$(git log -1 --format='%H')
if [ "$VERSION_LINE_OID" == "$HEAD_OID" ]; then echo "UPGRADE=1" >> $GITHUB_OUTPUT; echo "VERSION=$VERSION" >> $GITHUB_OUTPUT; fi
id: check_version
- name: package
if: ${{ steps.check_version.outputs.UPGRADE == '1' }}
run: |
make install_dev
make package
- name: release
uses: ncipollo/release-action@v1
if: ${{ steps.check_version.outputs.UPGRADE == '1' }}
with:
name: ${{ steps.check_version.outputs.VERSION }}
tag: ${{ steps.check_version.outputs.VERSION }}
token: ${{ secrets.GITHUB_TOKEN }}
replacesArtifacts: false
artifacts: hab.pex
40 changes: 40 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: ci

on:
workflow_dispatch:
pull_request:
types:
- closed
branches:
- main

jobs:
release:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
needs: check
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: check if version changed
run: |
export VERSION=$(python -c "from core.__version__ import __version__; print(__version__)")
export VERSION_LINE_OID=$(git blame core/__version__.py --root -l | grep "VERSION = " | cut -c 1-40)
export HEAD_OID=$(git log -1 --format='%H')
if [ "$VERSION_LINE_OID" == "$HEAD_OID" ]; then echo "UPGRADE=1" >> $GITHUB_OUTPUT; echo "VERSION=$VERSION" >> $GITHUB_OUTPUT; fi
id: check_version
- name: package
if: ${{ steps.check_version.outputs.UPGRADE == '1' }}
run: |
make install_dev
make package
- name: release
uses: ncipollo/release-action@v1
if: ${{ steps.check_version.outputs.UPGRADE == '1' }}
with:
name: ${{ steps.check_version.outputs.VERSION }}
tag: ${{ steps.check_version.outputs.VERSION }}
token: ${{ secrets.GITHUB_TOKEN }}
replacesArtifacts: false
artifacts: hab.pex,bin/hab
151 changes: 151 additions & 0 deletions bin/hab
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!/usr/bin/env bash

set -e

##############################################################################
##
## habitat start up script for UN*X
##
##############################################################################
__version__="latest"

if [[ "$HABITAT_DEBUG" = "true" ]]; then
set -x
fi

debug () {
if [[ "$HABITAT_DEBUG" = "true" ]]; then
echo "$*"
fi
}

info () {
echo "$*"
}

warn () {
echo "$*"
}

die () {
echo
echo "$*"
echo
exit 1
}

HABITAT_VERSION=${HABITAT_VERSION:-$__version__}

HABITAT_RELEASES_BASE_URL=https://github.com/tiktok/habitat/releases

if [[ $HABITAT_VERSION = 'latest' ]]; then
HABITAT_DOWNLOAD_URL=$HABITAT_RELEASES_BASE_URL/latest/download/hab.pex
HABITAT_VERSION=$(curl -I -sL -o /dev/null -w '%{url_effective}' $HABITAT_RELEASES_BASE_URL/latest | grep -oE '[^/]+$')
debug "Using latest version"
else
HABITAT_DOWNLOAD_URL=$HABITAT_RELEASES_BASE_URL/download/$HABITAT_VERSION/hab.pex
fi

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac

# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi

HABITAT_CACHE_DIR=$HOME/.habitat/bin
HABITAT_BIN=$HABITAT_CACHE_DIR/hab-$HABITAT_VERSION

install() {
if [[ ! -d "$HABITAT_CACHE_DIR" ]]; then
# shellcheck disable=SC2086
mkdir -p $HABITAT_CACHE_DIR
fi
if [[ ! -f "$HABITAT_BIN" ]]; then
info "Installing habitat ($HABITAT_VERSION) from url $HABITAT_DOWNLOAD_URL..."
curl -sL "$HABITAT_DOWNLOAD_URL" -o "$HABITAT_BIN"
chmod +x "$HABITAT_BIN"
fi
}

install

CURRENT_VERSION=$($HABITAT_BIN -v 2>/dev/null)

for arg in "$@"
do
if [ -d "$arg" ]; then
root_dir=$arg
fi
if [[ "$arg" == "sync" ]]; then
is_sync_command=true
fi
done

exit_code=0
if [ $is_sync_command ]; then
err_msg=$(HABITAT_BIN deps "$root_dir" 2>&1 1>/dev/null) || exit_code=$?
if [[ $exit_code = 126 ]]; then
# 126 means incompatible version
PATTERN="([0-9]+\.[0-9]+\.[0-9]{1,3}(-[a-z]+\.[0-9]+)?)"
HABITAT_VERSION=$(echo "$err_msg" | grep -E "^expected\ version:\ " | grep -E "$PATTERN" -o)
debug "required version ${HABITAT_VERSION} does not exist, try installing it first (current version is $CURRENT_VERSION)"
install
fi
fi

exit_code=0
debug Using habitat "$HABITAT_VERSION"
$HABITAT_BIN "$@" || exit_code=$?

exit $exit_code

0 comments on commit b7bc2a5

Please sign in to comment.