##// END OF EJS Templates
env-variables: make it safer if there's a syntax problem inside .ini file....
env-variables: make it safer if there's a syntax problem inside .ini file. It's better to not crash, since it means server wont start. Let users fix problems instead of breaking the startup because of that.

File last commit:

r2651:95f21d33 default
r3237:5cf82ecc default
Show More
ssh_wrapper.py
81 lines | 3.0 KiB | text/x-python | PythonLexer
ssh: embedded ssh support...
r2043 # -*- coding: utf-8 -*-
release: update copyright year to 2018
r2487 # Copyright (C) 2016-2018 RhodeCode GmbH
ssh: embedded ssh support...
r2043 #
# 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 click
ssh: allow customizing the base_url for running application....
r2188 from pyramid.paster import setup_logging
ssh-support: don't use API calls to fetch the data....
r2186
ssh: allow customizing the base_url for running application....
r2188 from rhodecode.lib.pyramid_utils import bootstrap
ssh-support: don't use API calls to fetch the data....
r2186 from .backends import SshWrapper
ssh: embedded ssh support...
r2043
log = logging.getLogger(__name__)
ssh-support: don't use API calls to fetch the data....
r2186 def setup_custom_logging(ini_path, debug):
ssh: embedded ssh support...
r2043 if debug:
# enabled rhodecode.ini controlled logging setup
ssh-support: don't use API calls to fetch the data....
r2186 setup_logging(ini_path)
ssh: embedded ssh support...
r2043 else:
# configure logging in a mode that doesn't print anything.
# in case of regularly configured logging it gets printed out back
# to the client doing an SSH command.
logger = logging.getLogger('')
null = logging.NullHandler()
# add the handler to the root logger
logger.handlers = [null]
@click.command()
@click.argument('ini_path', type=click.Path(exists=True))
@click.option(
'--mode', '-m', required=False, default='auto',
type=click.Choice(['auto', 'vcs', 'git', 'hg', 'svn', 'test']),
help='mode of operation')
@click.option('--user', help='Username for which the command will be executed')
@click.option('--user-id', help='User ID for which the command will be executed')
ssh-support: don't use API calls to fetch the data....
r2186 @click.option('--key-id', help='ID of the key from the database')
ssh: embedded ssh support...
r2043 @click.option('--shell', '-s', is_flag=True, help='Allow Shell')
@click.option('--debug', is_flag=True, help='Enabled detailed output logging')
ssh-support: don't use API calls to fetch the data....
r2186 def main(ini_path, mode, user, user_id, key_id, shell, debug):
setup_custom_logging(ini_path, debug)
ssh: embedded ssh support...
r2043
command = os.environ.get('SSH_ORIGINAL_COMMAND', '')
if not command and mode not in ['test']:
raise ValueError(
'Unable to fetch SSH_ORIGINAL_COMMAND from environment.'
'Please make sure this is set and available during execution '
'of this script.')
ssh-support: don't use API calls to fetch the data....
r2186 connection_info = os.environ.get('SSH_CONNECTION', '')
ssh-support: enabled full handling of all backends via SSH....
r2187
bootstrap: allow setting certain env flags for usage in CLI scripts....
r2651 with bootstrap(ini_path, env={'RC_CMD_SSH_WRAPPER': '1'}) as env:
ssh-support: don't use API calls to fetch the data....
r2186 try:
ssh_wrapper = SshWrapper(
command, connection_info, mode,
ssh-support: enabled full handling of all backends via SSH....
r2187 user, user_id, key_id, shell, ini_path, env)
ssh-support: don't use API calls to fetch the data....
r2186 except Exception:
log.exception('Failed to execute SshWrapper')
sys.exit(-5)
ssh: embedded ssh support...
r2043
ssh-support: don't use API calls to fetch the data....
r2186 return_code = ssh_wrapper.wrap()
sys.exit(return_code)