Skip to content

Commit 1243b15

Browse files
committed
Updating to read from json environment
Updating to be able to save results to /tmp (configurable)
1 parent a17534d commit 1243b15

File tree

6 files changed

+49
-53
lines changed

6 files changed

+49
-53
lines changed

.env.sample

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,2 @@
1-
# Fill in your values for user, dbname and password!
2-
DSN_ucdm=dbname=entity_store host=192.168.0.1 user=user_readonly password=user_password connect_timeout=5
3-
DSN_udw=dbname=entity_store host=192.168.0.1 user=user_readonly password=user_password connect_timeout=5
4-
# This is the date on your SIS files. This is filledi n the {date} in the 'sis_file' value in dbqueries.py
5-
SIS_DATE=2018-09-03
6-
#Select tables to read from
7-
SELECT_TABLES=person
8-
# Test this with python -m smtpd -n -c DebuggingServer localhost:1025
9-
#Host name of SMTP to send mail
10-
SMTP_HOST=localhost
11-
#Port for SMTP
12-
SMTP_PORT=1025
13-
#From address of SMTP for mail
14-
SMTP_FROM=
15-
#To address of SMTP for mail
16-
SMTP_TO=
17-
# The schedule for crontab
18-
CRONTAB_SCHEDULE=* * * * *
1+
# Location to env.json file
2+
ENV_FILE=/unizin-csv-validation/env.json

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.csv
22
.env
3+
env.json

dbqueries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@
371371
"""},
372372
'unizin_metadata' : {
373373
'index' : '',
374-
'sis_file' : 'unizin_metadata.csv',
374+
'sis_file' : 'metadata.csv',
375375
'dsn' : 'udw',
376376
'query_name': 'Unizin Metadata',
377377
'query' : """

env_sample.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"/* Fill in your values for user, dbname and password!": "*/",
3+
"DSN_ucdm": "dbname=entity_store host=192.168.0.1 user=user_readonly password=user_password connect_timeout=5",
4+
"DSN_udw": "dbname=entity_store host=192.168.0.1 user=user_readonly password=user_password connect_timeout=5",
5+
"/* This is the date on your SIS files. This is filledi n the {date} in the 'sis_file' value in dbqueries.py": "*/",
6+
"SIS_DATE": "2018-09-03",
7+
"/* Select tables to read from": "*/",
8+
"SELECT_TABLES": "person",
9+
"/* Test this with python -m smtpd -n -c DebuggingServer localhost:1025": "*/",
10+
"/* Host name of SMTP to send mail": "*/",
11+
"SMTP_HOST": "localhost",
12+
"/* Port for SMTP": "*/",
13+
"SMTP_PORT": 1025,
14+
"/* From address of SMTP for mail": "*/",
15+
"SMTP_FROM": "",
16+
"/* To address of SMTP for mail": "*/",
17+
"SMTP_TO": ""
18+
}

start.sh

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,4 @@
22

33
env
44

5-
if [ -z "${CRONTAB_SCHEDULE}" ]; then
6-
echo "CRONTAB_SCHEDULE environment variable not set, crontab cannot be started. Please set this to a crontab acceptable format. Just running command."
7-
python /unizin-csv-validation/validate.py -o 5
8-
else
9-
# in cron pod
10-
echo Running cron job pod
11-
echo "CRONTAB_SCHEDULE is ${CRONTAB_SCHEDULE}, RUN_AT_TIMES is ${RUN_AT_TIMES}"
12-
13-
# Make the log file available
14-
touch /var/log/cron.log
15-
16-
# Get the environment from docker saved
17-
# https://ypereirareis.github.io/blog/2016/02/29/docker-crontab-environment-variables/
18-
printenv | sed 's/^\([a-zA-Z0-9_]*\)=\(.*\)$/export \1="\2"/g' >> $HOME/.profile
19-
20-
echo "${CRONTAB_SCHEDULE} . $HOME/.profile; python /unizin-csv-validation/validate.py -o 5 >> /var/log/cron.log 2>&1" | crontab
21-
crontab -l && cron -L 15 && tail -f /var/log/cron.log
22-
fi
5+
python /unizin-csv-validation/validate.py -o 5

validate.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
#
33
# Copyright (C) 2018 University of Michigan Teaching and Learning
44

5-
UNIZIN_FILE = "unizin_{table}.csv"
6-
7-
RESULTS_FILE = open("u_results.txt", "w")
8-
ERRORS_FILE = open("u_errors.txt", "w")
5+
UNIZIN_FILE_FORMAT = "unizin_{table}.csv"
96

107
## don't modify anything below this line (except for experimenting)
118

12-
import sys, os, csv, itertools, argparse, smtplib
9+
import sys, os, csv, itertools, argparse, smtplib, tempfile, json
1310

1411
from email.mime.text import MIMEText
1512
from collections import OrderedDict
@@ -28,8 +25,18 @@
2825

2926
from tqdm import tqdm
3027

31-
from dotenv import load_dotenv
32-
load_dotenv()
28+
29+
try:
30+
with open(os.getenv("ENV_FILE", "/unizin-csv-validation/config/env.json")) as f:
31+
ENV = json.load(f)
32+
except FileNotFoundError as fnfe:
33+
print("Default config file or one defined in environment variable ENV_FILE not found. This is normal for the build, should define for operation")
34+
# Set ENV so collectstatic will still run in the build
35+
ENV = os.environ
36+
37+
OUT_DIR = ENV.get("TMP_DIR", "/tmp/")
38+
RESULTS_FILE = open(OUT_DIR + "u_results.txt", "w")
39+
ERRORS_FILE = open(OUT_DIR + "u_errors.txt", "w")
3340

3441
class SimpleQuoter(object):
3542
@staticmethod
@@ -63,11 +70,11 @@ def close_compare(i, j):
6370

6471
def compare_CSV(tablename):
6572
RESULTS_FILE.write(f"Comparing on {tablename}\n")
66-
sis_file = dbqueries.QUERIES[tablename]['sis_file'].format(date=os.getenv("SIS_DATE"))
73+
sis_file = dbqueries.QUERIES[tablename]['sis_file'].format(date=ENV.get("SIS_DATE"))
6774
index = dbqueries.QUERIES[tablename]['index']
6875
try:
6976
SIS_df = load_CSV_to_dict(sis_file.format(table=tablename), index)
70-
Unizin_df = load_CSV_to_dict(UNIZIN_FILE.format(table=tablename), index)
77+
Unizin_df = load_CSV_to_dict(OUT_DIR + UNIZIN_FILE_FORMAT.format(table=tablename), index)
7178
except Exception as e:
7279
print ("Exception ",e)
7380
return
@@ -118,10 +125,10 @@ def compare_CSV(tablename):
118125
continue
119126

120127
def load_Unizin_to_CSV(tablename):
121-
out_filename = UNIZIN_FILE.format(table=tablename)
128+
out_filename = OUT_DIR + UNIZIN_FILE_FORMAT.format(table=tablename)
122129
print (f"Loading ucdm {tablename} table to {out_filename}")
123130
# The DSN might switch depending on the data file
124-
conn = psycopg2.connect(os.getenv("DSN_"+dbqueries.QUERIES[tablename]['dsn']))
131+
conn = psycopg2.connect(ENV.get("DSN_"+dbqueries.QUERIES[tablename]['dsn']))
125132

126133
curs = conn.cursor()
127134

@@ -163,16 +170,16 @@ def email_results(filenames, subject=None):
163170
if canvas_date:
164171
subject = f"{subject} for {canvas_date:%B %d, %Y}"
165172
msg['Subject'] = subject
166-
msg['From'] = os.getenv("SMTP_FROM")
167-
msg['To'] = os.getenv("SMTP_TO")
173+
msg['From'] = ENV.get("SMTP_FROM")
174+
msg['To'] = ENV.get("SMTP_TO")
168175

169176
print (f"Emailing out {filename}")
170-
server = smtplib.SMTP(os.getenv("SMTP_HOST"), os.getenv("SMTP_PORT"), None, 5)
177+
server = smtplib.SMTP(ENV.get("SMTP_HOST"), ENV.get("SMTP_PORT"), None, 5)
171178
server.send_message(msg)
172179
server.quit()
173180

174181
#select_tables = ['academic_term']
175-
select_tables = list(csv.reader([os.getenv("SELECT_TABLES", "academic_term")]))[0]
182+
select_tables = list(csv.reader([ENV.get("SELECT_TABLES", "academic_term")]))[0]
176183

177184
print (select_tables)
178185

@@ -213,8 +220,11 @@ def email_results(filenames, subject=None):
213220
load_Unizin_to_CSV("number_of_courses_by_term")
214221
load_Unizin_to_CSV("unizin_metadata")
215222
subject = dbqueries.QUERIES["number_of_courses_by_term"].get('query_name')
216-
email_results([UNIZIN_FILE.format(table="unizin_metadata"),UNIZIN_FILE.format(table="number_of_courses_by_term")], subject=subject)
223+
email_results([OUT_DIR + UNIZIN_FILE_FORMAT.format(table="unizin_metadata"),OUT_DIR + UNIZIN_FILE_FORMAT.format(table="number_of_courses_by_term")], subject=subject)
217224
else:
218225
print(f"{option} is not currently a valid option")
219226

227+
RESULTS_FILE.close()
228+
ERRORS_FILE.close()
229+
220230
sys.exit(0)

0 commit comments

Comments
 (0)