Skip to content

Commit

Permalink
Merge pull request #72 from ochan1/no_vagrant_conda
Browse files Browse the repository at this point in the history
Standalone Development (no need to pass around keys) + fabfile Production
  • Loading branch information
ochan1 authored Feb 20, 2023
2 parents abfff21 + 17d4158 commit e6ecefc
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 111 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $ conda env create -f config/tbpweb-dev.yml # create our Conda Environment a
$ conda activate tbpweb-dev # enter our Conda Environment
$ python manage.py makemigrations # Create migrations files
$ python manage.py migrate # apply all database changes
$ python manage.py runserver 0.0.0.0:3000 # start local web server
$ python manage.py runserver # start local web server (127.0.0.1:8000)
$ conda deactivate # Exit the Conda Environment
```

Expand All @@ -37,9 +37,11 @@ You may choose to remove the development environment by running: `conda env remo

To run the Django development server (which runs a web server locally), run
```sh
$ python manage.py runserver 0.0.0.0:3000
$ python manage.py runserver
```
which will make the web site available at `http://localhost:3000`.
which will make the web site available at `http://127.0.0.1:8000`.

You may also use `python manage.py runserver 0.0.0.0:3000` to use `http://localhost:3000` or `http://0.0.0.0:3000`

If you would like to access the admin interface in the local web server, run
```sh
Expand All @@ -48,7 +50,7 @@ $ python manage.py createsuperuser

You will be prompted for some login info, after which you should be able to access the admin interface with your super user credentials at `http://localhost:3000/admin`.

If there are development conflicts between Operating System, a solution that works on Linux takes precedence (as the destination OS on Berkeley's OCF)! Unix developers should also try to make their code Windows-friendly for the Window developers.
If there are development conflicts between Operating Systems, a solution that works on Linux takes precedence (as the destination OS on Berkeley's OCF)! Unix developers should also try to make their code Windows-friendly for the Window developers.

## Deploy

Expand Down
2 changes: 1 addition & 1 deletion config/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ django-markdownx==2.0.27
django-picklefield==3.0.1
django==2.2.8
easy-thumbnails==2.7.1
fabric==2.4.0
fabric==2.6.0
gunicorn==19.9.0
idna==2.8
invoke==1.3.0
Expand Down
57 changes: 15 additions & 42 deletions fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from deploy import path

import argparse

TARGET_FLAG = "--target"
DEFAULT_TARGET = "prod"
import os

production_python = "TBPWEB_MODE=production python"

Expand Down Expand Up @@ -41,6 +39,8 @@ def global_defaults():
"name": "default",
"user": "tbp",
"host": "apphost.ocf.berkeley.edu",
"conda_env": "tbpweb-prod",
"run_blackbox_postdeploy": True,
"path": {
"root": "/home/t/tb/tbp/tbpweb",
"repo": "repo",
Expand Down Expand Up @@ -148,7 +148,7 @@ def setup(c: Connection, commit=None, release=None):

def create_conda(c: Connection):
print("-- Creating Conda Environment and Installing dependencies")
c.run("conda env create -f config/tbpweb-prod.yml")
c.run("conda env create --force -f config/tbpweb-prod.yml")


def activate_conda(c: Connection):
Expand All @@ -162,7 +162,6 @@ def update(c: Connection):
decrypt_secrets(c)
create_conda(c)
activate_conda(c)
install_deps(c)
django_migrate(c)
django_collectstatic(c)

Expand All @@ -177,14 +176,8 @@ def finish(c):
pass


# For the following @task functions, "target" is an ignored parameter
# It is a workaround to allow for use in using command line arguments
# For selecting a "target" in ns.configure
# Otherwise, fabfile will claim to not recognize it


@task
def deploy(c, target=DEFAULT_TARGET, commit=None):
def deploy(c, commit=None):
with Connection(c.deploy.host, user=c.deploy.user, config=c.config) as c:
setup(c, commit=commit)
update(c)
Expand All @@ -193,41 +186,21 @@ def deploy(c, target=DEFAULT_TARGET, commit=None):


@task
def rollback(c, target=DEFAULT_TARGET, release=None):
def rollback(c, release=None):
with Connection(c.deploy.host, user=c.deploy.user, config=c.config) as c:
setup(c, release=release)
update(c)
publish(c)
finish(c)


# Please add the "target" parameter if you are adding more @task functions
# to allow custom targets to be used (regardless if your function itself will use it or not)


def get_target(args):
target = args.target
if target not in configs:
message = '\n\tTarget Configuration "{}" is not a valid entry'.format(
TARGET_FLAG
)
message += "\n\tInvalid Entry: " + target
assert target in configs, message
return target


parser = argparse.ArgumentParser(
description='Target parameters for the fab file through the "fab" library'
)
parser.add_argument(
"--target",
default="prod",
help="The Target Configuration key to set the deployment setting",
)
args, unknown = parser.parse_known_args()

target_key = get_target(args)
print("Target Set:", target_key)
# Default mode is Prod
# Set a different target by setting the FAB_TARGET variable
tbpweb_mode = os.environ.get("FAB_TARGET", "prod")
if tbpweb_mode in ["dev", "prod"]:
# Validation
print("Target Set:", tbpweb_mode)
else:
raise ValueError(f"TARGET '{tbpweb_mode}' is not a valid value")

ns = Collection(deploy, rollback)
ns.configure(configs[target_key])
ns.configure(configs[tbpweb_mode])
2 changes: 0 additions & 2 deletions settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
# pylint: disable=W0401,W0614
if LOCAL_ENV == 'dev':
from .dev import *
elif LOCAL_ENV == 'staging':
from .staging import *
elif LOCAL_ENV == 'production':
from .production import *
else:
Expand Down
10 changes: 0 additions & 10 deletions settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
KEY_PATH = '/home/tbp/private'
if KEY_PATH not in sys.path:
sys.path.append(KEY_PATH)
try:
# pylint: disable=F0401
import settings.tbpweb_keys as tbpweb_keys
except ImportError:
print('Could not import tbpweb_keys. Please make sure tbpweb_keys.py exists '
'on the path, and that there are no errors in the module.')
sys.exit(1)

# Determine the path of your local workspace.
WORKSPACE_DJANGO_ROOT = os.path.abspath(
Expand Down Expand Up @@ -151,9 +144,6 @@
'compressor.finders.CompressorFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = tbpweb_keys.SECRET_KEY

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
Expand Down
20 changes: 20 additions & 0 deletions settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
# pylint: disable=F0401
from .project import HOSTNAME

try:
# pylint: disable=F0401
import settings.tbpweb_dev_keys as tbpweb_dev_keys
except ImportError:
print('Could not import tbpweb_dev_keys. Please make sure tbpweb_dev_keys.py exists '
'on the path, and that there are no errors in the module.')
sys.exit(1)

###############################################################################
# Private Variables for this dev instance
###############################################################################
Expand Down Expand Up @@ -48,6 +56,18 @@
}
}

SECRET_KEY = tbpweb_dev_keys.SECRET_KEY

# YouTube Secret Stuff
YT_USERNAME = 'BerkeleyTBP'
YT_PRODUCT = 'noiro'
YT_DEVELOPER_KEY = tbpweb_dev_keys.YT_DEVELOPER_KEY
YT_PASSWORD = tbpweb_dev_keys.YT_PASSWORD

# http://www.djangosnippets.org/snippets/1653/
RECAPTCHA_PRIVATE_KEY = tbpweb_dev_keys.RECAPTCHA_PRIVATE_KEY
RECAPTCHA_PUBLIC_KEY = tbpweb_dev_keys.RECAPTCHA_PUBLIC_KEY

# Check X-Forwarded-Protocol for http protocol so that request.is_secure()
# returns the correct value when dev server is behind a proxy.
# Make sure proxy config sets this header correctly:
Expand Down
20 changes: 19 additions & 1 deletion settings/production.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# pylint: disable=F0401
import os

from .tbpweb_keys import *
from .base import *
from .project import HOSTNAME
from .project import RESUMEQ_OFFICER_POSITION

try:
# pylint: disable=F0401
import settings.tbpweb_keys as tbpweb_keys
except ImportError:
print('Could not import tbpweb_keys. Please make sure tbpweb_keys.py exists '
'on the path, and that there are no errors in the module.')
sys.exit(1)

DEBUG = False
TEMPLATE_DEBUG = DEBUG
Expand All @@ -27,6 +33,18 @@
},
}

