def set_log_level(level): if level not in [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL]: logger.setLevel(logging.INFO) else: d1 = db.read_single_row("%s/integral_view_config.db" % db_path, "select * from global_params") cmd_list = [] if d1: cmd = ["update global_params set logging_level=? where id = ?", (level, 1)] else: cmd = ["insert into global_params (logging_level, id) values(?,?)", (level, 1)] cmd_list.append(cmd) db.execute_iud("%s/integral_view_config.db" % db_path, cmd_list) logger.setLevel(level)
def save_email_settings(d): conn = None try: db_path, err = common.get_db_path() if err: raise Exception(err) d1, err = db.read_single_row(db_path, "select * from email_config") if err: raise Exception(err) if d1: #Config exists so update ret, err = db.execute_iud(db_path, [[ "update email_config set server=?, port=?, username=?, pswd=?, tls=?, email_alerts=?, email_audit=?, email_quota=?, rcpt_list=? where id = ?", ( d["server"], d["port"], d["username"], d["pswd"], d["tls"], d["email_alerts"], d["email_audit"], d["email_quota"], d["rcpt_list"], 1, ) ]]) if err: raise Exception(err) else: #No config exists so insert ret, err = db.execute_iud(db_path, [[ "insert into email_config (server, port, username, pswd, tls, email_alerts,email_audit,email_quota, rcpt_list, id) values (?,?,?,?,?,?,?,?,?,?)", ( d["server"], d["port"], d["username"], d["pswd"], d["tls"], d["email_alerts"], d["email_audit"], d["email_quota"], d["rcpt_list"], 1, ) ]]) if err: raise Exception(err) except Exception, e: return False, 'Error saving email settings : %s' % str(e)
def get_log_level(): d = None conn = None try: d = db.read_single_row("%s/integral_view_config.db" % db_path, "select * from global_params where id=1") if d and "logging_level" in d: return d["logging_level"] else: # Not yet set so insert the default and return it cmd_list = [] cmd = ["insert into global_params (logging_level, id) values(?,?)", (logging.INFO, 1)] cmd_list.append(cmd) db.execute_iud("%s/integral_view_config.db" % db_path, cmd_list) return logging.INFO except Exception, e: print "Error inserting log level : %s" % str(e)
def refresh_alerts(request, random=None): try: from datetime import datetime cmd_list = [] #this command will insert or update the row value if the row with the user exists. cmd = ["INSERT OR REPLACE INTO admin_alerts (user, last_refresh_time) values (?,?);", (request.user.username, datetime.now())] cmd_list.append(cmd) db_path, err = common.get_db_path() if err: raise Exception(err) test, err = db.execute_iud("%s/integral_view_config.db"%db_path, cmd_list) if err: raise Exception(err) new_alerts_present, err = alerts.new_alerts() if err: raise Exception(err) if new_alerts_present: import json alerts_list, err = alerts.load_alerts() if err: raise Exception(err) if not alerts_list: raise Exception('Error loading alerts') new_alerts = json.dumps([dict(alert=pn) for pn in alerts_list]) return django.http.HttpResponse(new_alerts, mimetype='application/json') else: clss = "btn btn-default btn-sm" message = "View alerts" return django.http.HttpResponse("No New Alerts") except Exception, e: return django.http.HttpResponse("Error loading alerts : %s"%str(e))
def remove_cron(cron_task_id, user='******'): try: db_path, err = common.get_db_path() if err: raise Exception(err) tasks, err = get_tasks_by_cron_task_id(cron_task_id) if err: raise Exception(err) cmd_list = [] cmd_list.append( ['delete from cron_tasks where cron_task_id=%d' % cron_task_id]) cmd_list.append([ 'update tasks set status="cancelled" where cron_task_id=%s and status is not "completed"' % cron_task_id ]) if tasks: for task in tasks: cmd_list.append([ 'update subtasks set status="cancelled" where task_id=%d and status is not "completed"' % task['task_id'] ]) #print cmd_list ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) cron = crontab.CronTab(user) cron.remove_all(comment=str(cron_task_id)) cron.write() except Exception, e: return False, "Error deleting cron entry : %s" % str(e)
def get_log_level(): d = None conn = None log_level = None try: d, err = db.read_single_row(db_path, "select * from global_params where id=1") if err: raise Exception(err) if d and "logging_level" in d: log_level = d["logging_level"] else: #Not yet set so insert the default and return it cmd_list = [] cmd = [ "insert into global_params (logging_level, id) values(?,?)", ( logging.INFO, 1, ) ] cmd_list.append(cmd) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) log_level = logging.INFO except Exception, e: return None, "Error getting log level : %s" % str(e)
def refresh_alerts(request, random=None): try: from datetime import datetime cmd_list = [] #this command will insert or update the row value if the row with the user exists. cmd = ["INSERT OR REPLACE INTO admin_alerts (user, last_refresh_time) values (?,?);", (request.user.username, datetime.now())] cmd_list.append(cmd) db_path, err = common.get_db_path() if err: raise Exception(err) test, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) new_alerts_present, err = alerts.new_alerts() if err: raise Exception(err) if new_alerts_present: import json alerts_list, err = alerts.load_alerts(last_n = 5) if err: raise Exception(err) if not alerts_list: raise Exception('Error loading alerts') new_alerts = json.dumps([dict(alert=pn) for pn in alerts_list]) return django.http.HttpResponse(new_alerts, content_type='application/json') else: clss = "btn btn-default btn-sm" message = "View alerts" return django.http.HttpResponse("No New Alerts") except Exception, e: return django.http.HttpResponse("Error loading alerts : %s"%str(e))
def save_auth_settings(d): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = [ "update samba_global_common set workgroup=?, netbios_name=?, security=?, include_homes_section=? where id = ?", ( d["workgroup"], d["netbios_name"], d["security"], True, 1, ) ] cmd_list = [] cmd_list.append(cmd) if d["security"] == "ads": d1, err = db.read_single_row(db_path, "select * from samba_global_ad") if err: raise Exception(err) if d1: cmd = [ "update samba_global_ad set realm=?, password_server=?, ad_schema_mode=?, id_map_min=?, id_map_max=?, password_server_ip=? where id = ?", ( d["realm"], d["password_server"], 'rfc2307', 16777216, 33554431, d["password_server_ip"], 1, ) ] cmd_list.append(cmd) else: cmd = [ "insert into samba_global_ad (realm, password_server, ad_schema_mode, id_map_min, id_map_max, password_server_ip, id) values(?,?,?,?,?,?,?)", ( d["realm"], d["password_server"], 'rfc2307', 16777216, 33554431, d["password_server_ip"], 1, ) ] cmd_list.append(cmd) #print cmd_list ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error saving authentication settings : %s' % str(e)
def delete_auth_settings(): try: db_path, err = common.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud(db_path, [["delete from samba_auth "]]) if err: raise Exception(err) except Exception, e: return False, 'Error deleting authentication settings : %s' % str(e)
def delete_email_settings(): try: db_path, err = common.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud(["delete from email_config "]) if err: raise Exception(err) except Exception, e: return False, 'Error deleting email settings : %s' % str(e)
def delete_all_shares(): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd_list = [] cmd_list.append(["delete from samba_shares "]) cmd_list.append(["delete from samba_valid_users "]) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error deleting all CIFS shares : %s' % str(e)
def delete_remote_replication(remote_replication_id): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = "delete from remote_replications where remote_replication_id='%s'" % remote_replication_id rowid, err = db.execute_iud(db_path, [ [cmd], ], get_rowid=False) if err: raise Exception(err) except Exception, e: return False, 'Error deleting remote replication task : %s' % e
def update_remote_replication(remote_replication_id, new_cron_task_id): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = "update remote_replications set cron_task_id='%d' where remote_replication_id='%s'" % ( new_cron_task_id, remote_replication_id) rowid, err = db.execute_iud(db_path, [ [cmd], ], get_rowid=True) if err: raise Exception(err) except Exception, e: return False, 'Error updating remote replication task : %s' % e
def remove_task(task_id): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd_list = [['delete from tasks where task_id = %s' % task_id], ['delete from subtasks where task_id = %s' % task_id]] #print cmd_list status, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error removing task : %s' % e
def refresh_alerts(request, random=None): from datetime import datetime cmd_list = [] #this command will insert or update the row value if the row with the user exists. cmd = ["INSERT OR REPLACE INTO admin_alerts (user, last_refresh_time) values (?,?);", (request.user.username, datetime.now())] cmd_list.append(cmd) test = db.execute_iud("%s/integral_view_config.db"%common.get_db_path(), cmd_list) if alerts.new_alerts(): import json new_alerts = json.dumps([dict(alert=pn) for pn in alerts.load_alerts()]) return django.http.HttpResponse(new_alerts, mimetype='application/json') else: clss = "btn btn-default btn-sm" message = "View alerts" return django.http.HttpResponse("No New Alerts")
def delete_share(share_id): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd_list = [] cmd_list.append( ["delete from samba_shares where share_id=?", (share_id, )]) cmd_list.append( ["delete from samba_valid_users where share_id=?", (share_id, )]) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error deleting CIFS share : %s' % str(e)
def add_cron_task(command, description, min="1", hour='*', day='*', dow='*', month='*', user='******'): cron_task_id = None try: if not command or not description: raise Exception('Invalid parameters') db_path, err = common.get_db_path() if err: raise Exception(err) #cmd = ["insert into samba_global_ad (realm, password_server, ad_schema_mode, id_map_min, id_map_max, password_server_ip, id) values(?,?,?,?,?,?,?)", (d["realm"], d["password_server"], 'rfc2307', 16777216, 33554431, d["password_server_ip"], 1,)] cmd = [ 'insert into cron_tasks(command,description) values (?,?)', (command, description) ] #print cmd cron_task_id, err = db.execute_iud(db_path, [cmd], get_rowid=True) if err: raise Exception(err) log_path, err = common.get_log_folder_path() if err: raise Exception(err) log_dir = '%s/cron_logs' % log_path if not os.path.isdir(log_dir): os.mkdir(log_dir) log_file = '%s/%d.log' % (log_dir, cron_task_id) command = '%s >> %s 2>&1' % (command, log_file) cron = crontab.CronTab(user) job = cron.new(command=command, comment='%d' % cron_task_id) job.setall(min, hour, day, dow, month) if job.is_valid(): job.enable() cron.write() else: raise Exception('Cron entry not valid.') except Exception, e: return None, 'Error creating cron entry : %s' % str(e)
def save_share(share_id, name, comment, guest_ok, read_only, path, browseable, users, groups): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd_list = [] cmd_list.append([ "update samba_shares set comment=?, read_only=?, guest_ok=?, browseable=? where share_id=?", ( comment, read_only, guest_ok, browseable, share_id, ) ]) cmd_list.append( ["delete from samba_valid_users where share_id=?", (share_id, )]) if not guest_ok: if users: for user in users: cmd_list.append([ "insert into samba_valid_users (id, share_id, grp, name) values (NULL,?,?,?)", ( share_id, False, user, ) ]) if groups: for group in groups: cmd_list.append([ "insert into samba_valid_users (id, share_id, grp, name) values (NULL,?,?,?)", ( share_id, True, group, ) ]) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error saving CIFS share : %s' % str(e)
def change_auth_method(security): try: db_path, err = common.get_db_path() if err: raise Exception(err) cl = [] cl.append([ "update samba_global_common set security='%s' where id=1" % security ]) cl.append(["delete from samba_valid_users"]) ret, err = db.execute_iud(db_path, cl) if err: raise Exception(err) except Exception, e: return False, 'Error changing authentication method : %s' % str(e)
def add_remote_replication(source_dataset, destination_ip, destination_username, destination_pool, cron_task_id): remote_replication_id = None try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = "insert into remote_replications (source_dataset,destination_ip,destination_user_name,destination_pool, cron_task_id) values ('%s','%s','%s','%s', '%d')" % ( source_dataset, destination_ip, destination_username, destination_pool, cron_task_id) remote_replication_id, err = db.execute_iud(db_path, [ [cmd], ], get_rowid=True) if err: raise Exception(err) except Exception, e: return None, 'Error adding a remote replication task : %s' % e
def set_log_level(level): try: db_path, err = common.get_db_path() if err: raise Exception(err) if level not in [ logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL ]: logger.setLevel(logging.INFO) else: d1, err = db.read_single_row(db_path, "select * from global_params") if err: raise Exception(err) cmd_list = [] if d1: cmd = [ "update global_params set logging_level=? where id = ?", ( level, 1, ) ] else: cmd = [ "insert into global_params (logging_level, id) values(?,?)", ( level, 1, ) ] cmd_list.append(cmd) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) logger.setLevel(level) except Exception, e: return False, 'Error setting log level : %s' % str(e)
def set_log_level(level): try: db_path, err = common.get_db_path() if err: raise Exception(err) if level not in [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL]: logger.setLevel(logging.INFO) else: d1, err = db.read_single_row("%s/integral_view_config.db"%db_path, "select * from global_params") if err: raise Exception(err) cmd_list = [] if d1: cmd = ["update global_params set logging_level=? where id = ?", (level, 1,)] else: cmd = ["insert into global_params (logging_level, id) values(?,?)", (level, 1,)] cmd_list.append(cmd) ret, err = db.execute_iud("%s/integral_view_config.db"%db_path, cmd_list) if err: raise Exception(err) logger.setLevel(level) except Exception, e: return False, 'Error setting log level : %s'%str(e)
d1 = db.read_single_row("%s/integral_view_config.db"%db_path, "select * from samba_global_ad") if d1: #cmd = ["update samba_global_ad set realm=?, password_server=?, ad_schema_mode=?, id_map_min=?, id_map_max=? where id = ?", (d["realm"], d["password_server"], d["ad_schema_mode"], d["id_map_min"], d["id_map_max"], 1,)] cmd = ["update samba_global_ad set realm=?, password_server=?, ad_schema_mode=?, id_map_min=?, id_map_max=?, password_server_ip=? where id = ?", (d["realm"], d["password_server"], 'rfc2307', 16777216, 33554431, d["password_server_ip"], 1, )] cmd_list.append(cmd) else: #cmd = ["insert into samba_global_ad (realm, password_server, ad_schema_mode, id_map_min, id_map_max, id) values(?,?,?,?,?,?)", (d["realm"], d["password_server"], d["ad_schema_mode"], d["id_map_min"], d["id_map_max"], 1,)] cmd = ["insert into samba_global_ad (realm, password_server, ad_schema_mode, id_map_min, id_map_max, password_server_ip, id) values(?,?,?,?,?,?,?)", (d["realm"], d["password_server"], 'rfc2307', 16777216, 33554431, d["password_server_ip"], 1,)] cmd_list.append(cmd) #print cmd_list #Always try to create the fractalio_guest account for guest access - will fail if it exists so ok try: local_users.create_local_user('fractalio_guest', 'Fractalio_Guest_User', 'fractalioguest') except Exception, e: pass db.execute_iud("%s/integral_view_config.db"%db_path, cmd_list) def delete_auth_settings(): conn = None try : conn = sqlite3.connect("%s/integral_view_config.db"%db_path) cur = conn.cursor() cur.execute("delete from samba_auth ") cur.close() conn.commit() finally: if conn: conn.close() def load_shares_list(): l = []
def add_task(description, subtask_list, task_type_id=0, cron_task_id=0, node=socket.getfqdn(), initiate_time=None, attempts=3, run_as_user_name='root', retry_interval=1): ''' TODO - change function name to add_task - take a scheduled_task_id parameter, defaulting to -1 - change db parameters to new table ''' row_id = -1 try: if not description or not subtask_list: raise Exception("Insufficient parameters") now = int(time.time()) if not initiate_time: initiate_time = now log_path, err = common.get_log_folder_path() if err: raise Exception(err) log_dir = '%s/task_logs' % log_path if not os.path.isdir(log_dir): os.mkdir(log_dir) cmd = "insert into tasks (description,task_type_id, node, run_as_user_name, attempts, cron_task_id, retry_interval, create_time, initiate_time) VALUES ('%s','%d','%s','%s','%d','%d','%d', '%d', '%d');" % ( description, task_type_id, node, run_as_user_name, attempts, int(cron_task_id), retry_interval, now, initiate_time) #print cmd db_path, err = common.get_db_path() if err: raise Exception(err) row_id, err = db.execute_iud(db_path, [ [cmd], ], get_rowid=True) #print "\n check!" if err: raise Exception(err) if row_id: log_file = '%s/%d.log' % (log_dir, row_id) for subtask in subtask_list: for description, command in subtask.iteritems(): command = '%s &> %s' % (command, log_file) cmd = "insert into subtasks (description,command,task_id) values ('%s','%s','%d');" % ( description, command, row_id) status, err = db.execute_iud(db_path, [ [cmd], ], get_rowid=True) if err: raise Exception( 'Error creating scheduled command : %s' % err) else: raise Exception('Error creating scheduled task') except Exception, e: return False, ' Error adding task: %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
def create_share(name, comment, guest_ok, read_only, path, display_path, browseable, users, groups, vol): try: db_path, err = common.get_db_path() if err: raise Exception(err) d, err = load_auth_settings() if err: raise Exception(err) if not d: raise Exception( "Authentication settings not set. Please set authentication settings before creating shares." ) shl, err = load_shares_list() if err: raise Exception(err) if shl: for sh in shl: if sh["name"] == name: raise Exception("A share with that name already exists") share_id, err = db.execute_iud(db_path, [[ "insert into samba_shares (name, vol, path, display_path, comment, read_only, guest_ok, browseable, share_id) values (?,?, ?,?,?,?,?,?,NULL)", ( name, vol, path, display_path, comment, read_only, guest_ok, browseable, ) ]], True) if err: raise Exception(err) #print share_id, err cmd_list = [] if not guest_ok: if users: for user in users: cmd_list.append([ "insert into samba_valid_users (id, share_id, grp, name) values (NULL,?,?,?)", ( share_id, False, user, ) ]) if groups: for group in groups: cmd_list.append([ "insert into samba_valid_users (id, share_id, grp, name) values (NULL,?,?,?)", ( share_id, True, group, ) ]) ret, err = db.execute_iud(db_path, cmd_list, True) if err: raise Exception(err) except Exception, e: return False, 'Error creating CIFS share : %s' % str(e)
def change_auth_method(security): cl = [] cl.append(["update samba_global_common set security='%s' where id=1"%security]) cl.append(["delete from samba_valid_users"]) db.execute_iud("%s/integral_view_config.db"%db_path, cl)