##// END OF EJS Templates
feat(metric): added new metric calculation. Fixes: RCCE-5
ilin.s -
r5268:39887b2a default
parent child Browse files
Show More
@@ -1,79 +1,86 b''
1 1 # Copyright (C) 2016-2023 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19 import os
20 20 import sys
21 import time
21 22 import logging
22 23
23 24 import click
24 25
25 26 from pyramid.paster import setup_logging
26 27
27 28 from rhodecode.lib.pyramid_utils import bootstrap
29 from rhodecode.lib.statsd_client import StatsdClient
28 30 from .backends import SshWrapper
29 31
30 32 log = logging.getLogger(__name__)
31 33
32 34
33 35 def setup_custom_logging(ini_path, debug):
34 36 if debug:
35 37 # enabled rhodecode.ini controlled logging setup
36 38 setup_logging(ini_path)
37 39 else:
38 40 # configure logging in a mode that doesn't print anything.
39 41 # in case of regularly configured logging it gets printed out back
40 42 # to the client doing an SSH command.
41 43 logger = logging.getLogger('')
42 44 null = logging.NullHandler()
43 45 # add the handler to the root logger
44 46 logger.handlers = [null]
45 47
46 48
47 49 @click.command()
48 50 @click.argument('ini_path', type=click.Path(exists=True))
49 51 @click.option(
50 52 '--mode', '-m', required=False, default='auto',
51 53 type=click.Choice(['auto', 'vcs', 'git', 'hg', 'svn', 'test']),
52 54 help='mode of operation')
53 55 @click.option('--user', help='Username for which the command will be executed')
54 56 @click.option('--user-id', help='User ID for which the command will be executed')
55 57 @click.option('--key-id', help='ID of the key from the database')
56 58 @click.option('--shell', '-s', is_flag=True, help='Allow Shell')
57 59 @click.option('--debug', is_flag=True, help='Enabled detailed output logging')
58 60 def main(ini_path, mode, user, user_id, key_id, shell, debug):
59 61 setup_custom_logging(ini_path, debug)
60 62
61 63 command = os.environ.get('SSH_ORIGINAL_COMMAND', '')
62 64 if not command and mode not in ['test']:
63 65 raise ValueError(
64 66 'Unable to fetch SSH_ORIGINAL_COMMAND from environment.'
65 67 'Please make sure this is set and available during execution '
66 68 'of this script.')
67 69 connection_info = os.environ.get('SSH_CONNECTION', '')
68
70 time_start = time.time()
69 71 with bootstrap(ini_path, env={'RC_CMD_SSH_WRAPPER': '1'}) as env:
72 statsd = StatsdClient.statsd
70 73 try:
71 74 ssh_wrapper = SshWrapper(
72 75 command, connection_info, mode,
73 76 user, user_id, key_id, shell, ini_path, env)
74 77 except Exception:
75 78 log.exception('Failed to execute SshWrapper')
76 79 sys.exit(-5)
77
78 80 return_code = ssh_wrapper.wrap()
81 operation_took = time.time() - time_start
82 if statsd:
83 operation_took_ms = round(1000.0 * operation_took)
84 statsd.timing("rhodecode_ssh_wrapper_timing.histogram", operation_took_ms,
85 use_decimals=False)
79 86 sys.exit(return_code)
General Comments 0
You need to be logged in to leave comments. Login now