SECRET_KEY = tbpweb_keys.SECRET_KEY

# YouTube Secret Stuff
YT_USERNAME = 'BerkeleyTBP'
YT_PRODUCT = 'noiro'
YT_DEVELOPER_KEY = tbpweb_keys.YT_DEVELOPER_KEY
YT_PASSWORD = tbpweb_keys.YT_PASSWORD

# http://www.djangosnippets.org/snippets/1653/
RECAPTCHA_PRIVATE_KEY = tbpweb_keys.RECAPTCHA_PRIVATE_KEY
RECAPTCHA_PUBLIC_KEY = tbpweb_keys.RECAPTCHA_PUBLIC_KEY

# HTTPS support in production
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
Expand Down
12 changes: 0 additions & 12 deletions settings/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
environment would overwrite these. The base and site-specific settings files
must not overwrite these.
"""
# pylint: disable=F0401
import settings.tbpweb_keys as tbpweb_keys

# Custom setting used to include a short tag for the site in relevant content
# (like automatic email subject lines):
Expand Down Expand Up @@ -51,16 +49,6 @@
HELPDESK_SPAM_TO = TEST_ADDRESS
INDREL_SPAM_TO = TEST_ADDRESS

# YouTube Secret Stuff
YT_USERNAME = 'BerkeleyTBP'
YT_PRODUCT = 'noiro'
YT_DEVELOPER_KEY = tbpweb_keys.YT_DEVELOPER_KEY
YT_PASSWORD = tbpweb_keys.YT_PASSWORD

# http://www.djangosnippets.org/snippets/1653/
RECAPTCHA_PRIVATE_KEY = tbpweb_keys.RECAPTCHA_PRIVATE_KEY
RECAPTCHA_PUBLIC_KEY = tbpweb_keys.RECAPTCHA_PUBLIC_KEY

# Valid username regex
# Please use raw string notation (i.e. r'text') to keep regex sane.
VALID_USERNAME = r'^[a-z][a-z0-9]{2,29}$'
Expand Down
39 changes: 0 additions & 39 deletions settings/staging.py

This file was deleted.

21 changes: 21 additions & 0 deletions settings/tbpweb_dev_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
# Development only!
# If you need to add a key / password, add it inside tbpweb_keys.py in the OCF Production

# Development Secret Key (ok to share, but NEVER on Production)
SECRET_KEY = 'ayk+97c3^y&-u85=2&g&c5l3$*^y0hr^dunoo3d3c^wn0t)mnq'

# Other Production Keys
RECAPTCHA_PUBLIC_KEY = None
RECAPTCHA_PRIVATE_KEY = None

YT_PASSWORD = None
YT_DEVELOPER_KEY = None

# Production-only variables
PROD_DB_PASSWORD = None

# Development-only variables
DEV_DB_PASSWORD = None

EMAIL_HOST_PASSWORD = None

0 comments on commit e6ecefc

Please sign in to comment.