Exemplo n.º 1
0
  def _ExplainIfSudoNeeded(self, tf, dirs_to_remove, old_cwd):
    """Explains what to do if sudo needed to update gsutil software.

    Happens if gsutil was previously installed by a different user (typically if
    someone originally installed in a shared file system location, using sudo).

    Args:
      tf: Opened TarFile.
      dirs_to_remove: List of directories to remove.
      old_cwd: Path to the working directory we should chdir back to if sudo is
          needed. It's possible that we've chdir'd to a temp directory that's
          been deleted, which can cause odd behavior (e.g. OSErrors when opening
          the metrics subprocess). If this is not truthy, we won't attempt to
          chdir back to this value.

    Raises:
      CommandException: if errors encountered.
    """
    # If running under Windows or Cygwin we don't need (or have) sudo.
    if system_util.IS_CYGWIN or system_util.IS_WINDOWS:
      return

    user_id = os.getuid()
    if os.stat(gslib.GSUTIL_DIR).st_uid == user_id:
      return

    # Won't fail - this command runs after main startup code that insists on
    # having a config file.
    config_file_list = GetConfigFilePaths()
    config_files = ' '.join(config_file_list)
    self._CleanUpUpdateCommand(tf, dirs_to_remove, old_cwd)

    # Pick current protection of each boto config file for command that restores
    # protection (rather than fixing at 600) to support use cases like how GCE
    # installs a service account with an /etc/boto.cfg file protected to 644.
    chmod_cmds = []
    for config_file in config_file_list:
      mode = oct(stat.S_IMODE((os.stat(config_file)[stat.ST_MODE])))
      chmod_cmds.append('\n\tsudo chmod %s %s' % (mode, config_file))

    raise CommandException('\n'.join(
        textwrap.wrap(
            'Since it was installed by a different user previously, you will need '
            'to update using the following commands. You will be prompted for your '
            'password, and the install will run as "root". If you\'re unsure what '
            'this means please ask your system administrator for help:')) + (
                '\n\tsudo chmod 0644 %s\n\tsudo env BOTO_CONFIG="%s" %s update'
                '%s') % (config_files, config_files, self.gsutil_path,
                         ' '.join(chmod_cmds)),
                           informational=True)
Exemplo n.º 2
0
    def RunCommand(self):
        """Command entry point for the version command."""
        long_form = False
        if self.sub_opts:
            for o, _ in self.sub_opts:
                if o == '-l':
                    long_form = True

        if GetConfigFilePaths():
            config_paths = ', '.join(GetConfigFilePaths())
        else:
            config_paths = 'no config found'

        shipped_checksum = gslib.CHECKSUM
        try:
            cur_checksum = self._ComputeCodeChecksum()
        except IOError:
            cur_checksum = 'MISSING FILES'
        if shipped_checksum == cur_checksum:
            checksum_ok_str = 'OK'
        else:
            checksum_ok_str = '!= %s' % shipped_checksum

        sys.stdout.write('gsutil version: %s\n' % gslib.VERSION)

        if long_form:

            long_form_output = (
                'checksum: {checksum} ({checksum_ok})\n'
                'boto version: {boto_version}\n'
                'python version: {python_version}\n'
                'OS: {os_version}\n'
                'multiprocessing available: {multiprocessing_available}\n'
                'using cloud sdk: {cloud_sdk}\n'
                'pass cloud sdk credentials to gsutil: {cloud_sdk_credentials}\n'
                'config path(s): {config_paths}\n'
                'gsutil path: {gsutil_path}\n'
                'compiled crcmod: {compiled_crcmod}\n'
                'installed via package manager: {is_package_install}\n'
                'editable install: {is_editable_install}\n')

            sys.stdout.write(
                long_form_output.format(
                    checksum=cur_checksum,
                    checksum_ok=checksum_ok_str,
                    boto_version=boto.__version__,
                    python_version=sys.version.replace('\n', ''),
                    os_version='%s %s' %
                    (platform.system(), platform.release()),
                    multiprocessing_available=(
                        CheckMultiprocessingAvailableAndInit().is_available),
                    cloud_sdk=(os.environ.get('CLOUDSDK_WRAPPER') == '1'),
                    cloud_sdk_credentials=(os.environ.get(
                        'CLOUDSDK_CORE_PASS_CREDENTIALS_TO_GSUTIL') == '1'),
                    config_paths=config_paths,
                    gsutil_path=gslib.GSUTIL_PATH,
                    compiled_crcmod=UsingCrcmodExtension(crcmod),
                    is_package_install=gslib.IS_PACKAGE_INSTALL,
                    is_editable_install=gslib.IS_EDITABLE_INSTALL,
                ))

        return 0