Show More
@@ -1,64 +1,62 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2018 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | |
|
23 | 23 | from pyramid.view import view_config |
|
24 | 24 | |
|
25 | 25 | from rhodecode.apps._base import RepoAppView |
|
26 | 26 | from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator |
|
27 | 27 | from rhodecode.lib import repo_maintenance |
|
28 | 28 | |
|
29 | 29 | log = logging.getLogger(__name__) |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | class RepoMaintenanceView(RepoAppView): |
|
33 | 33 | def load_default_context(self): |
|
34 | 34 | c = self._get_local_tmpl_context() |
|
35 | ||
|
36 | ||
|
37 | 35 | return c |
|
38 | 36 | |
|
39 | 37 | @LoginRequired() |
|
40 | 38 | @HasRepoPermissionAnyDecorator('repository.admin') |
|
41 | 39 | @view_config( |
|
42 | 40 | route_name='edit_repo_maintenance', request_method='GET', |
|
43 | 41 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') |
|
44 | 42 | def repo_maintenance(self): |
|
45 | 43 | c = self.load_default_context() |
|
46 | 44 | c.active = 'maintenance' |
|
47 | 45 | maintenance = repo_maintenance.RepoMaintenance() |
|
48 | 46 | c.executable_tasks = maintenance.get_tasks_for_repo(self.db_repo) |
|
49 | 47 | return self._get_template_context(c) |
|
50 | 48 | |
|
51 | 49 | @LoginRequired() |
|
52 | 50 | @HasRepoPermissionAnyDecorator('repository.admin') |
|
53 | 51 | @view_config( |
|
54 | 52 | route_name='edit_repo_maintenance_execute', request_method='GET', |
|
55 | 53 | renderer='json', xhr=True) |
|
56 | 54 | def repo_maintenance_execute(self): |
|
57 | 55 | c = self.load_default_context() |
|
58 | 56 | c.active = 'maintenance' |
|
59 | 57 | _ = self.request.translate |
|
60 | 58 | |
|
61 | 59 | maintenance = repo_maintenance.RepoMaintenance() |
|
62 | 60 | executed_types = maintenance.execute(self.db_repo) |
|
63 | 61 | |
|
64 | 62 | return executed_types |
@@ -1,118 +1,175 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2017-2018 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | import logging |
|
21 | 21 | |
|
22 | 22 | log = logging.getLogger(__name__) |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | class MaintenanceTask(object): |
|
26 | 26 | human_name = 'undefined' |
|
27 | 27 | |
|
28 | 28 | def __init__(self, db_repo): |
|
29 | 29 | self.db_repo = db_repo |
|
30 | 30 | |
|
31 | 31 | def run(self): |
|
32 | 32 | """Execute task and return task human value""" |
|
33 | 33 | raise NotImplementedError() |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | class GitGC(MaintenanceTask): |
|
37 | 37 | human_name = 'GIT Garbage collect' |
|
38 | 38 | |
|
39 | 39 | def _count_objects(self, repo): |
|
40 | 40 | stdout, stderr = repo.run_git_command( |
|
41 | 41 | ['count-objects', '-v'], fail_on_stderr=False) |
|
42 | 42 | |
|
43 | 43 | errors = ' ' |
|
44 | 44 | objects = ' '.join(stdout.splitlines()) |
|
45 | 45 | |
|
46 | 46 | if stderr: |
|
47 | 47 | errors = '\nSTD ERR:' + '\n'.join(stderr.splitlines()) |
|
48 | 48 | return objects + errors |
|
49 | 49 | |
|
50 | 50 | def run(self): |
|
51 | 51 | output = [] |
|
52 | 52 | instance = self.db_repo.scm_instance() |
|
53 | 53 | |
|
54 | objects = self._count_objects(instance) | |
|
55 | output.append(objects) | |
|
56 | log.debug('GIT objects:%s', objects) | |
|
54 | objects_before = self._count_objects(instance) | |
|
57 | 55 | |
|
58 | stdout, stderr = instance.run_git_command( | |
|
59 |
|
|
|
56 | log.debug('GIT objects:%s', objects_before) | |
|
57 | cmd = ['gc', '--aggressive'] | |
|
58 | stdout, stderr = instance.run_git_command(cmd, fail_on_stderr=False) | |
|
59 | ||
|
60 | out = 'executed {}'.format(' '.join(cmd)) | |
|
61 | output.append(out) | |
|
60 | 62 | |
|
61 | out = 'executed git gc --aggressive' | |
|
63 | out = '' | |
|
62 | 64 | if stderr: |
|
63 | out = ''.join(stderr.splitlines()) | |
|
65 | out += ''.join(stderr.splitlines()) | |
|
64 | 66 | |
|
65 |
|
|
|
66 | out = ''.join(stdout.splitlines()) | |
|
67 | if stdout: | |
|
68 | out += ''.join(stdout.splitlines()) | |
|
67 | 69 | |
|
70 | if out: | |
|
68 | 71 | output.append(out) |
|
69 | 72 | |
|
70 | objects = self._count_objects(instance) | |
|
71 | log.debug('GIT objects:%s', objects) | |
|
72 | output.append(objects) | |
|
73 | objects_after = self._count_objects(instance) | |
|
74 | log.debug('GIT objects:%s', objects_after) | |
|
75 | output.append('objects before :' + objects_before) | |
|
76 | output.append('objects after :' + objects_after) | |
|
77 | ||
|
78 | return '\n'.join(output) | |
|
79 | ||
|
80 | ||
|
81 | class GitFSCK(MaintenanceTask): | |
|
82 | human_name = 'GIT FSCK' | |
|
83 | ||
|
84 | def run(self): | |
|
85 | output = [] | |
|
86 | instance = self.db_repo.scm_instance() | |
|
87 | ||
|
88 | cmd = ['fsck', '--full'] | |
|
89 | stdout, stderr = instance.run_git_command(cmd, fail_on_stderr=False) | |
|
90 | ||
|
91 | out = 'executed {}'.format(' '.join(cmd)) | |
|
92 | output.append(out) | |
|
93 | ||
|
94 | out = '' | |
|
95 | if stderr: | |
|
96 | out += ''.join(stderr.splitlines()) | |
|
97 | ||
|
98 | if stdout: | |
|
99 | out += ''.join(stdout.splitlines()) | |
|
100 | ||
|
101 | if out: | |
|
102 | output.append(out) | |
|
103 | ||
|
104 | return '\n'.join(output) | |
|
105 | ||
|
106 | ||
|
107 | class GitRepack(MaintenanceTask): | |
|
108 | human_name = 'GIT Repack' | |
|
109 | ||
|
110 | def run(self): | |
|
111 | output = [] | |
|
112 | instance = self.db_repo.scm_instance() | |
|
113 | cmd = ['repack', '-a', '-d', | |
|
114 | '--window-memory', '10m', '--max-pack-size', '100m'] | |
|
115 | stdout, stderr = instance.run_git_command(cmd, fail_on_stderr=False) | |
|
116 | ||
|
117 | out = 'executed {}'.format(' '.join(cmd)) | |
|
118 | output.append(out) | |
|
119 | out = '' | |
|
120 | ||
|
121 | if stderr: | |
|
122 | out += ''.join(stderr.splitlines()) | |
|
123 | ||
|
124 | if stdout: | |
|
125 | out += ''.join(stdout.splitlines()) | |
|
126 | ||
|
127 | if out: | |
|
128 | output.append(out) | |
|
73 | 129 | |
|
74 | 130 | return '\n'.join(output) |
|
75 | 131 | |
|
76 | 132 | |
|
77 | 133 | class HGVerify(MaintenanceTask): |
|
78 | 134 | human_name = 'HG Verify repo' |
|
79 | 135 | |
|
80 | 136 | def run(self): |
|
81 | 137 | instance = self.db_repo.scm_instance() |
|
82 | 138 | res = instance.verify() |
|
83 | 139 | return res |
|
84 | 140 | |
|
85 | 141 | |
|
86 | 142 | class SVNVerify(MaintenanceTask): |
|
87 | 143 | human_name = 'SVN Verify repo' |
|
88 | 144 | |
|
89 | 145 | def run(self): |
|
90 | 146 | instance = self.db_repo.scm_instance() |
|
91 | 147 | res = instance.verify() |
|
92 | 148 | return res |
|
93 | 149 | |
|
94 | 150 | |
|
95 | 151 | class RepoMaintenance(object): |
|
96 | 152 | """ |
|
97 | 153 | Performs maintenance of repository based on it's type |
|
98 | 154 | """ |
|
99 | 155 | tasks = { |
|
100 | 156 | 'hg': [HGVerify], |
|
101 | 'git': [GitGC], | |
|
157 | 'git': [GitFSCK, GitGC, GitRepack], | |
|
102 | 158 | 'svn': [SVNVerify], |
|
103 | 159 | } |
|
104 | 160 | |
|
105 | 161 | def get_tasks_for_repo(self, db_repo): |
|
106 | 162 | """ |
|
107 | 163 | fetches human names of tasks pending for execution for given type of repo |
|
108 | 164 | """ |
|
109 | 165 | tasks = [] |
|
110 | 166 | for task in self.tasks[db_repo.repo_type]: |
|
111 | 167 | tasks.append(task.human_name) |
|
112 | 168 | return tasks |
|
113 | 169 | |
|
114 | 170 | def execute(self, db_repo): |
|
115 | 171 | executed_tasks = [] |
|
116 | 172 | for task in self.tasks[db_repo.repo_type]: |
|
117 |
|
|
|
173 | output = task.human_name + ':\n' + task(db_repo).run() + '\n--\n' | |
|
174 | executed_tasks.append(output) | |
|
118 | 175 | return executed_tasks |
General Comments 0
You need to be logged in to leave comments.
Login now