2016-03-07 17:13:03 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
2016-03-17 17:55:27 +02:00
|
|
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
2016-03-07 17:13:03 +02:00
|
|
|
import sqlite3
|
2016-08-25 18:10:01 +03:00
|
|
|
import threading
|
2016-08-24 18:05:54 +03:00
|
|
|
from base.scope import Scope
|
2016-03-23 17:15:27 +02:00
|
|
|
|
|
|
|
|
2016-03-07 17:13:03 +02:00
|
|
|
class AhenkDbService(object):
|
|
|
|
"""
|
|
|
|
Sqlite manager for ahenk
|
|
|
|
"""
|
2016-03-23 17:15:27 +02:00
|
|
|
|
2016-03-07 17:13:03 +02:00
|
|
|
def __init__(self):
|
2016-09-21 12:05:48 +03:00
|
|
|
scope = Scope.get_instance()
|
|
|
|
self.logger = scope.get_logger()
|
|
|
|
self.configurationManager = scope.get_configuration_manager()
|
2016-03-23 17:15:27 +02:00
|
|
|
self.db_path = self.configurationManager.get('BASE', 'dbPath')
|
|
|
|
self.connection = None
|
2016-03-07 17:13:03 +02:00
|
|
|
self.cursor = None
|
|
|
|
|
2016-08-25 18:10:01 +03:00
|
|
|
self.lock = threading.Lock()
|
|
|
|
|
2016-06-27 17:17:20 +03:00
|
|
|
# TODO get columns anywhere
|
|
|
|
# TODO scheduler db init get here
|
|
|
|
|
2016-03-18 18:40:44 +02:00
|
|
|
def initialize_table(self):
|
2016-04-06 14:23:40 +03:00
|
|
|
|
2016-10-18 18:10:52 +03:00
|
|
|
self.check_and_create_table('task',
|
|
|
|
['id INTEGER', 'create_date TEXT', 'modify_date TEXT', 'command_cls_id TEXT',
|
|
|
|
'parameter_map BLOB', 'deleted INTEGER', 'plugin TEXT', 'cron_expr TEXT',
|
|
|
|
'file_server TEXT'])
|
|
|
|
self.check_and_create_table('policy',
|
|
|
|
['id INTEGER PRIMARY KEY AUTOINCREMENT', 'type TEXT', 'version TEXT', 'name TEXT',
|
2017-06-29 16:42:46 +03:00
|
|
|
'execution_id TEXT','expiration_date TEXT'])
|
2016-10-18 18:10:52 +03:00
|
|
|
self.check_and_create_table('profile', ['id INTEGER', 'create_date TEXT', 'label TEXT', 'description TEXT',
|
|
|
|
'overridable INTEGER', 'active TEXT', 'deleted TEXT',
|
|
|
|
'profile_data TEXT', 'modify_date TEXT', 'plugin TEXT'])
|
|
|
|
self.check_and_create_table('plugin',
|
|
|
|
['id INTEGER PRIMARY KEY AUTOINCREMENT', 'active TEXT', 'create_date TEXT',
|
|
|
|
'deleted TEXT', 'description TEXT', 'machine_oriented TEXT', 'modify_date TEXT',
|
|
|
|
'name TEXT', 'policy_plugin TEXT', 'user_oriented TEXT', 'version TEXT',
|
|
|
|
'task_plugin TEXT', 'x_based TEXT'])
|
|
|
|
self.check_and_create_table('registration',
|
|
|
|
['jid TEXT', 'password TEXT', 'registered INTEGER', 'dn TEXT', 'params TEXT',
|
|
|
|
'timestamp TEXT'])
|
|
|
|
self.check_and_create_table('contract', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'content BLOB', 'title TEXT',
|
|
|
|
'timestamp TEXT'])
|
|
|
|
self.check_and_create_table('agreement',
|
|
|
|
['id INTEGER PRIMARY KEY AUTOINCREMENT', 'contract_id TEXT', 'username TEXT',
|
|
|
|
'timestamp TEXT', 'choice TEXT'])
|
2017-07-06 11:10:30 +03:00
|
|
|
self.check_and_create_table('session', ['username TEXT', 'display TEXT', 'desktop TEXT', 'timestamp TEXT', 'ip TEXT'])
|
2016-06-27 17:17:20 +03:00
|
|
|
|
2017-06-22 21:14:44 +03:00
|
|
|
self.check_and_create_table('mail', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'command TEXT', 'mailstatus INTEGER', 'timestamp TEXT'])
|
|
|
|
|
2017-10-31 10:16:06 +03:00
|
|
|
self.check_and_create_table('service', ['id INTEGER PRIMARY KEY AUTOINCREMENT', 'serviceName TEXT', 'serviceStatus TEXT','timestamp TEXT','task_id INTEGER'])
|
|
|
|
|
2017-06-22 21:14:44 +03:00
|
|
|
|
2016-06-27 17:17:20 +03:00
|
|
|
def get_cols(self, table_name):
|
|
|
|
if table_name == 'agreement':
|
|
|
|
return ['contract_id', 'username', 'timestamp', 'choice']
|
|
|
|
elif table_name == 'contract':
|
|
|
|
return ['content', 'title', 'timestamp']
|
|
|
|
elif table_name == 'session':
|
2017-07-06 11:10:30 +03:00
|
|
|
return ['username', 'display', 'desktop', 'timestamp', 'ip']
|
2016-10-18 18:10:52 +03:00
|
|
|
elif table_name == 'task':
|
|
|
|
return ['id', 'create_date', 'modify_date', 'command_cls_id', 'parameter_map', 'deleted', 'plugin',
|
|
|
|
'cron_expr', 'file_server']
|
|
|
|
elif table_name == 'plugin':
|
|
|
|
return ['id', 'active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date', 'name',
|
|
|
|
'policy_plugin', 'user_oriented', 'version', 'task_plugin', 'x_based']
|
|
|
|
else:
|
|
|
|
return None
|
2016-03-18 18:40:44 +02:00
|
|
|
|
2016-03-16 10:40:17 +02:00
|
|
|
def connect(self):
|
2016-03-07 17:13:03 +02:00
|
|
|
try:
|
2016-03-23 17:15:27 +02:00
|
|
|
self.connection = sqlite3.connect(self.db_path, check_same_thread=False)
|
2016-03-07 17:13:03 +02:00
|
|
|
self.cursor = self.connection.cursor()
|
|
|
|
except Exception as e:
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.error('Database connection error: {0}'.format(str(e)))
|
2016-03-07 17:13:03 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def check_and_create_table(self, table_name, cols):
|
2016-08-25 18:10:01 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.lock.acquire(True)
|
|
|
|
if self.cursor:
|
|
|
|
cols = ', '.join([str(x) for x in cols])
|
|
|
|
self.cursor.execute('create table if not exists ' + table_name + ' (' + cols + ')')
|
|
|
|
else:
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.warning('Could not create table cursor is None! Table Name : {0}'.format(str(table_name)))
|
2016-08-25 18:10:01 +03:00
|
|
|
finally:
|
|
|
|
self.lock.release()
|
2016-03-07 17:13:03 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def drop_table(self, table_name):
|
2016-08-25 18:10:01 +03:00
|
|
|
try:
|
|
|
|
self.lock.acquire(True)
|
|
|
|
sql = 'DROP TABLE ' + table_name
|
|
|
|
self.cursor.execute(sql)
|
|
|
|
self.connection.commit()
|
|
|
|
finally:
|
|
|
|
self.lock.release()
|
2016-03-16 10:40:17 +02:00
|
|
|
|
2016-03-07 17:13:03 +02:00
|
|
|
def update(self, table_name, cols, args, criteria=None):
|
|
|
|
try:
|
2016-08-25 18:10:01 +03:00
|
|
|
self.lock.acquire(True)
|
2016-03-07 17:13:03 +02:00
|
|
|
if self.connection:
|
2016-03-29 15:29:49 +03:00
|
|
|
if criteria is None:
|
2016-03-07 17:13:03 +02:00
|
|
|
cols = ', '.join([str(x) for x in cols])
|
2016-03-23 17:15:27 +02:00
|
|
|
params = ', '.join(['?' for x in args])
|
|
|
|
sql = 'INSERT INTO ' + table_name + ' (' + cols + ') VALUES (' + params + ')'
|
2016-03-07 17:13:03 +02:00
|
|
|
else:
|
2016-03-23 17:15:27 +02:00
|
|
|
update_list = ''
|
2016-03-07 17:13:03 +02:00
|
|
|
for index in range(len(cols)):
|
2016-03-23 17:15:27 +02:00
|
|
|
update_list = update_list + ' ' + cols[index] + ' = ?,'
|
2016-03-07 17:13:03 +02:00
|
|
|
update_list = update_list.strip(',')
|
2016-03-23 17:15:27 +02:00
|
|
|
sql = 'UPDATE ' + table_name + ' SET ' + update_list + ' where ' + criteria
|
2016-03-17 17:55:27 +02:00
|
|
|
self.cursor.execute(sql, tuple(args))
|
2016-03-07 17:13:03 +02:00
|
|
|
self.connection.commit()
|
2016-04-06 14:23:40 +03:00
|
|
|
return self.cursor.lastrowid
|
2016-03-07 17:13:03 +02:00
|
|
|
else:
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.warning('Could not update table cursor is None! Table Name : {0}'.format(str(table_name)))
|
2016-04-06 14:23:40 +03:00
|
|
|
return None
|
2016-03-16 10:40:17 +02:00
|
|
|
except Exception as e:
|
2016-10-18 18:10:52 +03:00
|
|
|
self.logger.error(
|
|
|
|
'Updating table error ! Table Name : {0} Error Mesage: {1}'.format(str(table_name), str(e)))
|
2016-08-25 18:10:01 +03:00
|
|
|
finally:
|
|
|
|
self.lock.release()
|
2016-03-07 17:13:03 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def delete(self, table_name, criteria):
|
2016-08-25 18:10:01 +03:00
|
|
|
try:
|
|
|
|
self.lock.acquire(True)
|
|
|
|
if self.cursor:
|
|
|
|
sql = 'DELETE FROM ' + table_name
|
|
|
|
if criteria:
|
|
|
|
sql += ' where ' + str(criteria)
|
|
|
|
self.cursor.execute(sql)
|
|
|
|
self.connection.commit()
|
|
|
|
finally:
|
|
|
|
self.lock.release()
|
2016-03-07 17:13:03 +02:00
|
|
|
|
|
|
|
def findByProperty(self):
|
|
|
|
# Not implemented yet
|
|
|
|
pass
|
2016-03-23 16:39:39 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def select(self, table_name, cols='*', criteria='', orderby=''):
|
2016-03-07 17:13:03 +02:00
|
|
|
if self.cursor:
|
|
|
|
try:
|
2016-08-25 18:10:01 +03:00
|
|
|
self.lock.acquire(True)
|
2016-03-23 17:15:27 +02:00
|
|
|
if not cols == '*':
|
2016-03-07 17:13:03 +02:00
|
|
|
cols = ', '.join([str(x) for x in cols])
|
2016-03-23 17:15:27 +02:00
|
|
|
sql = 'SELECT ' + cols + ' FROM ' + table_name
|
|
|
|
if criteria != '':
|
2016-03-23 16:39:39 +02:00
|
|
|
sql += ' where '
|
|
|
|
sql += criteria
|
2016-03-23 17:15:27 +02:00
|
|
|
if orderby != '':
|
2016-03-23 16:39:39 +02:00
|
|
|
sql += ' order by '
|
|
|
|
sql += orderby
|
2016-03-17 17:55:27 +02:00
|
|
|
|
2016-03-07 17:13:03 +02:00
|
|
|
self.cursor.execute(sql)
|
|
|
|
rows = self.cursor.fetchall()
|
|
|
|
return rows
|
2016-06-27 17:17:20 +03:00
|
|
|
except:
|
2016-03-07 17:13:03 +02:00
|
|
|
raise
|
2016-08-25 18:10:01 +03:00
|
|
|
finally:
|
|
|
|
self.lock.release()
|
2016-03-07 17:13:03 +02:00
|
|
|
else:
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.warning('Could not select table cursor is None! Table Name : {0}'.format(str(table_name)))
|
2016-03-07 17:13:03 +02:00
|
|
|
|
2016-03-23 16:39:39 +02:00
|
|
|
def select_one_result(self, table_name, col, criteria=''):
|
|
|
|
if self.cursor:
|
|
|
|
try:
|
2016-08-25 18:10:01 +03:00
|
|
|
self.lock.acquire(True)
|
2016-03-23 16:39:39 +02:00
|
|
|
sql = 'SELECT ' + col + ' FROM ' + table_name
|
|
|
|
if criteria != '':
|
|
|
|
sql += ' where '
|
|
|
|
sql += criteria
|
|
|
|
self.cursor.execute(sql)
|
|
|
|
row = self.cursor.fetchone()
|
|
|
|
if row is not None:
|
|
|
|
return row[0]
|
2016-03-25 17:56:15 +02:00
|
|
|
else:
|
|
|
|
return None
|
2016-06-27 17:17:20 +03:00
|
|
|
except:
|
2016-03-23 16:39:39 +02:00
|
|
|
raise
|
2016-08-25 18:10:01 +03:00
|
|
|
finally:
|
|
|
|
self.lock.release()
|
2016-03-23 16:39:39 +02:00
|
|
|
else:
|
2016-09-21 12:05:48 +03:00
|
|
|
self.logger.warning('Could not select table cursor is None! Table Name : {0}'.format(str(table_name)))
|
2016-03-23 16:39:39 +02:00
|
|
|
|
2016-03-07 17:13:03 +02:00
|
|
|
def close(self):
|
|
|
|
try:
|
|
|
|
self.cursor.close()
|
|
|
|
self.connection.close()
|
|
|
|
except Exception as e:
|
2017-10-31 10:16:06 +03:00
|
|
|
self.logger.error('Closing database connection error: {0}'.format(str(e)))
|