1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import datetime
18
19 from cm_api.endpoints.types import *
20
21 __docformat__ = "epytext"
22
23 HOSTS_PATH = "/hosts"
24
25 -def create_host(resource_root, host_id, name, ipaddr, rack_id=None):
26 """
27 Create a host
28 @param resource_root: The root Resource object.
29 @param host_id: Host id
30 @param name: Host name
31 @param ipaddr: IP address
32 @param rack_id: Rack id. Default None
33 @return: An ApiHost object
34 """
35 apihost = ApiHost(resource_root, host_id, name, ipaddr, rack_id)
36 return call(resource_root.post, HOSTS_PATH, ApiHost, True, data=[apihost])[0]
37
39 """
40 Lookup a host by id
41 @param resource_root: The root Resource object.
42 @param host_id: Host id
43 @return: An ApiHost object
44 """
45 return call(resource_root.get, "%s/%s" % (HOSTS_PATH, host_id), ApiHost)
46
48 """
49 Get all hosts
50 @param resource_root: The root Resource object.
51 @return: A list of ApiHost objects.
52 """
53 return call(resource_root.get, HOSTS_PATH, ApiHost, True,
54 params=view and dict(view=view) or None)
55
57 """
58 Delete a host by id
59 @param resource_root: The root Resource object.
60 @param host_id: Host id
61 @return: The deleted ApiHost object
62 """
63 return call(resource_root.delete, "%s/%s" % (HOSTS_PATH, host_id), ApiHost)
64
65
67 _ATTRIBUTES = {
68 'hostId' : None,
69 'hostname' : None,
70 'ipAddress' : None,
71 'rackId' : None,
72 'status' : ROAttr(),
73 'lastHeartbeat' : ROAttr(datetime.datetime),
74 'roleRefs' : ROAttr(ApiRoleRef),
75 'healthSummary' : ROAttr(),
76 'healthChecks' : ROAttr(),
77 'hostUrl' : ROAttr(),
78 'commissionState' : ROAttr(),
79 'maintenanceMode' : ROAttr(),
80 'maintenanceOwners' : ROAttr(),
81 'numCores' : ROAttr(),
82 'numPhysicalCores' : ROAttr(),
83 'totalPhysMemBytes' : ROAttr(),
84 }
85
86 - def __init__(self, resource_root, hostId=None, hostname=None,
87 ipAddress=None, rackId=None):
89
91 return "<ApiHost>: %s (%s)" % (self.hostId, self.ipAddress)
92
95
97 """
98 Update this resource.
99 @return: The updated object.
100 """
101 return self._put('', ApiHost, data=self)
102
104 """
105 Retrieve the host's configuration.
106
107 The 'summary' view contains strings as the dictionary values. The full
108 view contains ApiConfig instances as the values.
109
110 @param view: View to materialize ('full' or 'summary')
111 @return: Dictionary with configuration data.
112 """
113 return self._get_config("config", view)
114
116 """
117 Update the host's configuration.
118
119 @param config: Dictionary with configuration to update.
120 @return: Dictionary with updated configuration.
121 """
122 return self._update_config("config", config)
123
124 - def get_metrics(self, from_time=None, to_time=None, metrics=None,
125 ifs=[], storageIds=[], view=None):
126 """
127 This endpoint is not supported as of v6. Use the timeseries API
128 instead. To get all metrics for a host with the timeseries API use
129 the query:
130
131 'select * where hostId = $HOST_ID'.
132
133 To get specific metrics for a host use a comma-separated list of
134 the metric names as follows:
135
136 'select $METRIC_NAME1, $METRIC_NAME2 where hostId = $HOST_ID'.
137
138 For more information see http://tiny.cloudera.com/tsquery_doc
139 @param from_time: A datetime; start of the period to query (optional).
140 @param to_time: A datetime; end of the period to query (default = now).
141 @param metrics: List of metrics to query (default = all).
142 @param ifs: network interfaces to query. Default all, use None to disable.
143 @param storageIds: storage IDs to query. Default all, use None to disable.
144 @param view: View to materialize ('full' or 'summary')
145 @return: List of metrics and their readings.
146 """
147 params = { }
148 if ifs:
149 params['ifs'] = ifs
150 elif ifs is None:
151 params['queryNw'] = 'false'
152 if storageIds:
153 params['storageIds'] = storageIds
154 elif storageIds is None:
155 params['queryStorage'] = 'false'
156 return self._get_resource_root().get_metrics(self._path() + '/metrics',
157 from_time, to_time, metrics, view, params)
158
160 """
161 Put the host in maintenance mode.
162
163 @return: Reference to the completed command.
164 @since: API v2
165 """
166 cmd = self._cmd('enterMaintenanceMode')
167 if cmd.success:
168 self._update(get_host(self._get_resource_root(), self.hostId))
169 return cmd
170
172 """
173 Take the host out of maintenance mode.
174
175 @return: Reference to the completed command.
176 @since: API v2
177 """
178 cmd = self._cmd('exitMaintenanceMode')
179 if cmd.success:
180 self._update(get_host(self._get_resource_root(), self.hostId))
181 return cmd
182
183 - def migrate_roles(self, role_names_to_migrate, destination_host_id,
184 clear_stale_role_data):
185 """
186 Migrate roles from this host to a different host.
187
188 Currently, this command applies only to HDFS NameNode, JournalNode,
189 and Failover Controller roles. In order to migrate these roles:
190
191 - HDFS High Availability must be enabled, using quorum-based storage.
192 - HDFS must not be configured to use a federated nameservice.
193
194 I{B{Migrating a NameNode role requires cluster downtime.}} HDFS, along
195 with all of its dependent services, will be stopped at the beginning
196 of the migration process, and restarted at its conclusion.
197
198 If the active NameNode is selected for migration, a manual failover
199 will be performed before the role is migrated. The role will remain in
200 standby mode after the migration is complete.
201
202 When migrating a NameNode role, the co-located Failover Controller
203 role must be migrated as well. The Failover Controller role name must
204 be included in the list of role names to migrate specified in the
205 arguments to this command (it will not be included implicitly). This
206 command does not allow a Failover Controller role to be moved by itself,
207 although it is possible to move a JournalNode independently.
208
209 @param role_names_to_migrate: list of role names to migrate.
210 @param destination_host_id: the id of the host to which the roles
211 should be migrated.
212 @param clear_stale_role_data: true to delete existing stale role data,
213 if any. For example, when migrating a
214 NameNode, if the destination host has
215 stale data in the NameNode data
216 directories (possibly because a NameNode
217 role was previously located there), this
218 stale data will be deleted before migrating
219 the role.
220 @return: Reference to the submitted command.
221 @since: API v10
222 """
223 args = dict(
224 roleNamesToMigrate = role_names_to_migrate,
225 destinationHostId = destination_host_id,
226 clearStaleRoleData = clear_stale_role_data)
227 return self._cmd('migrateRoles', data=args, api_version=10)
228
230 """
231 Update the rack ID of this host.
232 """
233 self.rackId = rackId
234 self._put_host()
235