Skip to content

Instantly share code, notes, and snippets.

@Chili-Man
Created August 22, 2014 06:31
Show Gist options
  • Save Chili-Man/935bd9aa853e40c4a59e to your computer and use it in GitHub Desktop.
Save Chili-Man/935bd9aa853e40c4a59e to your computer and use it in GitHub Desktop.
Installs Berkshelf API with RVM and bash. You should source the file
#!/bin/bash --login
# Installs Berks API, assuming you use rvm.
export BAPI_GEMSET='berks-api'
export BAPI_VERSION='2.1.0'
export GOD_VERSION='0.13.4'
export GOD_FILE_URL='https://gist.githubusercontent.com/Chili-Man/7191c5ba846f95a93759/raw/12d071029ee2562057204d6565475895abf171b0/BerkshelAPI.god.rb'
export GOD_FILE_PATH='BerkshelfAPI.god.rb'
export GOD_LOG_PATH='god.log'
export GOD_BAPI_PROC_NAME='BerkshelfAPI'
function rvm_installed() {
rvm -v &> /dev/null
if [ "$?" -ne 0 ]; then
echo 'rvm is not installed'
return 1
fi
}
function berks_api_installed() {
berks-api -v &> /dev/null
if [ "$?" -ne 0 ]; then
echo 'berks-api is not installed'
return 1
fi
}
function gemset_exists() {
gemset=$1
if [ -z "$gemset" ]; then gemset="$BAPI_GEMSET"; fi
rvm gemset list | grep "$gemset" &> /dev/null
}
function create_gemset() {
gemset=$1
if [ -z "$gemset" ]; then gemset="$BAPI_GEMSET"; fi
rvm gemset create "$gemset"
}
function load_gemset() {
gemset=$1
if [ -z "$gemset" ]; then gemset="$BAPI_GEMSET"; fi
rvm gemset use "$gemset"
}
function install_berks_api() {
gemset=$1
bapi_version=$2
god_version=$3
if [ -z "$gemset" ]; then gemset="$BAPI_GEMSET"; fi
if [ -z "$bapi_version" ]; then bapi_version="$BAPI_VERSION"; fi
if [ -z "$god_version" ]; then god_version="$GOD_VERSION"; fi
gemset_exists "$gemset"
if [ "$?" -ne 0 ]; then create_gemset "$gemset"; fi
load_gemset "$gemset"
gem install god --version "$god_version"
gem install berkshelf-api --version "$bapi_version"
}
function berks_api_running() {
god_bapi_proc_name=$1
if [ -z "$god_bapi_proc_name"]; then god_bapi_proc_name=$GOD_BAPI_PROC_NAME; fi
god status "$god_bapi_proc_name" &> /dev/null
}
function god_running() {
god status &> /dev/null
}
function run_berks_api() {
god_file_path=$1
god_log_path=$2
if [ -z "$god_file_path" ]; then god_file_path="$GOD_FILE_PATH"; fi
if [ -z "$god_log_path" ]; then god_log_path="$GOD_LOG_PATH"; fi
if [ ! -f "$god_file_path" ]; then
curl -s -o "$god_file_path" "$GOD_FILE_URL"
fi
berks_api_running
bapi_is_running=$?
god_running
god_is_running=$?
if [ "$god_is_running" -ne 0 ] && [ "$bapi_is_running" -ne 0 ]; then
# God and Berks API are not running
echo "Starting god and berks api . . ."
god -c "$god_file_path" -l "$god_log_path"
elif [ "$god_is_running" -eq 0 ] && [ "$bapi_is_running" -ne 0 ]; then
# God is running but the Berks API is not running
echo "Starting the Berkshelf API"
god load "$god_file_path"
else
echo "It seems that the Berkshelf API is already running"
fi
}
# Install The Berks API
rvm_installed
has_rvm=$?
berks_api_installed
has_bapi=$?
if [ "$has_rvm" -eq 0 ] && [ "$has_bapi" -ne 0 ]; then
echo 'installing the berkshelf api'
install_berks_api
fi
# Run the Berks API
run_berks_api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment