update db_export

This commit is contained in:
kjg 2020-02-28 00:19:29 +09:00
parent 2852b91719
commit 0498c2fb02
3 changed files with 83 additions and 33 deletions

75
db_export.py Executable file
View File

@ -0,0 +1,75 @@
import psycopg2 as pg2
from config import config
db_name = config['db']['db_name']
db_user = config['db']['db_user']
db_password = config['db']['db_password']
db_port = config['db']['db_port']
#with open("list") as data:
# lines = data.readlines()
#numbers=[]
#for line in lines:
# numbers.append(line.split())
#conn = pg2.connect("host = localhost dbname={} user={} password={} port={}".format(db_name,db_user,db_password,db_port))
#conn.autocommit = True
#cur = conn.cursor()
def setconn(u_id, vm_num, vm_port,vm_host):
conn = pg2.connect("host = localhost dbname={} user={} password={} port={}".format(db_name,db_user,db_password,db_port))
conn.autocommit = True
cur = conn.cursor()
#for con in numbers:
#u_id = con[0]
#vm_num = con[1]
#vm_port = con[2]
#vm_host = con[3]
cur.execute("SELECT entity_id FROM guacamole_entity WHERE name = '{}'".format(u_id))
row = cur.fetchone()
if row == None:
cur.execute("INSERT INTO guacamole_entity (name, type) VALUES ('{}','USER')".format(u_id))
cur.execute("SELECT entity_id FROM guacamole_entity WHERE name = '{}'".format(u_id))
row = cur.fetchone()
en_id = row[0]
cur.execute("INSERT INTO guacamole_user(entity_id, password_hash, password_date) VALUES ('{}', '\x74657374', now())".format(en_id))
print("create user : " , u_id)
else:
en_id = row[0]
cur.execute("SELECT password_hash FROM guacamole_user WHERE entity_id = '{}'".format(en_id))
row = cur.fetchone()
if row == None:
cur.execute("INSERT INTO guacamole_user(entity_id, password_hash, password_date) VALUES ('{}', '\x74657374', now())".format(en_id))
print("user exsit")
cn = "{}{}".format(u_id,vm_num)
cur.execute("SELECT connection_id FROM guacamole_connection WHERE connection_name = '{}'".format(cn))
row = cur.fetchone()
if row == None:
#create connection
cur.execute("INSERT INTO guacamole_connection (connection_name, protocol) VALUES ('{}', 'vnc')".format(cn))
cur.execute("SELECT MAX(connection_id) FROM guacamole_connection WHERE connection_name = '{}' AND parent_id IS NULL".format(cn))
temp_cn_id = cur.fetchone()
cn_id = temp_cn_id[0]
cur.execute("INSERT INTO guacamole_connection_parameter VALUES ('{}','hostname','{}')".format(cn_id, vm_host))
cur.execute("INSERT INTO guacamole_connection_parameter VALUES ('{}','port','{}')".format(cn_id,vm_port))
#connection permission
cur.execute("INSERT INTO guacamole_connection_permission(entity_id, connection_id, permission) VALUES ('{}', '{}', 'READ')".format(en_id,cn_id))
#clipboard-encoding
cur.execute("INSERT INTO guacamole_connection_parameter VALUES ('{}','clipboard-encoding','UTF-8')".format(cn_id))
print("create connection")
else:
cur.execute("SELECT MAX(connection_id) FROM guacamole_connection WHERE connection_name = '{}' AND parent_id IS NULL".format(cn))
temp_cn_id = cur.fetchone()
cn_id = temp_cn_id[0]
cur.execute("UPDATE guacamole_connection_parameter SET parameter_value='{}' where connection_id='{}' and parameter_name='hostname'".format(vm_host,cn_id))
cur.execute("UPDATE guacamole_connection_parameter SET parameter_value='{}' where connection_id='{}' and parameter_name='port'".format(vm_port,cn_id))
#cur.execute("UPDATE guacamole_connection_parameter SET parameter_value='UTF-8' where connection_id='{}' and parameter_name='clipboard-encoding'".format(cn_id))
print("no connection")
conn.close()
return None
#print("test")
#conn.close()

View File

@ -2,25 +2,17 @@ import json
from enum import IntEnum
from xmlrpc.client import ServerProxy as RPCClient
from xmltodict import parse
from config import config
from ldap_list import vm_list
from ldap_list3 import vm_list
from db_export import setconn
# Constants
ALL_VM_STATES = -1
START_ID = -1 # First id whatever it is
END_ID = -1 # Last id whatever it is
session_string = config['oca']['client_secrets']
f = open("list",'w')
f.close()
def put_under_list(obj):
if not isinstance(obj, list):
return [obj]
return obj
opnserver = config['oca']['opn_server']
class VMState(IntEnum):
INIT = 0
@ -60,49 +52,33 @@ class VM:
template = vm['TEMPLATE']
self.disk = put_under_list(template.get('DISK', []))
self.graphics = template.get('GRAPHICS', {})
self.memory = template.get('MEMORY', None)
self.nic = put_under_list(template.get('NIC', []))
self.vcpu = template.get('VCPU', None)
self.host = {
'name': ((vm.get('HISTORY_RECORDS', {}) or {}).get('HISTORY', {}) or {}).get('HOSTNAME', None),
'id': ((vm.get('HISTORY_RECORDS', {}) or {}).get('HISTORY', {}) or {}).get('HID', None),
}
self.snapshots = put_under_list(vm.get('SNAPSHOTS', []))
def get_data(self):
return {
attr: getattr(self, attr)
for attr in dir(self)
if not attr.startswith('__') and not callable(getattr(self, attr))
}
def main():
with RPCClient(confi['oca']['server']) as rpc_client:
with RPCClient(opnserver) as rpc_client:
success, response, *_ = rpc_client.one.vmpool.infoextended(
session_string , -2, -1, -1, 3
session_string , VmFilterFlag.AllResources.value, START_ID, END_ID, VMState.ACTIVE.value
)
if success:
vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']
f = open("list",'w')
for entry in vm_list.entries:
temp_uname = entry.mail
#print(temp_uname)
for i, vm in enumerate(vms):
vm_user = vm['UNAME']
vm_id = vm['ID']
vm_port = vm['TEMPLATE']['GRAPHICS'].get('PORT')
vm_host = vm['HISTORY_RECORDS']['HISTORY']['HOSTNAME']
#print(vm_user, vm_id, vm_port, vm_host)
if vm['UNAME'] == temp_uname:
print(entry.uid, vm_id, vm_port, vm_host, file=f)
#temp_line = entry.uid
#f.write(temp_line)
#print(entry.uid, vm_id, vm_port, vm_host)
setconn(entry.uid, vm_id, vm_port, vm_host)
#print(config['ldap']['admin_dn'])
f.close()
else:
print(response)

View File

@ -8,8 +8,7 @@ from ldap3.core import exceptions
LDAP_SERVER = config['ldap']['server']
LDAP_PASSWORD = config['ldap']['admin_password']
LDAP_USER = config['ldap']['admin_dn']
LDAP_PORT = config['ldap']['port']
#LDAP_ATTRS = jg
LDAP_PORT = int(config['ldap']['ldap_port'])
# Create the Server object with the given address.
server = Server(LDAP_SERVER, LDAP_PORT, get_info=ALL)