# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import yaml
import os
import psycopg2
import sys

__author__ = 'Oscar Eriksson <oscar.eriks@gmail.com>'

dino_env = os.getenv('DINO_ENVIRONMENT') or sys.argv[1]
dino_home = os.getenv('DINO_HOME') or sys.argv[2]

if dino_home is None:
    raise RuntimeError('need environment variable DINO_HOME')
if dino_env is None:
    raise RuntimeError('need environment variable DINO_ENVIRONMENT')


def load_secrets_file(config_dict: dict) -> dict:
    from string import Template
    import ast

    secrets_path = dino_home + '/secrets/%s.yaml' % dino_env

    # first substitute environment variables, which holds precedence over the yaml config (if it exists)
    template = Template(str(config_dict))
    template = template.safe_substitute(os.environ)

    if os.path.isfile(secrets_path):
        try:
            secrets = yaml.load(open(secrets_path))
        except Exception as e:
            raise RuntimeError("Failed to open secrets configuration {0}: {1}".format(secrets_path, str(e)))
        template = Template(template)
        template = template.safe_substitute(secrets)

    return ast.literal_eval(template)


config = yaml.load(open(dino_home + '/dino.yaml'))[dino_env]
config = load_secrets_file(config)

dbname = config['database']['db']
dbhost = config['database']['host']
dbport = config['database']['port']
dbuser = config['database']['user']
dbpass = config['database']['password']

try:
    conn = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" % (dbname, dbuser, dbhost, dbpass))
except:
    raise RuntimeError('could not connect to db')

cur = conn.cursor()
cur.execute("""delete from rooms_users_association_table""")
conn.commit()

@@ -582,16 +582,29 @@
 port sys
+%0Aimport redis
 %0A%0A__auth
@@ -1766,16 +1766,259 @@
 onfig)%0A%0A
+dbtype = config%5B'database'%5D%5B'type'%5D%0A%0Aif dbtype == 'redis':%0A    r_host, r_port = config%5B'cache'%5D%5B'host'%5D.split(':')%0A    r_db = config%5B'cache'%5D%5B'db'%5D%0A    r_server = redis.Redis(host=r_host, port=r_port, db=r_db)%0A    r_server.flushdb()%0Aelse:%0A    
 dbname =
@@ -2043,16 +2043,20 @@
 %5D%5B'db'%5D%0A
+    
 dbhost =
@@ -2083,16 +2083,20 @@
 'host'%5D%0A
+    
 dbport =
@@ -2123,16 +2123,20 @@
 'port'%5D%0A
+    
 dbuser =
@@ -2163,16 +2163,20 @@
 'user'%5D%0A
+    
 dbpass =
@@ -2208,21 +2208,29 @@
 word'%5D%0A%0A
+    
 try:%0A
+    
     conn
@@ -2337,16 +2337,20 @@
 s))%0A
+    
 except:%0A
     
@@ -2345,16 +2345,20 @@
 except:%0A
+    
     rais
@@ -2400,16 +2400,20 @@
 o db')%0A%0A
+    
 cur = co
@@ -2424,16 +2424,20 @@
 ursor()%0A
+    
 cur.exec
@@ -2489,16 +2489,20 @@
 ble%22%22%22)%0A
+    
 conn.com

clear db redis online table