-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpie_docker.py
115 lines (92 loc) · 3.27 KB
/
pie_docker.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
"""
Python3.6+ only
"""
__VERSION__='0.0.2'
from pie import *
def _make_list_parameter_safe(ls):
return list(ls) if ls is not None else []
class Docker:
def __init__(self,options=None):
self.options=_make_list_parameter_safe(options)
def cmd(self,docker_cmd,cmd_options=None):
"""
Builds a command and runs it like this:
docker <self.options> <docker_cmd> <cmd_options>
"""
docker_options_str=' '.join(self.options)
cmd_options_str=' '.join(cmd_options) if cmd_options is not None else ''
c=f'docker {docker_options_str} {docker_cmd} {cmd_options_str}'
print(c)
cmd(c)
def build(self,context,options=None):
"""
Builds a command and runs it like this:
docker <self.options> build <options> <context>
"""
ops=_make_list_parameter_safe(options)
ops.append(context)
self.cmd('build',ops)
def run(self,image,cmd_and_args=None,options=None):
"""
Builds a command and runs it like this:
docker <self.options> run <options> <image> <cmd_and_args>
"""
ops=_make_list_parameter_safe(options)
ops.append(image)
if cmd_and_args:
ops.append(cmd_and_args)
self.cmd('run',ops)
def exec(self,container,cmd_and_args=None,options=None):
"""
Builds a command and runs it like this:
docker <self.options> exec <options> <container> <cmd_and_args>
"""
ops=_make_list_parameter_safe(options)
ops.append(container)
if cmd_and_args:
ops.append(cmd_and_args)
self.cmd('exec',ops)
def stop(self,containers,options=None):
"""
Builds a command and runs it like this:
docker <self.options> stop <options> <containers>
"""
ops=_make_list_parameter_safe(options)
if isinstance(containers,str):
containers=[containers]
ops.extend(containers)
self.cmd('stop',ops)
class Volume:
"""
Named volume commands
"""
def __init__(self,docker_obj,name):
self.docker_obj=docker_obj
self.name=name
def create(self,options=None):
ops=_make_list_parameter_safe(options)
ops.append(self.name)
self.docker_obj.cmd('volume create',ops)
def rm(self,options=None):
ops=_make_list_parameter_safe(options)
ops.append(self.name)
self.docker_obj.cmd('volume rm',ops)
def volume(self,name):
return self.Volume(self,name)
class Network:
"""
Named network commands
"""
def __init__(self,docker_obj,name):
self.docker_obj=docker_obj
self.name=name
def create(self,options=None):
ops=_make_list_parameter_safe(options)
ops.append(self.name)
self.docker_obj.cmd('network create',ops)
def rm(self,options=None):
ops=_make_list_parameter_safe(options)
ops.append(self.name)
self.docker_obj.cmd('network rm',ops)
def network(self,name):
return self.Network(self,name)