Skip to content
Open
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ autoformatters: ## runs auto formatters
pip-compile:
python -m piptools compile --resolver=backtracking -o requirements/base.txt pyproject.toml
python -m piptools compile --resolver=backtracking --extra dev -o requirements/dev.txt pyproject.toml
python -m piptools compile --resolver=backtracking --extra prod -o requirements/prod.txt pyproject.toml

bootstrap: ## bootstrap project
pip install -r requirements/dev.txt
Expand Down
9 changes: 9 additions & 0 deletions fabconfig/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from fabconfig import santa_unchained
from fabconfig.base import LocalhostConfig

CONFIG_MAP = {
"santa_unchained": {
"local": LocalhostConfig,
"stage": santa_unchained.StageConfig,
},
}
56 changes: 56 additions & 0 deletions fabconfig/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os

from fabric.utils import _AttributeDict


class BaseConfig:
def __init__(self) -> None:
self.env = _AttributeDict()

self.env.git_server = "git@gitlab.deployed.pl"
self.env.git_repo = "spooler/spooler-service.git"
self.env.git_branch = "master"

self.env.project_dir = "santa-unchained-api"
self.env.project_name = "santa_unchained"

self.env.path = f"{self.env.project_dir}"
self.env.use_ssh_config = True
self.env.forward_agent = True
self.env.envname = "example"
self.env.pip_version = "22.2.2"
self.env.virtualenv_path = "~/venv"
self.env.virtualenv_args = "--python=python3.10"
self.env.pip_args = ""
self.env.project_path = self.env.project_name
self.env.requirements_file = "requirements/base.txt"
self.env.skip_rebuild_index_on_deploy = True
self.env.asyncworker = "celery"
self.env.warn_when_fixtures_fail = True
self.env.fixtures_format = "yaml"

self.collectstatic_excluded = ["*.scss", "*.md", "*.less", "demo", "src"]

self.env.excluded_files = self.get_excluded_files()

def init_roles(self) -> None:
"""by default all hosts have all roles"""
self.env.roledefs = {
"webserver": self.env.hosts,
"worker": self.env.hosts,
"extra": self.env.hosts,
}

def get_excluded_files(self) -> str:
return " ".join("--ignore=%s" % rule for rule in self.collectstatic_excluded)


class LocalhostConfig(BaseConfig):
def __init__(self) -> None:
super(LocalhostConfig, self).__init__()
self.env.hosts = ["localhost"]
self.env.envname = "local"
self.env["virtualenv_path"] = os.environ.get("VIRTUAL_ENV")
self.init_roles()
if not self.env["virtualenv_path"]:
print("Make sure your virtualenv is activated (with VIRTUAL_ENV set)")
21 changes: 21 additions & 0 deletions fabconfig/santa_unchained.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fabconfig.base import BaseConfig as DefaultConfig


class BaseConfig(DefaultConfig):
def __init__(self):
super(BaseConfig, self).__init__()
self.env.project_name = "santa_unchained"


class StageConfig(BaseConfig):
def __init__(self):
super().__init__()
self.env.envname = "stage"
self.env.settings = "santa_unchained.settings.production"
self.env.hosts = ["santa_unchained_stage@stage9.deployed.space:2222"]
self.env.vhost = "santa_unchained.deployed.space"
self.env.requirements_file = "requirements/prod.txt"
self.env.skip_dbbackup = True
self.init_roles()
self.env.roledefs["worker"] = []
self.env.roledefs["extra"] = []
72 changes: 72 additions & 0 deletions fabfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# encoding: utf-8

# fabfile format v3.0
from generix.deployment.base_fabfile import *
from generix.deployment.utils import extend_module_with_instance_methods

from fabconfig import CONFIG_MAP

# Example usages:
#
# First time run:
# fab <<env>> install
#
# To update:
# fab <<env>> deploy


