コード例 #1
0
def test_file_creation(path):
    try:
        iohelper.write_to_file(path, path)
        os.remove(path)
        return True
    except IOError:
        return False
コード例 #2
0
def test_file_creation(path):
    try:
        iohelper.write_to_file(path, path)
        os.remove(path)
        return True
    except IOError:
        return False
コード例 #3
0
 def write_to_disk(self):
     """Writes the runbook's definition to disk."""
     file_name = self.runbook_data.name + self.runbook_data.runbook_version_id + self.file_extension
     self.runbook_file_path = os.path.join(configuration.get_working_directory_path(), file_name)
     runbook_definition = str(self.runbook_data.definition.encode("utf-8"))
     if linuxutil.is_posix_host() is True:
         # replace dos end of line to unix end of line
         runbook_definition = runbook_definition.replace("\r\n", "\n")
     iohelper.write_to_file(self.runbook_file_path, data=runbook_definition, mode="w+b")
コード例 #4
0
 def write_to_disk(self):
     """Writes the runbook's definition to disk."""
     file_name = self.runbook_data["name"] + self.runbook_data[
         "runbookVersionId"] + self.file_extension
     self.runbook_file_path = os.path.join(
         configuration.get_working_directory_path(), file_name)
     iohelper.write_to_file(
         self.runbook_file_path,
         data=self.runbook_data["definition"].encode("utf-8"),
         mode="w+b")
コード例 #5
0
 def write_to_disk(self):
     """Writes the runbook's definition to disk."""
     file_name = self.runbook_data.name + self.runbook_data.runbook_version_id + self.file_extension
     self.runbook_file_path = os.path.join(
         configuration.get_working_directory_path(), file_name)
     runbook_definition = str(self.runbook_data.definition.encode("utf-8"))
     if linuxutil.is_posix_host() is True:
         # replace dos end of line to unix end of line
         runbook_definition = runbook_definition.replace("\r\n", "\n")
     iohelper.write_to_file(self.runbook_file_path,
                            data=runbook_definition,
                            mode="w+b")
コード例 #6
0
    def issue_request(self, url, headers, method, data):
        data_file_path = None
        headers = self.merge_headers(self.default_headers, headers)

        # if a body is included, write it to a temporary file (prevent body from leaking in ps/top)
        if method != self.GET and data is not None:
            serialized_data = self.json.dumps(data)

            # write data to disk
            data_file_name = base64.standard_b64encode(
                str(time.time()) + str(random.randint(0, sys.maxint)) +
                str(random.randint(0, sys.maxint)) +
                str(random.randint(0, sys.maxint)) +
                str(random.randint(0, sys.maxint)))
            data_file_path = os.path.join(
                configuration.get_temporary_request_directory_path(),
                data_file_name)
            iohelper.write_to_file(data_file_path, serialized_data)

            # insert Content-Type header
            headers.update(
                {self.CONTENT_TYPE_HEADER_KEY: self.APP_JSON_HEADER_VALUE})

        # ** nesting of try statement is required since try/except/finally isn't supported prior to 2.5 **
        try:
            try:
                cmd = self.build_request_cmd(url,
                                             headers,
                                             method=method,
                                             data_file_path=data_file_path)
                env = os.environ.copy()
                if self.os_is_redhat():
                    env["NSS_SDB_USE_CACHE"] = "no"
                p = subprocessfactory.create_subprocess(cmd,
                                                        env=env,
                                                        stdout=subprocess.PIPE,
                                                        stderr=subprocess.PIPE)
                out, err = p.communicate()

                if p.returncode != EXIT_SUCCESS:
                    raise Exception(
                        "Http request failed due to curl error. [returncode=" +
                        str(p.returncode) + "]" + "[stderr=" + str(err) + "]")

                return self.parse_raw_output(out)
            except Exception, e:
                raise Exception(
                    "Unknown exception while issuing request. [exception=" +
                    str(e) + "]" + "[stacktrace=" +
                    str(traceback.format_exc()) + "]")
        finally:
            if data_file_path is not None:
                os.remove(data_file_path)
コード例 #7
0
 def write_to_disk(self):
     """Writes the runbook's definition to disk."""
     file_name = self.runbook_data.name + self.runbook_data.runbook_version_id + self.file_extension
     self.runbook_file_path = os.path.join(
         configuration.get_working_directory_path(), file_name)
     # In python2 strings and bytes are treated the same. Hence the encoding had no affect on the string to be written. But in python3 encoding changes the string to bytes.
     # So there is no need because it is already encoded.
     runbook_definition = str(self.runbook_data.definition)
     if linuxutil.is_posix_host() is True:
         # replace dos end of line to unix end of line
         runbook_definition = runbook_definition.replace("\r\n", "\n")
     iohelper.write_to_file(self.runbook_file_path,
                            data=runbook_definition,
                            mode="w")
コード例 #8
0
    def issue_request(self, url, headers, method, data):
        data_file_path = None
        headers = self.merge_headers(self.default_headers, headers)

        # if a body is included, write it to a temporary file (prevent body from leaking in ps/top)
        if method != self.GET and data is not None:
            serialized_data = self.json.dumps(data)

            # write data to disk
            data_file_name = base64.standard_b64encode(str(time.time()) +
                                                       str(random.randint(0, sys.maxint)) +
                                                       str(random.randint(0, sys.maxint)) +
                                                       str(random.randint(0, sys.maxint)) +
                                                       str(random.randint(0, sys.maxint)))
            data_file_path = os.path.join(configuration.get_temporary_request_directory_path(), data_file_name)
            iohelper.write_to_file(data_file_path, serialized_data)

            # insert Content-Type header
            headers.update({self.CONTENT_TYPE_HEADER_KEY: self.APP_JSON_HEADER_VALUE})

        # ** nesting of try statement is required since try/except/finally isn't supported prior to 2.5 **
        try:
            try:
                cmd = self.build_request_cmd(url, headers, method=method, data_file_path=data_file_path)
                env = os.environ.copy()
                p = subprocessfactory.create_subprocess(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                out, err = p.communicate()

                if p.returncode != EXIT_SUCCESS:
                    raise Exception("Http request failed due to curl error. [returncode=" + str(p.returncode) + "]" +
                                    "[stderr=" + str(err) + "]")

                return self.parse_raw_output(out)
            except Exception, e:
                raise Exception("Unknown exception while issuing request. [exception=" + str(e) + "]" +
                                "[stacktrace=" + str(traceback.format_exc()) + "]")
        finally:
            if data_file_path is not None:
                os.remove(data_file_path)