Exemplo n.º 1
0
def _edit_config(config: str, string: str, holder: str, section: bool):
    """
    Funcion for actual editing the config file.

    :param config: path to config file
    :param string: string to be add
    :param holder: section or substinrg to update
    :param section: specify if holder is a section
    """
    old = f"#<{holder}>" if section else holder
    new = f"{string}\n{old}" if section else string

    with open(config, "r") as file:
        content = file.read()
        if (old is not None) and (old not in content):
            log.error(f"File {config} is not updated. "
                      f"Maybe placeholder in the config {config} "
                      f"for the section {holder} is missing?")
            raise Exception(f"Placeholder {old} is not present in {config}")

    content = content.replace(old, new)
    with open(config, "w+") as file:
        file.write(content)

    log.debug(f"{'Section' if section else 'Substring'} {holder} in config "
              f"file {config} is updated")
Exemplo n.º 2
0
 def __exit__(self, ext_type, ext_value, ext_traceback):
     if ext_type is not None:
         log.error("Exception in virtual smart card context")
         log.error(f"Exception type: {ext_type}")
         log.error(f"Exception value: {ext_value}")
         log.error(f"Exception traceback: {ext_traceback}")
     self._reset()
Exemplo n.º 3
0
 def __exit__(self, exp_type, exp_value, exp_traceback):
     if exp_type is not None:
         log.error("Exception in authselect context")
         log.error(f"Exception type: {exp_type}")
         log.error(f"Exception value: {exp_value}")
         log.error(f"Exception traceback: {exp_traceback}")
     self.remove()
Exemplo n.º 4
0
    def run_cmd(self,
                cmd: str,
                expect: str = None,
                pin: bool = True,
                passwd: str = None,
                shell=None):
        try:
            if shell is None:
                shell = pexpect.spawn(cmd, encoding='utf-8')
            shell.logfile = sys.stdout

            if passwd is not None:
                pattern = "PIN for " if pin else "Password"
                time.sleep(1)
                out = shell.expect([pexpect.TIMEOUT, pattern], timeout=10)

                if out != 1:
                    if out == 0:
                        log.error("Timed out on passsword / PIN waiting")
                    expect = pattern

                    raise pexpect.exceptions.EOF(f"Pattern '{pattern}' is not "
                                                 f"found in the output.")
                shell.sendline(passwd)

            if expect is not None:
                out = shell.expect([pexpect.TIMEOUT, expect], timeout=20)

                if out != 1:
                    if out == 0:
                        log.error("Time out")
                    raise pexpect.exceptions.EOF(f"Pattern '{expect}' is not "
                                                 f"found in the output.")

        except pexpect.exceptions.EOF as e:
            log.error(f"Pattern '{expect}' not found in output.\n"
                      f"Output:\n{str(shell.before)}")
            raise e
        except Exception as e:
            log.error(f"Unexpected exception: {str(e)}")
            raise e
        return shell
Exemplo n.º 5
0
def restart_service(service: str) -> int:
    """
    Restart given service and wait 5 sec

    :param service: service name
    :return: return code of systemcrt restart
    """
    try:
        result = subp.run(["systemctl", "restart", f"{service}"], check=True, encoding="utf8")
        sleep(5)
        log.debug(f"Service {service} is restarted")
        return result.returncode
    except subp.CalledProcessError as e:
        log.error(f"Command {' '.join(e.cmd)} is ended with non-zero return code ({e.returncode})")
        log.error(f"stdout:\n{e.stdout}")
        log.error(f"stderr:\n{e.stderr}")
        return e.returncode