Last active
August 16, 2019 17:40
-
-
Save tlrobinson/9760f80785846b97b9d961e4587d235e to your computer and use it in GitHub Desktop.
metabase-branch script for management multiple dev instances of Metabase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# This script will automatically create a checkout of a Metabase branch, give it | |
# a unique port, and optionally copy a template database (if a | |
# `metabase.db.mv.db` exists alongside `metabase-branch`) | |
# | |
# If you want to run multiple instances simultaneously you should use | |
# `foreman run lein run` so that the backend uses the assigned port. You also | |
# need to use `yarn build-watch` rather than `yarn build-hot`. | |
# | |
# Additionally, you should use unique domain names for each instance so you aren't | |
# logged out of other instances each time you log in. `*.localhost` works | |
# well if your DNS provider supports it (e.x. Cloudflare's 1.1.1.1) otherwise | |
# you can add entries to your /etc/hosts file automatically by uncommenting the | |
# line below beginning with "# echo "127.0.0.1" | |
# | |
# If a `$HOME/metabase/metabase` repository exists that will be used for the | |
# initialy clone in order to speed things up. | |
# | |
# | |
# Usage: | |
# | |
# Branch BRANCH_NAME in metabase/metabase repo: | |
# | |
# metabase-branch BRANCH_NAME | |
# | |
# Branch BRANCH_NAME in USER_NAME/metabase repo: | |
# | |
# metabase-branch USER_NAME BRANCH_NAME | |
# | |
# Branch BRANCH_NAME in USER_NAME/REPO_NAME repo: | |
# | |
# metabase-branch USER_NAME REPO_NAME BRANCH_NAME | |
# | |
set -eu | |
branch="master" | |
user="metabase" | |
repo="metabase" | |
if [ $# -eq 3 ]; then | |
user=$1 | |
repo=$2 | |
branch=$3 | |
elif [ $# -eq 2 ]; then | |
user=$1 | |
branch=$2 | |
elif [ $# -eq 1 ]; then | |
branch=$1 | |
else | |
echo "usage: $0 [USER [REPO]] BRANCH" | |
exit 1 | |
fi | |
if ! which foreman > /dev/null; then | |
echo "Please install foreman (e.x. 'gem install foreman')" | |
exit 1 | |
fi | |
cd "$(dirname "$0")" | |
metabase_db_template_path="$PWD/metabase.db.mv.db" | |
metabase_branches_path="$PWD" | |
repo_path="$metabase_branches_path/branches/$user-$branch" | |
next_port_path="$metabase_branches_path/next_port.txt" | |
mkdir -p "$metabase_branches_path/branches" | |
default_origin="[email protected]:metabase/metabase.git" | |
remote_origin="[email protected]:$user/$repo.git" | |
if [ -d "$HOME/metabase/metabase" ]; then | |
local_origin="$HOME/metabase/metabase" | |
fi | |
if [ "$user" == "metabase" ];then | |
origin="origin" | |
else | |
origin="$user" | |
fi | |
if ! [[ -d "$repo_path" ]]; then | |
if [ -z "$local_origin" ]; then | |
git clone --no-checkout "$default_origin" "$repo_path" | |
else | |
git clone --no-checkout "$local_origin" "$repo_path" | |
fi | |
cd "$repo_path" | |
git remote remove origin | |
git remote add "$origin" "$remote_origin" | |
git fetch "$origin" | |
if ! git checkout -b "$branch" "$origin/$branch"; then | |
echo "Failed" | |
exit 1 | |
fi | |
hostname="$user-$branch.localhost" | |
port="$(cat "$next_port_path" 2> /dev/null || echo "3100")" | |
expr "$port" + 1 > "$next_port_path" | |
echo "MB_JETTY_PORT=$port" >> ".env" | |
echo "MB_SITE_URL=\"http://$hostname:$port/\"" >> ".env" | |
# append host to /etc/hosts if your DNS doesn't support *.localhost wildcard (Cloudflare's 1.1.1.1 does) | |
# echo "127.0.0.1 $hostname" | sudo tee -a "/etc/hosts" | |
if [ -f "$metabase_db_template_path" ]; then | |
cp "$metabase_db_template_path" . | |
else | |
echo "No template db present. Copy a Metabase application DB to $metabase_db_template_path and future forks will use it as a template." | |
fi | |
fi | |
osascript <<- EOF | |
tell application "iTerm" | |
tell current window | |
set sessionA to session of (create tab with profile "Default") | |
set sessionC to (split vertically sessionA with profile "Default") | |
set sessionB to (split horizontally sessionA with profile "Default") | |
write sessionA text "export PS1=\"\${PS1}• \"; cd '$repo_path'" | |
write sessionA text "cat .env && foreman run lein run" | |
write sessionB text "export PS1=\"\${PS1}• \"; cd '$repo_path'" | |
write sessionB text "rm -f resources/frontend_client/index.html; yarn build-watch" | |
write sessionC text "export PS1=\"\${PS1}• \"; cd '$repo_path'" | |
write sessionC text "atom ." | |
end tell | |
end tell | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment