-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnections.py
82 lines (68 loc) · 2.8 KB
/
connections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import sqlite3
import XenAPI
# Class to handle opening and closing database connections
class DbConnection(object):
def __init__(self):
self.__connect()
self.create_table()
def __connect(self):
self.conn = sqlite3.connect("C:\\Users\Tom\Documents\pythonsqlite.db")
print(sqlite3.version)
self.c = self.conn.cursor()
def create_table(self):
# Try and create table
try:
self.c.execute('''CREATE TABLE hosts
(host_id integer primary key, host text, username text, password text)''')
self.c.execute('''CREATE TABLE vms
(vm_id integer primary key, vm_uuid text, vm_name text, record text, tracked bool,
host_id integer, FOREIGN KEY(host_id) REFERENCES hosts(host_id))''')
self.c.execute('''CREATE TABLE backups
(date date, vm_id integer, FOREIGN KEY(vm_id) REFERENCES vms(vm_id))''')
self.c.execute('''CREATE TABLE vdis
(vdi_id integer primary key, vdi_uuid text, vdi_name text, record text, vm_id,
FOREIGN KEY(vm_id) REFERENCES vms(vm_id))''')
except Exception as e:
if "already exists" in str(e):
pass
else:
raise e
def query(self, query, params=None):
try:
if params:
self.c.execute(query, params)
else:
self.c.execute(query)
result = self.c.fetchall()
return result
except Exception as e:
print("ERROR")
print(e)
def insert(self, query, params):
try:
self.c.execute(query, params)
self.conn.commit()
except Exception as e:
print(e)
def __del__(self):
self.conn.close()
class XAPI(object):
_xapi_session = None
@classmethod
def connect(cls, disconnect_atexit=True, hostname, username, password):
if cls._xapi_session is not None:
return cls._xapi_session
ignore_ssl = True
if hostname == 'localhost':
cls._xapi_session = XenAPI.xapi_local()
username = ''
password = ''
else:
cls._xapi_session = XenAPI.Session("http://%s/" % hostname, ignore_ssl=ignore_ssl)
if not password:
password = ''
try:
cls._xapi_session.login_with_password(username, password, '1.0', 'xenserver_guest.py')
except XenAPI.Failure as f:
print("Unable to log on to XenServer at %s as %s: %s" % (hostname, username, f.details))
return cls._xapi_session