From 9563bcff1110cfc474e8bd6d89013e5f50ee422e Mon Sep 17 00:00:00 2001 From: Arksine Date: Sat, 6 Mar 2021 20:21:32 -0500 Subject: [PATCH] update_manager: add environment variables to git fetch and git pull Pass GIT_HTTP_LOW_SPEED_LIMIT and GIT_HTTP_LOW_SPEED_TIME environment variables to the "git fetch" and "git pull" commands. If the remote is unreachable this should force the command to timeout before Moonraker forcefully terminates it. Signed-off-by: Eric Callahan --- moonraker/plugins/update_manager.py | 32 ++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/moonraker/plugins/update_manager.py b/moonraker/plugins/update_manager.py index 3dba5df..63407ff 100644 --- a/moonraker/plugins/update_manager.py +++ b/moonraker/plugins/update_manager.py @@ -259,10 +259,15 @@ class UpdateManager: 'github_limit_reset_time': self.gh_limit_reset_time, 'busy': self.current_update is not None} - async def execute_cmd(self, cmd, timeout=10., notify=False, retries=1): + async def execute_cmd(self, cmd, timeout=10., notify=False, + retries=1, env=None): shell_command = self.server.lookup_plugin('shell_command') + if env is not None: + os_env = dict(os.environ) + os_env.update(env) + env = os_env cb = self.notify_update_response if notify else None - scmd = shell_command.build_shell_command(cmd, callback=cb) + scmd = shell_command.build_shell_command(cmd, callback=cb, env=env) while retries: if await scmd.run(timeout=timeout, verbose=notify): break @@ -270,9 +275,13 @@ class UpdateManager: if not retries: raise self.server.error("Shell Command Error") - async def execute_cmd_with_response(self, cmd, timeout=10.): + async def execute_cmd_with_response(self, cmd, timeout=10., env=None): shell_command = self.server.lookup_plugin('shell_command') - scmd = shell_command.build_shell_command(cmd, None) + if env is not None: + os_env = dict(os.environ) + os_env.update(env) + env = os_env + scmd = shell_command.build_shell_command(cmd, None, env=env) result = await scmd.run_with_response(timeout, retries=5) if result is None: raise self.server.error(f"Error Running Command: {cmd}") @@ -558,9 +567,13 @@ class GitUpdater: f"git -C {self.repo_path} config --get" f" branch.{self.branch}.remote") if need_fetch: + env = { + 'GIT_HTTP_LOW_SPEED_LIMIT': "1000", + 'GIT_HTTP_LOW_SPEED_TIME ': "15" + } await self.execute_cmd( f"git -C {self.repo_path} fetch {self.remote} --prune -q", - retries=3) + timeout=20., retries=3, env=env) remote_url = await self.execute_cmd_with_response( f"git -C {self.repo_path} remote get-url {self.remote}") cur_hash = await self.execute_cmd_with_response( @@ -634,16 +647,21 @@ class GitUpdater: return self._notify_status("Updating Repo...") try: + env = { + 'GIT_HTTP_LOW_SPEED_LIMIT': "1000", + 'GIT_HTTP_LOW_SPEED_TIME ': "15" + } if self.detached: await self.execute_cmd( f"git -C {self.repo_path} fetch {self.remote} -q", - retries=3) + timeout=20., retries=3, env=env) await self.execute_cmd( f"git -C {self.repo_path} checkout" f" {self.remote}/{self.branch} -q") else: await self.execute_cmd( - f"git -C {self.repo_path} pull -q", retries=3) + f"git -C {self.repo_path} pull -q", timeout=20., + retries=3, env=env) except Exception: raise self._log_exc("Error running 'git pull'") # Check Semantic Versions