You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.9 KiB

## 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 init
git config pull.rebase true
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
show_repo_info
$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
}
function show_repo_info {
local git_local_branch=$(git rev-parse --abbrev-ref HEAD)
local git_current_version=$(git describe --tags 2>/dev/null | cut -d- -f1)
local git_ahead=$(git log "${git_current_version}"..HEAD --oneline | wc -l)
local git_dirty=$(git status -s | wc -l)
[[ $git_ahead -gt 0 ]] && git_current_version="${git_current_version}${git_ahead}"
[[ $git_dirty -gt 0 ]] && git_current_version="${git_current_version}${git_dirty}"
echo "found version => [$git_local_branch] $git_current_version"
}
## MAIN
set -Eue
assert_params $*
mkdir -p /opt/$USER
cd /opt/$USER
pull_repo
link_to_config
install_mise
install_rails