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