-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
185 lines (159 loc) · 6.41 KB
/
tasks.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
from datetime import datetime, timedelta, timezone
import re
from invoke import task
import os
import psycopg2
from psycopg2 import sql
@task
def create_database(ctx):
db_host = os.environ.get("POSTGRES_HOST")
db_port = os.environ.get("POSTGRES_PORT")
db_name = os.environ.get("POSTGRES_DB")
db_user = os.environ.get("POSTGRES_USER")
db_password = os.environ.get("POSTGRES_PASSWORD")
# Establishing connection to PostgreSQL server
# (connect to initial database 'postgres' to create a new database)
conn = psycopg2.connect(
host=db_host,
port=db_port,
dbname="postgres",
user=db_user,
password=db_password,
)
conn.autocommit = True
cursor = conn.cursor()
# Creating the database
try:
cursor.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(db_name)))
print(f"Database '{db_name}' created successfully")
except psycopg2.errors.DuplicateDatabase:
print(f"Database '{db_name}' already exists, not creating")
cursor.close()
conn.close()
# Connect to the new database
conn = psycopg2.connect(
host=db_host, port=db_port, dbname=db_name, user=db_user, password=db_password
)
conn.autocommit = True
cursor = conn.cursor()
# Path to the SQL script relative to tasks.py
sql_script_path = "uptime_service_validation/database/create_tables.sql"
# Running the SQL script file
with open(sql_script_path, "r") as file:
sql_script = file.read()
cursor.execute(sql_script)
print("'create_tables.sql' script completed successfully")
cursor.close()
conn.close()
@task
def init_database(ctx, batch_end_epoch=None, mins_ago=None, override_empty=False):
db_host = os.environ.get("POSTGRES_HOST")
db_port = os.environ.get("POSTGRES_PORT")
db_name = os.environ.get("POSTGRES_DB")
db_user = os.environ.get("POSTGRES_USER")
db_password = os.environ.get("POSTGRES_PASSWORD")
conn = psycopg2.connect(
host=db_host, port=db_port, dbname=db_name, user=db_user, password=db_password
)
cursor = conn.cursor()
if mins_ago is not None:
batch_end_epoch = (
datetime.now(timezone.utc) - timedelta(minutes=int(mins_ago))
).timestamp()
elif batch_end_epoch is None:
batch_end_epoch = datetime.now(timezone.utc).timestamp()
else:
datetime_pattern = re.compile(r"^\d{4}-\d{2}-\d{2}.\d{2}:\d{2}:\d{2}.*$")
# Convert batch_end_epoch to a timestamp in utc if it is a datetime string
# use regex to check if it is of format 'YYYY-MM-DD HH:MM:SS'
if datetime_pattern.match(batch_end_epoch):
batch_end_epoch = datetime.fromisoformat(batch_end_epoch).timestamp()
print(f"Converted datetime string to timestamp: {batch_end_epoch}")
else:
batch_end_epoch = int(batch_end_epoch)
print(f"Using provided timestamp: {batch_end_epoch}")
# Check if the table is empty, if override_empty is False
should_insert = True
if not override_empty:
cursor.execute("SELECT COUNT(*) FROM bot_logs")
count = cursor.fetchone()[0]
should_insert = count == 0
if should_insert:
processing_time = 0
files_processed = -1 # -1 indicates that this is initialization
file_timestamps = datetime.fromtimestamp(batch_end_epoch, timezone.utc)
batch_start_epoch = batch_end_epoch
# Inserting data into the bot_logs table
cursor.execute(
"INSERT INTO bot_logs (processing_time, files_processed, file_timestamps, batch_start_epoch, batch_end_epoch) \
VALUES (%s, %s, %s, %s, %s)",
(
processing_time,
files_processed,
file_timestamps,
batch_start_epoch,
batch_end_epoch,
),
)
print(f"Row inserted into bot_logs table. batch_end_epoch: {batch_end_epoch}.")
else:
print(
"Table bot_logs is not empty. Row not inserted. You can override this by passing --override-empty."
)
conn.commit()
cursor.close()
conn.close()
@task
def create_ro_user(ctx):
db_host = os.environ.get("POSTGRES_HOST")
db_port = os.environ.get("POSTGRES_PORT")
db_name = os.environ.get("POSTGRES_DB")
db_user = os.environ.get("POSTGRES_USER")
db_password = os.environ.get("POSTGRES_PASSWORD")
db_ro_user = os.environ.get("POSTGRES_RO_USER")
db_ro_password = os.environ.get("POSTGRES_RO_PASSWORD")
conn = psycopg2.connect(
host=db_host, port=db_port, dbname=db_name, user=db_user, password=db_password
)
cursor = conn.cursor()
# Check if the user exists
user_exists = False
cursor.execute("SELECT 1 FROM pg_roles WHERE rolname=%s;", (db_ro_user,))
user_exists = cursor.fetchone() is not None
if not user_exists:
cursor.execute(sql.SQL("CREATE USER {} WITH PASSWORD %s;").format(sql.Identifier(db_ro_user)), (db_ro_password,))
cursor.execute(sql.SQL("GRANT CONNECT ON DATABASE {} TO {};").format(sql.Identifier(db_name),sql.Identifier(db_ro_user)))
cursor.execute(sql.SQL("GRANT USAGE ON SCHEMA public TO {};").format(sql.Identifier(db_ro_user)))
cursor.execute(sql.SQL("GRANT SELECT ON ALL TABLES IN SCHEMA public TO {};").format(sql.Identifier(db_ro_user)))
cursor.execute(sql.SQL("ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO {};").format(sql.Identifier(db_ro_user)))
print(f"User {db_ro_user} created")
else:
print(f"User {db_ro_user} already exists")
conn.commit()
cursor.close()
conn.close()
@task
def drop_database(ctx):
db_host = os.environ.get("POSTGRES_HOST")
db_port = os.environ.get("POSTGRES_PORT")
db_name = os.environ.get("POSTGRES_DB")
db_user = os.environ.get("POSTGRES_USER")
db_password = os.environ.get("POSTGRES_PASSWORD")
# Establishing connection to PostgreSQL server
conn = psycopg2.connect(
host=db_host,
port=db_port,
dbname="postgres",
user=db_user,
password=db_password,
)
conn.autocommit = True
cursor = conn.cursor()
# Dropping the database
try:
cursor.execute(sql.SQL("DROP DATABASE {}").format(sql.Identifier(db_name)))
print(f"Database '{db_name}' dropped!")
except Exception as e:
print(f"Error dropping database '{db_name}'! Error: {e}")
cursor.close()
conn.close()