-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-alias.py
181 lines (156 loc) · 6.82 KB
/
docker-alias.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import argparse
import os.path
import sys
import termios
from lib.config import INIConfig, YAML_CONFIG_FILE_NAME, INI_CONFIG_FILE_PATH, VERSION
from lib.docker_util import DockerUtil
from lib.config_container import ConfigContainerUtil
class DockerAliasCLI(object):
ini_config = INIConfig()
add_description = 'Adds a new {yaml_config_file} to {ini_config_file}'.format(
yaml_config_file=YAML_CONFIG_FILE_NAME,
ini_config_file=INI_CONFIG_FILE_PATH
)
remove_description = 'Removes a {yaml_config_file} from {ini_config_file}'.format(
yaml_config_file=YAML_CONFIG_FILE_NAME,
ini_config_file=INI_CONFIG_FILE_PATH
)
list_description = 'List all containers and their commands'
run_description = 'Runs an command in an container'
build_description = 'Builds container-images'
usage = '''
docker-alias <command> [<args>] [--verbose]
Available Commands:
add {add_description}
build {build_description} [ all || <container_name> ]
list {list_description}
remove {remove_description}
run {run_description} [ <container_name> || <command_name> ]
Version: {version}
'''
quiet = False
def __init__(self):
parser = argparse.ArgumentParser(
description='docker-alias cli',
usage=self.usage.format(
add_description=self.add_description,
build_description=self.build_description,
list_description=self.list_description,
remove_description=self.remove_description,
run_description=self.run_description,
version=VERSION
)
)
parser.add_argument('command', help='Subcommand to run')
parser.add_argument(
'--quiet',
default=False,
action='store_true',
help='outputs only std-out and std-err from docker-subprocess'
)
args = parser.parse_args(sys.argv[1:2])
self.quiet = args.quiet
if not hasattr(self, args.command):
print('Unrecognized command')
parser.print_help()
sys.exit(1)
getattr(self, args.command)()
def add(self):
parser = argparse.ArgumentParser(
description=self.add_description
)
parser.add_argument('--path', help='optional path')
args = parser.parse_args(sys.argv[2:])
path = os.path.join(os.getcwd(), YAML_CONFIG_FILE_NAME)
if args.path:
path = args.path
if not path.endswith(YAML_CONFIG_FILE_NAME):
print('--path does not contain ' + YAML_CONFIG_FILE_NAME + '!')
sys.exit(1)
if not os.path.isfile(path):
print(YAML_CONFIG_FILE_NAME + ' does not exist!')
sys.exit(1)
self.ini_config.add_yaml_path(path)
print('Added ' + path + ' to config.ini')
def remove(self):
parser = argparse.ArgumentParser(
description=self.remove_description
)
parser.add_argument('--path', help='optional path')
args = parser.parse_args(sys.argv[2:])
path = os.path.join(os.getcwd(), YAML_CONFIG_FILE_NAME)
if args.path:
path = args.path
if not path.endswith(YAML_CONFIG_FILE_NAME):
print('--path does not contain ' + YAML_CONFIG_FILE_NAME + '!')
sys.exit(1)
if not os.path.isfile(path):
print(YAML_CONFIG_FILE_NAME + ' does not exist!')
sys.exit(1)
self.ini_config.remove_yaml_path(path)
print('Removed ' + path + ' from config.ini')
def list(self):
config_container_util = ConfigContainerUtil()
argparse.ArgumentParser(description=self.list_description)
config_containers = config_container_util.resolve_config_containers()
config_containers.reverse()
command_list = {}
for config_container in config_containers:
if not config_container.image:
config_container.image = DockerUtil.image_name_pattern.format(
fs_location_hash=config_container.fs_location_hash,
container_name=config_container.name
)
if config_container.commands:
for command in config_container.commands:
command_list[command.name] = " ".join(
DockerUtil(self.quiet).build_command(config_container, command=command)
)
else:
command_list[config_container] = " ".join(DockerUtil(self.quiet).build_command(config_container))
for key, cmd in command_list.items():
print(key + ": " + cmd + "\n")
def run(self):
config_container_util = ConfigContainerUtil()
parser = argparse.ArgumentParser(description=self.run_description, add_help=False)
args, unknown = parser.parse_known_args(sys.argv[2:])
if len(unknown) > 0:
wanted_container = unknown[0]
if '/' in wanted_container:
wanted_container = wanted_container.split('/')[-1]
unknown.pop(0)
attributes = unknown
for config_container in config_container_util.resolve_config_containers():
config_container = config_container_util.merge_config_containers(
config_container,
' '.join([wanted_container] + attributes)
)
if config_container.commands:
for command in config_container.commands:
if command.name == wanted_container:
sys.exit(DockerUtil(self.quiet).exec_docker(
config_container, command=command, attributes=attributes)
)
if config_container.name == wanted_container:
sys.exit(DockerUtil(self.quiet).exec_docker(config_container, attributes=attributes))
if not self.quiet:
print('Container ' + wanted_container + ' not found!')
sys.exit(1)
def build(self):
config_container_util = ConfigContainerUtil()
parser = argparse.ArgumentParser(description=self.build_description)
parser.add_argument('container')
args = parser.parse_args(sys.argv[2:])
if args.container == 'all':
for config_container in config_container_util.resolve_config_containers():
if config_container.build:
DockerUtil(self.quiet).build_image(config_container)
else:
for config_container in config_container_util.resolve_config_containers():
if config_container.build and config_container.name == args.container:
DockerUtil(self.quiet).build_image(config_container)
if __name__ == '__main__':
try:
DockerAliasCLI()
except KeyboardInterrupt:
pass