def get_remote_replications(remote_replication_id=None): replications = [] try: db_path, err = config.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.get_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 get_cron_tasks(cron_task_id=None, user='******'): """Function to return all the user created cron.""" cron_list = [] try: db_path, err = config.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.get_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 load_auth_access_group_list(): iscsi_auth_access_group_list = None try: db_path, err = config.get_db_path() if err: raise Exception(err) iscsi_auth_access_group_list, err = db.get_multiple_rows( db_path, "select * from iscsi_auth_access_groups") except Exception, e: return None, 'Error loading authorized access group list : %s' % str(e)
def get_shares_list(): """Load the list of currently created shares from the db. """ l = [] try: db_path, err = config.get_db_path() if err: raise Exception(err) l, err = db.get_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_initiators_list(): iscsi_initiator_list = None try: db_path, err = config.get_db_path() if err: raise Exception(err) iscsi_initiator_list, err = db.get_multiple_rows( db_path, "select * from iscsi_initiators;") if err: raise Exception(err) except Exception, e: return None, str(e)
def load_targets_list(): iscsi_target_list = [] try: db_path, err = config.get_db_path() if err: raise Exception(err) iscsi_target_list, err = db.get_multiple_rows( db_path, "select * from iscsi_targets") if err: raise Exception(err) except Exception, e: return None, 'Error loading ISCSI targets list : %s' % str(e)
def load_auth_access_users_info(auth_access_group_id): l = None try: db_path, err = config.get_db_path() if err: raise Exception(err) l, err = db.get_multiple_rows( db_path, "select * from iscsi_auth_access_users where auth_access_group_id = \'%d\'" % auth_access_group_id) if err: raise Exception(err) except Exception, e: return None, 'Error loading authorized access users information by group: %s' % str(e)
def get_valid_users_list(share_id): """Get the list of users from the db who have access to a share. """ l = None try: db_path, err = config.get_db_path() if err: raise Exception(err) l, err = db.get_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 = config.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.get_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 load_auth_access_users_info(auth_access_group_id): l = None try: db_path, err = config.get_db_path() if err: raise Exception(err) l, err = db.get_multiple_rows( db_path, "select * from iscsi_auth_access_users where auth_access_group_id = \'%d\'" % auth_access_group_id) if err: raise Exception(err) except Exception, e: return None, 'Error loading authorized access users information by group: %s' % str( e)
def get_subtasks(task_id): """For the given task_id, fetch all the entires from subtasks with matching task_id value.""" subtasks = None try: query = "select * from subtasks where task_id = '%d'" % task_id db_path, err = config.get_db_path() if err: raise Exception(err) subtasks, err = db.get_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_entries(): al = [] try: db_path, err = config.get_db_path() if err: raise Exception(err) query = 'select * from audit order by audit_id desc' rows, err = db.get_multiple_rows(db_path, query) if err: raise Exception(err) if rows: for row in rows: audit_entry, err = _parse_audit_entry(row) if err: raise Exception(err) al.append(audit_entry) except Exception, e: return None, 'Error loading audit entries : %s' % str(e)
def get_tasks_by_cron_task_id(cron_task_id, get_last_by=False): tasks = [] try: db_path, err = config.get_db_path() if err: raise Exception(err) if get_last_by is False: query = 'select * from tasks where cron_task_id="%d"' % int( cron_task_id) else: query = 'select * from tasks where cron_task_id="%d" order by "%s" desc limit 1' % ( int(cron_task_id), str(get_last_by)) tasks, err = db.get_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_tasks(node=None): """Returns all entries from tasks table.""" tasks = None try: db_path, err = config.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" tasks, err = db.get_multiple_rows(db_path, tasks_query) if err: raise Exception(err) except Exception, e: return None, 'Error retrieving tasks : %s' % e
def load_iscsi_volumes_list(vil): iscsi_volumes_list = [] try: db_path, err = config.get_db_path() if err: raise Exception(err) il, err = db.get_multiple_rows(db_path, "select * from iscsi_volumes") if err: raise Exception(err) if il: for i in il: current = False for v in vil: if v["name"] == i["vol_name"]: current = True break if current: iscsi_volumes_list.append(i["vol_name"]) except Exception, e: return None, 'Error loading ISCSI volumes list : %s' % str(e)
def get_remote_replications_with(source_dataset, destination_ip, destination_pool): replications = [] repls = [] try: db_path, err = config.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.get_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 main(): try: lck, err = lock.get_lock('integralstor_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 = config.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.get_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('integralstor_poll_for_alerts') except Exception, e: print "Error generating alerts : %s ! Exiting." % str(e) sys.exit(-1)
def process_tasks(node=socket.getfqdn()): """When called, processes/runs subtasks of each entry from tasks table if they satisfy/pass the required checks like status, last_run_time, retries, etc.""" ''' TODO - Needs a better docstring comment beriefly explaning what the function does ''' try: db_path, err = config.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.get_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.get_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