Ejemplo n.º 1
0
    def Run(self, args):
        """Run the authentication command."""

        token = args.token or console_io.PromptResponse('Refresh token: ')
        if not token:
            raise c_exc.InvalidArgumentException('token', 'No value provided.')

        creds = c_store.AcquireFromToken(token)
        account = args.account

        c_store.Refresh(creds)

        c_store.Store(creds, account)

        properties.PersistProperty(properties.VALUES.core.account, account)

        project = args.project
        if project:
            properties.PersistProperty(properties.VALUES.core.project, project)

        return creds
Ejemplo n.º 2
0
def _GetRcPaths(command_completion, path_update, rc_path, sdk_root, host_os):
    """Returns an _RcPaths object for the preferred user shell.

  Args:
    command_completion: bool, Whether or not to do command completion. If None,
      ask.
    path_update: bool, Whether or not to update PATH. If None, ask.
    rc_path: str, The path to the rc file to update. If None, ask.
    sdk_root: str, The path to the Cloud SDK root.
    host_os: str, The host os identification string.

  Returns:
    An _RcPaths() object for the preferred user shell.
  """

    # An initial guess on the preferred user shell based on the environment.
    preferred_shell = _GetPreferredShell(os.environ.get('SHELL', '/bin/sh'))
    if not command_completion and not path_update:
        rc_path = None
    elif not rc_path:
        file_name = _GetShellRcFileName(preferred_shell, host_os)
        rc_path = os.path.expanduser(os.path.join('~', file_name))

        rc_path_update = console_io.PromptResponse((
            'The Google Cloud SDK installer will now prompt you to update an rc '
            'file to bring the Google Cloud CLIs into your environment.\n\n'
            'Enter a path to an rc file to update, or leave blank to use '
            '[{rc_path}]:  ').format(rc_path=rc_path))
        if rc_path_update:
            rc_path = os.path.expanduser(rc_path_update)

    if rc_path:
        # Check the rc_path for a better hint at the user preferred shell.
        preferred_shell = _GetPreferredShell(rc_path, default=preferred_shell)

    return _RcPaths(preferred_shell, rc_path, sdk_root)
Ejemplo n.º 3
0
def UpdateRC(bash_completion, path_update, rc_path, bin_path):
    """Update the system path to include bin_path.

  Args:
    bash_completion: bool, Whether or not to do bash completion. If None, ask.
    path_update: bool, Whether or not to do bash completion. If None, ask.
    rc_path: str, The path to the rc file to update. If None, ask.
    bin_path: str, The absolute path to the directory that will contain
        Cloud SDK binaries.
  """

    host_os = platforms.OperatingSystem.Current()
    if host_os == platforms.OperatingSystem.WINDOWS:
        if path_update is None:
            path_update = console_io.PromptContinue(
                prompt_string='Update %PATH% to include Cloud SDK binaries?')
        if path_update:
            UpdatePathForWindows(bin_path)
        return

    if not rc_path:

        # figure out what file to edit
        if host_os == platforms.OperatingSystem.LINUX:
            if c_gce.Metadata().connected:
                file_name = '.bash_profile'
            else:
                file_name = '.bashrc'
        elif host_os == platforms.OperatingSystem.MACOSX:
            file_name = '.bash_profile'
        elif host_os == platforms.OperatingSystem.CYGWIN:
            file_name = '.bashrc'
        elif host_os == platforms.OperatingSystem.MSYS:
            file_name = '.profile'
        else:
            file_name = '.bashrc'
        rc_path = os.path.expanduser(os.path.join('~', file_name))

        rc_path_update = console_io.PromptResponse((
            'The Google Cloud SDK installer will now prompt you to update an rc '
            'file to bring the Google Cloud CLIs into your environment.\n\n'
            'Enter path to an rc file to update, or leave blank to use '
            '[{rc_path}]:  ').format(rc_path=rc_path))
        if rc_path_update:
            rc_path = os.path.expanduser(rc_path_update)

    if os.path.exists(rc_path):
        with open(rc_path) as rc_file:
            rc_data = rc_file.read()
            cached_rc_data = rc_data
    else:
        rc_data = ''
        cached_rc_data = ''

    updated_rc = False

    if path_update is None:
        path_update = console_io.PromptContinue(
            prompt_string=('\nModify profile to update your $PATH?'))

    path_rc_path = os.path.join(bootstrapping.SDK_ROOT, 'path.bash.inc')
    if path_update:
        path_comment = r'# The next line updates PATH for the Google Cloud SDK.'
        path_subre = re.compile(r'\n*' + path_comment + r'\n.*$', re.MULTILINE)

        path_line = "{comment}\nsource '{path_rc_path}'\n".format(
            comment=path_comment, path_rc_path=path_rc_path)
        filtered_data = path_subre.sub('', rc_data)
        rc_data = '{filtered_data}\n{path_line}'.format(
            filtered_data=filtered_data, path_line=path_line)
        updated_rc = True
    else:
        print("""\
Source [{path_rc_path}]
in your profile to add the Google Cloud SDK command line tools to your $PATH.
""".format(path_rc_path=path_rc_path))

    if bash_completion is None:
        bash_completion = console_io.PromptContinue(
            prompt_string=('\nModify profile to enable bash completion?'))

    completion_rc_path = os.path.join(bootstrapping.SDK_ROOT,
                                      'completion.bash.inc')
    if bash_completion:
        complete_comment = r'# The next line enables bash completion for gcloud.'
        complete_subre = re.compile(r'\n*' + complete_comment + r'\n.*$',
                                    re.MULTILINE)

        complete_line = "{comment}\nsource '{rc_path}'\n".format(
            comment=complete_comment, rc_path=completion_rc_path)
        filtered_data = complete_subre.sub('', rc_data)
        rc_data = '{filtered_data}\n{complete_line}'.format(
            filtered_data=filtered_data, complete_line=complete_line)
        updated_rc = True
    else:
        print("""\
Source [{completion_rc_path}]
in your profile to enable bash completion for gcloud.
""".format(completion_rc_path=completion_rc_path))

    if not updated_rc:
        return

    if cached_rc_data == rc_data:
        print('No changes necessary for [{rc}].'.format(rc=rc_path))
        return

    if os.path.exists(rc_path):
        rc_backup = rc_path + '.backup'
        print('Backing up [{rc}] to [{backup}].'.format(rc=rc_path,
                                                        backup=rc_backup))
        shutil.copyfile(rc_path, rc_backup)

    with open(rc_path, 'w') as rc_file:
        rc_file.write(rc_data)

    print("""\
[{rc_path}] has been updated.
Start a new shell for the changes to take effect.
""".format(rc_path=rc_path))