Ejemplo n.º 1
0
def _RunGsutilCommand(command_name, command_arg_str, run_concurrent=False):
    """Runs the specified gsutil command and returns the command's exit code.

  Args:
    command_name: The gsutil command to run.
    command_arg_str: Arguments to pass to the command.
    run_concurrent: Whether concurrent uploads should be enabled while running
      the command.

  Returns:
    The exit code of the call to the gsutil command.
  """
    command_path = _GetGsutilPath()

    if run_concurrent:
        command_args = ['-m', command_name]
    else:
        command_args = [command_name]

    command_args += command_arg_str.split(' ')
    env = None

    if platforms.OperatingSystem.Current(
    ) == platforms.OperatingSystem.WINDOWS:
        gsutil_args = execution_utils.ArgsForCMDTool(command_path + '.cmd',
                                                     *command_args)
    else:
        gsutil_args = execution_utils.ArgsForShellTool(command_path,
                                                       *command_args)
    log.debug('Running command: [{args}], Env: [{env}]'.format(
        args=' '.join(gsutil_args), env=env))
    return execution_utils.Exec(gsutil_args, no_exit=True, env=env)
Ejemplo n.º 2
0
def _ExecuteTool(args):
    """Executes a new tool with the given args, plus the args from the cmdline.

  Args:
    args: [str], The args of the command to execute.
  """
    execution_utils.Exec(args + sys.argv[1:], env=_GetToolEnv())
Ejemplo n.º 3
0
def _RunGsutilCommand(gsutil_args):
    """Run a gsutil command.

  Args:
    gsutil_args: The list of arguments to pass to gsutil.

  Returns:
    The exit code of the call to the gsutil command.
  """
    args = execution_utils.ArgsForBinaryTool(_GetGsutilPath(), *gsutil_args)
    return execution_utils.Exec(args, no_exit=True)
Ejemplo n.º 4
0
def RestartGcloud():
    """Calls gcloud again with the same arguments as this invocation and exit."""
    gcloud = os.path.join(os.path.dirname(googlecloudsdk.__file__), 'gcloud',
                          'gcloud.py')
    gcloud_args = sys.argv[1:]
    args = execution_utils.ArgsForPythonTool(gcloud, *tuple(gcloud_args))

    log.status.Print('Restarting gcloud command:\n  $ gcloud {args}\n'.format(
        args=' '.join(gcloud_args)))
    log.debug('Restarting gcloud: %s', args)
    log.out.flush()
    log.err.flush()

    execution_utils.Exec(args)
Ejemplo n.º 5
0
    def Run(self, args):
        """Connects to a Cloud SQL instance.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      A dict object representing the instance resource if fetching the instance
      was successful.
    Raises:
      HttpException: A http error response was received while executing api
          request.
      ToolException: An error other than http error occured while executing the
          command.
    """
        sql_client = self.context['sql_client']
        sql_messages = self.context['sql_messages']
        resources = self.context['registry']

        # Do the mysql executable check first. This way we can return an error
        # faster and not wait for whitelisting IP and other operations.
        mysql_executable = files.FindExecutableOnPath('mysql')
        if not mysql_executable:
            raise exceptions.ToolException(
                'Mysql client not found. Please install a mysql client and make sure '
                'it is in PATH to be able to connect to the database instance.'
            )

        util.ValidateInstanceName(args.instance)
        instance_ref = resources.Parse(args.instance,
                                       collection='sql.instances')

        acl_name = _WhitelistClientIP(instance_ref, sql_client, sql_messages,
                                      resources)

        # Get the client IP that the server sees. Sadly we can only do this by
        # checking the name of the authorized network rule.
        retryer = retry.Retryer(max_retrials=2, exponential_sleep_multiplier=2)
        try:
            instance_info, client_ip = retryer.RetryOnResult(
                _GetClientIP, [instance_ref, sql_client, acl_name],
                should_retry_if=None,
                sleep_ms=500)
        except retry.RetryException:
            raise exceptions.ToolException('Could not whitelist client IP.')

        # Check the version of IP and decide if we need to add ipv4 support.
        ip_type = util.GetIpVersion(client_ip)
        if ip_type == 4:
            if instance_info.settings.ipConfiguration.ipv4Enabled:
                ip_address = instance_info.ipAddresses[0].ipAddress
            else:
                # TODO(user): ask user if we should enable ipv4 addressing
                message = (
                    'It seems your client does not have ipv6 connectivity and '
                    'the database instance does not have an ipv4 address. '
                    'Please request an ipv4 address for this database instance'
                )
                raise exceptions.ToolException(message)
        elif ip_type == 6:
            ip_address = instance_info.ipv6Address

        # We have everything we need, time to party!
        mysql_args = [mysql_executable, '-h', ip_address]
        if args.user:
            mysql_args.append('-u')
            mysql_args.append(args.user)
        mysql_args.append('-p')
        execution_utils.Exec(mysql_args)