Skip to content

A flask developer's box of goodies. *not to be confused with the restaurant website builder

License

Notifications You must be signed in to change notification settings

PHX-Devs/bento-box

Repository files navigation

lad

bento-box

A flask developer's box of goodies.

code-sizecode-sizelicense

Table of Contents

Setup

Set Up the Vagrant box

  1. get the deps:
    • Vagrant
    • Virtualbox
    • optional: winscp, putty, puttygen
  2. open terminal/cmd in this dir
  3. vagrant up

SSH to the machine

2 options:

  • vagrant ssh
  • your ssh client of choice

Putty setup (optional)

TO USE PUTTY, convert the autogenerated private key to ppk

  • autogenerated: ./.vagrant/machines/default/virtualbox/private_key (pem)
  • putty: ./.vagrant/machines/default/virtualbox/private.ppk (putty) To Convert:
  1. run generate_putty_key.bat after 'vagrant up' completes
    • NOTE: you'll need winscp in your path for this step to work
    • ALTERNATIVELY: convert the key manually using puttygen
  2. configure putty session:
    • user: vagrant
    • host: localhost
    • port: 2222
    • private key: ./.vagrant/machines/default/virtualbox/private.ppk

Install on AWS, Google, Digital Ocean, etc

  1. make sure your instance is running centos8
  2. clone repo in /var/www/flask-modules
  3. run cloud_install.sh

Get Started

What you get

vagrant up

Gets you...

  • centos8
  • all rpms updated
  • python36 installed
  • flask installed
  • postgres 12 and postgis 3 installed
    • also a starter database called 'bento' and a schema called test
  • migra installed
  • empty 'root_app' module listening on the '/' path
  • hello world flask app installed and configured in nginx
  • example api module installed (with an actual database schema behind it)

Creating a new module

**for the sake of example, we'll act like we're adding a module named 'carbs'

  1. create a new dir in the root of this repo (/carbs/)
  2. make sure it has the following (probably best to copy one of the examples)
    • __init__.py (empty - if you're reading this in an editor... otherwise known as init.py)
    • carbs.py (main flask app here)
    • carbs.ini (uwsgi config items)
    • carbs.service (systemd service file)
    • wsgi.py (boilerplate - a bootstrapper for uwsgi to point to)
    • carbs.conf (nginx config file - be sure to change a few references)
    • ./templates/ dir (optional, if your module has a web ui) **If copy/pasting, make sure to review each of the above files
  3. add a line to /vagrant_provision/modules.sh for your new carbs module
    • will run next time your vagrant box provisions
    • to run the provision step manually (just for your module), comment out accordingly and run modules.sh
    • this step:
      • installs your module as a uwsgi proc running as a service
      • adds the nginx config to the appropriate dir
    • systemctl command will match the filename of your .service file (systemctl start carbs)
  4. restart nginx
    • 'systemctl restart nginx

Creating a new db schema

**for the sake of example, we'll act like we're adding a schema named 'carbs'

  1. add a schema create file in the db dir (/db/carbs_schema.sql)
    • this file should start with 'CREATE SCHEMA carbs;'
  2. add a line to /vagrant_provision/schemas.sh
    • runuser -l postgres -c "psql -U bento -f /var/www/flask-modules/db/carbs_schema.sql"
    • note: this will install the carbs schema to the bento database - if you've created your own database, use that

Creating a new cron job

**for the sake of example, we'll act like we're adding a cron named 'carbs'

  1. create the new dir in /crons/carbs
  2. make sure it has the following (probably best to copy the example_cron)
    • carbs.logrotate
    • carbs.py (main python cron job)
  3. add a line to /crons/crontab (make sure that there is a newline at the end of the file)
    • in our case */1 * * * * cd /var/www/flask-modules/crons/carbs && /bin/python3 /var/www/flask-modules/crons/carbs/carbs.py >> /var/log/carbs/carbs.log
  4. add a line to /vagrant_provision/crons.sh
    • crons+=(carbs)

Architecture

The following bash output is the directory structure and organization of bento-box:

tree
.
├── cloud_install.sh
├── config.py
├── crons
│   ├── crontab
│   └── example_cron
│       ├── example_cron.logrotate
│       ├── example_cron.py
│       └── say_hello.py
├── db
│   ├── create_database.sql
│   └── test_schema.sql
├── example_api
│   ├── example_api.conf
│   ├── example_api.ini
│   ├── example_api.py
│   ├── example_api.service
│   ├── __init__.py
│   └── wsgi.py
├── generate_putty_key.bat
├── hello_world
│   ├── hello_world.conf
│   ├── hello_world.ini
│   ├── hello_world.py
│   ├── hello_world.service
│   ├── __init__.py
│   ├── templates
│   │   └── entrees.html
│   └── wsgi.py
├── __init__.py
├── LICENSE
├── README.md
├── root_app
│   ├── __init__.py
│   ├── root_app.conf
│   ├── root_app.ini
│   ├── root_app.py
│   ├── root_app.service
│   ├── templates
│   │   ├── base_template.html
│   │   └── index.html
│   └── wsgi.py
├── static
│   ├── favicon.ico
│   └── image
│       ├── Bento.png
│       └── title_logo.png
├── utils
│   ├── db.py
│   └── __init__.py
├── Vagrantfile
└── vagrant_provision
    ├── crons.sh
    ├── dev_env.sh
    ├── modules.sh
    ├── nginx.sh
    ├── packages.sh
    ├── postgres.sh
    ├── schemas.sh
    └── static.conf

12 directories, 47 files

Note on db versioning/migrations

bento-box does not solve for db versioning at the moment. But it does include migra, a db diff tool. That's a good start toward a sane db "non-versioning" scheme. Read up on it here: https://djrobstep.com/docs/migra/quickstart

License

MIT © John Zechlin

About

A flask developer's box of goodies. *not to be confused with the restaurant website builder

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published