def _update_config(project_name, instance_name, server):
"""
Update server settings
:param project_name: Project instance name. One of defined key from config_map.
:param instance_name: server instance name e.g. stage, stage2, stage3, prod, local
:param server: which server or group of servers to use e.g. `fab prod:santa_unchained,extra install`
"""
config = CONFIG_MAP[project_name][instance_name]()
env.update(config.env)
if server:
try:
env.hosts = getattr(env, "hosts", [])[int(server) - 1]
except (ValueError, IndexError):
env.hosts = env.roledefs[server]


def stage(project_name="santa_unchained", server=None):
"""
Use stage1 server settings
:param project_name: Project instance name. One of defined key from CONFIG_MAP.
:param server: which server or group of servers to use e.g. `fab prod:santa_unchained,extra install`
"""
_update_config(project_name=project_name, server=server, instance_name="stage")


def prod(project_name="santa_unchained", server=None):
"""
Use production server settings
:param project_name: Project instance name. One of defined key from CONFIG_MAP.
:param server: which server or group of servers to use e.g. `fab prod:santa_unchained,extra install`
"""
_update_config(project_name=project_name, server=server, instance_name="prod")


def localhost(project_name="santa_unchained", server=None):
print((yellow("Localhost")))

_update_config(project_name=project_name, server=server, instance_name="local")
virtualenv_activate()


instance = WithExtraDeployment(localhost=localhost)

# trick that allows using class-based fabric scripts
# note possible ways to reuse fabric methods:
#
# 1) inherit from base class, override
# 2) write a wrapper
# 3) after extend_module_with_instance_methods call re-implement fabric task as a function

extend_module_with_instance_methods(__name__, instance)

# override by re-implementing a task
# def base_task1():
# fab.run("echo 'directly from module'")
108 changes: 61 additions & 47 deletions fixtures/wishes.json
Original file line number Diff line number Diff line change
@@ -1,77 +1,91 @@
[
{
{
"model": "wishes.address",
"pk": 1,
"fields": {
"street": "Lea",
"post_code": "114",
"city": "Kraków",
"country": "Polska",
"lng": "19.902106",
"lat": "50.071832"
"street": "Lea",
"house_number": "114",
"post_code": "30-133",
"city": "Kraków",
"country": "Polska",
"lng": "19.902106",
"lat": "50.071832"
}
},
{
},
{
"model": "wishes.address",
"pk": 2,
"fields": {
"street": "Jana Matejki 1/5",
"post_code": "00-481",
"city": "Warszawa",
"country": "Polska",
"lng": "21.025711",
"lat": "52.224361"
"street": "Jana Matejki",
"house_number": "1",
"post_code": "00-481",
"city": "Warszawa",
"country": "Polska",
"lng": "21.025711",
"lat": "52.224361"
}
},
{
},
{
"model": "wishes.wishlist",
"pk": 1,
"fields": {
"name": "Joe Kernel",
"email": "kernel@example.com",
"content": "Dear Santa and helpers, \r\nI have been very good this year. I am expecting a little sister. I don’t want her. Momy says her will be fun. I heard girls stink. I will trade you my sister when she comes from the stork for a elf. I want a race car and a Garage set for Christmas. There will be sugar cookies and burritos waiting for you. \r\n\r\nThank you, Santa",
"status": "NEW",
"slug": "lisa-smith",
"address": 1
"name": "Joe Kernel",
"email": "kernel@example.com",
"content": "Dear Santa and helpers, \r\nI have been very good this year. I am expecting a little sister. I don’t want her. Momy says her will be fun. I heard girls stink. I will trade you my sister when she comes from the stork for a elf. I want a race car and a Garage set for Christmas. There will be sugar cookies and burritos waiting for you. \r\n\r\nThank you, Santa",
"status": "ACCEPTED",
"slug": "lisa-smith",
"address": 1,
"created_at": "2022-12-15T09:08:52.796Z"
}
},
{
},
{
"model": "wishes.wishlist",
"pk": 2,
"fields": {
"name": "Michael",
"email": "child@example.com",
"content": "Dear Santa, \r\nI want new car for my dad.\r\n\r\nThanks",
"status": "NEW",
"slug": "michael",
"address": 2
"name": "Michael",
"email": "child@example.com",
"content": "Dear Santa, \r\nI want new car for my dad.\r\n\r\nThanks",
"status": "NEW",
"slug": "michael",
"address": 2,
"created_at": "2022-12-15T06:48:28.102Z"
}
},
{
},
{
"model": "wishes.wishlistitem",
"pk": 1,
"fields": {
"wish_list": 1,
"name": "Lego blocks",
"approved": false
"wish_list": 1,
"name": "Lego blocks",
"approved": false
}
},
{
},
{
"model": "wishes.wishlistitem",
"pk": 2,
"fields": {
"wish_list": 1,
"name": "Sister",
"approved": false
"wish_list": 1,
"name": "Sister",
"approved": false
}
},
{
},
{
"model": "wishes.wishlistitem",
"pk": 3,
"fields": {
"wish_list": 2,
"name": "car",
"approved": true
"wish_list": 2,
"name": "car",
"approved": true
}
}
},
{
"model": "wishes.package",
"pk": 1,
"fields": {
"wish_list": 1,
"status": "sent",
"size": "LARGE",
"created_at": "2022-12-15T09:08:52.796Z"
}
}
]
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ dependencies = [
"djangorestframework",
"django-filter",
"drf-spectacular",
"djangorestframework-camel-case",
"drf-mixin-tools",
"django-cors-headers",
]

