forked from adrianneg/infoset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
239 lines (187 loc) · 5.93 KB
/
setup.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python3
"""Infoset ORM classes.
Manages connection pooling among other things.
"""
# Main python libraries
import socket
from pathlib import Path
# Pip3 libraries
from sqlalchemy import create_engine
# Infoset libraries
try:
from infoset.utils import log
except:
print('You need to set your PYTHONPATH to include the infoset library')
sys.exit(2)
from infoset.utils import jm_configuration
from infoset.utils import jm_general
from infoset.metadata import metadata
import infoset.utils
from infoset.db.db_orm import BASE, Agent, Department, Host, BillType
from infoset.db.db_orm import Configuration, HostAgent
from infoset.db import DBURL
from infoset.db import db_agent
from infoset.db import db_configuration
from infoset.db import db_billtype
from infoset.db import db_department
from infoset.db import db_host
from infoset.db import db_hostagent
from infoset.db import db
from infoset.agents import agent
def insert_department():
"""Insert first department in the database.
Args:
None
Returns:
None
"""
# Initialize key variables
if db_department.idx_exists(1) is False:
record = Department(
code=jm_general.encode('_SYSTEM_RESERVED_'),
name=jm_general.encode('_SYSTEM_RESERVED_'))
database = db.Database()
database.add(record, 1102)
def insert_billtype():
"""Insert first billtype in the database.
Args:
None
Returns:
None
"""
# Initialize key variables
if db_billtype.idx_exists(1) is False:
record = BillType(
code=jm_general.encode('_SYSTEM_RESERVED_'),
name=jm_general.encode('_SYSTEM_RESERVED_'))
database = db.Database()
database.add(record, 1104)
def insert_agent_host():
"""Insert first agent and host in the database.
Args:
None
Returns:
None
"""
# Initialize key variables
idx_agent = 1
idx_host = 1
agent_name = '_infoset'
# Add agent
if db_agent.idx_exists(idx_agent) is False:
record = Agent(
id=jm_general.encode('_SYSTEM_RESERVED_'),
name=jm_general.encode(agent_name))
database = db.Database()
database.add(record, 1109)
# Generate a UID
uid = agent.get_uid(agent_name)
database = db.Database()
session = database.session()
record = session.query(Agent).filter(Agent.idx == idx_agent).one()
record.id = jm_general.encode(uid)
database.commit(session, 1073)
# Add host
if db_host.idx_exists(idx_host) is False:
record = Host(
description=jm_general.encode('Infoset Server'),
hostname=jm_general.encode(socket.getfqdn()))
database = db.Database()
database.add(record, 1106)
# Add to Agent / Host table
if db_hostagent.host_agent_exists(idx_host, idx_agent) is False:
record = HostAgent(idx_host=idx_host, idx_agent=idx_agent)
database = db.Database()
database.add(record, 1107)
def insert_config():
"""Insert first config in the database.
Args:
None
Returns:
None
"""
# Initialize key variables
key_values = [('version', '0.0.0.0')]
# Cycle through all the key value pairs
for item in key_values:
key = item[0]
value = item[1]
# Check if value exists and insert if not
if db_configuration.config_key_exists(key) is False:
record = Configuration(
config_key=jm_general.encode(key),
config_value=jm_general.encode(value))
database = db.Database()
database.add(record, 1108)
def server_setup():
"""Setup server.
Args:
None
Returns:
None
"""
# Initialize key variables
use_mysql = True
pool_size = 25
max_overflow = 25
# Get configuration
config = jm_configuration.Config()
# Create DB connection pool
if use_mysql is True:
# Add MySQL to the pool
engine = create_engine(
DBURL, echo=True,
encoding='utf8',
max_overflow=max_overflow,
pool_size=pool_size, pool_recycle=3600)
# Try to create the database
print('Attempting to create database tables')
try:
sql_string = (
'ALTER DATABASE %s CHARACTER SET utf8mb4 '
'COLLATE utf8mb4_general_ci') % (config.db_name())
engine.execute(sql_string)
except:
log_message = (
'Cannot connect to database %s. '
'Verify database server is started. '
'Verify database is created. '
'Verify that the configured database authentication '
'is correct.') % (config.db_name())
log.log2die(1036, log_message)
# Apply schemas
print('Applying Schemas')
BASE.metadata.create_all(engine)
# Insert database entries
insert_agent_host()
insert_billtype()
insert_department()
insert_config()
# Try some additional statements
metadata.insert_oids()
def main():
"""Process agent data.
Args:
None
Returns:
None
"""
# Get configuration
config = jm_configuration.Config()
# Run server setup if required
if config.server() is True:
server_setup()
# Install required PIP packages
print('Installing required pip3 packages')
pip3 = infoset.utils.jm_general.search_file('pip3')
if pip3 is None:
log_message = ('Cannot find python "pip3". Please install.')
log.log2die(1052, log_message)
utils_directory = infoset.utils.__path__[0]
requirements_file = ('%s/requirements.txt') % (
Path(utils_directory).parents[1])
script_name = (
'pip3 install --user --requirement %s') % (requirements_file)
infoset.utils.jm_general.run_script(script_name)
if __name__ == '__main__':
main()