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 'entityStatus' : ROAttr(),
85 'clusterRef' : ROAttr(ApiClusterRef),
86 }
87
88 - def __init__(self, resource_root, hostId=None, hostname=None,
89 ipAddress=None, rackId=None):
91
93 return "<ApiHost>: %s (%s)" % (self.hostId, self.ipAddress)
94
97
99 """
100 Update this resource.
101 @return: The updated object.
102 """
103 return self._put('', ApiHost, data=self)
104
106 """
107 Retrieve the host's configuration.
108
109 The 'summary' view contains strings as the dictionary values. The full
110 view contains ApiConfig instances as the values.
111
112 @param view: View to materialize ('full' or 'summary')
113 @return: Dictionary with configuration data.
114 """
115 return self._get_config("config", view)
116
118 """
119 Update the host's configuration.
120
121 @param config: Dictionary with configuration to update.
122 @return: Dictionary with updated configuration.
123 """
124 return self._update_config("config", config)
125
126 - def get_metrics(self, from_time=None, to_time=None, metrics=None,
127 ifs=[], storageIds=[], view=None):
128 """
129 This endpoint is not supported as of v6. Use the timeseries API
130 instead. To get all metrics for a host with the timeseries API use
131 the query:
132
133 'select * where hostId = $HOST_ID'.
134
135 To get specific metrics for a host use a comma-separated list of
136 the metric names as follows:
137
138 'select $METRIC_NAME1, $METRIC_NAME2 where hostId = $HOST_ID'.
139
140 For more information see http://tiny.cloudera.com/tsquery_doc
141 @param from_time: A datetime; start of the period to query (optional).
142 @param to_time: A datetime; end of the period to query (default = now).
143 @param metrics: List of metrics to query (default = all).
144 @param ifs: network interfaces to query. Default all, use None to disable.
145 @param storageIds: storage IDs to query. Default all, use None to disable.
146 @param view: View to materialize ('full' or 'summary')
147 @return: List of metrics and their readings.
148 """
149 params = { }
150 if ifs:
151 params['ifs'] = ifs
152 elif ifs is None:
153 params['queryNw'] = 'false'
154 if storageIds:
155 params['storageIds'] = storageIds
156 elif storageIds is None:
157 params['queryStorage'] = 'false'
158 return self._get_resource_root().get_metrics(self._path() + '/metrics',
159 from_time, to_time, metrics, view, params)
160
162 """
163 Put the host in maintenance mode.
164
165 @return: Reference to the completed command.
166 @since: API v2
167 """
168 cmd = self._cmd('enterMaintenanceMode')
169 if cmd.success:
170 self._update(get_host(self._get_resource_root(), self.hostId))
171 return cmd
172
174 """
175 Take the host out of maintenance mode.
176
177 @return: Reference to the completed command.
178 @since: API v2
179 """
180 cmd = self._cmd('exitMaintenanceMode')
181 if cmd.success:
182 self._update(get_host(self._get_resource_root(), self.hostId))
183 return cmd
184
185 - def migrate_roles(self, role_names_to_migrate, destination_host_id,
186 clear_stale_role_data):
187 """
188 Migrate roles from this host to a different host.
189
190 Currently, this command applies only to HDFS NameNode, JournalNode,
191 and Failover Controller roles. In order to migrate these roles:
192
193 - HDFS High Availability must be enabled, using quorum-based storage.
194 - HDFS must not be configured to use a federated nameservice.
195
196 I{B{Migrating a NameNode role requires cluster downtime.}} HDFS, along
197 with all of its dependent services, will be stopped at the beginning
198 of the migration process, and restarted at its conclusion.
199
200 If the active NameNode is selected for migration, a manual failover
201 will be performed before the role is migrated. The role will remain in
202 standby mode after the migration is complete.
203
204 When migrating a NameNode role, the co-located Failover Controller
205 role must be migrated as well. The Failover Controller role name must
206 be included in the list of role names to migrate specified in the
207 arguments to this command (it will not be included implicitly). This
208 command does not allow a Failover Controller role to be moved by itself,
209 although it is possible to move a JournalNode independently.
210
211 @param role_names_to_migrate: list of role names to migrate.
212 @param destination_host_id: the id of the host to which the roles
213 should be migrated.
214 @param clear_stale_role_data: true to delete existing stale role data,
215 if any. For example, when migrating a
216 NameNode, if the destination host has
217 stale data in the NameNode data
218 directories (possibly because a NameNode
219 role was previously located there), this
220 stale data will be deleted before migrating
221 the role.
222 @return: Reference to the submitted command.
223 @since: API v10
224 """
225 args = dict(
226 roleNamesToMigrate = role_names_to_migrate,
227 destinationHostId = destination_host_id,
228 clearStaleRoleData = clear_stale_role_data)
229 return self._cmd('migrateRoles', data=args, api_version=10)
230
232 """
233 Update the rack ID of this host.
234 """
235 self.rackId = rackId
236 self._put_host()
237