2 '''Generate map.txt for analog stats parsing''' 5 from urllib.parse
import urlparse
10 from funcy
import compose
11 from funcy
import partial
13 LOGGER = logging.getLogger(__name__)
14 LOGGER.setLevel(logging.DEBUG)
15 LOGGER.addHandler(logging.StreamHandler())
17 DATABASE_HOST = os.environ[
'DATABASE_HOST']
18 DATABASE_PORT = os.environ[
'DATABASE_PORT']
19 DATABASE_NAME = os.environ[
'DATABASE_NAME']
20 DATABASE_USER = os.environ[
'DATABASE_USER']
21 DATABASE_PASSWORD = os.environ[
'DATABASE_PASSWORD']
23 DB_URL =
'postgresql://%s:%s@%s:%s/%s' % (DATABASE_USER,
29 MAP_DIR = os.environ.get(
'ANALOG_MAP_DIR',
'/home/httpd/homecu/stats')
33 '''Query DB and create analog map.txt''' 34 if not os.path.exists(MAP_DIR):
36 map_filename = os.path.join(MAP_DIR,
'map.txt')
38 compose(partial(__write_analog_map_rows__, map_filename),
39 __query_cu_info__)(conn)
43 '''Create Database connection''' 44 return psycopg2.connect(DB_URL)
48 '''Query cuinfo for username and urls''' 50 with conn.cursor()
as cur:
51 cur.execute(
'SELECT user_name, web_host FROM cuinfo;')
56 '''Write map.txt with provided rows''' 57 with open(filename,
'w')
as map_file:
58 for (user_name, web_host)
in rows:
59 map_file.write(
'%s:%s %s' % (user_name.upper(),
61 urlparse(web_host).hostname))
63 map_file.write(os.linesep)
66 if __name__ ==
'__main__':
67 UID = pwd.getpwnam(
'www-data').pw_uid
69 LOGGER.info(
"Starting analog map.txt generation ...")
def __write_analog_map_rows__(filename, rows)
def __query_cu_info__(conn)