Example #1
0
 def add_task(self, service_name, command):
     """
     current supported commands are: stop, restart, undeploy_tars, patch_tars
     """
     utilities.log_debug("add_task for service %s, command is %s" %
                         (service_name, command))
     (ret, server_id) = self.get_server_id(service_name, self.deploy_ip)
     if ret is False:
         utilities.log_error(
             "%s failed for get server id failed, please check the existence of %s"
             % (command, service_name))
         return False
     items = [{
         "server_id": server_id,
         "command": command,
         "parameters": {}
     }]
     request_data = {"serial": 'true', "items": items}
     response = requests.post(self.add_task_url,
                              params=self.token_param,
                              json=request_data)
     if TarsService.parse_response("execute command " + command,
                                   response) is False:
         utilities.log_error(
             "add_task failed for error response, server name: %s, msg: %s"
             % (service_name, response.content))
         return False
     return True
Example #2
0
 def get_config_file_id(self, config_file_name, server_name, node_name):
     (ret, server_config_id) = self.get_server_config_file_id(
         config_file_name, server_name)
     if ret is False:
         return (False, 0)
     params = {
         "ticket": self.tars_token,
         "config_id": server_config_id,
         "level": TarsService.get_level(server_name),
         "application": self.app_name,
         "server_name": server_name,
         "set_name": "",
         "set_area": "",
         "set_group": ""
     }
     response = requests.get(self.node_config_file_list_url, params=params)
     if TarsService.parse_response(
             "query the node config file id for " + config_file_name,
             response) is False:
         return (False, 0)
     result = response.json()
     if "data" not in result or len(result["data"]) == 0:
         utilities.log_debug("the config %s doesn't exist" %
                             (config_file_name))
         return (False, 0)
     # try to find the config file info
     for item in result["data"]:
         if "filename" in item and item[
                 "filename"] == config_file_name and item[
                     "node_name"] == node_name:
             return (True, item["id"])
     utilities.log_error(
         "the node config file %s not found, node: %s, :%s:" %
         (config_file_name, node_name, str(result["data"])))
     return (False, 0)
Example #3
0
 def patch_tars(self, server_id, patch_id):
     utilities.log_debug(
         "patch tars for application %s, server_id: %s, patch_id: %s" %
         (self.app_name, server_id, patch_id))
     items = [{
         "server_id": server_id,
         "command": "patch_tars",
         "parameters": {
             "patch_id": patch_id,
             "bak_flag": 'false',
             "update_text": "",
             "group_name": ""
         }
     }]
     request_data = {"serial": 'true', "items": items}
     response = requests.post(self.add_task_url,
                              params=self.token_param,
                              json=request_data)
     if TarsService.parse_response("patch tars ", response) is False:
         utilities.log_error(
             "patch tars failed for error response, server id: %s, msg: %s"
             % (server_id, response.content))
         return False
     utilities.log_debug("patch tars response %s" % response.content)
     return True
Example #4
0
 def add_non_empty_server_config_file(self, config_file_name, server_name,
                                      config_file_path):
     "add server the config file"
     utilities.log_debug(
         "add config file for application %s, config file path: %s, service_name: %s"
         % (self.app_name, config_file_path, server_name))
     ret = self.add_server_config_file(config_file_name, server_name,
                                       config_file_path, False)
     if ret is False:
         ret = self.update_service_config(config_file_name, server_name, "",
                                          config_file_path)
     return ret
Example #5
0
 def create_application(self):
     "create application"
     if self.app_exists() is True:
         # utilities.log_error(
         #    "application %s already exists" % self.app_name)
         return False
     utilities.log_debug("create application: %s" % self.app_name)
     request_data = {'f_name': self.app_name}
     response = requests.post(self.add_application_url,
                              params=self.token_param,
                              data=request_data)
     return TarsService.parse_response(
         "create application " + self.app_name, response)
Example #6
0
 def add_config_file(self, config_file_name, server_name, node_name,
                     config_file_path, empty_server_config):
     "add the config file"
     (ret, id) = self.get_server_config_file_id(config_file_name,
                                                server_name)
     if ret is False:
         utilities.log_debug(
             "add config file for application %s, config file path: %s, service_name: %s"
             % (self.app_name, config_file_path, server_name))
         self.add_server_config_file(config_file_name, server_name,
                                     config_file_path, empty_server_config)
     return self.update_service_config(config_file_name, server_name,
                                       node_name, config_file_path)
Example #7
0
 def get_server_config_file_id(self, config_file_name, server_name):
     utilities.log_debug("query the config file id for %s" %
                         config_file_name)
     params = {
         "ticket": self.tars_token,
         "level": TarsService.get_level(server_name),
         "application": self.app_name,
         "server_name": server_name,
         "set_name": "",
         "set_area": "",
         "set_group": ""
     }
     response = requests.get(self.config_file_list_url, params=params)
     if TarsService.parse_response(
             "query the config file id for " + config_file_name,
             response) is False:
         return (False, 0)
     result = response.json()
     if "data" not in result or len(result["data"]) == 0:
         utilities.log_debug(
             "the config file id not found for %s because of empty return data, response: %s"
             % (config_file_name, response.content))
         return (False, 0)
     # try to find the config file info
     for item in result["data"]:
         if "filename" in item and item["filename"] == config_file_name:
             utilities.log_debug(
                 "get_server_config_file_id, server: %s, config_id: %s" %
                 (server_name, item["id"]))
             return (True, item["id"])
     utilities.log_debug("the config file %s not found" % config_file_name)
     return (False, 0)
