Explorar o código

[git_auth] Check and fix auth on GCE

Bug: 410713357
Change-Id: I6d39641451d78c192321b77477a5e0f741715f85
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6485110
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Allen Li <ayatane@chromium.org>
Allen Li hai 3 meses
pai
achega
f55de362c1
Modificáronse 1 ficheiros con 46 adicións e 0 borrados
  1. 46 0
      git_auth.py

+ 46 - 0
git_auth.py

@@ -497,6 +497,7 @@ class ConfigWizard(object):
         if self._check_sso_helper():
             self._println('SSO helper is available.')
             self._set_config('protocol.sso.allow', 'always', scope='global')
+        self._check_gce()
         self._println()
         self._run_gerrit_host_configuration(force_global=force_global)
         self._println()
@@ -776,6 +777,40 @@ class ConfigWizard(object):
             divergent_cookiefiles=divergent_cookiefiles,
         )
 
+    def _check_gce(self):
+        if not gerrit_util.GceAuthenticator.is_applicable():
+            return
+        self._println()
+        self._println('This appears to be a GCE VM.')
+        self._println('You will need to add `export SKIP_GCE_AUTH_FOR_GIT=1`')
+        self._println('to your .bashrc or similar.')
+        fallback_msg = 'Add `export SKIP_GCE_AUTH_FOR_GIT=1` to your .bashrc or similar.'
+        if os.name == 'nt':
+            # Can't automatically handle Windows yet.
+            self._println_action(fallback_msg)
+            return
+        rcfile: str | None = None
+        options = [
+            os.path.expanduser('~/.bashrc'),
+        ]
+
+        for p in options:
+            if os.path.exists(p):
+                self._println(f'We found {p!r}')
+                rcfile = p
+                break
+        if not rcfile:
+            self._println("We couldn't automatically detect your rcfile")
+            self._println("so you'll need to do it manually.")
+            self._println_action(fallback_msg)
+            return
+        if self._read_yn(f'Shall we add this line to {rcfile}?', default=True):
+            self._append_to_file(rcfile, 'export SKIP_GCE_AUTH_FOR_GIT=1')
+            self._println_action(
+                'Restart your shell/terminal to use the updated environment.')
+            return
+        self._println_action(fallback_msg)
+
     def _check_global_email(self) -> str:
         """Checks and returns user's global Git email.
 
@@ -917,6 +952,17 @@ class ConfigWizard(object):
 
     # Low level misc helpers
 
+    def _append_to_file(self, path: str, line: str) -> None:
+        """Append line to file.
+
+        One newline is written before and after the given line string.
+        """
+        with open(path, 'a') as f:
+            f.write('\n')
+            f.write(line)
+            f.write('\n')
+        self._println_notify(f'Added {line!r} to {path!r}')
+
     def _move_file(self, path: str) -> None:
         """Move file to a backup path."""
         backup = f'{path}.bak'