1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
#!/usr/bin/env python3.2
#
# Copyright (c) Net24 Limited, Christchurch, New Zealand 2011-2012
# and Voyager Internet Ltd, New Zealand, 2012-2013
#
# This file is part of py-magcode-core.
#
# Py-magcode-core is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Py-magcode-core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with py-magcode-core. If not, see <http://www.gnu.org/licenses/>.
#
import os
import sys
import json
import threading
import copy
from subprocess import *
from sqlalchemy import text
from sqlalchemy.exc import DBAPIError
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm.session import make_transient
from magcode.core.globals_ import settings
from magcode.core.logging import *
from magcode.core.database import *
from magcode.core.database.state_machine import StateMachine
from magcode.core.database.state_machine import SMEvent
from magcode.core.database.state_machine import StateMachineError
from magcode.core.database.state_machine import StateMachineFatalError
from magcode.core.database.state_machine import smregister
from magcode.core.database.event import Event
from magcode.core.database.event import eventregister
# some state static constant
PSTATE_START = 'START'
PSTATE_RUN = 'RUN'
PSTATE_SUCCESS = 'SUCCESS'
PSTATE_FAILURE = 'FAILURE'
process_sm_states = (PSTATE_START, PSTATE_RUN, PSTATE_SUCCESS, PSTATE_FAILURE)
_process_list = []
# Error Exceptions
class ProcessError(StateMachineError):
"""
Base exception for Process State Machine.
"""
pass
class ProcessFatalError(StateMachineFatalError):
"""
Base exception for Process State Machine Fatal errors.
"""
pass
class ProcessExecError(ProcessFatalError):
"""
Process could not be executed. Typically file not found, or
Invalid arguments to POpen
"""
def __init__(self, *args, **kwargs):
if (not len(args) and not len(kwargs)):
ProcessFatalError.__init__(self, "Execution failure")
else:
ProcessFatalError.__init__(self, *args, **kwargs)
class ProcessExitError(ProcessFatalError):
"""
Process exited with an error. Typically wrong arguments or something
"""
def __init__(self, *args, **kwargs):
if (not len(args) and not len(kwargs)):
ProcessFatalError.__init__(self, "Process exit() error")
else:
ProcessFatalError.__init__(self, *args, **kwargs)
class ProcessSignalError(ProcessFatalError):
"""
Process killed by signal.
"""
def __init__(self, *args, **kwargs):
if (not len(args) and not len(kwargs)):
ProcessFatalError.__init__(self, "Process killed by signal")
else:
ProcessFatalError.__init__(self, *args, **kwargs)
class ProcessTempFailError(ProcessError):
"""
Temporary failure - resource termporarily not available
"""
def __init__(self, *args, **kwargs):
if (not len(args) and not len(kwargs)):
ProcessError.__init__(self, "Temporary execution failure")
else:
ProcessError.__init__(self, *args, **kwargs)
class ProcessNoArgumentError(ProcessFatalError):
"""
No arguments or command given for a process
"""
def __init__(self, *args, **kwargs):
if (not len(args) and not len(kwargs)):
ProcessFatalError.__init__(self,
"No Arguments of command given for a process")
else:
ProcessFatalError.__init__(self, *args, **kwargs)
class ProcessNotAnEventError(ProcessFatalError):
"""
Event sent on success of process is not an event at all
"""
def __init__(self, pseudo_event=None, *args, **kwargs):
if (pseudo_event):
self.pseudo_event = pseudo_event
ProcessFatalError.__init__(self, "%s is not one of %s"
% (str(pseudo_event), sql_data['event_type_list']))
elif (not len(args) and not len(kwargs)):
ProcessFatalError.__init__(self,
"Object or string submitted is not representative of an Event()")
else:
ProcessFatalError.__init__(self, *args, **kwargs)
# State Machine Events
@eventregister
class ProcessSMExecEvent(SMEvent):
"""
Start a process running
"""
pass
@eventregister
class ProcessSMResetEvent(SMEvent):
"""
Event issued to clean up and reset process SM to START
"""
pass
# State Machine class
@smregister
class ProcessSM(StateMachine):
"""
Process State machine
Runs a process. State kept in SQL database. Uses the subprocess module,
running from a thread.
Event objects are ProcessSMExecEvent, ProcessSMSuccessEvent, and
ProcessSMFailureEvent
"""
# SQL Alchemy database table name
_table = 'sm_process'
# Events asscoiated with this state machine
_sm_events = (ProcessSMExecEvent, ProcessSMResetEvent)
# Event action methods
def _exec(self, event):
"""
Spawn a Thread that watches event via subprocess module
"""
db_session = event.db_session
self.state = PSTATE_RUN
self.start = db_clock_time(db_session)
db_session.flush()
log_str = "ProcessSM.run_thread() %s(%s)" \
% (self.name, self.exec_path)
error_str = None
try:
# Do the Dirty Work eh eh ahh!
argv = json.loads(self.argv)
process = Popen(args=argv, executable=self.exec_path,
cwd=self.cwd, env=self.env,
stdin=PIPE, stdout=PIPE, stderr=PIPE)
# On Posix, processes consume bytes (which could be utf-8)
stdin = bytes(self.stdin, 'utf8')
(stdout, stderr) = process.communicate(stdin)
# Convert returned text to Python unicode
self.stdout = stdout.decode()
self.stderr = stderr.decode()
except (IOError, OSError) as exc:
if (exc.filename):
error_str = "%s %s - %s" % (log_str, exc.filename, exc.strerror)
else:
error_str = "%s - %s" % (log_str, exc.strerror)
except ValueError as exc:
error_str = "%s - %s" % (log_str, str(exc))
if (error_str):
# Typically can't execute process
self.state = PSTATE_FAILURE
self.result_code = RCODE_FATAL
raise ProcessExecError(error_str)
if (process.returncode == os.EX_TEMPFAIL):
# Process terminated with a temporary failure
self.state = PSTATE_START
self.exit_code = process.returncode
self.result_code = RCODE_ERROR
self.finish = db_clock_time(db_session)
raise ProcessTempFailError(log_str + " - temporary failure.")
elif (process.returncode):
# Process termainated with > 0 exit code
self.state = PSTATE_FAILURE
self.result_code = RCODE_FATAL
self.exit_code = process.returncode
self.finish = db_clock_time(db_session)
raise ProcessExitError(log_str + " - failed." )
elif (process.returncode < 0):
# Process terminated by signal -N
self.state = PSTATE_FAILURE
self.result_code = RCODE_ERROR
self.exit_code = None
self.finish = db_clock_time(db_session)
raise ProcessSignalError(log_str + " - killed by signal %s."
% process.returncode * -1)
msg = log_str + " - ran successfully."
self.state = PSTATE_SUCCESS
self.exit_code = process.returncode
self.result_code = RCODE_OK
self.finish = db_clock_time(db_session)
# This can blow up in a real spectacular fashion!
# Need to register Events to a list of classes?
if (self.success_event):
if (not self.success_event in sql_data['event_type_list']):
raise ProcessNotAnEventError(pseudo_event=self.success_event)
elif (self.success_event_kwargs):
se_kwargs = json.loads(self.success_event_kwargs)
s_event = eval ("%s(**se_kwargs)" % self.success_event)
else:
s_event = eval ("%s()" % self.success_event)
db_session.add(s_event)
return (RCODE_OK, msg)
def _reset(self, event):
self.state = PSTATE_START
self.exit_code = None
self.result_code = None
self.stdout = None
self.stderr = None
self.start = None
self.finish = None
return (RCODE_OK, "Process %s(%s) - cleared output."
% (self.name, self.exec_path))
_sm_table = {
PSTATE_START: {
ProcessSMExecEvent: _exec,
},
# PSTATE_RUN occurs within an event
# thus empty row here.
PSTATE_RUN: {},
PSTATE_FAILURE: {
ProcessSMResetEvent: _reset
},
PSTATE_SUCCESS: {
ProcessSMResetEvent: _reset
},
}
def __init__(self, exec_path=None, argv=None, stdin=None,
name=None, success_event=None, success_event_kwargs=None):
self.state = PSTATE_START
if (not exec_path and not argv):
raise ProcessNoArgumentError
if (exec_path):
self.exec_path = exec_path
elif (argv):
self.exec_path = argv[0]
else:
raise ProcessNoArgumentError
if (name):
self.name = name
else:
self.name = os.path.basename(self.exec_path)
if (argv):
self.argv = json.dumps(argv)
self.stdin = stdin
if (success_event):
# This is a mess, but people will pass in all types of stuff,
# and we need text name of class for DB
if (type(success_event) is str):
if (success_event not in sql_data['event_type_list']):
raise ProcessNotAnEventError(psuedo_event=success_event)
self.success_event = success_event
elif (type(success_event) is type):
if (not issubclass(success_event, Event)):
raise ProcessNotAnEventError(psuedo_event=success_event)
self.success_event = success_event.__name__
elif (isinstance(success_event, Event)):
self.success_event = success_event.__class__.__name__
else:
raise ProcessNotAnEventError(psuedo_event=success_event)
else:
self.success_event = None
if (success_event_kwargs):
self.success_event_kwargs = json.dumps(success_event_kwargs)
def new_process(db_session, commit=False, time=None, delay=None, *args, **kwargs):
"""
Create a new process to be run at time, or after a delay
Time is in Unix Epoch, and delay in seconds. If time or delay are
not specified, it executes now.
time is a datetime.datetime, delay is datetime.timeinterval
"""
process_sm = ProcessSM(*args, **kwargs)
db_session.add(process_sm)
db_session.flush()
db_session.refresh(process_sm)
exec_event = ProcessSMExecEvent(process_sm.id_)
if (not time):
time = db_time(db_session)
exec_event.scheduled = time
if (delay):
exec_event.scheduled += delay
db_session.add(exec_event)
process_id = process_sm.id_
if commit:
db_session.commit()
else:
db_session.flush()
return process_id
def get_process_results(db_session, sm_id):
"""
Get the results of a process. Returns sm_object from the database,
given an id.
"""
try:
sm_obj = event.db_session.query(ProcessSM)\
.filter(text("id = '%s'" % sm_id)).one()
except NoResultFound:
sm_obj = None
return sm_obj
def reset_process(db_session, sm_id, commit=False):
"""
Reset the given process
"""
sm_obj = get_process_results(db_session, sm_id)
if (not sm_obj):
return
reset_event = ProcessSMResetEvent(sm_obj.id_)
db_session.add(reset_event)
if (commit):
db_session.commit()
else:
db.session.flush()
|