1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
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
117
118
120 h_child.execute(server_id=server_id, action_id=action_id)
121 a_ids = []
122
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
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
157