Skip to content

Commit

Permalink
applying black formatter to .py files in .
Browse files Browse the repository at this point in the history
  • Loading branch information
bri25yu committed Sep 13, 2021
1 parent e76755e commit dcf0c79
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 54 deletions.
111 changes: 61 additions & 50 deletions fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
TARGET_FLAG = "--target"
DEFAULT_TARGET = "prod"


def timestamp(c: Connection) -> str:
"""
Returns the server date-time, encoded as YYYYMMSS_HHMMSS.
"""
return c.run('date +%Y%m%d_%H%M%S').stdout.strip()
return c.run("date +%Y%m%d_%H%M%S").stdout.strip()


def create_dirs(c: Connection):
Expand All @@ -28,101 +29,98 @@ def create_dirs(c: Connection):
c.release_path,
)
for d in dirs:
c.run('mkdir -p {}'.format(d))
c.run("mkdir -p {}".format(d))


class DeployConfig(Config):
@staticmethod
def global_defaults():
hkn_defaults = {
'deploy': {
'name': 'default',
'user': 'hkn',
'host': 'apphost.ocf.berkeley.edu',
'path': {
'root': '/home/h/hk/hkn/hknweb',
'repo': 'repo',
'releases': 'releases',
'current': 'current',
'shared': 'shared',
"deploy": {
"name": "default",
"user": "hkn",
"host": "apphost.ocf.berkeley.edu",
"path": {
"root": "/home/h/hk/hkn/hknweb",
"repo": "repo",
"releases": "releases",
"current": "current",
"shared": "shared",
},
'repo_url': 'https://github.com/compserv/hknweb.git',
'branch': 'master',
'linked_files': [],
'linked_dirs': [],
'keep_releases': 10,
"repo_url": "https://github.com/compserv/hknweb.git",
"branch": "master",
"linked_files": [],
"linked_dirs": [],
"keep_releases": 10,
},
}
return merge_dicts(Config.global_defaults(), hkn_defaults)


targets = {
'prod': {
'deploy': {
'name': 'prod',
'branch': 'master',
"prod": {
"deploy": {
"name": "prod",
"branch": "master",
},
},
}

configs = {
target: DeployConfig(overrides=config)
for target, config in targets.items()
}
configs = {target: DeployConfig(overrides=config) for target, config in targets.items()}

# pprint(vars(configs['prod']))


def create_release(c: Connection):
print('-- Creating release')
print("-- Creating release")
git.check(c)
git.update(c)
c.commit = git.revision_number(c, c.commit)
git.create_archive(c)


def symlink_shared(c: Connection):
print('-- Symlinking shared files')
print("-- Symlinking shared files")
with c.cd(c.release_path):
c.run('ln -s {}/venv ./.venv'.format(c.shared_path), echo=True)
c.run("ln -s {}/venv ./.venv".format(c.shared_path), echo=True)


def decrypt_secrets(c):
print('-- Decrypting secrets')
print("-- Decrypting secrets")
with c.cd(c.release_path):
c.run('blackbox_postdeploy', echo=True)
c.run("blackbox_postdeploy", echo=True)


def install_deps(c: Connection):
print('-- Installing dependencies')
print("-- Installing dependencies")
with c.cd(c.release_path):
c.run("source .venv/bin/activate && make install-prod")


def django_migrate(c: Connection):
print('-- Migrating tables')
print("-- Migrating tables")
with c.cd(c.release_path):
c.run('HKNWEB_MODE=prod .venv/bin/python ./manage.py migrate')
c.run("HKNWEB_MODE=prod .venv/bin/python ./manage.py migrate")


def django_collectstatic(c: Connection):
print('-- Collecting static files')
print("-- Collecting static files")
with c.cd(c.release_path):
c.run('HKNWEB_MODE=prod .venv/bin/python ./manage.py collectstatic --noinput')
c.run("HKNWEB_MODE=prod .venv/bin/python ./manage.py collectstatic --noinput")


def symlink_release(c: Connection):
print('-- Symlinking current@ to release')
c.run('ln -sfn {} {}'.format(c.release_path, c.current_path), echo=True)
print("-- Symlinking current@ to release")
c.run("ln -sfn {} {}".format(c.release_path, c.current_path), echo=True)


def systemd_restart(c: Connection):
print('-- Restarting systemd unit')
c.run('systemctl --user restart hknweb.service', echo=True)
print("-- Restarting systemd unit")
c.run("systemctl --user restart hknweb.service", echo=True)


def setup(c: Connection, commit=None, release=None):
print('== Setup ==')
print("== Setup ==")
if release is None:
c.release = timestamp(c)
else:
Expand All @@ -137,43 +135,45 @@ def setup(c: Connection, commit=None, release=None):
c.commit = c.deploy.branch
else:
c.commit = commit
print('release: {}'.format(c.release))
print('commit: {}'.format(c.commit))
print("release: {}".format(c.release))
print("commit: {}".format(c.commit))
create_dirs(c)
if not path.file_exists(c, '{}/venv/bin/activate'.format(c.shared_path)):
if not path.file_exists(c, "{}/venv/bin/activate".format(c.shared_path)):
create_venv(c)


def create_venv(c: Connection):
c.run('python3.7 -m venv {}/venv'.format(c.shared_path))
c.run("python3.7 -m venv {}/venv".format(c.shared_path))


def update(c: Connection):
print('== Update ==')
print("== Update ==")
create_release(c)
symlink_shared(c)
decrypt_secrets(c)
if not path.dir_exists(c, '{}/venv'.format(c.shared_path)):
if not path.dir_exists(c, "{}/venv".format(c.shared_path)):
create_venv(c)
install_deps(c)
django_migrate(c)
django_collectstatic(c)


def publish(c: Connection):
print('== Publish ==')
print("== Publish ==")
symlink_release(c)
systemd_restart(c)


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):
with Connection(c.deploy.host, user=c.deploy.user, config=c.config) as c:
Expand All @@ -191,19 +191,30 @@ def rollback(c, target=DEFAULT_TARGET, release=None):
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\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')

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)
Expand Down
8 changes: 4 additions & 4 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import os
import sys

if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hknweb.settings')
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hknweb.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
Expand All @@ -15,8 +15,8 @@
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
'available on your PYTHONPATH environment variable? Did you '
'forget to activate a virtual environment?',
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?",
)
raise
execute_from_command_line(sys.argv)

0 comments on commit dcf0c79

Please sign in to comment.