summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.py24
-rw-r--r--opendc/models/model.py2
-rw-r--r--opendc/util/database.py30
-rw-r--r--setup.py6
4 files changed, 37 insertions, 25 deletions
diff --git a/main.py b/main.py
index 22a5e772..310abdc5 100644
--- a/main.py
+++ b/main.py
@@ -5,7 +5,7 @@ import json
import os
import sys
import traceback
-import urllib2
+import urllib
from flask import Flask, request, send_from_directory, jsonify
from flask_compress import Compress
from oauth2client import client, crypt
@@ -15,7 +15,7 @@ from opendc.models.user import User
from opendc.util import exceptions, rest, path_parser, database
if len(sys.argv) < 2:
- print "config file path not given as argument"
+ print("config file path not given as argument")
sys.exit(1)
# Get keys from config file
@@ -24,8 +24,8 @@ with open(sys.argv[1]) as f:
STATIC_ROOT = os.path.join(KEYS['ROOT_DIR'], 'opendc-frontend', 'build')
-database.init_connection_pool(user=KEYS['MYSQL_USER'], password=KEYS['MYSQL_PASSWORD'],
- database=KEYS['MYSQL_DATABASE'], host=KEYS['MYSQL_HOST'], port=KEYS['MYSQL_PORT'])
+database.init_connection_pool(user=KEYS['MONGODB_USER'], password=KEYS['MONGODB_PASSWORD'],
+ database=KEYS['MONGODB_DATABASE'], host=KEYS['MONGODB_HOST'], port=KEYS['MONGODB_PORT'])
FLASK_CORE_APP = Flask(__name__, static_url_path='', static_folder=STATIC_ROOT)
FLASK_CORE_APP.config['SECREY_KEY'] = KEYS['FLASK_SECRET']
@@ -68,7 +68,7 @@ def sign_in():
response = urllib2.urlopen(url=req, timeout=30)
res = response.read()
idinfo = json.loads(res)
- except crypt.AppIdentityError, e:
+ except crypt.AppIdentityError as e:
return 'Did not successfully authenticate'
user = User.from_google_id(idinfo['sub'])
@@ -115,12 +115,7 @@ def api_call(version, endpoint_path):
'token': request.headers.get('auth-token')
})
- print 'HTTP:\t{} to `/{}` resulted in {}: {}'.format(
- req.method,
- req.path,
- response.status['code'],
- response.status['description']
- )
+ print(f'HTTP:\t{req.method} to `/{req.path}` resulted in {response.status["code"]}: {response.status["description"]}')
sys.stdout.flush()
flask_response = jsonify(json.loads(response.to_JSON()))
@@ -151,12 +146,7 @@ def receive_message(message):
(request, response) = _process_message(message)
- print 'Socket:\t{} to `/{}` resulted in {}: {}'.format(
- request.method,
- request.path,
- response.status['code'],
- response.status['description']
- )
+ print(f'Socket:\t{request.method} to `/{request.path}` resulted in {response.status["code"]}: {response.status["description"]}')
sys.stdout.flush()
flask_socketio.emit('response', response.to_JSON(), json=True)
diff --git a/opendc/models/model.py b/opendc/models/model.py
index 44e4faf8..8ab410d4 100644
--- a/opendc/models/model.py
+++ b/opendc/models/model.py
@@ -291,7 +291,7 @@ class Model(object):
try:
last_row_id = database.execute(statement, values)
except Exception as e:
- print e
+ print(e)
raise exceptions.ForeignKeyError(e.message)
if 'id' in self.COLUMNS_PRIMARY_KEY:
diff --git a/opendc/util/database.py b/opendc/util/database.py
index ebb62bde..7ae7b82b 100644
--- a/opendc/util/database.py
+++ b/opendc/util/database.py
@@ -1,8 +1,12 @@
import json
import sys
+
from datetime import datetime
+from pymongo import MongoClient
+from bson.json_util import loads, dumps, RELAXED_JSON_OPTIONS, CANONICAL_JSON_OPTIONS
+
-from mysql.connector.pooling import MySQLConnectionPool
+#from mysql.connector.pooling import MySQLConnectionPool
# Get keys from config file
with open(sys.argv[1]) as f:
@@ -13,9 +17,27 @@ CONNECTION_POOL = None
def init_connection_pool(user, password, database, host, port):
- global CONNECTION_POOL
- CONNECTION_POOL = MySQLConnectionPool(pool_name="opendcpool", pool_size=5,
- user=user, password=password, database=database, host=host, port=port)
+ user = urllib.parse.quote_plus(user) #TODO: replace this with environment variable
+ password = urllib.parse.quote_plus(password) #TODO: same as above
+ database = urllib.parse.quote_plus(database)
+ host = urllib.parse.quote_plus(host)
+
+ global client
+ global opendcdb
+ global prefabs_collection
+ global user_collection
+ global topologies_collection
+
+ client = MongoClient('mongodb://%s:%s@%s/default_db?authSource=%s' % (user, password, host, database))
+ opendcdb = client.opendc
+ prefabs_collection = opendcdb.prefabs
+ topologies_collection = opendcdb.topologies
+ user_collection = opendcdb.users
+
+
+ #global CONNECTION_POOL
+ #CONNECTION_POOL = MySQLConnectionPool(pool_name="opendcpool", pool_size=5,
+ #user=user, password=password, database=database, host=host, port=port)
def execute(statement, t):
diff --git a/setup.py b/setup.py
index 65065414..681fe031 100644
--- a/setup.py
+++ b/setup.py
@@ -10,7 +10,7 @@ with open(path.join(here, 'README.md'), encoding='utf-8') as f:
setup(
name='opendc-web-server',
- version='0.1.0',
+ version='0.2.0',
description='Python web server for the OpenDC project',
long_description=long_description,
@@ -25,8 +25,8 @@ setup(
classifiers=[
'License :: OSI Approved :: MIT License',
- 'Programming Language :: Python :: 2',
- 'Programming Language :: Python :: 2.7',
+ 'Programming Language :: Python :: 3',
+ 'Programming Language :: Python :: 3.7',
],
keywords='opendc datacenter simulation web-server',