oshipka/oshipka.sh
2020-06-04 15:24:18 +02:00

179 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
if [ -v $OSHIPKA_PATH ]; then
echo "You need to specify OSHIPKA_PATH env variable"
exit 1
fi
echo "oshipka is at: $OSHIPKA_PATH"
#!/usr/bin/env bash
HELP="
Usage $0 [ bootstrap | model | db_migrate | db_upgrade | init | worker | web | venv | install | link | cert ]
bootstrap [PROJECT_PATH] Create a new project in PROJECT_PATH
model [MODEL_NAME] Create or update a model
init Install dev env
worker Start worker
web Start webapp
venv Init venv
install Install requirements
link Link dev oshipka
prod Run in prod
cert [DOMAIN] Install certificate
"
worker () {
source venv/bin/activate
python worker.py
}
web () {
source venv/bin/activate
python run.py
}
init_venv() {
virtualenv -p python3 venv
}
install_reqs() {
source venv/bin/activate
pip install -r requirements.txt
}
link_dev_oshipka() {
source venv/bin/activate
pip install -e ${OSHIPKA_PATH}
pip install -e ${TWW_PATH}
}
init() {
init_venv
install_reqs
link_dev_oshipka
}
install_cert() {
#[ ! -f /tmp/certbot-auto ] && wget -O /tmp/certbot-auto https://dl.eff.org/certbot-auto
#chmod +x /tmp/certbot-auto
#certbot=/tmp/certbot-auto
shift
PROJECT_DOMAIN=$1
sudo apt install certbot
certbot certonly --authenticator standalone --installer nginx --pre-hook "service nginx stop" --post-hook "service nginx start" --redirect --agree-tos --no-eff-email --email admin@app.com -d ${PROJECT_DOMAIN} --no-bootstrap
}
bootstrap() {
shift
PROJECT_PATH=$1
if [[ -z "$PROJECT_PATH" ]]; then
read -p "Enter project path: " PROJECT_PATH
fi
if [[ -z "$PROJECT_PATH" ]]; then
echo "ERROR: Specify project path"
exit 1
else
echo "INFO: Project path: $PROJECT_PATH"
PROJECT_PATH=`realpath $PROJECT_PATH`
echo "INFO: Absolute project path: $PROJECT_PATH"
if [[ "$PROJECT_PATH" == $OSHIPKA_PATH* ]]; then
echo "ERROR: Project path can't be inside this directory. Exiting..."
exit 1
fi
if [ -d $PROJECT_PATH ]; then
echo "ERROR: Project path exists. Please remove or specify another. Exiting..."
exit 1
else
echo "INFO: Project path doesn't exist, creating..."
mkdir -p $PROJECT_PATH
fi
fi
PROJECT_NAME=$(basename $PROJECT_PATH)
echo "INFO: Bootstrapping project $PROJECT_NAME..."
mkdir -p ${PROJECT_PATH}
cp -r ${OSHIPKA_PATH}/bootstrap/* ${PROJECT_PATH}/
cp ${OSHIPKA_PATH}/bootstrap/.gitignore ${PROJECT_PATH}/.gitignore
cd ${PROJECT_PATH}
init_venv
link_dev_oshipka
source venv/bin/activate
python manager.py db init
python manager.py db migrate -m "Initial migration."
python init_db.py
python manager.py db upgrade
}
run_in_prod() {
shift
PORT=$1
source venv/bin/activate
gunicorn -w 4 -b 0.0.0.0:${PORT} run:app
}
model() {
shift
source venv/bin/activate
python "${OSHIPKA_PATH}/vm_gen/vm_gen.py" "`pwd`"
}
db_migrate() {
shift
source venv/bin/activate
python init_db.py
db_upgrade
python manager.py db migrate
_post_migrate
}
db_upgrade() {
shift
source venv/bin/activate
python init_db.py
python manager.py db upgrade
}
_post_migrate() {
for i in migrations/versions/*.py; do
sed -i "s/sqlalchemy_utils.types.choice.ChoiceType(length=255), /sa.String(), / " "$i";
done
}
command_main() {
INITIAL_COMMAND=$1
case "$INITIAL_COMMAND" in
bootstrap) bootstrap "$@"
;;
model) model "$@"
;;
db_migrate) db_migrate "$@"
;;
db_upgrade) db_upgrade "$@"
;;
init) init "$@"
;;
worker) worker "$@"
;;
web) web "$@"
;;
venv) init_venv "$@"
;;
install) install_reqs "$@"
;;
link) link_dev_oshipka "$@"
;;
prod) run_in_prod "$@"
;;
cert) install_cert "$@"
;;
*) >&2 echo -e "${HELP}"
return 1
;;
esac
return $?
}
command_main "$@"