Exemplo n.º 1
0
def run_vtctl(clargs,
              auto_log=False,
              expect_fail=False,
              mode=VTCTL_AUTO,
              **kwargs):
    if mode == VTCTL_AUTO:
        if not expect_fail and vtctld:
            mode = VTCTL_RPC
        else:
            mode = VTCTL_VTCTL

    if mode == VTCTL_VTCTL:
        return run_vtctl_vtctl(clargs,
                               auto_log=auto_log,
                               expect_fail=expect_fail,
                               **kwargs)
    elif mode == VTCTL_VTCTLCLIENT:
        result = vtctld.vtctl_client(clargs)
        return result, ''
    elif mode == VTCTL_RPC:
        if auto_log:
            logging.debug('vtctl: %s', ' '.join(clargs))
        result = vtctl_client.execute_vtctl_command(vtctld_connection,
                                                    clargs,
                                                    info_to_debug=True,
                                                    action_timeout=120)
        return result, ''

    raise Exception('Unknown mode: %s', mode)
Exemplo n.º 2
0
    def execute_vtctl_command(self,
                              args,
                              action_timeout=60.0,
                              expect_fail=False,
                              max_wait_s=180.0):
        """Executes a vtctl command on a running vtctl job.

    This function attempts to execute on any running vtctl job, returning
    immediately when a call to execute_vtctl_command completes successfully.

    Args:
      args: args to pass to vtctl_client's execute_vtctl_command function
      action_timeout: total timeout for the action (float, in seconds)
      expect_fail: whether or not the vtctl command should fail (bool)
      max_wait_s: maximum amount of time to wait for success (float, in seconds)

    Returns:
      Result of executing vtctl command

    Raises:
      VtctlClientError: Could not successfully call execute_vtctl_command
    """
        start_time = time.time()
        while time.time() - start_time < max_wait_s:
            try:
                if self.protocol == 'grpc':
                    results = subprocess.check_output([
                        'vtctlclient', '-vtctl_client_protocol', self.protocol,
                        '-server', self.vtctl_addr
                    ] + args,
                                                      stderr=subprocess.STDOUT)
                else:
                    results = vtctl_client.execute_vtctl_command(
                        self.client, args, action_timeout=action_timeout)
                return results
            except Exception as e:
                if expect_fail:
                    logging.info('Expected vtctl error, got: %s', e.message
                                 or e.output)
                    raise VtctlClientError('Caught an expected vtctl error')
                logging.info('Vtctl error (vtctl %s): %s', ' '.join(args),
                             e.message or e.output)
            time.sleep(5)
        raise VtctlClientError(
            'Timed out on vtctl_client execute_vtctl_command')
Exemplo n.º 3
0
def run_vtctl(clargs, auto_log=False, expect_fail=False, mode=VTCTL_AUTO, **kwargs):
    if mode == VTCTL_AUTO:
        if not expect_fail and vtctld:
            mode = VTCTL_RPC
        else:
            mode = VTCTL_VTCTL

    if mode == VTCTL_VTCTL:
        return run_vtctl_vtctl(clargs, auto_log=auto_log, expect_fail=expect_fail, **kwargs)
    elif mode == VTCTL_VTCTLCLIENT:
        result = vtctld.vtctl_client(clargs)
        return result, ""
    elif mode == VTCTL_RPC:
        logging.debug("vtctl: %s", " ".join(clargs))
        result = vtctl_client.execute_vtctl_command(vtctld_connection, clargs, info_to_debug=True, action_timeout=120)
        return result, ""

    raise Exception("Unknown mode: %s", mode)
Exemplo n.º 4
0
  def execute_vtctl_command(self, args, action_timeout=60.0, expect_fail=False,
                            max_wait_s=180.0):
    """Executes a vtctl command on a running vtctl job.

    This function attempts to execute on any running vtctl job, returning
    immediately when a call to execute_vtctl_command completes successfully.

    Args:
      args: args to pass to vtctl_client's execute_vtctl_command function
      action_timeout: total timeout for the action (float, in seconds)
      expect_fail: whether or not the vtctl command should fail (bool)
      max_wait_s: maximum amount of time to wait for success (float, in seconds)

    Returns:
      Result of executing vtctl command

    Raises:
      VtctlClientError: Could not successfully call execute_vtctl_command
    """
    start_time = time.time()
    while time.time() - start_time < max_wait_s:
      try:
        if self.protocol == 'grpc':
          results = subprocess.check_output(
              ['vtctlclient', '-vtctl_client_protocol', self.protocol,
               '-server', self.vtctl_addr] + args, stderr=subprocess.STDOUT)
        else:
          results = vtctl_client.execute_vtctl_command(
              self.client, args, action_timeout=action_timeout)
        return results
      except Exception as e:
        if expect_fail:
          logging.info('Expected vtctl error, got: %s', e.message or e.output)
          raise VtctlClientError('Caught an expected vtctl error')
        logging.info('Vtctl error (vtctl %s): %s',
                     ' '.join(args), e.message or e.output)
      time.sleep(5)
    raise VtctlClientError('Timed out on vtctl_client execute_vtctl_command')