-
Notifications
You must be signed in to change notification settings - Fork 0
/
stature.py
106 lines (93 loc) · 3.73 KB
/
stature.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
#!/bin/env python
import sys
import logging
import time
import toml
import docker
import click
from cachet import Cachet
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
def is_swarm(cli):
"Is swarm mode enabled on this docker engine?"
return not cli.swarm.attrs == {}
def is_healthy(container):
container.reload() # causes a new request to daemon
try:
state = container.attrs['State']['Health']['Status']
if state == "healthy":
return True
elif state == "starting":
return False
else:
logging.error("unknown container state: %s", state)
except KeyError:
return False
def main(cli, cach, settings):
cs = cli.containers.list()
if not cs:
logging.error("No containers running!")
sys.exit(4)
if 'containers' not in settings:
logging.error("Write out your toml file! Try an empty containers section.")
for container in cs:
cach_id = None
name = container.name
labels = container.labels
if name in settings['containers']:
cach_id = settings['containers'][name]
elif filter(lambda k: "org.cachet" in k, labels.keys()):
if "org.cachet.id" in labels:
cach_id = labels['org.cachet.id']
elif "org.cachet.name" in labels:
args = {"name": labels["org.cachet.name"]}
if "org.cachet.description" in labels:
args["description"] = labels["org.cachet.description"]
if "org.cachet.link" in labels:
args["link"] = labels["org.cachet.link"]
logging.info("Creating Component: %s", args['name'])
# Check status then here
if is_healthy(container):
ret = cach.postComponents(status=1, **args)
else:
ret = cach.postComponents(status=2, **args)
# print(ret.json())
ret.raise_for_status()
cach_id = ret.json()['data']['id']
logging.info("Got component id: %s", cach_id)
settings['containers'][name] = cach_id
else:
logging.info(
"Container: %s not found in your toml file, nor does it have a docker label metadata, see the docs for refrence.", name)
continue
status = container.status
logging.debug("Cachet ID: %s",cach_id)
logging.debug("container: %s", container)
if cach_id:
if status == "running":
ret = cach.putComponentsByID(cach_id, status=1)
logging.debug(ret.text)
ret.raise_for_status()
elif status == "exited":
ret = cach.putComponentsByID(cach_id, status=4)
ret.raise_for_status()
return settings
@click.command(context_settings=CONTEXT_SETTINGS)
# @click.option("--all", '-a', default=False, is_flag=True, help="Check exited containers too")
@click.option("--keep", "-k", default=False, is_flag=True, help="Keep cachet ids in your toml file")
@click.option('--debug', default=False, is_flag=True)
@click.option('--verbose/--silent', default=True)
@click.option("--conf-file", '-f', default="docker2cachet.toml", type=click.Path(exists=True))
def run(conf_file, verbose, debug, keep):
settings = toml.load(conf_file)
if debug:
logging.basicConfig(level=logging.DEBUG)
if verbose:
logging.basicConfig(level=logging.INFO)
cli = docker.from_env()
cach = Cachet(settings['cachet']['url'], settings['cachet']['api_key'])
settings = main(cli, cach, settings)
if keep:
with open(conf_file, 'w') as f:
toml.dump(settings, f)
if __name__ == '__main__':
run()