Package backend :: Package server :: Package action_extra_data :: Module configfiles
[hide private]
[frames] | no frames]

Source Code for Module backend.server.action_extra_data.configfiles

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # config file-related error handling functions 
 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  # the "exposed" functions 
 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 # First, unmark any file as being failed 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 # We don't do execute_bulk here, since we want to know if each update has 58 # actually touched a row 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
101 -def mtime_upload(server_id, action_id, data={}):
102 # at this point in time, no rhnActionConfigFileName entries exist, because 103 # we didn't know them at schedule time... go ahead and create them now, and then 104 # just use the main upload to handle the updating of the state... 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 # if there are already rhnActionConfigFileName entries for this sid+aid, 114 # it's most likely a rescheduled action, and we'll need to blow away the old 115 # entries (they might not be valid any longer) 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={}):
137 log_debug(3) 138 139 action_status = rhnFlags.get('action_status') 140 server_kickstart.update_kickstart_session(server_id, 141 action_id, action_status, kickstart_state='complete', 142 next_action_type=None) 143 return
144 145
146 -def diff(server_id, action_id, data={}):
147 log_debug(3) 148 if not data: 149 # Nothing to do here 150 return 151 status = rhnFlags.get('action_status') 152 if status == 2: 153 # Completed 154 _reset_diff_errors(server_id, action_id) 155 missing_files = data.get('missing_files') or [] 156 _mark_missing_diff_files(server_id, action_id, missing_files) 157 diffs = data.get('diffs') or {} 158 _process_diffs(server_id, action_id, diffs)
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
170 -def _reset_diff_errors(server_id, action_id):
171 h = rhnSQL.prepare(_query_reset_diff_errors) 172 h.execute(server_id=server_id, action_id=action_id)
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
193 -def _mark_missing_diff_files(server_id, action_id, missing_files):
194 if not missing_files: 195 # Nothing to do 196 log_debug(4, "No missing files reported by client") 197 return 198 # First, fetch all of the files scheduled 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 # This shouldn't really happen 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
236 -def _process_diffs(server_id, action_id, diffs):
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
259 -def _lookup_action_revision_id(server_id, action_id, path):
260 h = rhnSQL.prepare(_query_lookup_action_revision_id) 261 h.execute(server_id=server_id, action_id=action_id, path=path) 262 row = h.fetchone_dict() 263 if not row: 264 return None 265 return row['id']
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
274 -def _add_result(action_config_revision_id, diff):
275 276 log_debug(4, action_config_revision_id, diff) 277 278 if diff: 279 blob_map = {'result': 'result'} 280 diff = to_string(diff) 281 else: 282 blob_map = None 283 diff = None 284 285 h = rhnSQL.prepare(_query_add_result_diff, blob_map=blob_map) 286 h.execute(action_config_revision_id=action_config_revision_id, 287 result=diff)
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
301 -def _disable_old_diffs(server_id):
302 h = rhnSQL.prepare(_query_lookup_old_diffs) 303 h.execute(server_id=server_id) 304 old_acr_ids = [x['id'] for x in h.fetchall_dict() or []] 305 if not old_acr_ids: 306 # Nothing to do here 307 return 308 309 h = rhnSQL.prepare(_query_delete_old_diffs) 310 h.execute_bulk({'action_config_revision_id': old_acr_ids})
311