## CONSTANTS REPO_URL=$1 REPO_BRANCH=$2 FORCE=${3:-false} RAILS_ENV=production KEY_LENGTH=10 ## FUNCTIONS function install_mise { if [[ ! -f $HOME/.local/bin/mise ]]; then echo -n 'installing mise...' curl -s https://mise.run | sh 2>&1 >/dev/null echo OK fi if [[ ! -f $HOME/.bashrc ]] || ! grep -q '.local/bin/mise activate bash' $HOME/.bashrc; then echo "eval \"\$($HOME/.local/bin/mise activate bash)\"" >>$HOME/.bashrc echo "cd /opt/$USER" >>$HOME/.bashrc source $HOME/.bashrc mise version fi if [[ ! -f $HOME/.config/mise/config.toml ]] || ! grep -q 'RAILS_ENV' $HOME/.config/mise/config.toml; then mise self-update -y 2>&1 >/dev/null mise settings add idiomatic_version_file_enable_tools ruby mise set --global RAILS_ENV=production mise set --global SECRET_KEY_BASE=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c $KEY_LENGTH) echo mise install successfully for production with SECRET_KEY_BASE fi } function pull_repo { if [[ ! -d .git ]]; then git config --global init.defaultBranch $REPO_BRANCH git config pull.rebase true git init git remote add origin $REPO_URL fi git branch --set-upstream-to=origin/$REPO_BRANCH $REPO_BRANCH if git fetch origin $REPO_BRANCH --dry-run --verbose 2>&1 | grep -q " =.*\-> origin\/$REPO_BRANCH"; then set +Eue $FORCE || exit 100 # means no change set -Eue fi git checkout -q $REPO_BRANCH git pull --tags } function install_rails { mise install mise exec -- bundle check >/dev/null || mise exec -- bundle install --quiet mise exec -- rails db:prepare mise exec -- rails assets:precompile } function assert_params { [[ $# -ge 2 ]] || (echo '2 params required: REPO_URL REPO_BRANCH [FORCE]' && exit 1) } function link_to_config { for file in /etc/$USER/*; do local destination=$(basename $file) case "$destination" in \*) echo "WARNING: no configuration file provided, you may copy from ./config/*_sample.yml to location: /etc/$USER" ;; service.conf) echo "environment (service.conf) converted to .env.production" ln -sf $(realpath $file) .env.production ;; *.yml | *.yaml) echo "yaml file: $(realpath $file) -> config" ln -sf $(realpath $file) config/ ;; esac done } ## MAIN set -Eue assert_params $* mkdir -p /opt/$USER cd /opt/$USER pull_repo link_to_config install_mise install_rails