forked from python-oca/python-oca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost.py
More file actions
140 lines (114 loc) · 3.78 KB
/
host.py
File metadata and controls
140 lines (114 loc) · 3.78 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
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
# -*- coding: UTF-8 -*-
from .pool import Pool, PoolElement, Template, extractString
class HostShare(Template):
def __repr__(self):
return '<oca.vm.HostShare()>'
class Host(PoolElement):
METHODS = {
'info': 'host.info',
'allocate': 'host.allocate',
'delete': 'host.delete',
'enable': 'host.enable',
'update': 'host.update'
}
INIT = 0
MONITORING_MONITORED = 1 # Currently monitoring, previously MONITORED
MONITORED = 2
ERROR = 3
DISABLED = 4
MONITORING_ERROR = 5 # Currently monitoring, previously ERROR
MONITORING_INIT = 6 # Currently monitoring, previously initialized
MONITORING_DISABLED = 7 # Currently monitoring, previously DISABLED
HOST_STATES = ['INIT', 'MONITORING_MONITORED', 'MONITORED', 'ERROR', 'DISABLED',
'MONITORING_ERROR', 'MONITORING_INIT', 'MONITORING_DISABLED']
SHORT_HOST_STATES = {
'INIT': 'on',
'MONITORING_MONITORED': 'on',
'MONITORED': 'on',
'ERROR': 'err',
'DISABLED': 'off',
'MONITORING_ERROR': 'on',
'MONITORING_INIT': 'on',
'MONITORING_DISABLED': 'on',
}
XML_TYPES = {
'id': int,
'name': extractString,
'state': int,
'im_mad': extractString,
'vm_mad': extractString,
'vn_mad': extractString,
'last_mon_time': int,
'cluster': extractString,
'cluster_id': int,
'vm_ids': ['VMS', lambda vms: [int(vmid.text) for vmid in vms]],
'template': ['TEMPLATE', Template],
'host_share': ['HOST_SHARE', HostShare],
}
ELEMENT_NAME = 'HOST'
@staticmethod
def allocate(client, hostname, im, vmm, tm, cluster_id=-1):
"""
Adds a host to the host list
Arguments
``hostname``
Hostname machine to add
``im``
Information manager'
``vmm``
Virtual machine manager.
``tm``
Transfer manager
"""
host_id = client.call(Host.METHODS['allocate'], hostname, im, vmm, tm, cluster_id)
return host_id
def __init__(self, xml, client):
# remove 'vn_mad' attribute in OpenNebula >= 5
xml_types = Host.XML_TYPES
if client.one_version is None:
client.version()
if client.one_version >= '5' and 'vn_mad' in xml_types:
del xml_types['vn_mad']
super(Host, self).__init__(xml, client)
self.id = self['ID'] if self['ID'] else None
def enable(self):
"""
Enable this host
"""
self.client.call(self.METHODS['enable'], self.id, True)
def disable(self):
"""
Disable this host.
"""
self.client.call(self.METHODS['enable'], self.id, False)
def update(self, template, merge=False):
"""
Update the template of this host. If merge is false (default),
the existing template is replaced.
"""
self.client.call(self.METHODS['update'], self.id, template, 1 if merge else 0)
@property
def str_state(self):
"""
String representation of host state.
One of 'INIT', 'MONITORING', 'MONITORED', 'ERROR', 'DISABLED'
"""
return self.HOST_STATES[int(self.state)]
@property
def short_state(self):
"""
Short string representation of host state. One of 'on', 'off', 'err'
"""
return self.SHORT_HOST_STATES[self.str_state]
def __repr__(self):
return '<oca.Host("%s")>' % self.name
class HostPool(Pool):
METHODS = {
'info': 'hostpool.info',
}
def __init__(self, client):
super(HostPool, self).__init__('HOST_POOL', 'HOST', client)
def _factory(self, xml):
h = Host(xml, self.client)
h._convert_types()
return h