1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 from spacewalk.common import rhnFlags
19 from spacewalk.common.rhnLog import log_debug, log_error
20 from spacewalk.common.stringutils import to_string
21 from spacewalk.server import rhnSQL
22 from spacewalk.server.rhnServer import server_kickstart
23
24
25 __rhnexport__ = ['upload', 'deploy', 'verify', 'diff', 'mtime_upload']
26
27 _query_reset_upload_files = rhnSQL.Statement("""
28 update rhnActionConfigFileName
29 set failure_id = NULL
30 where server_id = :server_id
31 and action_id = :action_id
32 """)
33 _query_mark_upload_files = rhnSQL.Statement("""
34 update rhnActionConfigFileName
35 set failure_id = :failure_id
36 where server_id = :server_id
37 and action_id = :action_id
38 and config_file_name_id = lookup_config_filename(:path)
39 """)
40
41
42 -def upload(server_id, action_id, data={}):
43 log_debug(3)
44
45
46 h = rhnSQL.prepare(_query_reset_upload_files)
47 h.execute(server_id=server_id, action_id=action_id)
48
49 if not data:
50 log_debug(4, "No data sent by client")
51 return
52
53 log_debug(6, 'data', data)
54
55 failure_table = rhnSQL.Table('rhnConfigFileFailure', 'label')
56 h = rhnSQL.prepare(_query_mark_upload_files)
57
58
59
60 reason_map = {'missing_files': 'missing',
61 'files_too_large': 'too_big',
62 'quota_failed': 'insufficient_quota',
63 }
64
65 for reason in reason_map.keys():
66 log_debug(6, 'reason', reason)
67 failed_files = data.get(reason)
68 log_debug(6, 'failed_files', failed_files)
69 if not failed_files:
70 continue
71
72 failure_id = failure_table[reason_map[reason]]['id']
73 log_debug(6, 'failure_id', failure_id)
74
75 for path in failed_files:
76 log_debug(6, 'path', path)
77 ret = h.execute(server_id=server_id, action_id=action_id,
78 failure_id=failure_id, path=path)
79 if not ret:
80 log_error("Could not find file %s for server %s, action %s" %
81 (path, server_id, action_id))
82
83
84 _query_any_action_config_filenames = rhnSQL.Statement("""
85 select config_file_name_id
86 from rhnActionConfigFileName
87 where server_id = :server_id
88 and action_id = :action_id
89 """)
90 _query_clear_action_config_filenames = rhnSQL.Statement("""
91 delete from rhnActionConfigFileName
92 where server_id = :server_id
93 and action_id = :action_id
94 """)
95 _query_create_action_config_filename = rhnSQL.Statement("""
96 insert into rhnActionConfigFileName (action_id, config_file_name_id, server_id)
97 values (:action_id, lookup_config_filename(:path), :server_id)
98 """)
99
100
102
103
104
105 paths = data.get('attempted_paths') or []
106
107 if not paths:
108 log_debug(6, "no matched files")
109 return
110
111 log_debug(6, 'attempted paths', paths)
112
113
114
115
116 h = rhnSQL.prepare(_query_any_action_config_filenames)
117 h.execute(server_id=server_id, action_id=action_id)
118 already_filenames = h.fetchone_dict() or []
119
120 if already_filenames:
121 h = rhnSQL.prepare(_query_clear_action_config_filenames)
122 h.execute(server_id=server_id, action_id=action_id)
123
124 num_paths = len(paths)
125
126 h = rhnSQL.prepare(_query_create_action_config_filename)
127 h.execute_bulk({
128 'action_id': [action_id] * num_paths,
129 'server_id': [server_id] * num_paths,
130 'path': paths,
131 })
132
133 upload(server_id, action_id, data)
134
135
136 -def deploy(server_id, action_id, data={}):
144
145
146 -def diff(server_id, action_id, data={}):
159
160 verify = diff
161
162 _query_reset_diff_errors = rhnSQL.Statement("""
163 update rhnActionConfigRevision
164 set failure_id = NULL
165 where server_id = :server_id
166 and action_id = :action_id
167 """)
168
169
173
174 _query_lookup_diff_files = rhnSQL.Statement("""
175 select acr.id, cfn.path
176 from rhnConfigFileName cfn,
177 rhnConfigFile cf,
178 rhnConfigRevision cr,
179 rhnActionConfigRevision acr
180 where acr.server_id = :server_id
181 and acr.action_id = :action_id
182 and acr.config_revision_id = cr.id
183 and cr.config_file_id = cf.id
184 and cf.config_file_name_id = cfn.id
185 """)
186 _query_mark_failed_diff_files = rhnSQL.Statement("""
187 update rhnActionConfigRevision
188 set failure_id = :failure_id
189 where id = :action_config_revision_id
190 """)
191
192
194 if not missing_files:
195
196 log_debug(4, "No missing files reported by client")
197 return
198
199 h = rhnSQL.prepare(_query_lookup_diff_files)
200 h.execute(server_id=server_id, action_id=action_id)
201 hash = {}
202 while 1:
203 row = h.fetchone_dict()
204 if not row:
205 break
206 action_config_revision_id, path = row['id'], row['path']
207 if path in hash:
208
209 log_error("Duplicate path for diff "
210 "(scheduler did not resolve config files? %s, %s" %
211 (hash[path], action_config_revision_id))
212 else:
213 hash[path] = action_config_revision_id
214
215 ids = []
216 for path in missing_files:
217 if path not in hash:
218 log_error("Client reports missing a file "
219 "that was not scheduled for diff? %s" % path)
220 continue
221 ids.append(hash[path])
222 if not ids:
223 log_debug(4, "No missing files found")
224 return
225 failure_table = rhnSQL.Table('rhnConfigFileFailure', 'label')
226 failure_id = failure_table['missing']['id']
227 failure_ids = [failure_id] * len(ids)
228
229 h = rhnSQL.prepare(_query_mark_failed_diff_files)
230 h.execute_bulk({
231 'action_config_revision_id': ids,
232 'failure_id': failure_ids,
233 })
234
235
237 _disable_old_diffs(server_id)
238 for file_path, diff in diffs.items():
239 action_config_revision_id = _lookup_action_revision_id(server_id,
240 action_id, file_path)
241 if action_config_revision_id is None:
242 log_error(
243 "Missing config file for action id %s, server id %s, path %s"
244 % (server_id, action_id, file_path))
245 continue
246 _add_result(action_config_revision_id, diff)
247
248 _query_lookup_action_revision_id = rhnSQL.Statement("""
249 select acr.id
250 from rhnConfigRevision cr, rhnConfigFile cf, rhnActionConfigRevision acr
251 where acr.action_id = :action_id
252 and acr.server_id = :server_id
253 and acr.config_revision_id = cr.id
254 and cr.config_file_id = cf.id
255 and cf.config_file_name_id = lookup_config_filename(:path)
256 """)
257
258
266
267 _query_add_result_diff = rhnSQL.Statement("""
268 insert into rhnActionConfigRevisionResult
269 (action_config_revision_id, result)
270 values (:action_config_revision_id, :result)
271 """)
272
273
288
289 _query_lookup_old_diffs = rhnSQL.Statement("""
290 select acr.id
291 from rhnActionConfigRevision acr
292 where acr.server_id = :server_id
293 """)
294
295 _query_delete_old_diffs = rhnSQL.Statement("""
296 delete from rhnActionConfigRevisionResult
297 where action_config_revision_id = :action_config_revision_id
298 """)
299
300
311