blob: e66ba44a39f5a50d8397df3681881c63edac978a [file] [log] [blame]
koder aka kdanilov108ac362017-01-19 20:17:16 +02001import logging
2import collections
3from typing import Dict, Sequence, NamedTuple
4
5from .node_interfaces import IRPCNode
6
7logger = logging.getLogger("wally")
8
9
10def log_nodes_statistic(nodes: Sequence[IRPCNode]) -> None:
11 logger.info("Found {0} nodes total".format(len(nodes)))
12
13 per_role = collections.defaultdict(int) # type: Dict[str, int]
14 for node in nodes:
15 for role in node.info.roles:
16 per_role[role] += 1
17
18 for role, count in sorted(per_role.items()):
19 logger.debug("Found {0} nodes with role {1}".format(count, role))
20
21
22
23OSRelease = NamedTuple("OSRelease",
24 [("distro", str),
25 ("release", str),
26 ("arch", str)])
27
28
29def get_os(node: IRPCNode) -> OSRelease:
30 """return os type, release and architecture for node.
31 """
32 arch = node.run("arch", nolog=True).strip()
33
34 try:
35 node.run("ls -l /etc/redhat-release", nolog=True)
36 return OSRelease('redhat', None, arch)
37 except:
38 pass
39
40 try:
41 node.run("ls -l /etc/debian_version", nolog=True)
42
43 release = None
44 for line in node.run("lsb_release -a", nolog=True).split("\n"):
45 if ':' not in line:
46 continue
47 opt, val = line.split(":", 1)
48
49 if opt == 'Codename':
50 release = val.strip()
51
52 return OSRelease('ubuntu', release, arch)
53 except:
54 pass
55
56 raise RuntimeError("Unknown os")