Example #8
0
 def get_server_patch(self, task_id):
     utilities.log_debug("get server patch, task_id: %s" % task_id)
     params = {"task_id": task_id, "ticket": self.tars_token}
     response = requests.get(self.get_server_patch_url, params=params)
     if TarsService.parse_response("get server patch", response) is False:
         return (False, "")
     if response.status_code != 200:
         return (False, "")
     # get the id
     result = response.json()
     result_data = result['data']
     if 'id' not in result_data:
         utilities.log_error(
             "get_server_patch failed for empty return message")
         return (False, "")
     id = result_data['id']
     return (True, id)
Example #9
0
    def upload_tars_package(self, service_name, package_path):
        """
        upload the tars package
        """
        package_name = service_name + ServiceInfo.tars_pkg_postfix
        utilities.log_debug(
            "upload tars package for service %s, package_path: %s, package_name: %s"
            % (service_name, package_path, package_name))
        if os.path.exists(package_path) is False:
            utilities.log_error(
                "upload tars package for service %s failed for the path %s not exists"
                % (service_name, package_path))
            return (False, 0)
        task_id = str(uuid.uuid4())
        form_data = MultipartEncoder(
            fields={
                "application":
                self.app_name,
                "task_id":
                task_id,
                "module_name":
                service_name,
                "comment":
                "upload package",
                "suse": (package_name, open(package_path, 'rb'),
                         'text/plain/binary')
            })

        response = requests.post(
            self.upload_package_url,
            data=form_data,
            params=self.token_param,
            headers={'Content-Type': form_data.content_type})
        if TarsService.parse_response("upload tars package " + package_path,
                                      response) is False:
            return (False, 0)
        # get the id
        (ret, id) = self.get_server_patch(task_id)
        if ret is True:
            utilities.log_info("upload tar package %s success, config id: %s" %
                               (package_path, id))
        return (ret, id)
Example #10
0
 def add_config_list(self, config_list, service_name, node_name,
                     config_file_list, empty_server_config):
     i = 0
     for config_file_path in config_file_list:
         config = config_list[i]
         utilities.log_info(
             "* add config for service %s, node: %s, config: %s" %
             (service_name, node_name, config))
         utilities.log_debug(
             "add config for service %s, node: %s, config: %s, path: %s" %
             (service_name, node_name, config, config_file_path))
         if self.add_config_file(config, service_name, node_name,
                                 config_file_path,
                                 empty_server_config) is False:
             utilities.log_error(
                 "add_config_list failed, config files info: %s" %
                 config_list)
             return False
         i = i + 1
     return True
Example #11
0
 def update_service_config(self, config_file_name, server_name, node_name,
                           config_file_path):
     utilities.log_debug(
         "update config file for application %s, config file path: %s, node: %s"
         % (self.app_name, config_file_path, node_name))
     if os.path.exists(config_file_path) is False:
         utilities.log_error(
             "update service config error:\n the config file %s doesn't exist, service: %s"
             % (config_file_path, server_name))
         return False
     ret = True
     config_id = 0
     if len(node_name) == 0:
         (ret, config_id) = self.get_server_config_file_id(
             config_file_name, server_name)
     else:
         ret, config_id = self.get_config_file_id(config_file_name,
                                                  server_name, node_name)
     if ret is False:
         return False
     try:
         fp = open(config_file_path)
         content = fp.read()
     except OSError as reason:
         utilities.log_error("load the configuration failed, error: %s" %
                             str(reason))
     request_data = {
         "id": config_id,
         "config": content,
         "reason": "update config file"
     }
     response = requests.post(self.update_config_url,
                              params=self.token_param,
                              json=request_data)
     if TarsService.parse_response(
             "update config file for application " + self.app_name +
             ", config file:" + config_file_name, response) is False:
         return False
     return True
Example #12
0
 def get_node_auto_port(self, node_name):
     # get the auto_port
     utilities.log_debug("get the un-occuppied port")
     params = {"node_name": node_name, "ticket": self.tars_token}
     response = requests.get(self.get_port_url, params=params)
     if TarsService.parse_response("get the un-occupied port",
                                   response) is False:
         return (False, 0)
     result = response.json()
     if 'data' not in result:
         utilities.log_error("get empty un-occupied port")
         return (False, 0)
     node_info = result['data']
     if len(node_info) <= 0:
         utilities.log_error("get empty un-occupied port")
         return (False, 0)
     if 'port' not in node_info[0]:
         utilities.log_error("get empty un-occupied port")
         return (False, 0)
     port = node_info[0]['port']
     utilities.log_debug("get the un-occupied port success, port: %s" %
                         (port))
     return (True, int(port))
Example #13
0
 def fetch_server_config_file(self, config_file_name, server_name):
     (ret,
      config_id) = self.get_server_config_file_id(config_file_name,
                                                  server_name)
     if ret is False:
         utilities.log_error(
             "fetch server config file failed, please check the existence of specified service, service: %s, config: %s"
             % (server_name, config_file_name))
         return (False, "")
     param = {"ticket": self.tars_token, "id": config_id}
     response = requests.get(self.fetch_config_url, params=param)
     if TarsService.parse_response("fetch service config " + server_name,
                                   response) is False:
         return (False, "")
     utilities.log_debug("fetch service config file success, response: %s" %
                         response.content)
     result = response.json()
     if "data" not in result or "config" not in result["data"]:
         utilities.log_error(
             "fetch service config file failed, response %s" %
             response.content)
         return (False, "")
     return (True, result["data"]["config"])