def get_cron_tasks(cron_task_id=None, user='******'): cron_list = [] try: db_path, err = common.get_db_path() if err: raise Exception(err) if cron_task_id is None: query = 'select * from cron_tasks' else: query = 'select * from cron_tasks where cron_task_id=%s' % cron_task_id cron_db_entries, err = db.read_multiple_rows(db_path, query) if err: raise Exception(err) #print cron_db_entries if cron_db_entries: cron = crontab.CronTab(user) for cron_db_entry in cron_db_entries: cron_dict = {} cron_dict['description'] = cron_db_entry['description'] cron_dict['cron_task_id'] = cron_db_entry['cron_task_id'] jobs = cron.find_comment(str(cron_db_entry['cron_task_id'])) if jobs: for job in jobs: cron_dict['schedule_description'] = job.description( use_24hour_time_format=True) cron_dict['job'] = job break cron_list.append(cron_dict) except Exception, e: return None, 'Error listing all cron entries : %s' % str(e)
def get_remote_replications(remote_replication_id=None): replications = [] try: db_path, err = common.get_db_path() if err: raise Exception(err) if remote_replication_id is not None: cmd = "select * from remote_replications where remote_replication_id='%s'" % remote_replication_id else: cmd = "select * from remote_replications" replications, err = db.read_multiple_rows(db_path, cmd) if err: raise Exception(err) if replications is not None: #print 'replications - ', replications for replication in replications: cron_tasks, err = scheduler_utils.get_cron_tasks( replication['cron_task_id']) #print cron_tasks if err: raise Exception(err) if not cron_tasks: raise Exception('Specified replication schedule not found') replication['schedule_description'] = cron_tasks[0][ 'schedule_description'] replication['description'] = cron_tasks[0]['description'] except Exception, e: return None, 'Error retrieving remote replications : %s' % e
def load_shares_list(): l = [] try: db_path, err = common.get_db_path() if err: raise Exception(err) l, err = db.read_multiple_rows(db_path, 'select * from samba_shares') if err: raise Exception(err) except Exception, e: return None, 'Error loading CIFS shares list : %s' % str(e)
def load_valid_users_list(share_id): l = None try: db_path, err = common.get_db_path() if err: raise Exception(err) l, err = db.read_multiple_rows( db_path, 'select * from samba_valid_users where share_id = %s' % share_id) if err: raise Exception(err) except Exception, e: return None, 'Error loading valid users list : %s' % str(e)
def get_tasks_by_cron_task_id(cron_task_id): tasks = [] try: db_path, err = common.get_db_path() if err: raise Exception(err) query = 'select * from tasks where cron_task_id="%d"' % int( cron_task_id) #print query tasks, err = db.read_multiple_rows(db_path, query) if err: raise Exception(err) except Exception, e: return None, 'Error retrieving tasks by cron task id: %s' % e
def get_subtasks(task_id): ''' TODO - change name to get_subtasks - change db params ''' subtasks = None try: query = "select * from subtasks where task_id = '%d'" % task_id db_path, err = common.get_db_path() if err: raise Exception(err) subtasks, err = db.read_multiple_rows(db_path, query) if err: raise Exception(err) if not subtasks: raise Exception('No subtasks found for the specified task.') except Exception, e: return None, 'Error retrieving subtasks : %s' % e
def get_tasks(node=None): tasks = None try: db_path, err = common.get_db_path() if err: raise Exception(err) #start_time = int((datetime.datetime.now() - datetime.timedelta(minutes=minutes)).strftime("%s")) #end_time = int((datetime.datetime.now() + datetime.timedelta(minutes=minutes)).strftime("%s")) if not node: tasks_query = "select * from tasks order by initiate_time desc" else: tasks_query = "select * from scheduler_tasks order by initiate_time desc" #print tasks_query tasks, err = db.read_multiple_rows(db_path, tasks_query) if err: raise Exception(err) #print tasks except Exception, e: return None, 'Error retrieving tasks : %s' % e
def main(): try: lck, err = lock.get_lock('unicell_poll_for_alerts') if err: raise Exception(err) if not lck: raise Exception('Could not acquire lock. Exiting.') alert_list = [] now = int(time.time()) db_path, err = common.get_db_path() if err: raise Exception(err) tasks_query = "select * from tasks where last_run_time > '%d' and (status = 'error-retrying' or status = 'failed');" % ( now - 110) #print "\ntasks_query: ", tasks_query rows, err = db.read_multiple_rows(db_path, tasks_query) #print "\nrows: ", rows if err: raise Exception(err) if rows: for row in rows: msg = "%s: %s." % (row['status'], row['description']) alert_list.append(msg) #print "\nalert_list: ", alert_list if alert_list: alerts.raise_alert(alert_list) lock.release_lock('unicell_poll_for_alerts') except Exception, e: print "Error generating alerts : %s ! Exiting." % str(e) sys.exit(-1)
def get_remote_replications_with(source_dataset, destination_ip, destination_pool): replications = [] repls = [] try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = "select * from remote_replications where source_dataset='%s' and destination_ip='%s' and destination_pool='%s'" % ( source_dataset, destination_ip, destination_pool) replications, err = db.read_multiple_rows(db_path, cmd) if err: raise Exception(err) if replications is not None: #print 'replications - ', replications for replication in replications: repls, err = get_remote_replications( replication['remote_replication_id']) if err: raise Exception(err) except Exception, e: return None, 'Error retrieving remote replications : %s' % e
def process_tasks(node=socket.getfqdn()): try: db_path, err = common.get_db_path() if err: raise Exception(err) current_user = getpass.getuser() now = int(time.time()) tasks_query = "select * from tasks where node == '" + node + "' and (status == 'error-retrying' or status == 'queued') and (initiate_time <= '%d');" % ( now) print tasks_query tasks_to_process, err = db.read_multiple_rows(db_path, tasks_query) if err: raise Exception(err) print tasks_to_process if tasks_to_process is not None: for task in tasks_to_process: #print 'Processing task ', task['task_id'] if task['last_run_time']: seconds_since_last_run = (now - task['last_run_time']) #retry_interval is in minutes! if seconds_since_last_run < task['retry_interval'] * 60: continue #Mark the task as running cmd = "update tasks set status = 'running', last_run_time=%d where task_id = '%d'" % ( now, task['task_id']) status, err = db.execute_iud(db_path, [ [cmd], ], get_rowid=True) #print status, err if err: raise Exception(err) audit_str = "%s" % task['description'] audit.audit("task_start", audit_str, None, system_initiated=True) attempts = task['attempts'] run_as_user_name = task['run_as_user_name'] #Now process subtasks for the task subtasks_query = "select * from subtasks where task_id == '%d' and (status == 'error-retrying' or status == 'queued') order by subtask_id" % task[ 'task_id'] subtasks, err = db.read_multiple_rows(db_path, subtasks_query) if err: raise Exception(err) #Assume task is complete unless proven otherwise task_completed = True # Iteriate through all the unfinished subtasks related to the task for subtask in subtasks: #print 'subtask is ', subtask subtask_id = subtask["subtask_id"] status_update = "update subtasks set status = 'running' where subtask_id = '%d' and status is not 'cancelled'" % subtask_id status, err = db.execute_iud(db_path, [ [status_update], ], get_rowid=True) if err: task_completed = False break #print subtask['command'] #print 'username is ', task['run_as_user_name'] #Now actually execute the command #print 'not current user' #This task is not to be meant to be executed by the current user so switch to that user #print 'command is ', row['command'] (out, return_code), err = command.execute_with_rc( subtask["command"], shell=True, run_as_user_name=run_as_user_name) #print out, return_code, err if out[0]: output = re.sub("'", "", ''.join(out[0])) else: output = None if out[1]: error = re.sub("'", "", ''.join(out[1])) else: error = None if return_code == 0: # This means the command was successful. So update to completed status_update = "update subtasks set status = 'completed', return_code='%d' where subtask_id = '%d' and status is not 'cancelled';" % ( return_code, subtask_id) status, err = db.execute_iud(db_path, [ [status_update], ], get_rowid=True) if err: task_completed = False break else: continue else: # Subtask command failed if attempts > 1 or attempts == -2: status_update = 'update subtasks set status = "error-retrying", return_code="%d" where subtask_id = "%d" and status is not "cancelled";' % ( return_code, subtask_id) elif attempts in [0, 1]: status_update = 'update scheduler_commands set status = "failed", return_code="%d"" where subtask_id = "%d" and status is not "cancelled";' % ( return_code, subtask_id) execute, err = db.execute_iud(db_path, [ [status_update], ], get_rowid=True) task_completed = False break if task_completed: status_update = "update tasks set status = 'completed' where task_id = '%d'" % task[ 'task_id'] else: if attempts > 1: status_update = "update tasks set status = 'error-retrying', attempts = %d where task_id = '%d' and status is not 'cancelled'" % ( attempts - 1, task['task_id']) elif attempts == -2: status_update = "update tasks set status = 'error-retrying', attempts = %d where task_id = '%d' and status is not 'cancelled'" % ( -2, task['task_id']) else: status_update = "update tasks set status = 'failed', attempts = '%d' where task_id = '%d' and status is not 'cancelled'" % ( 0, task['task_id']) status, err = db.execute_iud(db_path, [ [status_update], ], get_rowid=True) if err: raise Exception(err) if task_completed: audit.audit("task_complete", audit_str, None, system_initiated=True) else: audit.audit("task_fail", audit_str, None, system_initiated=True) except Exception as e: return False, 'Error processing tasks : %s' % e else: return True, None