Kaynağa Gözat

AMBARI-6553. Python client: Add all required methods for removing a slave node(Greg Hill via Subin)

subin 11 yıl önce
ebeveyn
işleme
e19a0c8e29

+ 11 - 0
ambari-client/python-client/src/main/python/ambari_client/core/coreutils.py

@@ -14,3 +14,14 @@
 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 #  See the License for the specific language governing permissions and
 #  limitations under the License.
+
+import re
+
+def normalize_all_caps(name):
+	"""
+	This converts all caps words into normal case.  
+	i.e. 'NAGIOS_SERVER' becomes 'Nagios Server'
+	"""
+	normalized = name.lower()
+	normalized = re.sub('_(\w)', lambda match: ' ' + match.group(1).upper(), normalized)
+	return normalized[0].upper() + normalized[1:]

+ 82 - 1
ambari-client/python-client/src/main/python/ambari_client/model/component.py

@@ -16,6 +16,7 @@
 #  limitations under the License.
 
 import logging
+from ambari_client.core.coreutils import normalize_all_caps
 from ambari_client.model.base_model import BaseModel, ModelList
 from ambari_client.model import paths, utils, status
 
@@ -73,9 +74,22 @@ def _get_service_component(
         resource_root,
         "ServiceComponentInfo")
 
+def _delete_host_component(
+        resource_root, 
+        cluster_name , 
+        host_name , 
+        component_name):
+    path = paths.HOSTS_COMPONENT_PATH % (
+        cluster_name, host_name , component_name)
+    resp = resource_root.delete(path)
+    return utils.ModelUtils.create_model(
+        status.StatusModel, 
+        resp, 
+        resource_root, 
+        "NO_KEY")
 
-class ComponentModel(BaseModel):
 
+class ComponentModel(BaseModel):
     """
     The ComponentModel class
     """
@@ -124,3 +138,70 @@ class ComponentModel(BaseModel):
                 self._get_cluster_name(), self.host_name, self.component_name) + "?fields=metrics"
         metricjson = self._get_resource_root().get(metricpath)
         return metricjson
+
+
+    def delete(self):
+        return _delete_host_component(self._get_resource_root(), self._get_cluster_name(), self.host_name, self.component_name)
+
+    def install(self):
+        data = {
+            "RequestInfo": {
+                "context": "Install %s" % normalize_all_caps(self.component_name),
+            },
+            "HostRoles": {
+                "state": "INSTALLED",
+            },
+        }
+        root_resource = self._get_resource_root()
+        resp = root_resource.put(path=self._path() + '/' + self.component_name, payload=data)
+        return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY")
+
+    def start(self):
+        data = {
+            "RequestInfo": {
+                "context": "Start %s" % normalize_all_caps(self.component_name),
+            },
+            "HostRoles": {
+                "state": "STARTED",
+            },
+        }
+        root_resource = self._get_resource_root()
+        resp = root_resource.put(path=self._path() + '/' + self.component_name, payload=data)
+        return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY")
+
+    def stop(self):
+        data = {
+            "RequestInfo": {
+                "context": "Stop %s" % normalize_all_caps(self.component_name),
+            },
+            "HostRoles": {
+                "state": "INSTALLED",
+            },
+        }
+        root_resource = self._get_resource_root()
+        resp = root_resource.put(path=self._path() + '/' + self.component_name, payload=data)
+        return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY")
+
+    def restart(self):
+        # need to move this to utils, handle _ gracefully
+        data = {
+            "RequestInfo": {
+                "command": "RESTART", 
+                "context": "Restart %s" % normalize_all_caps(self.component_name),
+                "operation_level": {
+                    "level": "SERVICE",
+                    "cluster_name": self._get_cluster_name(),
+                    "service_name": self.service_name,
+
+                },
+            },
+            "Requests/resource_filters": [{
+                "service_name": self.service_name,
+                "component_name": self.component_name,
+                "hosts": self.host_name,
+            }],
+        }
+        root_resource = self._get_resource_root()
+        path = paths.CLUSTER_REQUESTS_PATH % self._get_cluster_name()
+        resp = root_resource.post(path=path, payload=data)
+        return utils.ModelUtils.create_model(status.StatusModel, resp, root_resource, "NO_KEY")

