oshipka/oshipka.sh

215 lines
5.3 KiB
Bash
Raw Normal View History

2020-03-18 12:32:40 +01:00
#!/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="
2020-06-04 16:40:52 +02:00
Usage $0 [ bootstrap | model | db_migrate | db_upgrade | db_populate | db_recreate | db_purge_recreate | init | worker | web | venv | install | link | cert ]
2020-03-18 12:32:40 +01:00
bootstrap [PROJECT_PATH] Create a new project in PROJECT_PATH
2020-05-02 22:28:37 +02:00
init Install dev env
2020-06-04 16:40:52 +02:00
model Create or update a model from files in webapp/view_models/*.yaml
db_migrate DB migration
db_upgrade DB upgrade to last migration
db_populate Populate db with data from data_static/ and populate.py
db_recreate Delete the database, recreate to latest migration and populate
db_purge_recreate Same as db_recreate but also purge the migrations
2020-03-19 14:13:58 +01:00
worker Start worker
web Start webapp
2020-05-02 22:28:37 +02:00
2020-03-26 09:17:17 +01:00
venv Init venv
install Install requirements
2020-04-15 21:13:28 +02:00
link Link dev oshipka
2020-04-15 21:33:31 +02:00
prod Run in prod
2020-04-15 21:13:28 +02:00
cert [DOMAIN] Install certificate
2020-03-18 12:32:40 +01:00
"
2020-03-19 14:13:58 +01:00
worker () {
2020-03-25 16:23:24 +01:00
source venv/bin/activate
2020-03-20 12:44:42 +01:00
python worker.py
2020-03-19 14:13:58 +01:00
}
web () {
2020-03-25 16:23:24 +01:00
source venv/bin/activate
2020-03-19 14:13:58 +01:00
python run.py
}
2020-03-25 16:23:24 +01:00
init_venv() {
virtualenv -p python3 venv
}
install_reqs() {
source venv/bin/activate
pip install -r requirements.txt
}
2020-04-15 21:13:28 +02:00
link_dev_oshipka() {
source venv/bin/activate
pip install -e ${OSHIPKA_PATH}
2020-05-02 13:40:29 +02:00
pip install -e ${TWW_PATH}
2020-04-15 21:13:28 +02:00
}
2020-05-02 22:28:37 +02:00
init() {
init_venv
install_reqs
link_dev_oshipka
}
2020-04-15 21:13:28 +02:00
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
}
2020-03-18 12:32:40 +01:00
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}/
2020-03-25 16:23:24 +01:00
cp ${OSHIPKA_PATH}/bootstrap/.gitignore ${PROJECT_PATH}/.gitignore
2020-06-04 22:11:57 +02:00
mkdir ${PROJECT_PATH}/data
mkdir ${PROJECT_PATH}/webapp/view_models
2020-03-26 09:17:17 +01:00
cd ${PROJECT_PATH}
2020-03-25 16:23:24 +01:00
init_venv
2020-04-15 21:13:28 +02:00
link_dev_oshipka
2020-06-04 15:24:18 +02:00
source venv/bin/activate
python manager.py db init
2020-06-04 16:40:52 +02:00
python manager.py db migrate -m "001"
_post_migrate
2020-06-04 15:24:18 +02:00
python manager.py db upgrade
2020-03-18 12:32:40 +01:00
}
2020-04-15 21:33:31 +02:00
run_in_prod() {
shift
PORT=$1
2020-04-15 21:56:06 +02:00
source venv/bin/activate
2020-04-15 21:33:31 +02:00
gunicorn -w 4 -b 0.0.0.0:${PORT} run:app
}
2020-06-03 16:00:51 +02:00
model() {
shift
source venv/bin/activate
2020-06-03 18:11:37 +02:00
python "${OSHIPKA_PATH}/vm_gen/vm_gen.py" "`pwd`"
2020-06-03 16:00:51 +02:00
}
2020-06-04 15:24:18 +02:00
db_migrate() {
shift
source venv/bin/activate
python manager.py db migrate
_post_migrate
}
db_upgrade() {
shift
source venv/bin/activate
python manager.py db upgrade
}
2020-06-04 16:40:52 +02:00
db_purge_recreate() {
shift
source venv/bin/activate
rm -rf data/db.sqlite data/search_index migrations/
python manager.py db init
db_migrate
db_upgrade
db_populate
}
db_populate() {
shift
source venv/bin/activate
python init_populate.py
}
db_recreate() {
shift
source venv/bin/activate
rm -rf data/db.sqlite data/search_index
db_upgrade
db_populate
}
2020-06-04 15:24:18 +02:00
_post_migrate() {
for i in migrations/versions/*.py; do
sed -i "s/sqlalchemy_utils.types.choice.ChoiceType(length=255), /sa.String(), / " "$i";
2020-06-04 16:40:52 +02:00
sed -i "s/oshipka.persistance.LiberalBoolean(), /sa.Boolean(), / " "$i";
2020-06-04 15:24:18 +02:00
done
}
2020-03-18 12:32:40 +01:00
command_main() {
INITIAL_COMMAND=$1
case "$INITIAL_COMMAND" in
bootstrap) bootstrap "$@"
2020-03-19 14:13:58 +01:00
;;
2020-06-03 16:00:51 +02:00
model) model "$@"
;;
2020-06-04 15:24:18 +02:00
db_migrate) db_migrate "$@"
;;
db_upgrade) db_upgrade "$@"
;;
2020-06-04 16:40:52 +02:00
db_populate) db_populate "$@"
;;
db_recreate) db_recreate "$@"
;;
db_purge_recreate) db_purge_recreate "$@"
;;
2020-05-02 22:28:37 +02:00
init) init "$@"
;;
2020-03-19 14:13:58 +01:00
worker) worker "$@"
;;
2020-03-26 09:17:17 +01:00
web) web "$@"
;;
venv) init_venv "$@"
;;
install) install_reqs "$@"
;;
2020-04-15 21:13:28 +02:00
link) link_dev_oshipka "$@"
;;
2020-04-15 21:33:31 +02:00
prod) run_in_prod "$@"
;;
2020-04-15 21:13:28 +02:00
cert) install_cert "$@"
2020-03-18 12:32:40 +01:00
;;
*) >&2 echo -e "${HELP}"
return 1
;;
esac
return $?
}
2020-03-26 09:17:17 +01:00
command_main "$@"