This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
make
executable file
·147 lines (112 loc) · 4.73 KB
/
make
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
"""Run script.
"""
import logging
import os
import shlex
import shutil
import subprocess
import sys
from time import sleep
from typing import List
logger = logging.getLogger("cli")
try:
from clinner.command import Type, command
from clinner.run import Main
except Exception:
logger.error("Package clinner is not installed, run 'pip install clinner' to install it")
sys.exit(1)
try:
import jinja2
templates = jinja2.Environment(loader=jinja2.FileSystemLoader("."), trim_blocks=True, lstrip_blocks=True)
except Exception:
logger.error("Package jinja2 is not installed, run 'pip install jinja2' to install it")
sys.exit(1)
APP = "goobox-nodes"
IMAGE = f"goobox/{APP}"
APP_PATH = f"/srv/apps/{APP}/app"
@command(
command_type=Type.PYTHON,
args=(
(("-t", "--tag"), {"help": "Docker image tag", "default": f"{IMAGE}:latest"}),
(("-p", "--production"), {"help": "Build production image", "action": "store_true"}),
),
parser_opts={"help": "Build docker image"},
)
def build(*args, **kwargs):
context = {
"from_image": "python:3.7-slim",
"labels": ['maintainer="GooBox <[email protected]>"'],
"project": APP,
"app": APP.replace("-", "_"),
"runtime_packages": ["nginx"],
"build_packages": ["build-essential"],
"meta_files": ["alembic.ini", "setup.cfg", "nginx.conf"],
"production": kwargs["production"],
}
dockerfile = templates.get_template("Dockerfile.j2").render(**context)
logger.debug("---- Dockerfile ----\n%s\n--------------------", dockerfile)
subprocess.run(shlex.split(f"docker build -t {kwargs['tag']} -f- .") + list(args), input=dockerfile.encode("utf-8"))
@command(command_type=Type.PYTHON, parser_opts={"help": "Clean directory"})
def clean(*args, **kwargs):
if os.getuid() != 0:
logger.error("It is necessary to call clean with sudo")
return None
for path in (".pytest_cache", ".coverage", "test-results", "uvicorn.pid"):
try:
if os.path.isfile(path):
os.remove(path)
else:
shutil.rmtree(path)
logger.info("Removed successfully: %s", path)
except Exception:
logger.error("Cannot remove: %s", path)
@command(command_type=Type.SHELL, parser_opts={"help": "Start docker compose stack"})
def up(*args, **kwargs) -> List[List[str]]:
return [shlex.split(f"docker-compose up") + list(args)]
@command(command_type=Type.SHELL, parser_opts={"help": "Stop docker compose stack"})
def down(*args, **kwargs) -> List[List[str]]:
return [shlex.split(f"docker-compose down") + list(args)]
@command(
command_type=Type.SHELL,
args=((("--compose",), {"help": "Docker compose file", "default": "docker-compose.yml"}),),
parser_opts={"help": "Run command through entrypoint"},
)
def run(*args, **kwargs) -> List[List[str]]:
environment = " ".join([f"-e {i}" for i in kwargs["environment"]])
if kwargs["alone"]:
return [shlex.split(f"docker run {environment} {IMAGE}") + list(args)]
else:
return [
shlex.split(f"docker-compose -f {kwargs['compose']} pull --ignore-pull-failures"),
shlex.split(f"docker-compose -f {kwargs['compose']} run {environment} api") + list(args),
]
@command(command_type=Type.SHELL, parser_opts={"help": "Black code formatting"})
def black(*args, **kwargs):
kwargs["alone"] = True
return run("black", *args, **kwargs)
@command(command_type=Type.SHELL, parser_opts={"help": "Flake8 code analysis"})
def flake8(*args, **kwargs):
kwargs["alone"] = True
return run("flake8", *args, **kwargs)
@command(command_type=Type.SHELL, parser_opts={"help": "Isort imports formatting"})
def isort(*args, **kwargs):
kwargs["alone"] = True
return run("isort", *args, **kwargs)
@command(command_type=Type.SHELL, parser_opts={"help": "Run lint"})
def lint(*args, **kwargs) -> List[List[str]]:
return black("--check", ".", **kwargs) + flake8(**kwargs) + isort("--check-only", **kwargs)
@command(command_type=Type.SHELL, parser_opts={"help": "Run tests"})
def test(*args, **kwargs) -> List[List[str]]:
if not kwargs["alone"]:
sleep(5)
kwargs["compose"] = "docker-compose-ci.yml" if kwargs["ci"] else "docker-compose.yml"
kwargs["environment"].append("TESTING=true")
return run("pytest", *args, **kwargs)
class Make(Main):
def add_arguments(self, parser):
parser.add_argument("--ci", help="Run CI environment", action="store_true")
parser.add_argument("--alone", help="Run app container alone", action="store_true")
parser.add_argument("-e", "--environment", help="Environment variable", action="append", default=[])
if __name__ == "__main__":
sys.exit(Make().run())