|
42 |
password = open('/etc/admin-diary.secret').read().strip() |
42 |
password = open('/etc/admin-diary.secret').read().strip() |
43 |
|
43 |
|
44 |
dbms = ucr.get('admin/diary/dbms') |
44 |
dbms = ucr.get('admin/diary/dbms') |
|
|
45 |
dbhost = ucr.get('admin/diary/dbhost', 'localhost') |
45 |
|
46 |
|
46 |
get_logger = partial(get_logger, 'backend') |
47 |
get_logger = partial(get_logger, 'backend') |
47 |
|
48 |
|
48 |
@contextmanager |
49 |
@contextmanager |
49 |
def connection(module): |
50 |
def connection(module): |
50 |
if dbms == 'mysql': |
51 |
if dbms == 'mysql': |
51 |
conn = module.connect(db='admindiary', user='admindiary', host='localhost', passwd=password) |
52 |
conn = module.connect(db='admindiary', user='admindiary', host=dbhost, passwd=password) |
52 |
elif dbms == 'postgresql': |
53 |
elif dbms == 'postgresql': |
53 |
conn = module.connect(dbname='admindiary', user='admindiary', host='localhost', password=password) |
54 |
conn = module.connect(dbname='admindiary', user='admindiary', host=dbhost, password=password) |
54 |
yield conn |
55 |
yield conn |
55 |
conn.commit() |
56 |
conn.commit() |
56 |
conn.close() |
57 |
conn.close() |
Lines 68-79
if ucr.get('admin/diary/dbms') == 'postgresql':
|
Link Here
|
---|
|
68 |
|
69 |
|
69 |
def _postgresql_add(entry): |
70 |
def _postgresql_add(entry): |
70 |
with cursor(psycopg2) as cur: |
71 |
with cursor(psycopg2) as cur: |
71 |
cur.execute("INSERT INTO entries (username, hostname, message, args, issued, tags, diary_id, event_name) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", (entry.username, entry.hostname, entry.message, entry.args, entry.issued, entry.tags, entry.diary_id, entry.event_name)) |
72 |
cur.execute("INSERT INTO entries (username, hostname, message, args, timestamp, tags, context_id, event_name) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", (entry.username, entry.hostname, entry.message, entry.args, entry.timestamp, entry.tags, entry.context_id, entry.event_name)) |
72 |
get_logger().info('Successfully added %s to postgresql. (%s)' % (entry.diary_id, entry.event_name)) |
73 |
get_logger().info('Successfully added %s to postgresql. (%s)' % (entry.context_id, entry.event_name)) |
73 |
|
74 |
|
74 |
def _postgresql_query(): |
75 |
def _postgresql_query(): |
75 |
with cursor(psycopg2) as cur: |
76 |
with cursor(psycopg2) as cur: |
76 |
args = ['id', 'username', 'hostname', 'message', 'args', 'issued', 'tags', 'diary_id', 'event_name'] |
77 |
args = ['id', 'username', 'hostname', 'message', 'args', 'timestamp', 'tags', 'context_id', 'event_name'] |
77 |
cur.execute("SELECT %s FROM entries" % ', '.join(args)) |
78 |
cur.execute("SELECT %s FROM entries" % ', '.join(args)) |
78 |
rows = cur.fetchall() |
79 |
rows = cur.fetchall() |
79 |
res = [dict(zip(args, row)) for row in rows] |
80 |
res = [dict(zip(args, row)) for row in rows] |
Lines 90-106
elif ucr.get('admin/diary/dbms') == 'mysql':
|
Link Here
|
---|
|
90 |
|
91 |
|
91 |
def _mysql_add(entry): |
92 |
def _mysql_add(entry): |
92 |
with cursor(MySQLdb) as cur: |
93 |
with cursor(MySQLdb) as cur: |
93 |
cur.execute("INSERT INTO entries (username, hostname, message, issued, diary_id, event_name) VALUES (%s, %s, %s, %s, %s, %s)", (entry.username, entry.hostname, entry.message, entry.issued, entry.diary_id, entry.event_name)) |
94 |
cur.execute("INSERT INTO entries (username, hostname, message, timestamp, context_id, event_name) VALUES (%s, %s, %s, %s, %s, %s)", (entry.username, entry.hostname, entry.message, entry.timestamp, entry.context_id, entry.event_name)) |
94 |
entry_id = cur.lastrowid |
95 |
entry_id = cur.lastrowid |
95 |
for arg in entry.args: |
96 |
for arg in entry.args: |
96 |
cur.execute("INSERT INTO arguments (log_entry_id, arg) VALUES (%s, %s)", (entry_id, arg)) |
97 |
cur.execute("INSERT INTO arguments (log_entry_id, arg) VALUES (%s, %s)", (entry_id, arg)) |
97 |
for tag in entry.tags: |
98 |
for tag in entry.tags: |
98 |
cur.execute("INSERT INTO tags (log_entry_id, tag) VALUES (%s, %s)", (entry_id, tag)) |
99 |
cur.execute("INSERT INTO tags (log_entry_id, tag) VALUES (%s, %s)", (entry_id, tag)) |
99 |
get_logger().info('Successfully added %s to mysql. (%s)' % (entry.diary_id, entry.event_name)) |
100 |
get_logger().info('Successfully added %s to mysql. (%s)' % (entry.context_id, entry.event_name)) |
100 |
|
101 |
|
101 |
def _mysql_query(): |
102 |
def _mysql_query(): |
102 |
with cursor(MySQLdb) as cur: |
103 |
with cursor(MySQLdb) as cur: |
103 |
args = ['id', 'username', 'hostname', 'message', 'issued', 'diary_id', 'event_name'] |
104 |
args = ['id', 'username', 'hostname', 'message', 'timestamp', 'context_id', 'event_name'] |
104 |
cur.execute("SELECT %s FROM entries" % ', '.join(args)) |
105 |
cur.execute("SELECT %s FROM entries" % ', '.join(args)) |
105 |
rows = cur.fetchall() |
106 |
rows = cur.fetchall() |
106 |
res = [dict(zip(args, row)) for row in rows] |
107 |
res = [dict(zip(args, row)) for row in rows] |