Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Use Flask's built-in shell #339

Merged
merged 6 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ An in-depth guide can be followed at <a href=docs/development-create-slack-bot/r

### Verify Installation

1. `make dev-shell`
1. `make flaskshell`
1. Try sending a message to a channel for the development **Slack Workspace** with the command:

```python
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ shell: ## log into into app container -- bash-shell
shell-db: ## log into database container -- psql
docker-compose exec db psql -w --username "bbdev_user" --dbname "busy-beaver"

devshell: ## open ipython shell with application context
docker-compose exec app ipython -i scripts/dev/shell.py
flaskshell: ## open ipython shell with application context
docker-compose exec app flask shell

flaskcli: ## use flask cli to run commands args='args'te
docker-compose exec app flask $(args)
Expand Down
4 changes: 2 additions & 2 deletions busy_beaver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration

from .config import LOGGING_CONFIG, SENTRY_DSN, SENTRY_ENV_FILTER
from .config import ENVIRONMENT, LOGGING_CONFIG, SENTRY_DSN

# observability
load_dict_config(LOGGING_CONFIG)
logger = logging.getLogger(__name__)
if SENTRY_DSN:
sentry_sdk.init(
dsn=SENTRY_DSN,
environment=SENTRY_ENV_FILTER,
environment=ENVIRONMENT,
integrations=[FlaskIntegration(), SqlalchemyIntegration()],
)

Expand Down
21 changes: 20 additions & 1 deletion busy_beaver/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import inspect

from flask import Flask, request
from secure import SecureHeaders
from whitenoise import WhiteNoise

from . import models
from .blueprints import cfps_bp, events_bp, github_bp, healthcheck_bp, slack_bp, web_bp
from .common.oauth import OAuthError
from .config import DATABASE_URI, REDIS_URI, SECRET_KEY
from .common.wrappers import SlackClient
from .config import DATABASE_URI, ENVIRONMENT, REDIS_URI, SECRET_KEY
from .exceptions import NotAuthorized, StateMachineError, ValidationError
from .extensions import bootstrap, db, login_manager, migrate, rq
from .toolbox import make_response
Expand Down Expand Up @@ -68,4 +72,19 @@ def set_secure_headers(response):
secure_headers.flask(response)
return response

@app.shell_context_processor
def create_shell_context():
context = {"app": app, "db": db, "rq": rq}

db_models = inspect.getmembers(models, lambda member: inspect.isclass(member))
for name, reference in db_models:
context[name] = reference

# development environment should only have one slack instance
if ENVIRONMENT == "development":
installation = models.SlackInstallation.query.first()
context["slack"] = SlackClient(installation.bot_access_token)

return context

return app
4 changes: 3 additions & 1 deletion busy_beaver/apps/debug/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ def add(x, y):


"""
make devshell
# How to debug

make flaskshell

from datetime import timedelta
from busy_beaver.apps.debug.workflows import add
Expand Down
4 changes: 2 additions & 2 deletions busy_beaver/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

IN_PRODUCTION = os.getenv("IN_PRODUCTION", False)
SENTRY_ENV_FILTER = "production" if IN_PRODUCTION else "staging"
ENVIRONMENT = os.getenv("ENVIRONMENT", "development")
IN_PRODUCTION = ENVIRONMENT == "production"
TASK_QUEUE_MAX_RETRIES = 1

SECRET_KEY = os.getenv("SECRET_KEY", "abcdef").encode("utf-8")
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ services:
depends_on: &app_depends_on
- db
environment: &app_env_vars
ENVIRONMENT: development
PYTHONPATH: .
BASE_URL: http://0.0.0.0:5000
FLASK_APP: /app/busy_beaver/__init__.py
Expand Down
4 changes: 0 additions & 4 deletions helm/charts/busybeaver/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ Environment Variables
{{- define "busybeaver.env_vars" }}
- name: ENVIRONMENT
value: {{ .Values.environment }}
{{- if eq .Values.environment "production" }}
- name: IN_PRODUCTION
value: "1"
{{- end }}
- name: BASE_URL
value: "https://{{ .Values.ingress.host }}"
- name: PYTHONPATH
Expand Down
4 changes: 3 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
alembic==1.3.2
bootstrap-flask==1.4
boto3
boto3==1.15.0
cryptography==2.7
factory_boy==2.12.0
finite-state-machine==0.2.0
flask-login==0.5.0
Flask-Migrate==2.5.2
Flask-RQ2==18.3
flask-shell-ipython==0.4.1
Flask-SQLAlchemy==2.4.1
Flask-WTF==0.14.3
Flask==1.1.1
gunicorn==20.0.4
inflect==2.1.0
ipython==7.18.1
marshmallow==3.3.0
psycopg2-binary==2.8.4
python-dateutil==2.8.1
Expand Down
20 changes: 17 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,56 @@
#
aiohttp==3.5.4 # via slackclient
alembic==1.3.2 # via -r requirements.in, flask-migrate
appnope==0.1.0 # via ipython
asn1crypto==0.24.0 # via cryptography
async-timeout==3.0.1 # via aiohttp
attrs==19.1.0 # via aiohttp
backcall==0.2.0 # via ipython
blinker==1.4 # via sentry-sdk
bootstrap-flask==1.4 # via -r requirements.in
boto3==1.15.0 # via -r requirements.in
botocore==1.18.0 # via boto3, s3transfer
certifi==2019.3.9 # via requests, sentry-sdk
cffi==1.12.3 # via cryptography
chardet==3.0.4 # via aiohttp, requests
click==7.0 # via flask, rq
click==7.0 # via flask, flask-shell-ipython, rq
croniter==0.3.30 # via rq-scheduler
cryptography==2.7 # via -r requirements.in
decorator==4.4.2 # via validators
decorator==4.4.2 # via ipython, validators
factory_boy==2.12.0 # via -r requirements.in
faker==1.0.7 # via factory-boy
finite-state-machine==0.2.0 # via -r requirements.in
flask-login==0.5.0 # via -r requirements.in
flask-migrate==2.5.2 # via -r requirements.in
flask-rq2==18.3 # via -r requirements.in
flask-shell-ipython==0.4.1 # via -r requirements.in
flask-sqlalchemy==2.4.1 # via -r requirements.in, flask-migrate
flask-wtf==0.14.3 # via -r requirements.in
flask==1.1.1 # via -r requirements.in, bootstrap-flask, flask-login, flask-migrate, flask-rq2, flask-sqlalchemy, flask-wtf, sentry-sdk
flask==1.1.1 # via -r requirements.in, bootstrap-flask, flask-login, flask-migrate, flask-rq2, flask-shell-ipython, flask-sqlalchemy, flask-wtf, sentry-sdk
gunicorn==20.0.4 # via -r requirements.in
idna==2.7 # via requests, yarl
infinity==1.5 # via intervals
inflect==2.1.0 # via -r requirements.in
intervals==0.8.1 # via wtforms-components
ipython-genutils==0.2.0 # via traitlets
ipython==7.18.1 # via -r requirements.in, flask-shell-ipython
itsdangerous==1.1.0 # via flask, flask-wtf
jedi==0.17.2 # via ipython
jinja2==2.10.1 # via flask
jmespath==0.10.0 # via boto3, botocore
mako==1.0.12 # via alembic
markupsafe==1.1.1 # via jinja2, mako, wtforms
marshmallow==3.3.0 # via -r requirements.in
multidict==4.5.2 # via aiohttp, yarl
oauthlib==3.0.1 # via requests-oauthlib
parso==0.7.1 # via jedi
pexpect==4.8.0 # via ipython
pickleshare==0.7.5 # via ipython
prompt-toolkit==3.0.8 # via ipython
psycopg2-binary==2.8.4 # via -r requirements.in
ptyprocess==0.6.0 # via pexpect
pycparser==2.19 # via cffi
pygments==2.7.2 # via ipython
python-dateutil==2.8.1 # via -r requirements.in, alembic, botocore, croniter, faker
python-editor==1.0.4 # via alembic
python-json-logger==0.1.11 # via -r requirements.in
Expand All @@ -61,8 +73,10 @@ slackclient==2.5.0 # via -r requirements.in
sqlalchemy-utils==0.36.1 # via -r requirements.in
sqlalchemy==1.3.12 # via -r requirements.in, alembic, flask-sqlalchemy, sqlalchemy-utils
text-unidecode==1.2 # via faker
traitlets==5.0.5 # via ipython
urllib3==1.24.3 # via botocore, requests, sentry-sdk
validators==0.15.0 # via wtforms-components
wcwidth==0.2.5 # via prompt-toolkit
werkzeug==0.16.0 # via -r requirements.in, flask
whitenoise==5.0.1 # via -r requirements.in
wtforms-components==0.10.4 # via -r requirements.in
Expand Down
1 change: 0 additions & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
-r requirements.txt
black
flake8
ipython
isort
pdbpp
pip-tools
Expand Down
36 changes: 0 additions & 36 deletions scripts/dev/shell.py

This file was deleted.

15 changes: 0 additions & 15 deletions scripts/prod/shell.py

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/start_async_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration

from busy_beaver.app import create_app
from busy_beaver.config import LOGGING_CONFIG, SENTRY_DSN, SENTRY_ENV_FILTER
from busy_beaver.config import ENVIRONMENT, LOGGING_CONFIG, SENTRY_DSN
from busy_beaver.extensions import rq
from busy_beaver.toolbox.rq import retry_failed_job

Expand All @@ -14,7 +14,7 @@
if SENTRY_DSN:
sentry_sdk.init(
SENTRY_DSN,
environment=SENTRY_ENV_FILTER,
environment=ENVIRONMENT,
integrations=[RqIntegration(), SqlalchemyIntegration()],
)

Expand Down