-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydock.py
153 lines (128 loc) · 3.73 KB
/
pydock.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pathlib
import os
import subprocess
import tempfile
import click
import yaml
import docker_compose
import structures
import service_template
import exceptions
@click.group()
def cli():
pass
@cli.command()
@click.option('--depends-on','-d' , multiple=True)
@click.option('--requires-packages', '-r', multiple=True)
@click.option('--base-python-image', '-b', default='python')
def init(depends_on, requires_packages, base_python_image):
"""
sets up a base Dockerfile and service definition file
"""
_generate_requirements_file(requires_packages)
_generate_docker_file(base_python_image)
_generate_pydock_service_file(
app_name=_app_name(),
depends_on=depends_on
)
@cli.command()
def exec():
"""
opens a terminal shell in the app container
"""
_exec()
@cli.command()
def down():
"""
stops and removes the running containers, networks and volumes
"""
_down()
@cli.command()
@click.argument('tag')
def build(tag):
"""
builds app container with given tag
"""
_build(tag)
def _down():
subprocess.run(['docker-compose', 'down'])
def _exec():
contents = _service_def_file_contents()
_generate_docker_compose(contents)
_run_docker_compose()
def _build(tag):
contents = _service_def_file_contents()
_generate_docker_compose(contents)
os.environ['TAG'] = tag
subprocess.run(['docker-compose', 'build'])
def _app_name():
return pathlib.Path('.').absolute().name
def _service_def_file_contents():
path = pathlib.Path('pydock_service_def.py')
if not path.exists():
raise exceptions.PydockException('unable to locate service definition file')
with path.open() as f:
return f.read()
def _run_docker_compose():
# set tag for the development docker container
os.environ['TAG'] = 'latest'
subprocess.run(
['docker-compose', 'run', _app_name(), 'bash'],
)
def _generate_requirements_file(requires_packages):
with open('requirements.txt', 'w') as f:
for package in requires_packages:
print(package, file=f)
def _generate_docker_file(base_python_image):
with open('Dockerfile', 'w') as f:
f.write(_docker_file_template(base_python_image))
def _generate_pydock_service_file(app_name, depends_on):
dependencies = _service_dependency_def(depends_on)
app = _app_service_def(
app_name=app_name,
service_dependencies=dependencies
)
code = service_template.generate_service_file([app] + dependencies)
with open('pydock_service_def.py', 'w') as f:
print(code, file=f)
def _generate_docker_compose(code):
compose_yaml = docker_compose.generate_docker_compose(code)
with open('docker-compose.yaml', 'w') as f:
yaml.dump(compose_yaml, f, default_flow_style=False)
def _service_dependency_def(dependencies):
services = []
for d in dependencies:
name = d.split(':')[0] if ':' in d else d
services.append(
structures.ServiceDef(
name=name,
image=d
)
)
return services
def _app_service_def(app_name, service_dependencies):
return structures.ServiceDef(
name=app_name,
image=f'{app_name}:${{TAG}}',
build={
'context': '.',
'args': {
'app_name': pathlib.Path('.').absolute().name
}
},
volumes=[f'.:/{app_name}'],
deps=[s.name for s in service_dependencies]
)
def _base_image():
return 'python:3.7-slim'
def _docker_file_template(base_python_image):
template = f'''
FROM {base_python_image}
ARG app_name
ADD . /$app_name
WORKDIR /$app_name
RUN pip install -r requirements.txt
'''
return template
if __name__ == '__main__':
cli()