[project.optional-dependencies]
Expand All @@ -28,6 +31,9 @@ dev = [
"factory_boy",
"pytest-factoryboy",
]
prod = [
"gunicorn",
]

[tool.black]
max-line-length = 88
Expand Down
9 changes: 9 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ decorator==5.1.1
django==4.1.3
# via
# django-admin-display
# django-cors-headers
# django-debug-toolbar
# django-extensions
# django-filter
# djangorestframework
# drf-mixin-tools
# drf-spectacular
# santa-unchained (pyproject.toml)
django-admin-display==1.3.0
# via santa-unchained (pyproject.toml)
django-cors-headers==3.13.0
# via santa-unchained (pyproject.toml)
django-debug-toolbar==3.7.0
# via santa-unchained (pyproject.toml)
django-extensions==3.2.1
Expand All @@ -37,8 +41,13 @@ django-filter==22.1
# via santa-unchained (pyproject.toml)
djangorestframework==3.14.0
# via
# drf-mixin-tools
# drf-spectacular
# santa-unchained (pyproject.toml)
djangorestframework-camel-case==1.3.0
# via santa-unchained (pyproject.toml)
drf-mixin-tools==0.0.3
# via santa-unchained (pyproject.toml)
drf-spectacular==0.24.2
# via santa-unchained (pyproject.toml)
environs==9.5.0
Expand Down
9 changes: 9 additions & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ decorator==5.1.1
django==4.1.3
# via
# django-admin-display
# django-cors-headers
# django-debug-toolbar
# django-extensions
# django-filter
# djangorestframework
# drf-mixin-tools
# drf-spectacular
# santa-unchained (pyproject.toml)
django-admin-display==1.3.0
# via santa-unchained (pyproject.toml)
django-cors-headers==3.13.0
# via santa-unchained (pyproject.toml)
django-debug-toolbar==3.7.0
# via santa-unchained (pyproject.toml)
django-extensions==3.2.1
Expand All @@ -45,8 +49,13 @@ django-filter==22.1
# via santa-unchained (pyproject.toml)
djangorestframework==3.14.0
# via
# drf-mixin-tools
# drf-spectacular
# santa-unchained (pyproject.toml)
djangorestframework-camel-case==1.3.0
# via santa-unchained (pyproject.toml)
drf-mixin-tools==0.0.3
# via santa-unchained (pyproject.toml)
drf-spectacular==0.24.2
# via santa-unchained (pyproject.toml)
environs==9.5.0
Expand Down
Loading