refactoring
This commit is contained in:
parent
4813152679
commit
ccd8f24adf
21 changed files with 245 additions and 155 deletions
|
@ -1,100 +1,2 @@
|
|||
# coding: utf-8
|
||||
|
||||
from flask import Flask, g, render_template, request
|
||||
from flask_babel import Babel
|
||||
from flaskext.markdown import Markdown
|
||||
|
||||
from SWSCloudCore.views import viewHomepage
|
||||
from SWSCloudCore.views.account import viewAccount
|
||||
from SWSCloudCore.views.administrator import viewAdministrator
|
||||
from SWSCloudCore.views.api import viewAPI
|
||||
from SWSCloudCore.views.containers import viewContainers
|
||||
from SWSCloudCore.views.documents import viewDocuments
|
||||
from SWSCloudCore.views.kb import viewKB
|
||||
from SWSCloudCore.views.server_api import viewServerAPI
|
||||
from SWSCloudCore.views.support import viewSupport
|
||||
from SWSCloudCore.views.tasks import viewTasks
|
||||
from SWSCloudCore.views.payments import viewPayments
|
||||
|
||||
from SWSCloudCore import models
|
||||
from SWSCloudCore.models import database
|
||||
from SWSCloudCore.settings import settings
|
||||
|
||||
app = Flask(__name__, static_folder='static', static_url_path='')
|
||||
# app.config['SERVER_NAME'] = settings.get('Application', 'SERVER_NAME')
|
||||
app.config['DEBUG'] = settings.getboolean('Application', 'DEBUG')
|
||||
app.config['SECRET_KEY'] = settings.get("Application", "SECRET_KEY")
|
||||
Markdown(app)
|
||||
babel = Babel(app)
|
||||
|
||||
|
||||
# /
|
||||
app.register_blueprint(viewHomepage)
|
||||
app.register_blueprint(viewSupport)
|
||||
app.register_blueprint(viewKB)
|
||||
app.register_blueprint(viewDocuments)
|
||||
app.register_blueprint(viewAPI)
|
||||
# /tasks
|
||||
app.register_blueprint(viewTasks)
|
||||
# /containers
|
||||
app.register_blueprint(viewContainers)
|
||||
# /id
|
||||
app.register_blueprint(viewAccount)
|
||||
# /payments
|
||||
app.register_blueprint(viewPayments)
|
||||
# /api
|
||||
app.register_blueprint(viewServerAPI)
|
||||
# /administrator
|
||||
app.register_blueprint(viewAdministrator)
|
||||
|
||||
|
||||
@app.errorhandler(404)
|
||||
def page_not_found(e):
|
||||
return render_template('errors/404.html'), 404
|
||||
|
||||
|
||||
@app.errorhandler(403)
|
||||
def page_not_found(e):
|
||||
return render_template('errors/403.html'), 403
|
||||
|
||||
|
||||
@app.errorhandler(410)
|
||||
def page_not_found(e):
|
||||
return render_template('errors/410.html'), 410
|
||||
|
||||
|
||||
@app.errorhandler(500)
|
||||
def page_not_found(e):
|
||||
print e
|
||||
return render_template('errors/500.html'), 500
|
||||
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
# app.logger.debug("db.connect")
|
||||
g.errors = {'total': 0, 'items': []}
|
||||
g.settings = dict()
|
||||
|
||||
try:
|
||||
database.connect()
|
||||
except Exception as e:
|
||||
# TODO: code to email alert
|
||||
print e
|
||||
print request.path
|
||||
# g.endpoint = request.endpoint.replace('.', '/')
|
||||
return render_template('errors/500.html'), 500
|
||||
|
||||
# извлекаем настройки и определяем их в глобальную переменную
|
||||
for setting in models.Settings.select(models.Settings.key, models.Settings.val).execute():
|
||||
g.settings[setting.key] = setting.val
|
||||
|
||||
|
||||
@app.after_request
|
||||
def after_request(response):
|
||||
# app.logger.debug("db.close")
|
||||
try:
|
||||
database.close()
|
||||
except Exception as e:
|
||||
# TODO: code to email alert
|
||||
print e
|
||||
return response
|
||||
from SWSCloudCore.application import app
|
||||
from SWSCloudCore.config import config
|
||||
|
|
100
SWSCloudCore/application.py
Normal file
100
SWSCloudCore/application.py
Normal file
|
@ -0,0 +1,100 @@
|
|||
# coding: utf-8
|
||||
|
||||
from flask import Flask, g, render_template, request
|
||||
from flask_babel import Babel
|
||||
from flaskext.markdown import Markdown
|
||||
|
||||
from SWSCloudCore.views import viewHomepage
|
||||
from SWSCloudCore.views.account import viewAccount
|
||||
from SWSCloudCore.views.administrator import viewAdministrator
|
||||
from SWSCloudCore.views.api import viewAPI
|
||||
from SWSCloudCore.views.containers import viewContainers
|
||||
from SWSCloudCore.views.documents import viewDocuments
|
||||
from SWSCloudCore.views.kb import viewKB
|
||||
from SWSCloudCore.views.server_api import viewServerAPI
|
||||
from SWSCloudCore.views.support import viewSupport
|
||||
from SWSCloudCore.views.tasks import viewTasks
|
||||
from SWSCloudCore.views.payments import viewPayments
|
||||
|
||||
from SWSCloudCore import models
|
||||
from SWSCloudCore.models import database
|
||||
from SWSCloudCore.config import config
|
||||
|
||||
app = Flask(__name__, static_folder='static', static_url_path='')
|
||||
# app.config['SERVER_NAME'] = settings.get('Application', 'SERVER_NAME')
|
||||
app.config['DEBUG'] = config.getboolean('Application', 'DEBUG')
|
||||
app.config['SECRET_KEY'] = config.get("Application", "SECRET_KEY")
|
||||
Markdown(app)
|
||||
babel = Babel(app)
|
||||
|
||||
|
||||
# /
|
||||
app.register_blueprint(viewHomepage)
|
||||
app.register_blueprint(viewSupport)
|
||||
app.register_blueprint(viewKB)
|
||||
app.register_blueprint(viewDocuments)
|
||||
app.register_blueprint(viewAPI)
|
||||
# /tasks
|
||||
app.register_blueprint(viewTasks)
|
||||
# /containers
|
||||
app.register_blueprint(viewContainers)
|
||||
# /id
|
||||
app.register_blueprint(viewAccount)
|
||||
# /payments
|
||||
app.register_blueprint(viewPayments)
|
||||
# /api
|
||||
app.register_blueprint(viewServerAPI)
|
||||
# /administrator
|
||||
app.register_blueprint(viewAdministrator)
|
||||
|
||||
|
||||
@app.errorhandler(404)
|
||||
def page_not_found(e):
|
||||
return render_template('errors/404.html'), 404
|
||||
|
||||
|
||||
@app.errorhandler(403)
|
||||
def page_not_found(e):
|
||||
return render_template('errors/403.html'), 403
|
||||
|
||||
|
||||
@app.errorhandler(410)
|
||||
def page_not_found(e):
|
||||
return render_template('errors/410.html'), 410
|
||||
|
||||
|
||||
@app.errorhandler(500)
|
||||
def page_not_found(e):
|
||||
print e
|
||||
return render_template('errors/500.html'), 500
|
||||
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
# app.logger.debug("db.connect")
|
||||
g.errors = {'total': 0, 'items': []}
|
||||
g.settings = dict()
|
||||
|
||||
try:
|
||||
database.connect()
|
||||
except Exception as e:
|
||||
# TODO: code to email alert
|
||||
print e
|
||||
print request.path
|
||||
# g.endpoint = request.endpoint.replace('.', '/')
|
||||
return render_template('errors/500.html'), 500
|
||||
|
||||
# извлекаем настройки и определяем их в глобальную переменную
|
||||
for setting in models.Settings.select(models.Settings.key, models.Settings.val).execute():
|
||||
g.settings[setting.key] = setting.val
|
||||
|
||||
|
||||
@app.after_request
|
||||
def after_request(response):
|
||||
# app.logger.debug("db.close")
|
||||
try:
|
||||
database.close()
|
||||
except Exception as e:
|
||||
# TODO: code to email alert
|
||||
print e
|
||||
return response
|
33
SWSCloudCore/config.py
Normal file
33
SWSCloudCore/config.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
# coding: utf-8
|
||||
|
||||
import sys
|
||||
import os
|
||||
import ConfigParser
|
||||
|
||||
__config_file__ = os.getenv('CLOUD_CONFIG_FILE', '/etc/sws/cloud/core.ini')
|
||||
|
||||
# setting file read
|
||||
config = ConfigParser.ConfigParser()
|
||||
if os.path.exists(__config_file__):
|
||||
config.read(__config_file__)
|
||||
|
||||
if not config.has_section('Application'):
|
||||
sys.exit(1)
|
||||
if not config.has_option('Application', 'DEBUG'):
|
||||
sys.exit(1)
|
||||
if not config.has_option('Application', 'SECRET_KEY'):
|
||||
sys.exit(1)
|
||||
if not config.has_section('Database'):
|
||||
sys.exit(1)
|
||||
if not config.has_option('Database', 'name'):
|
||||
sys.exit(1)
|
||||
if not config.has_option('Database', 'host'):
|
||||
sys.exit(1)
|
||||
if not config.has_option('Database', 'port'):
|
||||
sys.exit(1)
|
||||
if not config.has_option('Database', 'user'):
|
||||
sys.exit(1)
|
||||
if not config.has_option('Database', 'password'):
|
||||
sys.exit(1)
|
||||
else:
|
||||
sys.exit('config file not found: %s' % __config_file__)
|
|
@ -1,8 +1,38 @@
|
|||
# coding: utf-8
|
||||
|
||||
__author__ = 'vanzhiganov'
|
||||
from SWSCloudCore.models import Settings
|
||||
|
||||
|
||||
class ControllerSettings:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def get(self, key=''):
|
||||
x = Settings.select().where(Settings.key == key)
|
||||
return x.get().val
|
||||
|
||||
def set(self, key, val):
|
||||
if self.exists(key):
|
||||
# update
|
||||
return True
|
||||
# insert
|
||||
return True
|
||||
|
||||
def create(self, key, val):
|
||||
x = Settings.create(key=key, val=val)
|
||||
return x.id
|
||||
|
||||
def update(self, key, val):
|
||||
x = Settings.update(val=val).where(Settings.key == key)
|
||||
x.execute()
|
||||
return True
|
||||
|
||||
def delete(self, key):
|
||||
x = Settings.delete().where(Settings.key == key)
|
||||
x.execute()
|
||||
return True
|
||||
|
||||
def exists(self, key):
|
||||
if Settings.select().where(Settings.key == key).count() == 0:
|
||||
return False
|
||||
return True
|
||||
|
|
|
@ -12,4 +12,4 @@ class ControllerManageSettings:
|
|||
return None
|
||||
|
||||
def delete(self):
|
||||
return None
|
||||
return None
|
||||
|
|
|
@ -6,15 +6,15 @@ from peewee import DateTimeField, TextField, FloatField, BigIntegerField
|
|||
from peewee import Model, UUIDField, CharField, ForeignKeyField, IntegerField
|
||||
from peewee import PostgresqlDatabase
|
||||
|
||||
from SWSCloudCore.settings import settings
|
||||
from SWSCloudCore.config import config
|
||||
|
||||
# connect to database
|
||||
database = PostgresqlDatabase(
|
||||
settings.get('Database', 'name'),
|
||||
host=settings.get('Database', 'host'),
|
||||
port=settings.getint('Database', 'port'),
|
||||
user=settings.get('Database', 'user'),
|
||||
password=settings.get('Database', 'password'),
|
||||
config.get('Database', 'name'),
|
||||
host=config.get('Database', 'host'),
|
||||
port=config.getint('Database', 'port'),
|
||||
user=config.get('Database', 'user'),
|
||||
password=config.get('Database', 'password'),
|
||||
threadlocals=True,
|
||||
fields={'uuid': 'uuid'}
|
||||
)
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
# coding: utf-8
|
||||
|
||||
import sys
|
||||
import os
|
||||
import ConfigParser
|
||||
|
||||
default_file = '/etc/sws/cloud/settings.ini'
|
||||
# default_file = '%s/gocloud.ini' % os.getenv('HOME')
|
||||
|
||||
settings_file = os.getenv('CLOUDNS_SETTINGS_FILE', default_file)
|
||||
|
||||
# current_path = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# setting file read
|
||||
settings = ConfigParser.ConfigParser()
|
||||
if os.path.exists(settings_file):
|
||||
settings.read(settings_file)
|
||||
else:
|
||||
sys.exit('settings file not found: %s' % settings_file)
|
|
@ -10,7 +10,7 @@ from SWSCloudCore import models
|
|||
"""
|
||||
Утилита добавления администратора
|
||||
Как использовать:
|
||||
swscloud-admin-add.py --email admin@cloud.com --password qwerty
|
||||
cloud-admin-add.py --email admin@cloud.com --password qwerty
|
||||
"""
|
||||
|
||||
parser = argparse.ArgumentParser(description='')
|
|
@ -1,27 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
from SWSCloudCore import models
|
||||
|
||||
# TODO: connect to database
|
||||
|
||||
models.database.create_tables({
|
||||
models.Admins,
|
||||
tables = [
|
||||
models.DataCenters,
|
||||
models.Servers,
|
||||
models.ServersSettings,
|
||||
models.Ips,
|
||||
models.Settings,
|
||||
models.Users,
|
||||
models.UsersBalance,
|
||||
models.UsersBalanceTransactions,
|
||||
models.UsersDetails,
|
||||
models.UsersRecoveryCodes,
|
||||
models.UsersSecrets,
|
||||
models.UsersDetails,
|
||||
models.UsersBalance,
|
||||
models.UsersBalanceTransactions,
|
||||
models.SSHKeys,
|
||||
models.Containers,
|
||||
models.ContainersStatistics,
|
||||
models.ContainersStatisticsState,
|
||||
models.Tasks,
|
||||
}, safe=True)
|
||||
models.Settings,
|
||||
models.Admins,
|
||||
models.Notifications,
|
||||
models.NotificationsSecurity,
|
||||
]
|
||||
|
||||
# TODO: close connect with database
|
||||
models.database.connect()
|
||||
models.database.create_tables(tables, safe=True)
|
||||
models.database.close()
|
45
cloud-settings.py
Normal file
45
cloud-settings.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/bash
|
||||
# coding: utf-8
|
||||
|
||||
import argparse
|
||||
from SWSCloudCore.controllers.settings import ControllerSettings
|
||||
|
||||
|
||||
class SettingsCli(object):
|
||||
def __init__(self):
|
||||
self.parser = argparse.ArgumentParser(description='Process some integers.')
|
||||
self.parser.add_argument('--key', metavar='k', type=str, default='', help='settings key')
|
||||
self.parser.add_argument('--value', metavar='v', type=str, default='', help='settings value')
|
||||
# get, set, delete
|
||||
self.parser.add_argument('--action', metavar='a', type=str, help='action to invoke (get is default)')
|
||||
self.controller = ControllerSettings()
|
||||
|
||||
def run(self):
|
||||
args = self.parser.parse_args()
|
||||
if args.action:
|
||||
action = args.action.lower()
|
||||
if hasattr(self, action):
|
||||
# invoke method if it exists in self
|
||||
return getattr(self, action)(args.key, args.value)
|
||||
# unsupported action
|
||||
else:
|
||||
self.parser.print_help()
|
||||
|
||||
def get(self, key, value=None):
|
||||
key = self.controller.get(key)
|
||||
return 1 if not key else key
|
||||
|
||||
def set(self, key, value):
|
||||
if self.controller.exists(key):
|
||||
code = int(self.controller.update(key, value))
|
||||
else:
|
||||
code = int(self.controller.create(key, value))
|
||||
return code
|
||||
|
||||
def delete(self, key, value=None):
|
||||
return int(self.controller.delete(key))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
x = SettingsCli().run()
|
||||
exit(x)
|
24
setup.py
24
setup.py
|
@ -4,7 +4,7 @@ from setuptools import setup
|
|||
|
||||
setup(
|
||||
name='SWSCloudCore',
|
||||
version='2.1.6',
|
||||
version='2.1.7',
|
||||
author='Vyacheslav Anzhiganov',
|
||||
author_email='hello@anzhiganov.com',
|
||||
packages=[
|
||||
|
@ -77,21 +77,23 @@ setup(
|
|||
]
|
||||
},
|
||||
scripts=[
|
||||
'exec/swscloud-db-init.py',
|
||||
'exec/swscloud-settings-init.py',
|
||||
'exec/swscloud-runserver.py',
|
||||
'cloud-db-init.py',
|
||||
'cloud-settings.py',
|
||||
#
|
||||
'exec/cloud-settings-init.py',
|
||||
'exec/cloud-runserver.py',
|
||||
# accounts
|
||||
'exec/swscloud-admin-add.py',
|
||||
'exec/cloud-admin-add.py',
|
||||
# TODO: make it
|
||||
# 'exec/swscloud-admin-delete.py',
|
||||
'exec/swscloud-admin-ls.py',
|
||||
'exec/swscloud-admin-passwd.py',
|
||||
'exec/cloud-admin-ls.py',
|
||||
'exec/cloud-admin-password.py',
|
||||
# datacenters
|
||||
'exec/swscloud-dc-add.py',
|
||||
'exec/swscloud-dc-ls.py',
|
||||
'cloud-dc-add.py',
|
||||
'exec/cloud-dc-ls.py',
|
||||
# servers
|
||||
'exec/swscloud-server-add.py',
|
||||
'exec/swscloud-server-ls.py',
|
||||
'exec/cloud-server-add.py',
|
||||
'exec/cloud-server-ls.py',
|
||||
],
|
||||
install_requires=[
|
||||
'Flask==0.10',
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
from SWSCloudCore import app
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
Loading…
Add table
Reference in a new issue