Show More
@@ -21,10 +21,13 b'' | |||||
21 | import logging |
|
21 | import logging | |
22 | import os |
|
22 | import os | |
23 |
|
23 | |||
24 | from rhodecode import events |
|
24 | # Do not use `from rhodecode import events` here, it will be overridden by the | |
|
25 | # events module in this package due to pythons import mechanism. | |||
|
26 | from rhodecode.events import RepoGroupEvent | |||
25 | from rhodecode.config.middleware import _bool_setting, _string_setting |
|
27 | from rhodecode.config.middleware import _bool_setting, _string_setting | |
26 |
|
28 | |||
27 | from .subscribers import generate_config_subscriber |
|
29 | from .events import ModDavSvnConfigChange | |
|
30 | from .subscribers import generate_config_subscriber, AsyncSubprocessSubscriber | |||
28 | from . import config_keys |
|
31 | from . import config_keys | |
29 |
|
32 | |||
30 |
|
33 | |||
@@ -36,8 +39,17 b' def includeme(config):' | |||||
36 | _sanitize_settings_and_apply_defaults(settings) |
|
39 | _sanitize_settings_and_apply_defaults(settings) | |
37 |
|
40 | |||
38 | if settings[config_keys.generate_config]: |
|
41 | if settings[config_keys.generate_config]: | |
|
42 | # Add subscriber to generate the Apache mod dav svn configuration on | |||
|
43 | # repository group events. | |||
|
44 | config.add_subscriber(generate_config_subscriber, RepoGroupEvent) | |||
|
45 | ||||
|
46 | # Prepare reload command to pass it to the subprocess module and add a | |||
|
47 | # subscriber to execute it on configuration changes. | |||
|
48 | cmd = settings[config_keys.reload_command] | |||
|
49 | cmd = cmd.split(' ') if cmd else cmd | |||
39 | config.add_subscriber( |
|
50 | config.add_subscriber( | |
40 | generate_config_subscriber, events.RepoGroupEvent) |
|
51 | AsyncSubprocessSubscriber(cmd=cmd, timeout=5), | |
|
52 | ModDavSvnConfigChange) | |||
41 |
|
53 | |||
42 |
|
54 | |||
43 | def _sanitize_settings_and_apply_defaults(settings): |
|
55 | def _sanitize_settings_and_apply_defaults(settings): |
@@ -19,8 +19,10 b'' | |||||
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | import logging |
|
21 | import logging | |
|
22 | import subprocess32 | |||
|
23 | from threading import Thread | |||
22 |
|
24 | |||
23 | from rhodecode.lib.utils import get_rhodecode_base_path |
|
25 | ||
24 | from .utils import generate_mod_dav_svn_config |
|
26 | from .utils import generate_mod_dav_svn_config | |
25 |
|
27 | |||
26 |
|
28 | |||
@@ -34,9 +36,97 b' def generate_config_subscriber(event):' | |||||
34 | changes. |
|
36 | changes. | |
35 | """ |
|
37 | """ | |
36 | try: |
|
38 | try: | |
37 | generate_mod_dav_svn_config( |
|
39 | generate_mod_dav_svn_config(event.request.registry) | |
38 | settings=event.request.registry.settings, |
|
|||
39 | parent_path_root=get_rhodecode_base_path()) |
|
|||
40 | except Exception: |
|
40 | except Exception: | |
41 | log.exception( |
|
41 | log.exception( | |
42 | 'Exception while generating subversion mod_dav_svn configuration.') |
|
42 | 'Exception while generating subversion mod_dav_svn configuration.') | |
|
43 | ||||
|
44 | ||||
|
45 | class Subscriber(object): | |||
|
46 | def __call__(self, event): | |||
|
47 | self.run(event) | |||
|
48 | ||||
|
49 | def run(self, event): | |||
|
50 | raise NotImplementedError('Subclass has to implement this.') | |||
|
51 | ||||
|
52 | ||||
|
53 | class AsyncSubscriber(Subscriber): | |||
|
54 | def __init__(self, *args, **kwargs): | |||
|
55 | self._init_args = args | |||
|
56 | self._init_kwargs = kwargs | |||
|
57 | ||||
|
58 | def __call__(self, event): | |||
|
59 | kwargs = {'event': event} | |||
|
60 | kwargs.update(self._init_kwargs) | |||
|
61 | self.thread = Thread( | |||
|
62 | target=self.run, args=self._init_args, kwargs=kwargs) | |||
|
63 | self.thread.start() | |||
|
64 | ||||
|
65 | ||||
|
66 | class AsyncSubprocessSubscriber(AsyncSubscriber): | |||
|
67 | def run(self, event, cmd, timeout=None): | |||
|
68 | log.debug('Executing command %s.', cmd) | |||
|
69 | try: | |||
|
70 | output = subprocess32.check_output( | |||
|
71 | cmd, timeout=timeout, stderr=subprocess32.STDOUT) | |||
|
72 | log.debug('Command finished %s', cmd) | |||
|
73 | if output: | |||
|
74 | log.debug('Command output: %s', output) | |||
|
75 | except subprocess32.TimeoutExpired as e: | |||
|
76 | log.exception('Timeout while executing command.') | |||
|
77 | if e.output: | |||
|
78 | log.error('Command output: %s', e.output) | |||
|
79 | except subprocess32.CalledProcessError as e: | |||
|
80 | log.exception('Error while executing command.') | |||
|
81 | if e.output: | |||
|
82 | log.error('Command output: %s', e.output) | |||
|
83 | except: | |||
|
84 | log.exception( | |||
|
85 | 'Exception while executing command %s.', cmd) | |||
|
86 | ||||
|
87 | ||||
|
88 | # class ReloadApacheSubscriber(object): | |||
|
89 | # """ | |||
|
90 | # Subscriber to pyramids event system. It executes the Apache reload command | |||
|
91 | # if set in ini-file. The command is executed asynchronously in a separate | |||
|
92 | # task. This is done to prevent a delay of the function which triggered the | |||
|
93 | # event in case of a longer running command. If a timeout is passed to the | |||
|
94 | # constructor the command will be terminated after expiration. | |||
|
95 | # """ | |||
|
96 | # def __init__(self, settings, timeout=None): | |||
|
97 | # self.thread = None | |||
|
98 | # cmd = self.get_command_from_settings(settings) | |||
|
99 | # if cmd: | |||
|
100 | # kwargs = { | |||
|
101 | # 'cmd': cmd, | |||
|
102 | # 'timeout': timeout, | |||
|
103 | # } | |||
|
104 | # self.thread = Thread(target=self.run, kwargs=kwargs) | |||
|
105 | ||||
|
106 | # def __call__(self, event): | |||
|
107 | # if self.thread is not None: | |||
|
108 | # self.thread.start() | |||
|
109 | ||||
|
110 | # def get_command_from_settings(self, settings): | |||
|
111 | # cmd = settings[config_keys.reload_command] | |||
|
112 | # return cmd.split(' ') if cmd else cmd | |||
|
113 | ||||
|
114 | # def run(self, cmd, timeout=None): | |||
|
115 | # log.debug('Executing svn proxy reload command %s.', cmd) | |||
|
116 | # try: | |||
|
117 | # output = subprocess32.check_output( | |||
|
118 | # cmd, timeout=timeout, stderr=subprocess32.STDOUT) | |||
|
119 | # log.debug('Svn proxy reload command finished.') | |||
|
120 | # if output: | |||
|
121 | # log.debug('Command output: %s', output) | |||
|
122 | # except subprocess32.TimeoutExpired as e: | |||
|
123 | # log.exception('Timeout while executing svn proxy reload command.') | |||
|
124 | # if e.output: | |||
|
125 | # log.error('Command output: %s', e.output) | |||
|
126 | # except subprocess32.CalledProcessError as e: | |||
|
127 | # log.exception('Error while executing svn proxy reload command.') | |||
|
128 | # if e.output: | |||
|
129 | # log.error('Command output: %s', e.output) | |||
|
130 | # except: | |||
|
131 | # log.exception( | |||
|
132 | # 'Exception while executing svn proxy reload command %s.', cmd) |
@@ -21,31 +21,37 b'' | |||||
21 | import codecs |
|
21 | import codecs | |
22 | import logging |
|
22 | import logging | |
23 | import os |
|
23 | import os | |
24 |
|
||||
25 | from pyramid.renderers import render |
|
24 | from pyramid.renderers import render | |
26 |
|
25 | |||
27 |
from rhodecode. |
|
26 | from rhodecode.events import trigger | |
|
27 | from rhodecode.lib.utils import get_rhodecode_realm, get_rhodecode_base_path | |||
28 | from rhodecode.model.db import RepoGroup |
|
28 | from rhodecode.model.db import RepoGroup | |
|
29 | ||||
29 | from . import config_keys |
|
30 | from . import config_keys | |
|
31 | from .events import ModDavSvnConfigChange | |||
30 |
|
32 | |||
31 |
|
33 | |||
32 | log = logging.getLogger(__name__) |
|
34 | log = logging.getLogger(__name__) | |
33 |
|
35 | |||
34 |
|
36 | |||
35 |
def generate_mod_dav_svn_config( |
|
37 | def generate_mod_dav_svn_config(registry): | |
36 | """ |
|
38 | """ | |
37 | Generate the configuration file for use with subversion's mod_dav_svn |
|
39 | Generate the configuration file for use with subversion's mod_dav_svn | |
38 | module. The configuration has to contain a <Location> block for each |
|
40 | module. The configuration has to contain a <Location> block for each | |
39 | available repository group because the mod_dav_svn module does not support |
|
41 | available repository group because the mod_dav_svn module does not support | |
40 | repositories organized in sub folders. |
|
42 | repositories organized in sub folders. | |
41 | """ |
|
43 | """ | |
|
44 | settings = registry.settings | |||
42 | config = _render_mod_dav_svn_config( |
|
45 | config = _render_mod_dav_svn_config( | |
43 |
parent_path_root= |
|
46 | parent_path_root=get_rhodecode_base_path(), | |
44 | list_parent_path=settings[config_keys.list_parent_path], |
|
47 | list_parent_path=settings[config_keys.list_parent_path], | |
45 | location_root=settings[config_keys.location_root], |
|
48 | location_root=settings[config_keys.location_root], | |
46 | repo_groups=RepoGroup.get_all_repo_groups()) |
|
49 | repo_groups=RepoGroup.get_all_repo_groups()) | |
47 | _write_mod_dav_svn_config(config, settings[config_keys.config_file_path]) |
|
50 | _write_mod_dav_svn_config(config, settings[config_keys.config_file_path]) | |
48 |
|
51 | |||
|
52 | # Trigger an event on mod dav svn configuration change. | |||
|
53 | trigger(ModDavSvnConfigChange(), registry) | |||
|
54 | ||||
49 |
|
55 | |||
50 | def _render_mod_dav_svn_config( |
|
56 | def _render_mod_dav_svn_config( | |
51 | parent_path_root, list_parent_path, location_root, repo_groups): |
|
57 | parent_path_root, list_parent_path, location_root, repo_groups): |
General Comments 0
You need to be logged in to leave comments.
Login now