1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/pgadmin_ynh.git synced 2024-09-03 19:56:38 +02:00
pgadmin_ynh/scripts/config_database.py
2018-11-30 17:58:12 +01:00

41 lines
1.6 KiB
Python

#!/usr/bin/python
import imp
import sqlite3
import sys
# Import crypto from pgadmin project
crypto = imp.load_source('crypt', '/opt/yunohost/pgadmin/lib/python__PYTHON_VERSION__/site-packages/pgadmin4/pgadmin/utils/crypto.py')
# Get arguments
username = sys.argv[1]
password = sys.argv[2]
# Connect to sqlite3
conn = sqlite3.connect('/var/lib/pgadmin/pgadmin4.db')
# Get encrypte user password from the database
cursor = conn.execute('SELECT `password`,1 FROM `user`')
user_encrypted_password = cursor.fetchone()[0]
# Encrypt database password
crypted_password = crypto.encrypt(password, user_encrypted_password)
# Declare database data to put in database
data = {'id': 1,'user_id': 1, 'servergroup_id' : 1, 'name': 'Yunohost Server', 'host': 'localhost', 'port': 5432,'maintenance_db':'postgres','username':username,
'ssl_mode':'prefer', 'comment' : '', 'password' :crypted_password,'role':'', 'discovery_id':'', 'hostaddr':'','db_res':'','passfile':'',
'sslcert' :'','sslkey':'','sslrootcert':'','sslcrl':''}
# Insert new data in database
cursor = conn.cursor()
cursor.execute('''INSERT INTO `server`(
`id`,`user_id`,`servergroup_id`,`name`,`host`,`port`,`maintenance_db`,`username`,
`ssl_mode`,`comment`,`password`,`role`,`discovery_id`,`hostaddr`,`db_res`,`passfile`,`sslcert`,`sslkey`,`sslrootcert`,`sslcrl`
) VALUES(
:id,:user_id,:servergroup_id,:name,:host,:port,:maintenance_db,:username,
:ssl_mode,:comment,:password,:role,:discovery_id,:hostaddr,:db_res,:passfile,:sslcert,:sslkey,:sslrootcert,:sslcrl
)''', data)
conn.commit()
# Close connection
conn.close()