提交 cff18d25 编写于 作者: W willwolf

atune: refactor flask server code

上级 b9193edb
......@@ -168,3 +168,10 @@ env:
@echo "export ATUNED_CLIENTKEY=$(GRPC_CERT_PATH)/client.key" >> $(CERT_PATH)/env
@echo "export ATUNED_SERVERCN=server" >> $(CERT_PATH)/env
@echo "END SET ENVIRONMENT VARIABLES"
startup:
systemctl daemon-reload
systemctl restart atuned
systemctl restart atune-engine
run: all install startup
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Huawei Technologies Co., Ltd.
# Copyright (c) 2020 Huawei Technologies Co., Ltd.
# A-Tune is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
......@@ -9,77 +9,65 @@
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Create: 2019-10-29
# Create: 2020-08-25
"""
Application initialization, including log configuration, restful api registration.
Flask application initialization, including log configuration, restful api registration.
"""
import os
import ssl
import sys
import logging
from configparser import ConfigParser
from logging.handlers import SysLogHandler
from flask import Flask
from flask_restful import Api
sys.path.insert(0, os.path.dirname(__file__) + "/../")
from analysis.atuned import configurator, monitor, collector, profile
LOG = logging.getLogger('werkzeug')
LOG.setLevel(logging.ERROR)
APP = Flask(__name__)
API = Api(APP)
def config_log(level):
"""app config log"""
logging_format = logging.Formatter('atuned: %(asctime)s [%(levelname)s] '
'%(name)s[line:%(lineno)d] : %(message)s')
syslog_handler = SysLogHandler(address="/dev/log", facility=SysLogHandler.LOG_LOCAL0)
syslog_handler.setFormatter(logging_format)
root_logger = logging.getLogger()
root_logger.setLevel(level)
root_logger.addHandler(syslog_handler)
def main(filename):
"""app main function"""
if not os.path.exists(filename):
return
config = ConfigParser()
config.read(filename)
level = logging.getLevelName(config.get("log", "level").upper())
config_log(level)
APP.config.update(SESSION_COOKIE_SECURE=True,
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SAMESITE='Lax')
API.add_resource(configurator.Configurator, '/v1/setting', '/setting')
API.add_resource(monitor.Monitor, '/v1/monitor', '/monitor')
API.add_resource(collector.Collector, '/v1/collector', '/v1/collector')
API.add_resource(profile.Profile, '/v1/profile', '/v1/profile')
if config.has_option("server", "rest_tls") and config.get("server", "rest_tls") == "true":
cert_file = config.get("server", "tlsrestservercertfile")
key_file = config.get("server", "tlsrestserverkeyfile")
ca_file = config.get("server", "tlsrestcacertfile")
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(certfile=cert_file, keyfile=key_file)
context.load_verify_locations(ca_file)
context.verify_mode = ssl.CERT_REQUIRED
APP.run(host=config.get("server", "rest_host"), port=config.get("server", "rest_port"),
ssl_context=context)
else:
APP.run(host=config.get("server", "rest_host"), port=config.get("server", "rest_port"))
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit(-1)
main(sys.argv[1])
class App:
"""flask application"""
def __init__(self):
self.app = Flask(__name__)
self.app.config.update(SESSION_COOKIE_SECURE=True,
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SAMESITE='Lax')
self.api = Api(self.app)
@staticmethod
def config_log(level):
"""app config log"""
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
logging_format = logging.Formatter('atuned: %(asctime)s [%(levelname)s] '
'%(module)s [%(pathname)s:%(lineno)d] : %(message)s')
syslog_handler = SysLogHandler(address="/dev/log", facility=SysLogHandler.LOG_LOCAL0)
syslog_handler.setFormatter(logging_format)
root_logger = logging.getLogger()
root_logger.setLevel(level)
root_logger.addHandler(syslog_handler)
def add_resource(self):
"""flask app add resource"""
def startup_app(self, filename, host_tag, port_tag, tls_tag, cert_tag, key_tag, ca_tag):
"""start flask app"""
if not os.path.exists(filename):
return
config = ConfigParser()
config.read(filename)
level = logging.getLevelName(config.get("log", "level").upper())
self.config_log(level)
self.add_resource()
host = config.get("server", host_tag)
port = config.get("server", port_tag)
context = None
if config.has_option("server", tls_tag) and config.get("server", tls_tag) == "true":
cert_file = config.get("server", cert_tag)
key_file = config.get("server", key_tag)
ca_file = config.get("server", ca_tag)
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(certfile=cert_file, keyfile=key_file)
context.load_verify_locations(ca_file)
context.verify_mode = ssl.CERT_REQUIRED
self.app.run(host=host, port=port, ssl_context=context)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Huawei Technologies Co., Ltd.
# Copyright (c) 2020 Huawei Technologies Co., Ltd.
# A-Tune is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
......@@ -12,70 +12,37 @@
# Create: 2020-07-17
"""
Engine application initialization, including log configuration, restful api registration.
Engine application implementation.
"""
import os
import ssl
import sys
import logging
from configparser import ConfigParser
from logging import handlers
from flask import Flask
from flask_restful import Api
from app import App
sys.path.insert(0, os.path.dirname(__file__) + "/../")
from analysis.engine import optimizer, classification, train, transfer
LOG = logging.getLogger('werkzeug')
LOG.setLevel(logging.ERROR)
APP = Flask(__name__)
API = Api(APP)
def config_log(level):
"""app config log"""
logging_format = logging.Formatter('atuned: %(asctime)s [%(levelname)s] '
'%(name)s[line:%(lineno)d] : %(message)s')
syslog_handler = handlers.SysLogHandler(address="/dev/log")
syslog_handler.setFormatter(logging_format)
class AppEngine(App):
"""app engine"""
root_logger = logging.getLogger()
root_logger.setLevel(level)
root_logger.addHandler(syslog_handler)
def add_resource(self):
"""flask app add resource"""
self.api.add_resource(optimizer.Optimizer, '/v1/optimizer',
'/v1/optimizer/<string:task_id>')
self.api.add_resource(classification.Classification, '/v1/classification',
'/v1/classification')
self.api.add_resource(train.Training, '/v1/training', '/v1/training')
self.api.add_resource(transfer.Transfer, '/v1/transfer', '/transfer')
def main(filename):
"""app main function"""
if not os.path.exists(filename):
return
config = ConfigParser()
config.read(filename)
level = logging.getLevelName(config.get("log", "level").upper())
config_log(level)
APP.config.update(SESSION_COOKIE_SECURE=True,
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SAMESITE='Lax')
API.add_resource(optimizer.Optimizer, '/v1/optimizer', '/v1/optimizer/<string:task_id>')
API.add_resource(classification.Classification, '/v1/classification', '/v1/classification')
API.add_resource(train.Training, '/v1/training', '/v1/training')
API.add_resource(transfer.Transfer, '/v1/transfer', '/transfer')
if config.has_option("server", "engine_tls") and config.get("server", "engine_tls") == "true":
cert_file = config.get("server", "tlsengineservercertfile")
key_file = config.get("server", "tlsengineserverkeyfile")
ca_file = config.get("server", "tlsenginecacertfile")
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(certfile=cert_file, keyfile=key_file)
context.load_verify_locations(ca_file)
context.verify_mode = ssl.CERT_REQUIRED
APP.run(host=config.get("server", "engine_host"), port=config.get("server", "engine_port"),
ssl_context=context)
else:
APP.run(host=config.get("server", "engine_host"), port=config.get("server", "engine_port"))
app_engine = AppEngine()
app_engine.startup_app(filename, "engine_host", "engine_port", "engine_tls",
"tlsengineservercertfile", "tlsengineserverkeyfile",
"tlsenginecacertfile")
if __name__ == '__main__':
......
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Huawei Technologies Co., Ltd.
# A-Tune is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Create: 2019-10-29
"""
Rest application implementation.
"""
import os
import sys
from app import App
sys.path.insert(0, os.path.dirname(__file__) + "/../")
from analysis.atuned import configurator, monitor, collector, profile
class AppRest(App):
"""app rest"""
def add_resource(self):
"""flask app add resource"""
self.api.add_resource(configurator.Configurator, '/v1/setting', '/setting')
self.api.add_resource(monitor.Monitor, '/v1/monitor', '/monitor')
self.api.add_resource(collector.Collector, '/v1/collector', '/v1/collector')
self.api.add_resource(profile.Profile, '/v1/profile', '/v1/profile')
def main(filename):
"""app main function"""
app_engine = AppRest()
app_engine.startup_app(filename, "rest_host", "rest_port", "rest_tls",
"tlsrestservercertfile", "tlsrestserverkeyfile", "tlsrestcacertfile")
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit(-1)
main(sys.argv[1])
......@@ -50,7 +50,7 @@ func (p *PyEngine) Set(cfg *config.Cfg) {
func (p *PyEngine) Run() error {
cmdSlice := make([]string, 0)
cmdSlice = append(cmdSlice, "python3")
cmdSlice = append(cmdSlice, path.Join(config.DefaultAnalysisPath, "app.py"))
cmdSlice = append(cmdSlice, path.Join(config.DefaultAnalysisPath, "app_rest.py"))
cmdSlice = append(cmdSlice, path.Join(config.DefaultConfPath, "atuned.cnf"))
cmdStr := strings.Join(cmdSlice, " ")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册