+ 80 - 0
ambari-client/python-client/src/main/python/ambari_client/model/host.py

@@ -321,3 +321,83 @@ class HostModel(BaseModel):
             self._get_cluster_name(),
             self.host_name,
             component_name)
+
+    def install_all_components(self):
+        root_resource = self._get_resource_root()
+        path = paths.HOSTS_COMPONENTS_PATH % (self._get_cluster_name(), 
+                                              self.host_name)
+        data = {
+            "RequestInfo": {
+                "context" :"Install All Components",
+            }, 
+            "Body": {
+                "HostRoles": {"state": "INSTALLED"},
+            },
+        }
+        resp = root_resource.put(path=path, payload=data)
+        return utils.ModelUtils.create_model(status.StatusModel, resp, 
+                                             root_resource, "NO_KEY")
+
+    def start_all_components(self):
+        root_resource = self._get_resource_root()
+        path = paths.HOSTS_COMPONENTS_PATH % (self._get_cluster_name(), 
+                                              self.host_name)
+        data = {
+            "RequestInfo": {
+                "context" :"Start All Components",
+            }, 
+            "Body": {
+                "HostRoles": {"state": "STARTED"},
+            },
+        }
+        resp = root_resource.put(path=path, payload=data)
+        return utils.ModelUtils.create_model(status.StatusModel, resp, 
+                                             root_resource, "NO_KEY")
+
+    def stop_all_components(self):
+        root_resource = self._get_resource_root()
+        path = paths.HOSTS_COMPONENTS_PATH % (self._get_cluster_name(), 
+                                              self.host_name)
+        data = {
+            "RequestInfo": {
+                "context" :"Stop All Components",
+            }, 
+            "Body": {
+                "HostRoles": {"state": "INSTALLED"},
+            },
+        }
+        resp = root_resource.put(path=path, payload=data)
+        return utils.ModelUtils.create_model(status.StatusModel, resp, 
+                                             root_resource, "NO_KEY")
+
+    def enable_maintenance_mode(self):
+        root_resource = self._get_resource_root()
+        path = paths.HOSTS_COMPONENTS_PATH % (self._get_cluster_name(), 
+                                              self.host_name)
+        data = {
+            "RequestInfo": {
+                "context" :"Start Maintanence Mode",
+            }, 
+            "Body": {
+                "HostRoles": {"maintenance_state": "ON"},
+            },
+        }
+        resp = root_resource.put(path=path, payload=data)
+        return utils.ModelUtils.create_model(status.StatusModel, resp, 
+                                             root_resource, "NO_KEY")
+
+    def disable_maintenance_mode(self):
+        root_resource = self._get_resource_root()
+        path = paths.HOSTS_COMPONENTS_PATH % (self._get_cluster_name(), 
+                                              self.host_name)
+        data = {
+            "RequestInfo": {
+                "context" :"Stop Maintanence Mode",
+            }, 
+            "Body": {
+                "HostRoles": {"maintenance_state": "OFF"},
+            },
+        }
+        resp = root_resource.put(path=path, payload=data)
+        return utils.ModelUtils.create_model(status.StatusModel, resp, 
+                                             root_resource, "NO_KEY")

+ 1 - 1
ambari-client/python-client/src/main/python/ambari_client/model/paths.py

@@ -21,7 +21,7 @@ CLUSTER_HOSTS_PATH = "/clusters/%s/hosts"
 CLUSTER_HOST_PATH = "/clusters/%s/hosts/%s"
 CLUSTER_START_ALL_SERVICES = "/clusters/%s/services?ServiceInfo/state=INSTALLED"
 CLUSTER_STOP_ALL_SERVICES = "/clusters/%s/services?ServiceInfo"
-
+CLUSTER_REQUESTS_PATH = "/clusters/%s/requests"
 
 SERVICES_PATH = "/clusters/%s/services"
 SERVICE_PATH = "/clusters/%s/services/%s"