Exemplo n.º 1
0
    def run(self, args):

        init_args(args, self.args_lst)
        config_file = args.file_uri
        if not os.path.exists(config_file):
            err_info = ("Failure: upload config file failed, "
                        "reason: File Not Exist")
            self.err_list.append(err_info)
            raise FailException(*self.err_list)
        config_type = args.config_type
        url = "/api/maintenance/%s" % URL_DICT[config_type]
        try:
            with open(config_file, "rb") as config_file_data:
                file_read = config_file_data.read()
        except (TypeError, ValueError, KeyError, IOError, Exception) as err:
            self.err_list.append(str(err))
            raise FailException(*self.err_list)
        else:
            _, file_name = os.path.split(config_file)
            fields = {
                "config": (file_name, file_read, "application/octet-stream")
            }
        client = RestfulClient(args)
        try:
            resp = client.upload_request("POST", url, fields=fields)
            if (isinstance(resp, dict) and resp.get(Constant.COMPLETE_CODE)
                    == Constant.SUCCESS_0):
                self.get_process(client, config_type)
            elif (isinstance(resp, dict)
                  and resp.get(Constant.COMPLETE_CODE) != Constant.SUCCESS_0):
                err_info = (
                    "Failure: import %s config file failed, reason: %s" %
                    (config_type, resp.get(Constant.COMPLETE_CODE)))
                self.err_list.append(err_info)
            else:
                err_info = ("Failure: import %s config file failed, "
                            "reason: Response can not parse" % config_type)
                self.err_list.append(err_info)
            if self.err_list:
                raise FailException(*self.err_list)
        finally:
            if client.cookie:
                client.delete_session()
        return self.suc_list
Exemplo n.º 2
0
    def run(self, args):

        init_args(args, self.args_lst)
        if args.u_name is None:
            args.u_name = args.username
        config_file = args.file_uri

        if not os.path.isfile(config_file):
            err_info = ("Failure: import SSH key failed, reason: "
                        "file does not exist")
            self.err_list.append(err_info)
            raise FailException(*self.err_list)
        try:
            with open(config_file, "rb") as config_file_data:
                file_read = config_file_data.read()
        except (TypeError, ValueError, KeyError, IOError, Exception) as err:
            self.err_list.append(str(err))
            raise FailException(*self.err_list)
        else:
            _, file_name = os.path.split(config_file)
            fields = {
                "upload_ssh_key": (file_name, file_read,
                                   "application/octet-stream")
            }
        client = RestfulClient(args)
        try:

            url = "/api/settings/users"
            resp = client.send_request("get", url)
            if isinstance(resp, list):
                for info in resp:
                    if info.get("name") == args.u_name:
                        user_id = info.get("id")

                        url = "/api/settings/user/ssh-key-upload/%s" % user_id
                        import_resp = client.upload_request("POST", url,
                                                            fields=fields)
                        if (isinstance(import_resp, dict) and import_resp.get(
                                Constant.COMPLETE_CODE)
                                == Constant.SUCCESS_0):
                            url = "/api/settings/users/%s" % user_id
                            tmp_resp = client.send_request("get", url)
                            if isinstance(tmp_resp, list) and tmp_resp:
                                data = build_payload(tmp_resp[0])
                            else:
                                err = ("Failure: failed to get user "
                                       "configuration information: %s" %
                                       args.u_name)
                                self.err_list.append(err)
                                raise FailException(*self.err_list)
                            config_resp = client.send_request("put", url, data)
                            if (isinstance(config_resp, dict) and
                                    config_resp.get(Constant.COMPLETE_CODE)
                                    == Constant.SUCCESS_0):
                                suc_info = ("Success: import SSH "
                                            "key successfully")
                                self.suc_list.append(suc_info)
                            else:
                                err_info = "Failure: SSH enabled failed"
                                self.err_list.append(err_info)
                                raise FailException(*self.err_list)
                        else:
                            err_info = "Failure: SSH key import failed"
                            self.err_list.append(err_info)
                            raise FailException(*self.err_list)
                        break
                else:
                    err = "Failure: the user does not exist: %s" % args.u_name
                    self.err_list.append(err)
                    raise FailException(*self.err_list)
            else:
                err = "Failure: filed to get user list"
                self.err_list.append(err)
                raise FailException(*self.err_list)
        finally:
            if client.cookie:
                client.delete_session()
        return self.suc_list