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

Source Code for Module backend.server.action.virt

  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   
 16  import sys 
 17  from spacewalk.common.rhnLog import log_debug 
 18  from spacewalk.server import rhnSQL 
 19  from spacewalk.server.rhnLib import InvalidAction 
 20  from spacewalk.common.usix import raise_with_tb 
 21   
 22  __rhnexport__ = ['refresh', 
 23                   'shutdown', 
 24                   'reboot', 
 25                   'resume', 
 26                   'start', 
 27                   'schedulePoller', 
 28                   'suspend', 
 29                   'destroy', 
 30                   'setMemory', 
 31                   'setVCPUs' 
 32                   ] 
 33   
 34  ########################################################################### 
 35  # SQL Queries for each virtualization action type. 
 36  ########################################################################### 
 37  _query_refresh = rhnSQL.Statement(""" 
 38      select  avf.action_id, 
 39      from    rhnActionVirtRefresh 
 40      where   avf.action_id = :action_id 
 41  """) 
 42   
 43  _query_shutdown = rhnSQL.Statement(""" 
 44      select  avs.action_id, 
 45              avs.uuid 
 46      from    rhnActionVirtShutdown avs 
 47      where   avs.action_id = :action_id 
 48  """) 
 49   
 50  _query_suspend = rhnSQL.Statement(""" 
 51      select  avs.action_id, 
 52              avs.uuid 
 53      from    rhnActionVirtSuspend avs 
 54      where   avs.action_id = :action_id 
 55  """) 
 56   
 57  _query_resume = rhnSQL.Statement(""" 
 58      select  avr.action_id, 
 59              avr.uuid 
 60      from    rhnActionVirtResume avr 
 61      where   avr.action_id = :action_id 
 62  """) 
 63   
 64  _query_reboot = rhnSQL.Statement(""" 
 65      select  avr.action_id, 
 66              avr.uuid 
 67      from    rhnActionVirtReboot avr 
 68      where   avr.action_id = :action_id 
 69   
 70  """) 
 71   
 72  _query_destroy = rhnSQL.Statement(""" 
 73      select  avd.action_id, 
 74              avd.uuid 
 75      from    rhnActionVirtDestroy avd 
 76      where   avd.action_id = :action_id 
 77  """) 
 78   
 79  _query_start = rhnSQL.Statement(""" 
 80      select  avs.action_id, 
 81              avs.uuid 
 82      from    rhnActionVirtStart avs 
 83      where   avs.action_id = :action_id 
 84  """) 
 85   
 86  _query_setMemory = rhnSQL.Statement(""" 
 87      select  asm.action_id, 
 88              asm.uuid, 
 89              asm.memory 
 90      from    rhnActionVirtSetMemory asm 
 91      where   asm.action_id = :action_id 
 92  """) 
 93   
 94  _query_getVCPUs = rhnSQL.Statement(""" 
 95      select  av.action_id, 
 96              av.uuid, 
 97              av.vcpu 
 98      from    rhnActionVirtVCPU av 
 99      where   av.action_id = :action_id 
100  """) 
101   
102  _query_schedulePoller = rhnSQL.Statement(""" 
103      select  asp.action_id, 
104              asp.minute, 
105              asp.hour, 
106              asp.dom, 
107              asp.month, 
108              asp.dow 
109      from    rhnActionVirtSchedulePoller asp 
110      where   asp.action_id = :action_id 
111  """) 
112   
113  ########################################################################## 
114  # Functions that return the correct parameters that the actions are 
115  # called with. They all take in the server_id and action_id as params. 
116  ########################################################################## 
117   
118   
119 -class NoUUIDException(Exception):
120
121 - def __init__(self):
122 Exception.__init__(self)
123 124
125 -class NoRowFoundException(Exception):
126
127 - def __init__(self):
128 Exception.__init__(self)
129 130
131 -def _get_uuid(query_str, action_id):
132 log_debug(3) 133 134 prepared_query = rhnSQL.prepare(query_str) 135 prepared_query.execute(action_id=action_id) 136 row = prepared_query.fetchone_dict() 137 138 if not row: 139 raise NoRowFoundException() 140 141 if 'uuid' not in row: 142 raise NoUUIDException() 143 144 uuid = row['uuid'] 145 return uuid
146 147 # Returns an empty tuple, since the virt.refresh action has no params. 148 149
150 -def refresh(server_id, action_id, dry_run=0):
151 log_debug(3, dry_run) 152 153 prepared_query = rhnSQL.prepare(_query_refresh) 154 prepared_query.execute(action_id=action_id) 155 row = prepared_query.fetchone_dict() 156 157 if not row: 158 raise NoRowFoundException() 159 160 # Sanity check. If this doesn't pass then something is definitely screwed up. 161 if not row['action_id']: 162 raise InvalidAction("Refresh action is missing an action_id.") 163 164 return ()
165 166 # Returns a uuid 167 168
169 -def action(action_name, query, server_id, action_id, dry_run=0):
170 log_debug(3, action_name, dry_run) 171 try: 172 uuid = _get_uuid(query, action_id) 173 except NoRowFoundException: 174 raise_with_tb(InvalidAction("No %s actions found." % action_name.lower()), sys.exc_info()[2]) 175 except NoUUIDException: 176 raise_with_tb(InvalidAction("%s action %s has no uuid associated with it." % 177 (action_name, str(action_id))), sys.exc_info()[2]) 178 return (uuid,)
179 180
181 -def start(server_id, action_id, dry_run=0):
182 return action("Start", _query_start, server_id, action_id, dry_run=0)
183 184
185 -def shutdown(server_id, action_id, dry_run=0):
186 return action("Shutdown", _query_shutdown, server_id, action_id, dry_run=0)
187 188
189 -def suspend(server_id, action_id, dry_run=0):
190 return action("Suspend", _query_suspend, server_id, action_id, dry_run=0)
191 192
193 -def resume(server_id, action_id, dry_run=0):
194 return action("Resume", _query_resume, server_id, action_id, dry_run=0)
195 196
197 -def reboot(server_id, action_id, dry_run=0):
198 return action("Reboot", _query_reboot, server_id, action_id, dry_run=0)
199 200
201 -def destroy(server_id, action_id, dry_run=0):
202 return action("Destroy", _query_destroy, server_id, action_id, dry_run=0)
203 204 # Returns a uuid and the amount of memory to allocate to the domain. 205 206
207 -def setMemory(server_id, action_id, dry_run=0):
208 log_debug(3, dry_run) 209 210 prepared_query = rhnSQL.prepare(_query_setMemory) 211 prepared_query.execute(action_id=action_id) 212 row = prepared_query.fetchone_dict() 213 214 if not row: 215 raise InvalidAction("No setMemory actions found.") 216 217 if 'uuid' not in row: 218 raise InvalidAction("Set Memory action %s has no uuid." % str(action_id)) 219 220 if 'memory' not in row: 221 raise InvalidAction("setMemory action %s has no memory set." % str(action_id)) 222 223 uuid = row['uuid'] 224 memory = row['memory'] 225 226 return (uuid, memory)
227 228 # Returns a uuid and the amount of VCPUs to allocate to the domain. 229 230
231 -def setVCPUs(server_id, action_id, dry_run=0):
232 log_debug(3, dry_run) 233 234 prepared_query = rhnSQL.prepare(_query_getVCPUs) 235 prepared_query.execute(action_id=action_id) 236 row = prepared_query.fetchone_dict() 237 238 if not row: 239 raise InvalidAction("No VCPU actions found.") 240 241 return row['uuid'], row['vcpu']
242 243 244 # Returns the minute, hour, dom, month, and dow to call schedulePoller with.
245 -def schedulePoller(server_id, action_id, dry_run=0):
246 log_debug(3, dry_run) 247 248 prepared_query = rhnSQL.prepare(_query_schedulePoller) 249 prepared_query.execute(action_id=action_id) 250 row = prepared_query.fetchone_dict() 251 252 if not row: 253 raise InvalidAction("No schedulePoller actions found.") 254 255 if 'minute' not in row: 256 raise InvalidAction("schedulePoller action %s has no minute associated with it." % str(action_id)) 257 258 if 'hour' not in row: 259 raise InvalidAction("schedulePoller action %s has no hour associated with it." % str(action_id)) 260 261 if 'dom' not in row: 262 raise InvalidAction("schedulePoller action %s has no day of the month associated with it." % str(action_id)) 263 264 if 'month' not in row: 265 raise InvalidAction("schedulePoller action %s has no month associated with it." % str(action_id)) 266 267 if 'dow' not in row: 268 raise InvalidAction("schedulePoller action %s has no day of the week associated with it." % str(action_id)) 269 270 return (row['minute'], row['hour'], row['dom'], row['month'], row['dow'])
271