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

Source Code for Module backend.server.rhnAction

  1  # 
  2  # Copyright (c) 2008--2015 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  # 
 16   
 17  from spacewalk.common.rhnLog import log_debug 
 18  from spacewalk.server import rhnSQL 
 19   
 20   
21 -def schedule_action(action_type, action_name=None, delta_time=0, 22 scheduler=None, org_id=None, prerequisite=None):
23 action_id = rhnSQL.Sequence('rhn_event_id_seq').next() 24 25 at = rhnSQL.Table('rhnActionType', 'label') 26 if not at.has_key(action_type): 27 raise ValueError("Unknown action type %s" % action_type) 28 29 params = { 30 'action_id': action_id, 31 'org_id': org_id, 32 'action_type_id': at[action_type]['id'], 33 'action_name': action_name, 34 'delta': delta_time, 35 'scheduler': scheduler, 36 'prerequisite': prerequisite, 37 } 38 39 h = rhnSQL.prepare(""" 40 insert into rhnAction 41 (id, org_id, action_type, name, scheduler, earliest_action, prerequisite) 42 values (:action_id, :org_id, :action_type_id, :action_name, :scheduler, 43 current_timestamp + numtodsinterval(:delta, 'second'), :prerequisite) 44 """) 45 h.execute(**params) 46 47 return action_id
48 49
50 -def schedule_server_action(server_id, action_type, action_name=None, 51 delta_time=0, scheduler=None, org_id=None, prerequisite=None):
52 if not org_id: 53 h = rhnSQL.prepare("select org_id from rhnServer where id = :id") 54 h.execute(id=server_id) 55 row = h.fetchone_dict() 56 if not row: 57 raise ValueError("Invalid server id %s" % server_id) 58 org_id = row['org_id'] 59 60 action_id = schedule_action(action_type, 61 action_name, 62 delta_time=delta_time, 63 scheduler=scheduler, 64 org_id=org_id, 65 prerequisite=prerequisite, 66 ) 67 68 # Insert an action as Queued 69 h = rhnSQL.prepare(""" 70 insert into rhnServerAction (server_id, action_id, status) 71 values (:server_id, :action_id, 0) 72 """) 73 h.execute(server_id=server_id, action_id=action_id) 74 75 return action_id
76 77
78 -def update_server_action(server_id, action_id, status, result_code=None, 79 result_message=""):
80 log_debug(4, server_id, action_id, status, result_code, result_message) 81 h = rhnSQL.prepare(""" 82 update rhnServerAction 83 set status = :status, 84 result_code = :result_code, 85 result_msg = :result_message, 86 completion_time = current_timestamp 87 where action_id = :action_id 88 and server_id = :server_id 89 """) 90 h.execute(action_id=action_id, server_id=server_id, 91 status=status, result_code=result_code, 92 result_message=result_message[:1024])
93 94 95 _query_lookup_action_childs = rhnSQL.Statement(""" 96 select a.id 97 from rhnAction a 98 join rhnServerAction sa 99 on sa.action_id = a.id 100 where prerequisite = :action_id 101 and sa.server_id = :server_id 102 """) 103 104 _query_lookup_action = rhnSQL.Statement(""" 105 select sa.action_id, sa.status 106 from rhnServerAction sa 107 where sa.server_id = :server_id 108 and sa.action_id = :action_id 109 """) 110 111
112 -def invalidate_action(server_id, action_id):
113 log_debug(4, server_id, action_id) 114 h = rhnSQL.prepare(_query_lookup_action) 115 h_child = rhnSQL.prepare(_query_lookup_action_childs) 116 return _invalidate_action_recursive(server_id, action_id, h, h_child)
117 118
119 -def _invalidate_action_recursive(server_id, action_id, h, h_child):
120 h_child.execute(server_id=server_id, action_id=action_id) 121 a_ids = [] 122 # invalidate childs first 123 while 1: 124 row = h_child.fetchone_dict() 125 if not row: 126 break 127 child_ids = _invalidate_action_recursive(server_id, row['id'], h, h_child) 128 a_ids += child_ids 129 h.execute(server_id=server_id, action_id=action_id) 130 s_row = h.fetchone_dict() 131 if s_row and s_row['status'] != 3: 132 # not already failed 133 c_action_id = s_row['action_id'] 134 a_ids.append(c_action_id) 135 update_server_action(server_id=server_id, action_id=c_action_id, 136 status=3, result_code=-100, result_message="Prerequisite failed") 137 138 return a_ids
139 140 _query_schedule_server_packages_update_by_arch = rhnSQL.Statement(""" 141 insert into rhnActionPackage (id, action_id, name_id, package_arch_id, \ 142 parameter) 143 values (sequence_nextval('rhn_act_p_id_seq'), :action_id, :name_id, :arch_id, \ 144 'upgrade') 145 """) 146 147
148 -def schedule_server_packages_update_by_arch(server_id, package_arch_ids, org_id=None, prerequisite=None, action_name="Package update"):
149 action_id = schedule_server_action(server_id, 150 action_type='packages.update', action_name=action_name, 151 org_id=org_id, prerequisite=prerequisite) 152 h = rhnSQL.prepare(_query_schedule_server_packages_update_by_arch) 153 154 for name_id, arch_id in package_arch_ids: 155 h.execute(action_id=action_id, name_id=name_id, arch_id=arch_id) 156 return action_id
157