Exemplo n.º 1
0
  def _CreateBotoConfig(self):
    gsutil_path = _FindGsutil()
    if not gsutil_path:
      log.debug('Unable to find [gsutil]. Not configuring default .boto '
                'file')
      return

    boto_path = platforms.ExpandHomePath(os.path.join('~', '.boto'))
    if os.path.exists(boto_path):
      log.debug('Not configuring default .boto file. File already '
                'exists at [{boto_path}].'.format(boto_path=boto_path))
      return

    # 'gsutil config -n' creates a default .boto file that the user can read and
    # modify.
    command_args = ['config', '-n', '-o', boto_path]
    if platforms.OperatingSystem.Current() == platforms.OperatingSystem.WINDOWS:
      gsutil_args = execution_utils.ArgsForCMDTool(gsutil_path,
                                                   *command_args)
    else:
      gsutil_args = execution_utils.ArgsForExecutableTool(gsutil_path,
                                                          *command_args)

    return_code = execution_utils.Exec(gsutil_args, no_exit=True,
                                       out_func=log.file_only_logger.debug,
                                       err_func=log.file_only_logger.debug)
    if return_code == 0:
      log.status.write("""\
Created a default .boto configuration file at [{boto_path}]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
""".format(boto_path=boto_path))

    else:
      log.status.write('Error creating a default .boto configuration file. '
                       'Please run [gsutil config -n] if you would like to '
                       'create this file.\n')
Exemplo n.º 2
0
def RunGsutilCommand(command_name,
                     command_args=None,
                     run_concurrent=False,
                     out_func=log.file_only_logger.debug,
                     err_func=log.file_only_logger.debug):
  """Runs the specified gsutil command and returns the command's exit code.

  This is more reliable than storage_api.StorageClient.CopyFilesToGcs especially
  for large files.

  Args:
    command_name: The gsutil command to run.
    command_args: List of arguments to pass to the command.
    run_concurrent: Whether concurrent uploads should be enabled while running
      the command.
    out_func: str->None, a function to call with the stdout of the gsutil
        command.
    err_func: str->None, a function to call with the stderr of the gsutil
        command.

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

  args = ['-m', command_name] if run_concurrent else [command_name]
  if command_args is not None:
    args += command_args

  if platforms.OperatingSystem.Current() == platforms.OperatingSystem.WINDOWS:
    gsutil_args = execution_utils.ArgsForCMDTool(command_path + '.cmd', *args)
  else:
    gsutil_args = execution_utils.ArgsForExecutableTool(command_path, *args)
  log.debug('Running command: [{args}]]'.format(args=' '.join(gsutil_args)))
  return execution_utils.Exec(gsutil_args, no_exit=True,
                              out_func=out_func,
                              err_func=err_func)
Exemplo n.º 3
0
def RunKubectlCommand(args, out_func=None, err_func=None):
  """Shells out a command to kubectl.

  This command should be called within the context of a TemporaryKubeconfig
  context manager in order for kubectl to be configured to access the correct
  cluster.

  Args:
    args: list of strings, command line arguments to pass to the kubectl
        command. Should omit the kubectl command itself. For example, to
        execute 'kubectl get pods', provide ['get', 'pods'].
    out_func: str->None, a function to call with the stdout of the kubectl
        command
    err_func: str->None, a function to call with the stderr of the kubectl
        command

  Raises:
    Error: if kubectl could not be called
    KubectlError: if the invocation of kubectl was unsuccessful
  """
  kubectl_path = files.FindExecutableOnPath(_KUBECTL_COMPONENT_NAME,
                                            config.Paths().sdk_bin_path)
  if kubectl_path is None:
    raise Error(MISSING_KUBECTL_MSG)

  try:
    retval = execution_utils.Exec(
        execution_utils.ArgsForExecutableTool(kubectl_path, *args),
        no_exit=True,
        out_func=out_func,
        err_func=err_func,
        universal_newlines=True)
  except (execution_utils.PermissionError,
          execution_utils.InvalidCommandError) as e:
    raise KubectlError(six.text_type(e))
  if retval:
    raise KubectlError('kubectl returned non-zero status code.')
Exemplo n.º 4
0
def ExecuteJavaClass(java_bin,
                     jar_dir,
                     main_jar,
                     main_class,
                     java_flags=None,
                     main_args=None):
    """Execute a given java class within a directory of jars.

  Args:
    java_bin: str, path to the system Java binary
    jar_dir: str, directory of jars to put on class path
    main_jar: str, main jar (placed first on class path)
    main_class: str, name of the main class in the jar
    java_flags: [str], flags for the java binary
    main_args: args for the command
  """
    java_flags = java_flags or []
    main_args = main_args or []
    jar_dir_path = os.path.join(SDK_ROOT, jar_dir, '*')
    main_jar_path = os.path.join(SDK_ROOT, jar_dir, main_jar)
    classpath = main_jar_path + os.pathsep + jar_dir_path
    java_args = (['-cp', classpath] + list(java_flags) + [main_class] +
                 list(main_args))
    _ExecuteTool(execution_utils.ArgsForExecutableTool(java_bin, *java_args))
Exemplo n.º 5
0
def GetArgsForLegacyScript(script_name, args, interpreter=None):
  """Returns a list of args for execution of the given script.

  Args:
    script_name: The name of the script to run.  The extension determines which
      interpreter will be used for execution.  Python will be used for .py.
      If there is no extension it is assumed it is just a binary file and is
      executed directly.  .exe files are executed directly on Windows, or
      error raised if not on Windows. If it ends with a single dot, it sees
      what OS it is running on and either executes .cmd or .sh.
    args:  The arguments for the script itself
    interpreter: an interpreter to use rather than trying to derive it from
      the extension name.  The value is the same as an extension, right now
      only implemented for 'py'.

  Returns:
    a list of args for the process executor

  Raises:
    ValueError: if the script type cannot be determined or is invalid for OS
  """

  (_, tail) = os.path.splitext(script_name)
  # No extension, just try to execute it
  if not tail and not interpreter:
    # In our tests, users generally just refer to 'gcloud'. But in windows,
    # there is no 'gcloud' file, there is gcloud.cmd. This isn't an issue on the
    # shell because the shell, given <name>, is smart enough to detect that
    # there is a file <name>.cmd. subprocess.Popen is not smart enough to know
    # this, however, unless shell=True is given. We'd like to avoid that if
    # possible, however.
    if _IsOnWindows() and script_name == 'gcloud':
      script_name = 'gcloud.cmd'
    return execution_utils.ArgsForExecutableTool(FullPath(script_name), *args)

  # Strip the '.'
  if interpreter:
    ext = interpreter
  else:
    ext = tail[1:]

  # Python, same across platforms
  if ext == 'py':
    return execution_utils.ArgsForPythonTool(FullPath(script_name), *args)

  # .exe, windows only
  if ext == 'exe':
    if not _IsOnWindows():
      raise ValueError('Extention for {0} is only valid for WINDOWS'.format(
          script_name))
    return execution_utils.ArgsForExecutableTool(FullPath(script_name), *args)

  # ending in a '.'
  if not ext:
    if _IsOnWindows():
      return execution_utils.ArgsForCMDTool(
          FullPath(script_name + 'cmd'), *args)
    else:
      return execution_utils.ArgsForExecutableTool(
          FullPath(script_name + 'sh'), *args)
  else:
    raise ValueError('Unknown script type: {0}'.format(script_name))