##// END OF EJS Templates
Added tag v5.1.0 for changeset df8a724f8e06
Added tag v5.1.0 for changeset df8a724f8e06

File last commit:

r5326:cede61e3 default
r5472:e040e532 stable
Show More
hg.py
157 lines | 5.5 KiB | text/x-python | PythonLexer
copyrights: updated for 2023
r5088 # Copyright (C) 2016-2023 RhodeCode GmbH
ssh-support: enabled full handling of all backends via SSH....
r2187 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import os
import sys
import logging
import tempfile
import textwrap
Mercurial: fix ssh-server support for UI objects...
r3626 import collections
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325
refactor(ssh-wrapper): changed SSHVcsServer to SshVcsServer, updated call_service_api method.
r5326 from .base import SshVcsServer
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325
from rhodecode.lib.api_utils import call_service_api
ssh-support: enabled full handling of all backends via SSH....
r2187
log = logging.getLogger(__name__)
class MercurialTunnelWrapper(object):
process = None
def __init__(self, server):
self.server = server
self.stdin = sys.stdin
self.stdout = sys.stdout
Mercurial: fix ssh-server support for UI objects...
r3626 self.hooks_env_fd, self.hooks_env_path = tempfile.mkstemp(prefix='hgrc_rhodecode_')
ssh-support: enabled full handling of all backends via SSH....
r2187
def create_hooks_env(self):
Mercurial: fix ssh-server support for UI objects...
r3626 repo_name = self.server.repo_name
ssh: fix invocation of custom hgrc
r3660 hg_flags = self.server.config_to_hgrc(repo_name)
ssh-support: enabled full handling of all backends via SSH....
r2187
content = textwrap.dedent(
'''
ssh: call custom hooks via SSH backend
r3637 # RhodeCode SSH hooks version=2.0.0
Mercurial: fix ssh-server support for UI objects...
r3626 {custom}
ssh-support: enabled full handling of all backends via SSH....
r2187 '''
Mercurial: fix ssh-server support for UI objects...
r3626 ).format(custom='\n'.join(hg_flags))
root = self.server.get_root_store()
hgrc_custom = os.path.join(root, repo_name, '.hg', 'hgrc_rhodecode')
hgrc_main = os.path.join(root, repo_name, '.hg', 'hgrc')
ssh-support: enabled full handling of all backends via SSH....
r2187
Mercurial: fix ssh-server support for UI objects...
r3626 # cleanup custom hgrc file
if os.path.isfile(hgrc_custom):
with open(hgrc_custom, 'wb') as f:
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 f.write(b'')
Mercurial: fix ssh-server support for UI objects...
r3626 log.debug('Cleanup custom hgrc file under %s', hgrc_custom)
# write temp
ssh-support: enabled full handling of all backends via SSH....
r2187 with os.fdopen(self.hooks_env_fd, 'w') as hooks_env_file:
hooks_env_file.write(content)
Mercurial: fix ssh-server support for UI objects...
r3626 return self.hooks_env_path
ssh-support: enabled full handling of all backends via SSH....
r2187
Mercurial: fix ssh-server support for UI objects...
r3626 def remove_configs(self):
os.remove(self.hooks_env_path)
ssh-support: enabled full handling of all backends via SSH....
r2187
Mercurial: fix ssh-server support for UI objects...
r3626 def command(self, hgrc_path):
ssh-support: enabled full handling of all backends via SSH....
r2187 root = self.server.get_root_store()
command = (
Mercurial: fix ssh-server support for UI objects...
r3626 "cd {root}; HGRCPATH={hgrc} {hg_path} -R {root}{repo_name} "
ssh-support: enabled full handling of all backends via SSH....
r2187 "serve --stdio".format(
root=root, hg_path=self.server.hg_path,
Mercurial: fix ssh-server support for UI objects...
r3626 repo_name=self.server.repo_name, hgrc=hgrc_path))
ssh-support: enabled full handling of all backends via SSH....
r2187 log.debug("Final CMD: %s", command)
return command
def run(self, extras):
# at this point we cannot tell, we do further ACL checks
# inside the hooks
action = '?'
# permissions are check via `pre_push_ssh_auth` hook
self.server.update_environment(action=action, extras=extras)
Mercurial: fix ssh-server support for UI objects...
r3626 custom_hgrc_file = self.create_hooks_env()
try:
return os.system(self.command(custom_hgrc_file))
finally:
self.remove_configs()
ssh-support: enabled full handling of all backends via SSH....
r2187
refactor(ssh-wrapper): changed SSHVcsServer to SshVcsServer, updated call_service_api method.
r5326 class MercurialServer(SshVcsServer):
ssh-support: enabled full handling of all backends via SSH....
r2187 backend = 'hg'
statsd/audit-logs: cleanup push/pull user agent code....
r4858 repo_user_agent = 'mercurial'
ssh: call custom hooks via SSH backend
r3637 cli_flags = ['phases', 'largefiles', 'extensions', 'experimental', 'hooks']
ssh-support: enabled full handling of all backends via SSH....
r2187
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 def __init__(self, store, ini_path, repo_name, user, user_permissions, settings, env):
super().__init__(user, user_permissions, settings, env)
ssh-support: enabled full handling of all backends via SSH....
r2187
self.store = store
self.ini_path = ini_path
self.repo_name = repo_name
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 self._path = self.hg_path = settings['ssh.executable.hg']
Mercurial: fix ssh-server support for UI objects...
r3626 self.tunnel = MercurialTunnelWrapper(server=self)
def config_to_hgrc(self, repo_name):
fix(ssh): Added alternative SshWrapper and changes needed to support it + service api. Fixes: RCCE-6
r5314 # Todo: once transition is done only call to service api should exist
if self.hooks_protocol == 'celery':
refactor(ssh-wrapper): changed SSHVcsServer to SshVcsServer, updated call_service_api method.
r5326 data = call_service_api(self.settings, {
fix(ssh): Added alternative SshWrapper and changes needed to support it + service api. Fixes: RCCE-6
r5314 "method": "service_config_to_hgrc",
"args": {"cli_flags": self.cli_flags, "repo_name": repo_name}
})
return data['flags']
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 else:
from rhodecode.model.db import RhodeCodeUi
from rhodecode.model.settings import VcsSettingsModel
ui_sections = collections.defaultdict(list)
ui = VcsSettingsModel(repo=repo_name).get_ui_settings(section=None, key=None)
ssh-support: enabled full handling of all backends via SSH....
r2187
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 # write default hooks
default_hooks = [
('pretxnchangegroup.ssh_auth', 'python:vcsserver.hooks.pre_push_ssh_auth'),
('pretxnchangegroup.ssh', 'python:vcsserver.hooks.pre_push_ssh'),
('changegroup.ssh', 'python:vcsserver.hooks.post_push_ssh'),
ssh: call custom hooks via SSH backend
r3637
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 ('preoutgoing.ssh', 'python:vcsserver.hooks.pre_pull_ssh'),
('outgoing.ssh', 'python:vcsserver.hooks.post_pull_ssh'),
]
ssh: call custom hooks via SSH backend
r3637
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 for k, v in default_hooks:
ui_sections['hooks'].append((k, v))
ssh: call custom hooks via SSH backend
r3637
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 for entry in ui:
if not entry.active:
ssh: call custom hooks via SSH backend
r3637 continue
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 sec = entry.section
key = entry.key
ssh: call custom hooks via SSH backend
r3637
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 if sec in self.cli_flags:
# we want only custom hooks, so we skip builtins
if sec == 'hooks' and key in RhodeCodeUi.HOOKS_BUILTIN:
continue
Mercurial: fix ssh-server support for UI objects...
r3626
feat(ssh-wrapper-speedup): major rewrite of code to address imports problem with ssh-wrapper-v2...
r5325 ui_sections[sec].append([key, entry.value])
flags = []
for _sec, key_val in ui_sections.items():
flags.append(' ')
flags.append(f'[{_sec}]')
for key, val in key_val:
flags.append(f'{key}= {val}')
return flags