Project

General

Profile

Feature #11176 » sanitize_foreman.py

Python3 script to programmatically execute SQL statements (built to run against rh-python34 SCL) - Martin Jackson, 07/21/2015 10:46 AM

 
#!/usr/bin/env python3

import sys
import json
import psycopg2
import socket
import collections
import subprocess


if __name__ == '__main__':
boxname = socket.gethostname()
if boxname.startswith('oser'):
raise RuntimeError("Cowardly refusing to run on {0}".format(boxname))

whoami = subprocess.check_output('whoami').decode('utf-8').strip()
if whoami != 'postgres':
raise RuntimeError("Cowardly refusing to run as {0}".format(whoami))

cfg = json.load(
open("sanitize_foreman.json", "r"),
object_pairs_hook=collections.OrderedDict
)

db = psycopg2.connect(database='foreman')
cur = db.cursor()

for q in cfg['queries']:
print("Executing:\n{0}".format(q))
cur.execute(q)
db.commit()
(1-1/2)