Skip to content
Snippets Groups Projects

run_common.sh

Merged Kate Patterson requested to merge run_common into dev
1 file
+ 90
25
Compare changes
  • Side-by-side
  • Inline
+ 90
25
#!/bin/bash
GL_RESOURCE=~/resource
ENV=$1
BRANCH=$ENV
RESOURCE=~/resource/$PROJECT
REPO=~/code/$PROJECT
LOGS=$RESOURCE/logs
VENV=$REPO/venv
CONDA_ENV=~/miniconda3/envs/$PROJECT
DB_BACKUPS=$RESOURCE/db_backups
[[ "$ENV" != "master" ]] && SUFFIX="_$ENV" || SUFFIX=""
get_os()
{
unameOut="$(uname -s)"
@@ -13,6 +25,18 @@ get_os()
echo ${machine}
}
OS=$( get_os )
validate_env()
{
if [[ "$ENV" = "dev" ]] || [[ "$ENV" = "stg" ]] || [[ "$ENV" = "master" ]]; then
echo "Environment: $ENV"
else
echo "Invalid environment: $ENV. Expected dev, stg or master"
exit 1
fi
}
install_virtualenv()
{
if [[ "$(python -c "import virtualenv")" != "" ]]; then
@@ -20,37 +44,60 @@ install_virtualenv()
fi
}
init_conda()
init_venv_27()
{
if [[ ! -d "$REPO/venv" ]]; then
mkdir "$REPO/venv"
virtualenv -p python2.7 $REPO/venv
source venv/bin/activate
pip install -r $REPO/requirements.txt
fi
}
init_venv()
{
if [[ ! -d "$REPO/venv" ]]; then
mkdir "$REPO/venv"
virtualenv -p python3.7 $REPO/venv
source venv/bin/activate
pip install -r $REPO/requirements.txt
fi
}
init_conda_env()
{
cd $REPO
if [! -d ""]; then
conda env create -f environment.yml
~/miniconda3/bin/conda env create -f="$REPO/environment.yml"
fi
}
update_conda()
update_conda_env()
{
conda env update -f=environment.yml
~/miniconda3/bin/conda env update -f="$REPO/environment.yml"
}
update_code()
{
git status
echo "- Updating to latest"
echo "- Updating to latest"
git pull
echo "- Updating submodules"
echo "- Updating submodules"
git submodule update --recursive
echo "- Update complete"
echo "- Update complete"
}
migrate_database()
{
"$CONDA_ENV/bin/python $REPO/manage.py migrate"
}
status_redis()
{
echo "- Checking status of redis-server"
if [[ "$(pgrep -f "redis-server")" != "" ]]; then
echo " redis-server is running"
echo "- redis-server status: Running"
return 0
else
echo " redis-server is not running"
echo "- redis-server status: Not running"
return 1
fi
}
@@ -58,7 +105,7 @@ status_redis()
stop_redis()
{
if status_redis; then
echo "- Stopping redis-server"
echo "-> Stopping redis-server"
pgrep -f "redis-server" | sudo xargs kill -9
fi
}
@@ -67,19 +114,18 @@ start_redis()
{
if ! status_redis; then
mkdir -p $GL_RESOURCE
echo "- Starting redis-server"
echo "-> Starting redis-server"
redis-server >> ~/resource/redis.log 2>&1 &
fi
}
status_nginx()
{
echo "- Checking status of nginx"
if [[ "$(pgrep -f "nginx")" != "" ]]; then
echo " nginx is running"
echo "- nginx status: Running"
return 0
else
echo " nginx is not running"
echo "- nginx status: Not running"
return 1
fi
}
@@ -87,8 +133,8 @@ status_nginx()
stop_nginx()
{
if status_nginx; then
echo "- Stopping redis-server"
pgrep -f "redis-server" | sudo xargs kill -9
echo "-> Stopping nginx"
pgrep -f "nginx" | sudo xargs kill -9
fi
}
@@ -97,7 +143,7 @@ start_nginx()
if ! status_nginx; then
if [[ "$OS" = "Mac" ]]; then
sudo nginx
elif [[ $OS = "Linux" ]]; then
elif [[ "$OS" = "Linux" ]]; then
sudo systemctl start nginx
else
echo "start_nginx does not handle $OS"
@@ -107,7 +153,7 @@ start_nginx()
init_logs()
{
mkdir -p $LOGS
mkdir -p "$LOGS"
if [[ "$(ls $LOGS)" != "" ]]; then
sudo chmod ug+w $LOGS/*
fi
@@ -115,14 +161,33 @@ init_logs()
show_common_help()
{
echo "Additional commands:"
echo " get_os"
echo " install_virtualenv"
echo " init_conda, update_conda"
echo " validate_env"
echo " install_virtualenv, init_venv_27, init_venv"
echo " init_conda_env, update_conda_env"
echo " update_code"
echo " status_redis, start_redis, stop_redis"
echo " status_nginx, start_nginx, stop_nginx"
echo ""
echo "Usage: ./run.sh [environment] [command]"
}
GL_RESOURCE=~/resource
OS=$(get_os)
\ No newline at end of file
execute()
{
if [[ "$#" -ne 2 ]]; then
show_help
echo "Illegal number of parameters - expected environment and command."
exit 1
else
validate_env
init_logs
$2
RESULT=$?
if [[ "$RESULT" -ne 1 && "$RESULT" -ne 0 ]]; then
show_help
echo -e "\nError: Invalid parameter: $2\n"
exit 1
fi
fi
}
Loading