-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdockdash.py
More file actions
executable file
·106 lines (86 loc) · 3.36 KB
/
Copy pathdockdash.py
File metadata and controls
executable file
·106 lines (86 loc) · 3.36 KB
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
#!/usr/bin/env python3
__author__ = "Sean Landry"
__github__= "opensean"
__version__= "0.0.1"
__date__= "2017jul06"
__status__ = "development"
__description__= "simple dashboard for docker containers"
import subprocess
import logging
import yaml
import os
class DockDash():
def __init__(self, containers = None,
log = logging.INFO):
self.logger = self.init_logger(log)
self.logger.debug("logger intialized")
self.containers = self.inspect_containers(containers = containers)
self.config_path = os.path.join(os.environ.get('HOME'),
".tmuxinator/dockdash.yml")
self.config = self.build_config(containers = self.containers)
def init_logger(self, log = logging.DEBUG):
rootLogger = logging.getLogger()
class_logger = logging.getLogger(self.__class__.__name__)
## change root logger level if not equal to log arg, necessary if
## working with this class in ipython session, logging library uses
## a hierarchy structure meaning child loggers will pass args to
## parent first, root loggers default level is 30 (logging.WARNING),
## need to set root logger to log arg level to allow child logger a
## chance to handle log output
if rootLogger.level != log:
logging.basicConfig(level = log)
class_logger.setLevel(log)
return class_logger
def build_config(self, containers = None):
"""
Creates a dictionary that can be used to configure a tmuxinator
project.
Args:
containers (lst): a list of containers names or numerical IDs.
Returns:
config (dict): in tmuxinator format.
yaml.dump() with create a tmuxinator .yml template with the
following format:
# ~/.tmuxinator/dockdash.yml
name: dockdash
root: ~/
windows:
- dockdash:
layout: tiled
panes:
- docker attach container_name
"""
self.logger.debug("building tmuxinator dockdash.yml")
config = {}
config["name"] = "dockdash"
config["root"] = "~/"
config["windows"] = [{"dockdash": {"layout": "tiled", "panes":[]}}]
attach = "docker attach"
p = config['windows'][0]['dockdash']['panes']
for c in containers:
p.append(" ".join((attach, c)))
return config
def inspect_containers(self, containers = None):
"""
Find all running containers if none are specified.
Args:
containers (lst): a list of container names or numerical IDs.
Returns:
containers (lst): a list of container names or numerical IDs.
"""
self.logger.debug("inspecting containers")
if containers:
return containers
else:
proc = subprocess.Popen(["docker", "ps", "-a", "-q"],
stdout = subprocess.PIPE)
return [c for c in proc.communicate()[0].decode().strip().split('\n')]
def run(self):
"""
Start the tmuxinator project.
"""
self.logger.info('starting dockdash')
with open(self.config_path, "w") as cOUT:
yaml.dump(self.config, cOUT)
argLst = ["tmuxinator", "start", "dockdash"]
subprocess.run(argLst)