/
hostlist.py
102 lines (75 loc) · 2.31 KB
/
hostlist.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
import json
from multiprocessing import Lock
from multiprocessing.managers import BaseManager
from host_state import HostAlive
from host import Host
from dynamic_host import DynamicHost
class HostList(list):
count = -1
lock = None
def __init__(self, hostlist=[]):
super(HostList, self).__init__()
self.count = len(hostlist)
if len(hostlist) > 0:
self.extend(hostlist)
# self.lock = Lock()
def append(self, host):
# with self.lock:
self.count = self.count + 1
super(HostList, self).append(host)
def create_host(self):
# with self.lock:
self.count = self.count + 1
host = Host(self.count)
super(HostList, self).append(host)
return host
def create_dynamic_host(self):
# with self.lock:
self.count = self.count + 1
host = DynamicHost(self.count)
super(HostList, self).append(host)
return host
def get_by_hostname(self, name):
# with self.lock:
for host in self:
hostname = host.get_capabilities('hostname')
if hostname == name:
return host
return None
def get_by_id(self, id):
# with self.lock:
for host in self:
if host.get_id() == id:
return host
return None
def get_by_state(self, state):
# with self.lock:
hosts = HostList()
for host in self:
if host.get_state() == state:
super(HostList, hosts).append(host)
return hosts
def dump(self):
# with self.lock:
for h in self:
h.dump()
def to_json(self):
# with self.lock:
l = []
for host in self:
l.append(host.to_json())
return l
@staticmethod
def from_json(data):
hostlist = HostList()
for l in data:
# host = hostlist.create_host()
host = Host()
d = eval(l)
host.update(d)
if 'id' in d:
host._id = int(d['id'])
else:
log.warning('No host ID in data')
hostlist.append(host)
return hostlist