-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.py
131 lines (115 loc) · 4.03 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import subprocess
import pathlib
from os import remove
from sys import argv
from yaml import dump
from re import sub
PROJECT_PATH = str(pathlib.Path().absolute())
def setup(project_name):
command = ['pipenv', 'run', 'django-admin', 'startproject', project_name, '.']
subprocess.run(command)
create_docker_compose_yaml(project_name)
replace_default_database_settings(project_name)
create_initial_dot_env(project_name)
create_caddyfile()
def create_docker_compose_yaml(project_name):
yaml_dict = {
"version": "3.7",
"services": {
"web": {
"container_name": f"{project_name}-web",
"build": {"context": ".", "dockerfile": "Dockerfile"},
"command": (
f"pipenv run gunicorn {project_name}.wsgi"
" --bind 0.0.0.0:8000"
),
"env_file": ".env",
"image": "docker.teste",
"depends_on": ["postgres"],
"restart": "on-failure",
"volumes": [".:/app"],
},
"caddy": {
"image": "caddy:2.3.0-alpine",
"container_name": f"caddy",
"links": ["web:web"],
"ports": ["80:80", "443:443"],
"volumes": ["./Caddyfile:/etc/caddy/Caddyfile"],
},
"migrations": {
"build": {"context": ".", "dockerfile": "Dockerfile"},
"command": "pipenv run python manage.py migrate",
"env_file": ".env",
"image": "docker.teste",
"depends_on": ["postgres"],
"restart": "on-failure",
"volumes": [".:/app"],
},
"postgres": {
"container_name": f"{project_name}-postgres",
"image": "postgres:13.3-alpine",
"volumes": ["postgres_data:/var/lib/postgresql/data/"],
"restart": "on-failure",
"env_file": ".env",
},
},
"volumes": {"postgres_data": None},
}
create_file('docker-compose.yaml', dump(yaml_dict, sort_keys=False))
def replace_default_database_settings(project_name):
settings_path = f'{project_name}/settings.py'
path_lib_str = 'from pathlib import Path'
new_conf = (
'{\n'
" 'ENGINE': 'django.db.backends.postgresql',\n"
" 'NAME': os.environ.get('POSTGRES_DB'),\n"
" 'USER': os.environ.get('POSTGRES_USER'),\n"
" 'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),\n"
" 'HOST': os.environ.get('POSTGRES_HOST'),\n"
" 'PORT': 5432,\n"
' },'
)
with open(settings_path, 'rt') as default_settings:
default_settings = default_settings.read()
settings = default_settings.replace(path_lib_str, f'{path_lib_str}\nimport os\n')
create_file(
settings_path,
sub(
r"({\n\s*\'E\w+.*)\n\s*\S\w+\S*\s.*\n.*",
new_conf,
settings,
)
)
def create_caddyfile():
create_file(
'Caddyfile',
(
'localhost {\n'
f' root {PROJECT_PATH[1:]}\n'
' file_server /static\n'
' reverse_proxy web:8000\n'
'}'
)
)
def create_initial_dot_env(project_name):
create_file(
'.env',
(
f'DJANGO_SETTINGS_MODULE={project_name}.settings\n'
'POSTGRES_DB=postgres\n'
'POSTGRES_USER=postgres\n'
'POSTGRES_PASSWORD=postgres\n'
'POSTGRES_HOST=postgres\n'
'PYTHONPATH=.'
)
)
def create_file(path, content):
with open(f'{PROJECT_PATH}/{path}', 'w') as file:
file.write(content)
if __name__ == '__main__':
if argv[-1] == __file__:
raise Exception('You must provide the project name.')
setup(argv[-1])
print(*['Project created', 'Now run "make up".'], sep='\n')
subprocess.run(['pipenv', 'uninstall', 'PyYAML'])
remove(f'{PROJECT_PATH}/setup.py')