1 | """ |
||
2 | Enarksh |
||
3 | |||
4 | Copyright 2013-2016 Set Based IT Consultancy |
||
5 | |||
6 | Licence MIT |
||
7 | """ |
||
8 | from enarksh.DataLayer import DataLayer |
||
9 | from enarksh.xml_reader.resource import create_resource, ReadWriteLockResource, CountingResource |
||
10 | |||
11 | |||
12 | class Host: |
||
13 | """ |
||
14 | Program for loading a host definition into the database. |
||
15 | """ |
||
16 | |||
17 | # ------------------------------------------------------------------------------------------------------------------ |
||
18 | def __init__(self): |
||
19 | """ |
||
20 | Object constructor. |
||
21 | """ |
||
22 | self._hostname = '' |
||
23 | """ |
||
24 | The name of this host. |
||
25 | |||
26 | :type: str |
||
27 | """ |
||
28 | |||
29 | self._hst_id = 0 |
||
30 | """ |
||
31 | The ID of this host when it is stored in the databases. |
||
32 | |||
33 | :type: int |
||
34 | """ |
||
35 | |||
36 | self._resources = {} |
||
37 | """ |
||
38 | The resources of this host. |
||
39 | |||
40 | :type: dict |
||
41 | """ |
||
42 | |||
43 | # ------------------------------------------------------------------------------------------------------------------ |
||
44 | def load_db(self, hostname): |
||
45 | """ |
||
46 | Loads the definition of this host from the database. |
||
47 | |||
48 | :param str hostname: The name of the host that must be loaded. |
||
49 | """ |
||
50 | self._hostname = hostname |
||
51 | |||
52 | host = DataLayer.enk_reader_host_load_host(hostname) |
||
53 | self._hst_id = host['hst_id'] |
||
54 | |||
55 | resources_data = DataLayer.enk_back_get_host_resources() |
||
56 | for resource_data in resources_data: |
||
57 | resource = create_resource(resource_data['rtp_id'], resource_data['rsc_id'], None) |
||
58 | self._resources[resource.name] = resource |
||
59 | |||
60 | # ------------------------------------------------------------------------------------------------------------------ |
||
61 | def get_resource_by_name(self, resource_name): |
||
62 | """ |
||
63 | :param str resource_name: |
||
64 | |||
65 | :rtype: Resource|None |
||
66 | """ |
||
67 | if resource_name in self._resources: |
||
68 | return self._resources[resource_name] |
||
69 | |||
70 | return None |
||
71 | |||
72 | # ------------------------------------------------------------------------------------------------------------------ |
||
73 | def read_xml(self, xml): |
||
74 | """ |
||
75 | :param xml.etree.ElementTree.Element xml: |
||
76 | """ |
||
77 | for element in list(xml): |
||
78 | tag = element.tag |
||
79 | if tag == 'Hostname': |
||
80 | self._hostname = element.text |
||
81 | |||
82 | elif tag == 'Resources': |
||
83 | self._read_xml_resources(element) |
||
84 | |||
85 | else: |
||
86 | raise Exception("Unexpected tag '{0!s}'.".format(tag)) |
||
87 | |||
88 | # ------------------------------------------------------------------------------------------------------------------ |
||
89 | def validate(self): |
||
90 | """ |
||
91 | Validates this node against rules which are not imposed by XSD. |
||
92 | """ |
||
93 | errors = [] |
||
94 | self._validate_helper(errors) |
||
95 | |||
96 | # ------------------------------------------------------------------------------------------------------------------ |
||
97 | def _validate_helper(self, errors): |
||
98 | """ |
||
99 | Helper function for validation this node. |
||
100 | |||
101 | :param list errors: A list of error messages. |
||
102 | """ |
||
103 | if self._hostname != 'localhost': |
||
104 | err = {'uri': self.get_uri(), |
||
105 | 'rule': 'Currently, only localhost is supported.', |
||
106 | 'error': "Hostname must be 'localhost', found: '{0!s}'.".format(self._hostname)} |
||
107 | errors.append(err) |
||
108 | |||
109 | # ------------------------------------------------------------------------------------------------------------------ |
||
110 | def get_uri(self, obj_type='host'): |
||
111 | """ |
||
112 | Returns the URI of this host. |
||
113 | |||
114 | :param str obj_type: The entity type. |
||
115 | |||
116 | :rtype: str |
||
117 | """ |
||
118 | return '//' + obj_type + '/' + self._hostname |
||
119 | |||
120 | # ------------------------------------------------------------------------------------------------------------------ |
||
121 | def store(self): |
||
122 | """ |
||
123 | Stores the definition of this host into the database. |
||
124 | """ |
||
125 | # Get uri_id for this host. |
||
126 | uri_id = DataLayer.enk_misc_insert_uri(self.get_uri()) |
||
127 | |||
128 | # Store the definition of the host self. |
||
129 | self._store_self(uri_id) |
||
130 | |||
131 | # Store the resources of this host. |
||
132 | for resource in self._resources.values(): |
||
133 | resource.store(self._hst_id, None) |
||
134 | |||
135 | # ------------------------------------------------------------------------------------------------------------------ |
||
136 | def _store_self(self, uri_id): |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
137 | """ |
||
138 | Stores the definition of this host into the database. |
||
139 | |||
140 | :param int uri_id: The ID of the URI of this node. |
||
141 | """ |
||
142 | details = DataLayer.enk_reader_host_load_host(self._hostname) |
||
143 | self._hst_id = details['hst_id'] |
||
144 | |||
145 | # ------------------------------------------------------------------------------------------------------------------ |
||
146 | def _read_xml_resources(self, xml): |
||
147 | """ |
||
148 | :param xml.etree.ElementTree.Element xml: |
||
149 | """ |
||
150 | for element in list(xml): |
||
151 | tag = element.tag |
||
152 | if tag == 'CountingResource': |
||
153 | resource = CountingResource(self) |
||
154 | |||
155 | elif tag == 'ReadWriteLockResource': |
||
156 | resource = ReadWriteLockResource(self) |
||
157 | |||
158 | else: |
||
159 | raise Exception("Unexpected tag '{0!s}'.".format(tag)) |
||
160 | |||
161 | resource.read_xml(element) |
||
162 | name = resource.name |
||
163 | # Check for resources with duplicate names. |
||
164 | if name in self._resources: |
||
165 | raise Exception("Duplicate resource '{0!s}'.".format(name)) |
||
166 | |||
167 | self._resources[name] = resource |
||
168 | |||
169 | # ---------------------------------------------------------------------------------------------------------------------- |
||
170 |