diff --git a/Makefile b/Makefile index 70bbc2cfb047431035886c7714ba1cf60195e5ae..9d0129e787e1587ab6fc63b027a5c032fccb53e0 100755 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/analysis/app.py b/analysis/app.py index 6bb343694a59e33e1589eb48cfd1fbf6bfa440a6..b6cd262812fdf071a85a09d8e4ef1761c9b08ea3 100644 --- a/analysis/app.py +++ b/analysis/app.py @@ -1,6 +1,6 @@ #!/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) diff --git a/analysis/app_engine.py b/analysis/app_engine.py index c4fdc38e6ef28add0793a68423fe25520bb56200..cbfee3ba1a6bafaf86d72fee33a590f6536c6a1a 100644 --- a/analysis/app_engine.py +++ b/analysis/app_engine.py @@ -1,6 +1,6 @@ #!/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/') + 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/') - 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__': diff --git a/analysis/app_rest.py b/analysis/app_rest.py new file mode 100644 index 0000000000000000000000000000000000000000..627f6734ef1d354ae6ad5b56bdbbc4a12c09ee4e --- /dev/null +++ b/analysis/app_rest.py @@ -0,0 +1,48 @@ +#!/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]) diff --git a/common/service/pyservice/pyservice.go b/common/service/pyservice/pyservice.go index 67f2f0bfca40aca640255754f07c06f8b116a3f2..99b223994c9d417133d181f53d61c718736daee0 100644 --- a/common/service/pyservice/pyservice.go +++ b/common/service/pyservice/pyservice.go @@ -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, " ")