diff --git a/deps/2_nginx/sysom.conf b/deps/2_nginx/sysom.conf index c0d01514406d727094313188aa986369a75b463b..c504cf8d28f66436ab788ce5591ec0b12717ecc0 100644 --- a/deps/2_nginx/sysom.conf +++ b/deps/2_nginx/sysom.conf @@ -179,6 +179,23 @@ server { # 7020 resvered for cluster_health + + # 7021 resvered for offline-push + location /api/v1/offline_push/ { + proxy_pass http://127.0.0.1:7021; + proxy_read_timeout 180; + proxy_redirect off; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + # 7022 resvered for key-server + location /api/v1/key_server/ { + proxy_pass http://127.0.0.1:7022; + proxy_read_timeout 180; + proxy_redirect off; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + location /api/ { proxy_pass http://127.0.0.1:7001; proxy_read_timeout 180s; diff --git a/script/server/sysom_key_server/clear.sh b/script/server/sysom_key_server/clear.sh new file mode 100644 index 0000000000000000000000000000000000000000..87af8db16f5f8367b70cb824bad180cce1fcab58 --- /dev/null +++ b/script/server/sysom_key_server/clear.sh @@ -0,0 +1,14 @@ +#!/bin/bash +BaseDir=$(dirname $(readlink -f "$0")) +SERVICE_NAME=sysom-key_server + +clear_app() { + rm -rf /etc/supervisord.d/${SERVICE_NAME}.ini + ###use supervisorctl update to stop and clear services### + supervisorctl update +} + +# Stop first +bash -x $BaseDir/stop.sh + +clear_app diff --git a/script/server/sysom_key_server/db_migrate.sh b/script/server/sysom_key_server/db_migrate.sh new file mode 100644 index 0000000000000000000000000000000000000000..f530e1f82f0e3becd05b158b68263bc37fbd0447 --- /dev/null +++ b/script/server/sysom_key_server/db_migrate.sh @@ -0,0 +1,18 @@ +#!/bin/bash +SERVICE_SCRIPT_DIR=$(basename $(dirname $0)) +SERVICE_HOME=${MICROSERVICE_HOME}/${SERVICE_SCRIPT_DIR} +VIRTUALENV_HOME=$GLOBAL_VIRTUALENV_HOME + +source_virtualenv() { + echo "INFO: activate virtualenv..." + source ${VIRTUALENV_HOME}/bin/activate || exit 1 +} + +db_migrate() { + pushd ${SERVICE_HOME} + alembic upgrade head + popd +} + +source_virtualenv +db_migrate \ No newline at end of file diff --git a/script/server/sysom_key_server/init.sh b/script/server/sysom_key_server/init.sh new file mode 100644 index 0000000000000000000000000000000000000000..dda31e47f0df367696dde90b8d5bafa62cdf5ea2 --- /dev/null +++ b/script/server/sysom_key_server/init.sh @@ -0,0 +1,25 @@ +#!/bin/bash +SERVICE_SCRIPT_DIR=$(basename $(dirname $0)) +SERVICE_HOME=${MICROSERVICE_HOME}/${SERVICE_SCRIPT_DIR} +BaseDir=$(dirname $(readlink -f "$0")) +SERVICE_NAME=sysom-key_server + +init_conf() { + cp ${SERVICE_NAME}.ini /etc/supervisord.d/ + ###change the install dir base on param $1### + sed -i "s;/usr/local/sysom;${APP_HOME};g" /etc/supervisord.d/${SERVICE_NAME}.ini + cpu_num=$(cat /proc/cpuinfo | grep processor | wc -l) + sed -i "s/threads = 3/threads = $cpu_num/g" ${SERVICE_HOME}/conf/gunicorn.py +} + +init_app() { + init_conf + bash -x $BaseDir/db_migrate.sh + ###if supervisor service started, we need use "supervisorctl update" to start new conf#### + supervisorctl update +} + +init_app + +# Start +bash -x $BaseDir/start.sh diff --git a/script/server/sysom_key_server/install.sh b/script/server/sysom_key_server/install.sh new file mode 100644 index 0000000000000000000000000000000000000000..826c0ddd24e9dbcb6f67d09dfc654e99c37fb18d --- /dev/null +++ b/script/server/sysom_key_server/install.sh @@ -0,0 +1,29 @@ +#!/bin/bash +SERVICE_SCRIPT_DIR=$(basename $(dirname $0)) +SERVICE_HOME=${MICROSERVICE_HOME}/${SERVICE_SCRIPT_DIR} +SERVICE_SCRIPT_HOME=${MICROSERVICE_SCRIPT_HOME}/${SERVICE_SCRIPT_DIR} +VIRTUALENV_HOME=$GLOBAL_VIRTUALENV_HOME +SERVICE_NAME=sysom-key_server + +if [ "$UID" -ne 0 ]; then + echo "Please run as root" + exit 1 +fi + +install_requirement() { + pushd ${SERVICE_SCRIPT_HOME} + pip install -r requirements.txt + popd +} + +source_virtualenv() { + echo "INFO: activate virtualenv..." + source ${VIRTUALENV_HOME}/bin/activate || exit 1 +} + +install_app() { + source_virtualenv + install_requirement +} + +install_app diff --git a/script/server/sysom_key_server/requirements.txt b/script/server/sysom_key_server/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..16ff76238d72bbd45e64efbe3afce5274f67efb0 --- /dev/null +++ b/script/server/sysom_key_server/requirements.txt @@ -0,0 +1,20 @@ +clogger==0.0.1 +channel_job>=0.0.1 +cec_base>=0.0.1 +cec_redis>=0.0.1 +sysom_utils>=0.0.1 +alembic==1.7.7 +anyio==3.6.2 +asyncer==0.0.2 +asyncssh==2.12.0 +fastapi==0.83.0 +PyMySQL==1.0.2 +pyyaml==6.0 +pyyaml-include==1.3 +uvicorn==0.16.0 +gunicorn==20.1.0 +python-multipart==0.0.5 +###################################################################### +# Add your custom python requirements here +##################################################################### +pycryptodome==3.19.0 \ No newline at end of file diff --git a/script/server/sysom_key_server/start.sh b/script/server/sysom_key_server/start.sh new file mode 100644 index 0000000000000000000000000000000000000000..cdf4009fb514f1b47d4da868bb8dd9845568f59f --- /dev/null +++ b/script/server/sysom_key_server/start.sh @@ -0,0 +1,29 @@ +#!/bin/bash +SERVICE_NAME=sysom-key_server + +is_start() { + status=`supervisorctl status ${SERVICE_NAME} | awk '{print $2}'` + result=`echo "RUNNING STARTING" | grep $status` + if [[ "$result" != "" ]] + then + return 1 + else + return 0 + fi +} + +start_app() { + is_start + if [[ $? == 0 ]]; then + supervisorctl start $SERVICE_NAME + is_start + if [[ $? == 0 ]]; then + echo "${SERVICE_NAME} service start fail, please check log" + exit 1 + else + echo "supervisorctl start ${SERVICE_NAME} success..." + fi + fi +} + +start_app diff --git a/script/server/sysom_key_server/stop.sh b/script/server/sysom_key_server/stop.sh new file mode 100644 index 0000000000000000000000000000000000000000..5a423e82da907a04058ef7ac8b1e7335fef17750 --- /dev/null +++ b/script/server/sysom_key_server/stop.sh @@ -0,0 +1,8 @@ +#!/bin/bash +SERVICE_NAME=sysom-key_server + +stop_app() { + supervisorctl stop $SERVICE_NAME +} + +stop_app diff --git a/script/server/sysom_key_server/sysom-key_server.ini b/script/server/sysom_key_server/sysom-key_server.ini new file mode 100644 index 0000000000000000000000000000000000000000..dd5edf3ab9d35c5912257e92b16eb8326bcc6970 --- /dev/null +++ b/script/server/sysom_key_server/sysom-key_server.ini @@ -0,0 +1,9 @@ +[program:sysom-key_server] +directory=/usr/local/sysom/server/sysom_key_server +command=/usr/local/sysom/environment/virtualenv/bin/gunicorn -c ./conf/gunicorn.py main:app +startsecs=3 +autostart=true +autorestart=true +environment=PATH=/usr/local/sysom/virtualenv/bin:%(ENV_PATH)s +stderr_logfile=/var/log/sysom/sysom-key_server-error.log +stdout_logfile=/var/log/sysom/sysom-key_server.log diff --git a/script/server/sysom_key_server/uninstall.sh b/script/server/sysom_key_server/uninstall.sh new file mode 100644 index 0000000000000000000000000000000000000000..0aac04c09286c7f24e98d8a20ce131cdbec36375 --- /dev/null +++ b/script/server/sysom_key_server/uninstall.sh @@ -0,0 +1,9 @@ +#!/bin/bash +BaseDir=$(dirname $(readlink -f "$0")) + +uninstall_app() { + # do nothing + echo "" +} + +uninstall_app \ No newline at end of file diff --git a/script/server/sysom_offline_push/clear.sh b/script/server/sysom_offline_push/clear.sh new file mode 100644 index 0000000000000000000000000000000000000000..5fbc000dd4b0a14bc3738adab7c62459b0c7a8c1 --- /dev/null +++ b/script/server/sysom_offline_push/clear.sh @@ -0,0 +1,14 @@ +#!/bin/bash +BaseDir=$(dirname $(readlink -f "$0")) +SERVICE_NAME=sysom-offline_push + +clear_app() { + rm -rf /etc/supervisord.d/${SERVICE_NAME}.ini + ###use supervisorctl update to stop and clear services### + supervisorctl update +} + +# Stop first +bash -x $BaseDir/stop.sh + +clear_app diff --git a/script/server/sysom_offline_push/db_migrate.sh b/script/server/sysom_offline_push/db_migrate.sh new file mode 100644 index 0000000000000000000000000000000000000000..f530e1f82f0e3becd05b158b68263bc37fbd0447 --- /dev/null +++ b/script/server/sysom_offline_push/db_migrate.sh @@ -0,0 +1,18 @@ +#!/bin/bash +SERVICE_SCRIPT_DIR=$(basename $(dirname $0)) +SERVICE_HOME=${MICROSERVICE_HOME}/${SERVICE_SCRIPT_DIR} +VIRTUALENV_HOME=$GLOBAL_VIRTUALENV_HOME + +source_virtualenv() { + echo "INFO: activate virtualenv..." + source ${VIRTUALENV_HOME}/bin/activate || exit 1 +} + +db_migrate() { + pushd ${SERVICE_HOME} + alembic upgrade head + popd +} + +source_virtualenv +db_migrate \ No newline at end of file diff --git a/script/server/sysom_offline_push/init.sh b/script/server/sysom_offline_push/init.sh new file mode 100644 index 0000000000000000000000000000000000000000..c2a8cea52144693eff0b4da5dca6f1624e3df66c --- /dev/null +++ b/script/server/sysom_offline_push/init.sh @@ -0,0 +1,25 @@ +#!/bin/bash +SERVICE_SCRIPT_DIR=$(basename $(dirname $0)) +SERVICE_HOME=${MICROSERVICE_HOME}/${SERVICE_SCRIPT_DIR} +BaseDir=$(dirname $(readlink -f "$0")) +SERVICE_NAME=sysom-offline_push + +init_conf() { + cp ${SERVICE_NAME}.ini /etc/supervisord.d/ + ###change the install dir base on param $1### + sed -i "s;/usr/local/sysom;${APP_HOME};g" /etc/supervisord.d/${SERVICE_NAME}.ini + cpu_num=$(cat /proc/cpuinfo | grep processor | wc -l) + sed -i "s/threads = 3/threads = $cpu_num/g" ${SERVICE_HOME}/conf/gunicorn.py +} + +init_app() { + init_conf + bash -x $BaseDir/db_migrate.sh + ###if supervisor service started, we need use "supervisorctl update" to start new conf#### + supervisorctl update +} + +init_app + +# Start +bash -x $BaseDir/start.sh diff --git a/script/server/sysom_offline_push/install.sh b/script/server/sysom_offline_push/install.sh new file mode 100644 index 0000000000000000000000000000000000000000..77f16e8c0d81d7f02361d9fa765cb98f125256fe --- /dev/null +++ b/script/server/sysom_offline_push/install.sh @@ -0,0 +1,29 @@ +#!/bin/bash +SERVICE_SCRIPT_DIR=$(basename $(dirname $0)) +SERVICE_HOME=${MICROSERVICE_HOME}/${SERVICE_SCRIPT_DIR} +SERVICE_SCRIPT_HOME=${MICROSERVICE_SCRIPT_HOME}/${SERVICE_SCRIPT_DIR} +VIRTUALENV_HOME=$GLOBAL_VIRTUALENV_HOME +SERVICE_NAME=sysom-offline_push + +if [ "$UID" -ne 0 ]; then + echo "Please run as root" + exit 1 +fi + +install_requirement() { + pushd ${SERVICE_SCRIPT_HOME} + pip install -r requirements.txt + popd +} + +source_virtualenv() { + echo "INFO: activate virtualenv..." + source ${VIRTUALENV_HOME}/bin/activate || exit 1 +} + +install_app() { + source_virtualenv + install_requirement +} + +install_app diff --git a/script/server/sysom_offline_push/requirements.txt b/script/server/sysom_offline_push/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..ae727ce7f2ac932d0454321941846cd1a6ba6be6 --- /dev/null +++ b/script/server/sysom_offline_push/requirements.txt @@ -0,0 +1,21 @@ +clogger==0.0.1 +channel_job>=0.0.1 +cec_base>=0.0.1 +cec_redis>=0.0.1 +sysom_utils>=0.0.1 +alembic==1.7.7 +anyio==3.6.2 +asyncer==0.0.2 +asyncssh==2.12.0 +fastapi==0.83.0 +PyMySQL==1.0.2 +pyyaml==6.0 +pyyaml-include==1.3 +uvicorn==0.16.0 +gunicorn==20.1.0 +python-multipart==0.0.5 +###################################################################### +# Add your custom python requirements here +###################################################################### +pycryptodome==3.19.0 +aliyun-log-python-sdk==0.8.12 \ No newline at end of file diff --git a/script/server/sysom_offline_push/start.sh b/script/server/sysom_offline_push/start.sh new file mode 100644 index 0000000000000000000000000000000000000000..4aa7da559ceffdf18d344b09dd0e8a41ad88d5b7 --- /dev/null +++ b/script/server/sysom_offline_push/start.sh @@ -0,0 +1,29 @@ +#!/bin/bash +SERVICE_NAME=sysom-offline_push + +is_start() { + status=`supervisorctl status ${SERVICE_NAME} | awk '{print $2}'` + result=`echo "RUNNING STARTING" | grep $status` + if [[ "$result" != "" ]] + then + return 1 + else + return 0 + fi +} + +start_app() { + is_start + if [[ $? == 0 ]]; then + supervisorctl start $SERVICE_NAME + is_start + if [[ $? == 0 ]]; then + echo "${SERVICE_NAME} service start fail, please check log" + exit 1 + else + echo "supervisorctl start ${SERVICE_NAME} success..." + fi + fi +} + +start_app diff --git a/script/server/sysom_offline_push/stop.sh b/script/server/sysom_offline_push/stop.sh new file mode 100644 index 0000000000000000000000000000000000000000..a71c1d8b618f07d8f071ed96644bf55ec375880f --- /dev/null +++ b/script/server/sysom_offline_push/stop.sh @@ -0,0 +1,8 @@ +#!/bin/bash +SERVICE_NAME=sysom-offline_push + +stop_app() { + supervisorctl stop $SERVICE_NAME +} + +stop_app diff --git a/script/server/sysom_offline_push/sysom-offline_push.ini b/script/server/sysom_offline_push/sysom-offline_push.ini new file mode 100644 index 0000000000000000000000000000000000000000..d11932c019d549b0a4ffe0b2fa2deb87c40ba607 --- /dev/null +++ b/script/server/sysom_offline_push/sysom-offline_push.ini @@ -0,0 +1,9 @@ +[program:sysom-offline_push] +directory=/usr/local/sysom/server/sysom_offline_push +command=/usr/local/sysom/environment/virtualenv/bin/gunicorn -c ./conf/gunicorn.py main:app +startsecs=3 +autostart=true +autorestart=true +environment=PATH=/usr/local/sysom/virtualenv/bin:%(ENV_PATH)s +stderr_logfile=/var/log/sysom/sysom-offline_push-error.log +stdout_logfile=/var/log/sysom/sysom-offline_push.log diff --git a/script/server/sysom_offline_push/uninstall.sh b/script/server/sysom_offline_push/uninstall.sh new file mode 100644 index 0000000000000000000000000000000000000000..0aac04c09286c7f24e98d8a20ce131cdbec36375 --- /dev/null +++ b/script/server/sysom_offline_push/uninstall.sh @@ -0,0 +1,9 @@ +#!/bin/bash +BaseDir=$(dirname $(readlink -f "$0")) + +uninstall_app() { + # do nothing + echo "" +} + +uninstall_app \ No newline at end of file diff --git a/sysom_server/sysom_key_server/alembic.ini b/sysom_server/sysom_key_server/alembic.ini new file mode 100644 index 0000000000000000000000000000000000000000..f6ab9febcd93add9d0ea8857f857d7f30f1fe48f --- /dev/null +++ b/sysom_server/sysom_key_server/alembic.ini @@ -0,0 +1,102 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = alembic + +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python-dateutil library that can be +# installed by adding `alembic[tz]` to the pip requirements +# string value is passed to dateutil.tz.gettz() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to alembic/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +sqlalchemy.url = "" + + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/sysom_server/sysom_key_server/alembic/README b/sysom_server/sysom_key_server/alembic/README new file mode 100644 index 0000000000000000000000000000000000000000..98e4f9c44effe479ed38c66ba922e7bcc672916f --- /dev/null +++ b/sysom_server/sysom_key_server/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/sysom_server/sysom_key_server/alembic/env.py b/sysom_server/sysom_key_server/alembic/env.py new file mode 100644 index 0000000000000000000000000000000000000000..ad3427d1ae1d3cabd68a13e5197cb4b1f0c7adcb --- /dev/null +++ b/sysom_server/sysom_key_server/alembic/env.py @@ -0,0 +1,115 @@ +import inspect +import app.models as models +from logging.config import fileConfig +from sqlalchemy import engine_from_config, Table +from sqlalchemy import pool +from app.models import Base +from alembic import context +from conf.settings import YAML_CONFIG, SQLALCHEMY_DATABASE_URL + +################################################################## +# Load yaml config first +################################################################## +mysql_config = YAML_CONFIG.get_server_config().db.mysql + +################################################################## +# Scan models +################################################################## +service_tables = [] +for name, data in inspect.getmembers(models): + if inspect.isclass(data): + if data.__module__ != "app.models": + continue + if "__tablename__" in data.__dict__: + service_tables.append(data.__dict__["__tablename__"]) + elif "__table__" in data.__dict__: + service_tables.append(data.__dict__["__table__"]) + elif isinstance(data, Table): + service_tables.append(name) + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# Update mysql config according config.yml +config.set_main_option( + "sqlalchemy.url", + SQLALCHEMY_DATABASE_URL +) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + +def include_object(object, name, type_, reflected, compare_to): + if type_ == "table" and name not in service_tables: + return False + return True + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + include_object=include_object, + version_table="key_server_version", + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, target_metadata=target_metadata, + include_object=include_object, + version_table="key_server_version" + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/sysom_server/sysom_key_server/alembic/script.py.mako b/sysom_server/sysom_key_server/alembic/script.py.mako new file mode 100644 index 0000000000000000000000000000000000000000..2c0156303a8df3ffdc9de87765bf801bf6bea4a5 --- /dev/null +++ b/sysom_server/sysom_key_server/alembic/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/sysom_server/sysom_key_server/alembic/versions/.gitkeep b/sysom_server/sysom_key_server/alembic/versions/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/sysom_server/sysom_key_server/app/__init__.py b/sysom_server/sysom_key_server/app/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..fa55a3b99b108af4e7ce3257af1b0c5c30196868 --- /dev/null +++ b/sysom_server/sysom_key_server/app/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File __init__.py +Description: +""" \ No newline at end of file diff --git a/sysom_server/sysom_key_server/app/crud.py b/sysom_server/sysom_key_server/app/crud.py new file mode 100644 index 0000000000000000000000000000000000000000..1bdc78f152db90e018a90a8c3cf850df226f00ac --- /dev/null +++ b/sysom_server/sysom_key_server/app/crud.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File crud.py +Description: +""" +from typing import Optional, List +from sqlalchemy.orm import Session +from app import models, schemas, query + +################################################################################################ +# Define database crud here +################################################################################################ + +# def get_person_by_name(db: Session, name: str) -> Optional[models.Person]: +# return db.query(models.Person).filter(models.Person.name == name).first() + +# def create_person(db: Session, person: schemas.Person) -> models.Person: +# person = models.Person(**person.dict()) +# db.add(person) +# db.commit() +# db.refresh(person) +# return person + +# def del_person_by_id(db: Session, person_id: int): +# person = db.get(models.Person, person_id) +# db.delete(person) +# db.commit() + +# def get_person_list(db: Session, query_params: query.PersonQueryParams) -> List[models.Person]: +# return ( +# query_params.get_query_exp(db) +# .all() +# ) diff --git a/sysom_server/sysom_key_server/app/database.py b/sysom_server/sysom_key_server/app/database.py new file mode 100644 index 0000000000000000000000000000000000000000..ea519ec54c013eb7ffe0ffa8c39d56ce5cf8d46c --- /dev/null +++ b/sysom_server/sysom_key_server/app/database.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File database.py +Description: +""" +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker +from conf.settings import SQLALCHEMY_DATABASE_URL +from sysom_utils import FastApiResponseHelper + +engine = create_engine( + SQLALCHEMY_DATABASE_URL, connect_args={} +) + +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + +Base = declarative_base() + +FastApiResponseHelper.bind_base_class(Base) \ No newline at end of file diff --git a/sysom_server/sysom_key_server/app/models.py b/sysom_server/sysom_key_server/app/models.py new file mode 100644 index 0000000000000000000000000000000000000000..e61ee5f19c00f40804e568fb393cc0835d52fdd5 --- /dev/null +++ b/sysom_server/sysom_key_server/app/models.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File models.py +Description: +""" +from sqlalchemy import Column, Integer, String, DateTime +from sqlalchemy.sql import func +from app.database import Base + + +########################################################################### +# Define databse model here +########################################################################### + +# @reference https://fastapi.tiangolo.com/zh/tutorial/sql-databases/ +# class Person(Base): +# __tablename__ = "sys_person" +# id = Column(Integer, primary_key=True) +# name = Column(String(254), unique=True) +# age = Column(Integer) +# created_at = Column(DateTime(timezone=True), server_default=func.now()) \ No newline at end of file diff --git a/sysom_server/sysom_key_server/app/query.py b/sysom_server/sysom_key_server/app/query.py new file mode 100644 index 0000000000000000000000000000000000000000..fcbcd0898fc8d3f2be8d64ce694d7aa99ccb2332 --- /dev/null +++ b/sysom_server/sysom_key_server/app/query.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- # +""" +Time 2023/09/19 15:41 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File schemas.py +Description: +""" +from typing import Optional +from app import models +from sysom_utils import BaseQueryParams + + +# class PersonQueryParams(BaseQueryParams): + +# # 1. 指定要查询的模型 +# __modelclass__ = models.Person + +# # 2. 定义排序字段 +# sort: str = "-created_at" + +# # 3. 定义支持用于过滤的参数 +# name: Optional[str] = None +# age: Optional[str] = None + +# # 4. 指定哪些字段是枚举类型,并且指明对应的枚举类 +# __enum_fields__ = { +# } \ No newline at end of file diff --git a/sysom_server/sysom_key_server/app/routers/aes.py b/sysom_server/sysom_key_server/app/routers/aes.py new file mode 100644 index 0000000000000000000000000000000000000000..1909692e6c15ee39d8c0813e03128fd2918ff542 --- /dev/null +++ b/sysom_server/sysom_key_server/app/routers/aes.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +''' +@File : aes.py +@Time : 2023/12/19 13:55:26 +@Author : pengchen +@e-mail : wb-cp908446@alibaba-inc.com +@Version : 1.0 +@Desc : +''' +from Crypto.Cipher import AES +from Crypto.Util.Padding import pad, unpad + +def convert_to_bytes(data): + data_bytes = data.encode() # 将字符串转换为bytes类型 + if len(data_bytes) % 16 != 0: + padding_length = 16 - (len(data_bytes) % 16) + data_bytes += b'\x00' * padding_length # 使用b'\x00'进行填充 + return data_bytes + +def aes_encrypt(key, message): + # 创建AES加密器 + cipher = AES.new(key, AES.MODE_ECB) + # 进行填充 + padded_data = pad(message, AES.block_size) + # 进行加密 + ciphertext = cipher.encrypt(padded_data) + return ciphertext + +def aes_decrypt(key, ciphertext): + # 创建AES解密器 + cipher = AES.new(key, AES.MODE_ECB) + # 进行解密 + decrypted_data = cipher.decrypt(ciphertext) + # 进行去填充 + unpadded_data = unpad(decrypted_data, AES.block_size) + return unpadded_data + + diff --git a/sysom_server/sysom_key_server/app/routers/health.py b/sysom_server/sysom_key_server/app/routers/health.py new file mode 100644 index 0000000000000000000000000000000000000000..cd614ff7637f43188dd74f6d413ffc3812b9f485 --- /dev/null +++ b/sysom_server/sysom_key_server/app/routers/health.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File health.py +Description: +""" +from fastapi import APIRouter + + +router = APIRouter() + + +@router.get("/check") +async def get_channel_config(): + return { + "code": 0, + "err_msg": "", + "data": "" + } diff --git a/sysom_server/sysom_key_server/app/routers/key_service.py b/sysom_server/sysom_key_server/app/routers/key_service.py new file mode 100644 index 0000000000000000000000000000000000000000..c842814bf4b364fc99d00b9ba75996c36f2bfc3b --- /dev/null +++ b/sysom_server/sysom_key_server/app/routers/key_service.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/17 16:27 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File health.py +Description: +""" +import os +import time +import base64 +from app.routers.aes import aes_encrypt, convert_to_bytes +from fastapi import APIRouter +from conf.common import YAML_CONFIG + +addtion = YAML_CONFIG.get_service_config().get("ADDTION", None) + +router = APIRouter() + +@router.get("/key") +async def get_key_server_handler(): + original_bt = str(int(time.time())) + original_bt = original_bt.encode() + + #时间加密 + timekey = aes_encrypt(convert_to_bytes("pyroscope"), original_bt) + # 进行时间二次加密 + ciphertimetwo = aes_encrypt(convert_to_bytes("treelib"), timekey) + ciphertimetwo = (base64.b64encode(ciphertimetwo)).decode() + + # 原始数据 + addtion_byte = addtion.encode() + # 数据进行加密 + ciphermsgtext = aes_encrypt(timekey, addtion_byte) + encypt = (base64.b64encode(ciphermsgtext)).decode() + original_msg = ciphertimetwo + encypt + return original_msg diff --git a/sysom_server/sysom_key_server/app/routers/person.py b/sysom_server/sysom_key_server/app/routers/person.py new file mode 100644 index 0000000000000000000000000000000000000000..ebf6e39c0c84575adb4cb345bd3a3b9f983bf0d2 --- /dev/null +++ b/sysom_server/sysom_key_server/app/routers/person.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File health.py +Description: +""" +from fastapi import APIRouter, Depends +from sqlalchemy.orm import Session +from app.query import PersonQueryParams +from app.database import get_db +from app.crud import get_person_list, get_person_by_name +from app.schemas import Person +from sysom_utils import StandardListResponse, StandardResponse + + +router = APIRouter() + + +@router.get("/get") +async def get_specific_person( + person_name: str, db: Session = Depends(get_db) +): + person = get_person_by_name(db, person_name) + return StandardResponse(person, Person) + +@router.get("/list") +async def get_persons( + query_params: PersonQueryParams = Depends(), db: Session = Depends(get_db) +): + person_list = get_person_list(db, query_params) + return StandardListResponse(person_list, Person) diff --git a/sysom_server/sysom_key_server/app/schemas.py b/sysom_server/sysom_key_server/app/schemas.py new file mode 100644 index 0000000000000000000000000000000000000000..4e8bdef4fae252d625a9c1fa6501a8c802417f26 --- /dev/null +++ b/sysom_server/sysom_key_server/app/schemas.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File schemas.py +Description: +""" +from pydantic import BaseModel +from datetime import datetime + +########################################################################### +# Define schemas here +########################################################################### + +# @reference https://fastapi.tiangolo.com/zh/tutorial/response-model/ +# class Person(BaseModel): +# id: int +# name: str +# age: int +# created_at: datetime + +# class Config: +# orm_mode = True \ No newline at end of file diff --git a/sysom_server/sysom_key_server/conf/common.py b/sysom_server/sysom_key_server/conf/common.py new file mode 100644 index 0000000000000000000000000000000000000000..a176ddb032acd47bf38f01781ad9c2527f8cedcf --- /dev/null +++ b/sysom_server/sysom_key_server/conf/common.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File common.py +Description: +""" +from pathlib import Path +from sysom_utils import ConfigParser, SysomFramework + +BASE_DIR = Path(__file__).resolve().parent.parent + +################################################################## +# Load yaml config first +################################################################## +YAML_GLOBAL_CONFIG_PATH = f"{BASE_DIR.parent.parent}/conf/config.yml" +YAML_SERVICE_CONFIG_PATH = f"{BASE_DIR}/config.yml" + +YAML_CONFIG = ConfigParser(YAML_GLOBAL_CONFIG_PATH, YAML_SERVICE_CONFIG_PATH) + +mysql_config = YAML_CONFIG.get_server_config().db.mysql +service_config = YAML_CONFIG.get_service_config() + +SysomFramework.init(YAML_CONFIG) + +################################################################## +# fastapi config +################################################################## +SQLALCHEMY_DATABASE_URL = ( + f"{mysql_config.dialect}+{mysql_config.engine}://{mysql_config.user}:{mysql_config.password}@" + f"{mysql_config.host}:{mysql_config.port}/{mysql_config.database}" +) \ No newline at end of file diff --git a/sysom_server/sysom_key_server/conf/develop.py b/sysom_server/sysom_key_server/conf/develop.py new file mode 100644 index 0000000000000000000000000000000000000000..af922d74d8b665a6b89579078826fe30583825fa --- /dev/null +++ b/sysom_server/sysom_key_server/conf/develop.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File develoop.py +Description: +""" +from .common import * + +''' +开发环境配置项 +''' + +DEBUG = True \ No newline at end of file diff --git a/sysom_server/sysom_key_server/conf/gunicorn.py b/sysom_server/sysom_key_server/conf/gunicorn.py new file mode 100644 index 0000000000000000000000000000000000000000..a49f6994cc70b9d602b91b6472710005be8cba35 --- /dev/null +++ b/sysom_server/sysom_key_server/conf/gunicorn.py @@ -0,0 +1,23 @@ +''' +Channel Service Gunicorn Settings +''' +from conf.common import YAML_CONFIG + +bind = YAML_CONFIG.get_service_config().get("bind", "127.0.0.1") +port = YAML_CONFIG.get_service_config().get("port", "80") + +workers = 2 # 指定工作进程数 + +threads = 3 + +bind = f'{bind}:{port}' + +worker_class = 'uvicorn.workers.UvicornWorker' # 工作模式线程, 默认为sync模式 + +max_requests = 2000 # 设置最大并发数量为2000 (每个worker处理请求的工作线程) + +accesslog = '/var/log/sysom/sysom-key_server-access.log' + +loglevel = 'error' + +proc_name = 'sysom_key_server_service' diff --git a/sysom_server/sysom_key_server/conf/product.py b/sysom_server/sysom_key_server/conf/product.py new file mode 100644 index 0000000000000000000000000000000000000000..efce0452b49b6295ae314d64fda555412944029d --- /dev/null +++ b/sysom_server/sysom_key_server/conf/product.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File product.py +Description: +""" +from .common import * + +''' +生产环境配置项 +''' + +DEBUG = False diff --git a/sysom_server/sysom_key_server/conf/settings.py b/sysom_server/sysom_key_server/conf/settings.py new file mode 100644 index 0000000000000000000000000000000000000000..40550d5e5d8d9f7d47322cd54c3983eca4321388 --- /dev/null +++ b/sysom_server/sysom_key_server/conf/settings.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File settings.py +Description: +""" +import os + +env = os.environ.get("env", "product") + + +if env == "develop": + from .develop import * +elif env == "testing": + from .testing import * +elif env == "product": + from .product import * \ No newline at end of file diff --git a/sysom_server/sysom_key_server/conf/testing.py b/sysom_server/sysom_key_server/conf/testing.py new file mode 100644 index 0000000000000000000000000000000000000000..4bfc178d31f8cfe7ec4a77218286784fcf0d3471 --- /dev/null +++ b/sysom_server/sysom_key_server/conf/testing.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File testing.py +Description: +""" +from .common import * + +''' +测试环境配置项 +''' +DEBUG = True diff --git a/sysom_server/sysom_key_server/config.yml b/sysom_server/sysom_key_server/config.yml new file mode 100644 index 0000000000000000000000000000000000000000..247c64cd780da16f257fa1083d6cf4af927dfa45 --- /dev/null +++ b/sysom_server/sysom_key_server/config.yml @@ -0,0 +1,35 @@ +vars: + SERVICE_NAME: &SERVICE_NAME sysom_key_server + SERVICE_CONSUMER_GROUP: + !concat &SERVICE_CONSUMER_GROUP [*SERVICE_NAME, "_consumer_group"] + +sysom_server: + cec: + consumer_group: *SERVICE_CONSUMER_GROUP + +sysom_service: + service_name: *SERVICE_NAME + service_dir: *SERVICE_NAME + protocol: http + host: 127.0.0.1 + bind: 127.0.0.1 + port: 7022 + framework: + gcache: + protocol: redis + node_dispatch: + cmg: + tags: + - key_server + - FastApi + # Metadata of service + metadata: + check: + type: http + url: "/api/v1/key_server/health/check" + interval: 10 + timeout: 10 + deregister: 25 + header: + tls_skip_verify: false + diff --git a/sysom_server/sysom_key_server/lib/README.md b/sysom_server/sysom_key_server/lib/README.md new file mode 100644 index 0000000000000000000000000000000000000000..3ec74424ba6ecde5d035d0c113bafb0ddc6e4cfa --- /dev/null +++ b/sysom_server/sysom_key_server/lib/README.md @@ -0,0 +1 @@ +The current directory holds the public libraries or utils needed for microservices \ No newline at end of file diff --git a/sysom_server/sysom_key_server/main.py b/sysom_server/sysom_key_server/main.py new file mode 100644 index 0000000000000000000000000000000000000000..ce85cc7da2d040389a56714a22dc5eecc7aaa615 --- /dev/null +++ b/sysom_server/sysom_key_server/main.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/17 16:27 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File ssh.py +Description: +""" +from clogger import logger +from fastapi import FastAPI +from app.routers import health, key_service +from conf.settings import YAML_CONFIG +from sysom_utils import CmgPlugin, SysomFramework + + +app = FastAPI() + +app.include_router(health.router, prefix="/api/v1/key_server/health") +# app.include_router(health.router, prefix="/api/v1/key_server/person") +app.include_router(key_service.router, prefix="/api/v1/key_server") + +############################################################################# +# Write your API interface here, or add to app/routes +############################################################################# + + +def init_framwork(): + SysomFramework\ + .init(YAML_CONFIG) \ + .load_plugin_cls(CmgPlugin) \ + .start() + logger.info("SysomFramework init finished!") + + +@app.on_event("startup") +async def on_start(): + init_framwork() + + ############################################################################# + # Perform some microservice initialization operations over here + ############################################################################# + + +@app.on_event("shutdown") +async def on_shutdown(): + pass \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/alembic.ini b/sysom_server/sysom_offline_push/alembic.ini new file mode 100644 index 0000000000000000000000000000000000000000..f6ab9febcd93add9d0ea8857f857d7f30f1fe48f --- /dev/null +++ b/sysom_server/sysom_offline_push/alembic.ini @@ -0,0 +1,102 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = alembic + +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python-dateutil library that can be +# installed by adding `alembic[tz]` to the pip requirements +# string value is passed to dateutil.tz.gettz() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to alembic/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +sqlalchemy.url = "" + + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/sysom_server/sysom_offline_push/alembic/README b/sysom_server/sysom_offline_push/alembic/README new file mode 100644 index 0000000000000000000000000000000000000000..98e4f9c44effe479ed38c66ba922e7bcc672916f --- /dev/null +++ b/sysom_server/sysom_offline_push/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/alembic/env.py b/sysom_server/sysom_offline_push/alembic/env.py new file mode 100644 index 0000000000000000000000000000000000000000..ba0ab30f4545b4bb62256426179889913dc9b253 --- /dev/null +++ b/sysom_server/sysom_offline_push/alembic/env.py @@ -0,0 +1,115 @@ +import inspect +import app.models as models +from logging.config import fileConfig +from sqlalchemy import engine_from_config, Table +from sqlalchemy import pool +from app.models import Base +from alembic import context +from conf.settings import YAML_CONFIG, SQLALCHEMY_DATABASE_URL + +################################################################## +# Load yaml config first +################################################################## +mysql_config = YAML_CONFIG.get_server_config().db.mysql + +################################################################## +# Scan models +################################################################## +service_tables = [] +for name, data in inspect.getmembers(models): + if inspect.isclass(data): + if data.__module__ != "app.models": + continue + if "__tablename__" in data.__dict__: + service_tables.append(data.__dict__["__tablename__"]) + elif "__table__" in data.__dict__: + service_tables.append(data.__dict__["__table__"]) + elif isinstance(data, Table): + service_tables.append(name) + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# Update mysql config according config.yml +config.set_main_option( + "sqlalchemy.url", + SQLALCHEMY_DATABASE_URL +) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + +def include_object(object, name, type_, reflected, compare_to): + if type_ == "table" and name not in service_tables: + return False + return True + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + include_object=include_object, + version_table="offline_push_version", + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, target_metadata=target_metadata, + include_object=include_object, + version_table="offline_push_version" + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/sysom_server/sysom_offline_push/alembic/script.py.mako b/sysom_server/sysom_offline_push/alembic/script.py.mako new file mode 100644 index 0000000000000000000000000000000000000000..2c0156303a8df3ffdc9de87765bf801bf6bea4a5 --- /dev/null +++ b/sysom_server/sysom_offline_push/alembic/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/sysom_server/sysom_offline_push/alembic/versions/.gitkeep b/sysom_server/sysom_offline_push/alembic/versions/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/sysom_server/sysom_offline_push/app/__init__.py b/sysom_server/sysom_offline_push/app/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..fa55a3b99b108af4e7ce3257af1b0c5c30196868 --- /dev/null +++ b/sysom_server/sysom_offline_push/app/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File __init__.py +Description: +""" \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/app/crud.py b/sysom_server/sysom_offline_push/app/crud.py new file mode 100644 index 0000000000000000000000000000000000000000..1bdc78f152db90e018a90a8c3cf850df226f00ac --- /dev/null +++ b/sysom_server/sysom_offline_push/app/crud.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File crud.py +Description: +""" +from typing import Optional, List +from sqlalchemy.orm import Session +from app import models, schemas, query + +################################################################################################ +# Define database crud here +################################################################################################ + +# def get_person_by_name(db: Session, name: str) -> Optional[models.Person]: +# return db.query(models.Person).filter(models.Person.name == name).first() + +# def create_person(db: Session, person: schemas.Person) -> models.Person: +# person = models.Person(**person.dict()) +# db.add(person) +# db.commit() +# db.refresh(person) +# return person + +# def del_person_by_id(db: Session, person_id: int): +# person = db.get(models.Person, person_id) +# db.delete(person) +# db.commit() + +# def get_person_list(db: Session, query_params: query.PersonQueryParams) -> List[models.Person]: +# return ( +# query_params.get_query_exp(db) +# .all() +# ) diff --git a/sysom_server/sysom_offline_push/app/database.py b/sysom_server/sysom_offline_push/app/database.py new file mode 100644 index 0000000000000000000000000000000000000000..ea519ec54c013eb7ffe0ffa8c39d56ce5cf8d46c --- /dev/null +++ b/sysom_server/sysom_offline_push/app/database.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File database.py +Description: +""" +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker +from conf.settings import SQLALCHEMY_DATABASE_URL +from sysom_utils import FastApiResponseHelper + +engine = create_engine( + SQLALCHEMY_DATABASE_URL, connect_args={} +) + +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + +Base = declarative_base() + +FastApiResponseHelper.bind_base_class(Base) \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/app/models.py b/sysom_server/sysom_offline_push/app/models.py new file mode 100644 index 0000000000000000000000000000000000000000..e61ee5f19c00f40804e568fb393cc0835d52fdd5 --- /dev/null +++ b/sysom_server/sysom_offline_push/app/models.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File models.py +Description: +""" +from sqlalchemy import Column, Integer, String, DateTime +from sqlalchemy.sql import func +from app.database import Base + + +########################################################################### +# Define databse model here +########################################################################### + +# @reference https://fastapi.tiangolo.com/zh/tutorial/sql-databases/ +# class Person(Base): +# __tablename__ = "sys_person" +# id = Column(Integer, primary_key=True) +# name = Column(String(254), unique=True) +# age = Column(Integer) +# created_at = Column(DateTime(timezone=True), server_default=func.now()) \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/app/offlinepush/addDecode.py b/sysom_server/sysom_offline_push/app/offlinepush/addDecode.py new file mode 100644 index 0000000000000000000000000000000000000000..d6b3b64fd8f819ec29321731338d1f30101c8177 --- /dev/null +++ b/sysom_server/sysom_offline_push/app/offlinepush/addDecode.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +""" +------------------------------------------------- + File Name: addDecode + Description : + Author : liaozhaoyan + date: 2023/11/6 +------------------------------------------------- + Change Activity: + 2023/11/6: +------------------------------------------------- +""" +__author__ = 'liaozhaoyan' + +import os +from ctypes import cdll, c_char_p, c_int, create_string_buffer + +lib = cdll.LoadLibrary('./libaddenv.so') + +def decode_lib(s): + add_decode = lib.add_decode + add_decode.argtypes = [c_char_p, c_char_p, c_char_p] + add_decode.restype = c_int + + e1 = create_string_buffer(100) + e2 = create_string_buffer(100) + + add_decode(s.encode(), e1, e2) + return e1.value.decode(), e2.value.decode() + + +if __name__ == "__main__": + pass diff --git a/sysom_server/sysom_offline_push/app/offlinepush/libaddenv.so b/sysom_server/sysom_offline_push/app/offlinepush/libaddenv.so new file mode 100644 index 0000000000000000000000000000000000000000..dac2fabf19b8762eb4acd35f79eb970414f54927 Binary files /dev/null and b/sysom_server/sysom_offline_push/app/offlinepush/libaddenv.so differ diff --git a/sysom_server/sysom_offline_push/app/offlinepush/ossApi.py b/sysom_server/sysom_offline_push/app/offlinepush/ossApi.py new file mode 100644 index 0000000000000000000000000000000000000000..d6731dce41f455161e689f9093cd909c40cdbf40 --- /dev/null +++ b/sysom_server/sysom_offline_push/app/offlinepush/ossApi.py @@ -0,0 +1,89 @@ +import os +import base64 +import datetime +import requests +from ctypes import cdll, c_char_p, c_int, create_string_buffer +from email.utils import formatdate + + +class CossApi(object): + def __init__(self, endpoint, bucket, addition): + super(CossApi, self).__init__() + self._endpoint = endpoint + self._bucket = bucket + self._addition = addition + self._setupLib() + + def _setupLib(self): + so_dir = os.path.dirname(os.path.abspath(__file__)) + lib = cdll.LoadLibrary(so_dir + '/libaddenv.so') + + oss_decode = lib.oss_decode + oss_decode.argtypes = [c_char_p, c_char_p, c_char_p, c_char_p] + # addition, content, ak, sign + oss_decode.restype = c_int + + self._oss_decode = oss_decode + + def _sign(self, verb, cType, date, uri): + vars = [ + verb, "", cType, date, uri + ] + s = "\n".join(vars) + ak = create_string_buffer(100) + sign = create_string_buffer(20) + ret = self._oss_decode(self._addition.encode(), s.encode(), ak, sign) + if ret < 0: + raise ValueError("bad decoder result.") + return ak.value.decode(), base64.b64encode(sign).decode() + + def _rfc1123(self): + return formatdate(timeval=None, localtime=False, usegmt=True) + + def _packHeaders(self, verb, path, size): + host = self._bucket + "." + self._endpoint + date = self._rfc1123() + cType = "application/octet-stream" + uri = path + uris = "/" + self._bucket + uri + ak, sign = self._sign(verb, cType, date, uris) + auth = "OSS " + ak + ":" + sign + return "http://" + host + uri, { + "Host": host, + "Content-Type": cType, + "Content-Length": str(size), + "date": date, + "authorization": auth + } + + def put(self, fName, path, timeMode=True): + with open(fName, 'rb') as f: + s = f.read() + if timeMode: + path = datetime.datetime.now().strftime("/%Y-%m") + "/" + path + url, headers = self._packHeaders("PUT", path, len(s)) + ret = requests.put(url, data=s, headers=headers) + if ret.status_code != "200": + print(ret.status_code, ret.content) + + def put_stream(self, stream, path): + url, headers = self._packHeaders("PUT", path, len(stream)) + ret = requests.put(url, data=stream, headers=headers, stream=True, verify=False) + if ret.status_code != "200": + print(ret.status_code, ret.content) + + def get(self, path, fName, timeMode=True): + if timeMode: + path = datetime.datetime.now().strftime("/%Y-%m") + "/" + path + url, headers = self._packHeaders("GET", path, 0) + ret = requests.get(url, headers=headers) + if ret.status_code != 200: + print(ret.status_code, ret.content) + return ret.status_code, ret.content + else: + with open(fName, "wb") as f: + f.write(ret.content) + return 200, '' + +if __name__ == "__main__": + pass \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/app/offlinepush/sysom_offline_push.py b/sysom_server/sysom_offline_push/app/offlinepush/sysom_offline_push.py new file mode 100644 index 0000000000000000000000000000000000000000..bc43f9033130268171a304e28e131bc27f79aa89 --- /dev/null +++ b/sysom_server/sysom_offline_push/app/offlinepush/sysom_offline_push.py @@ -0,0 +1,168 @@ +# -*- encoding: utf-8 -*- +''' +@File : tornado_aes.py +@Time : 2023/12/19 13:39:29 +@Author : pengchen +@e-mail : wb-cp908446@alibaba-inc.com +@Version : 1.0 +@Desc : +''' + +import time +import os +import traceback +import zipfile +from clogger import logger +from aliyun.log.consumer import ConsumerWorker, ConsumerProcessorBase, LogHubConfig +from conf.common import YAML_CONFIG +from app.offlinepush.addDecode import decode_lib +from app.offlinepush.ossApi import CossApi +from sysom_utils import SysomFramework + + +CHECKPOINT_SAVE_TIIME = 3 +region = YAML_CONFIG.get_service_config().get("SYSOM_REGION", None) +addition = YAML_CONFIG.get_service_config().get("SYSOM_ADDITION", None) + +def get_result(task_id, stdout): + g_client = SysomFramework.gclient("sysom_diagnosis") + resp_get = g_client.get("api/v1/tasks/%s/"%task_id) + if resp_get.status_code == 200: + data = resp_get.json().get("data") + status = resp_get.json().get("data").get("status") + if status != "Running": + logger.error("The specified task:%s status is not running, cannot execute sbs_task_result task" % task_id) + return + data = { + "task_id": task_id, + "results": stdout + } + res = g_client.post("api/v1/tasks/sbs_task_result/", data=data) + if res.status_code == 200: + status = res.json().get("data").get("status") + return + else: + logger.error("post results failed of task id %s"%task_id) + logger.error(res.content, res.status_code) + return + else: + logger.error("get results failed of task id %s"%task_id) + logger.error(resp_get.content) + return + + +class CorigCli(object): + + def __init__(self): + super(CorigCli, self).__init__() + + def get_oss(self, path, fName, ossregion): + oss = CossApi("oss-%s.aliyuncs.com"%ossregion, + "sysom-pro-%s"%ossregion, + addition + ) + return oss.get(path, fName, timeMode=False) + + def push(self, d): + ossregion = d.get("region", "") + if not ossregion: + return + path = "/" + d["date"] + "/" + d["uuid"] + status, content = self.get_oss(path, d["uuid"], ossregion) + if status != 200: + logger.error(status, content) + return + if os.path.exists(d["uuid"]): + with zipfile.ZipFile(d["uuid"], 'r') as zip_ref: + file_names = zip_ref.namelist() + if "stdout" in file_names: + with zip_ref.open("stdout") as file: + content = file.read().decode() + get_result(d["uuid"], content) + os.remove(d["uuid"]) + +class CslsHander(ConsumerProcessorBase): + cli = CorigCli() + header = "log" + + def __init__(self): + super(CslsHander, self).__init__() + self._last_check_time = time.time() + + def _saveCheckpoint(self, check_point_tracker): + current_time = time.time() + if current_time - self._last_check_time > CHECKPOINT_SAVE_TIIME: + try: + self._last_check_time = current_time + check_point_tracker.save_check_point(True) + except Exception: + traceback.print_exc() + else: + try: + check_point_tracker.save_check_point(False) + except Exception: + traceback.print_exc() + return None + + def process(self, log_groups, check_point_tracker): + for log_group in log_groups.LogGroups: + for log in log_group.Logs: + d = {} + for content in log.Contents: + d[content.Key] = content.Value + self.cli.push(d) + self._saveCheckpoint(check_point_tracker) + + +class Cslscon(object): + def __init__(self, decode): + e1, e2 = decode_lib("%s" % decode) + slsConD = { + "host": "%s.log.aliyuncs.com"%region, + "project": "sysom-metrics-%s"%region, + "logstore": "offline", + "user": e1, + "pass": e2, + "group": "inner", + "number": "1" + } + self._argd = slsConD.copy() + super(Cslscon, self).__init__() + + def _setupOpt(self, group_name): + return LogHubConfig(self._argd['host'], + self._argd['user'], + self._argd['pass'], + self._argd['project'], + self._argd['logstore'], + self._argd['group'], + group_name) + + def _startOpt(self, opt): + w = ConsumerWorker(CslsHander, consumer_option=opt) + self._ws.append(w) + w.start() + + def _installOpts(self, cli, header): + self._ws = [] + CslsHander.cli = cli + if header: + CslsHander.header = header + if "gname" in self._argd: + opt = self._setupOpt(self._argd['gname']) + self._startOpt(opt) + print("%s start." % self._argd['gname']) + return + for i in range(int(self._argd['number'])): + group_name = self._argd['group'] + "_%d" % os.getpid() + "_%d" % i + opt = self._setupOpt(group_name) + self._startOpt(opt) + print("%s start.." % group_name) + + def consume(self, cli, header=None): + self._installOpts(cli, header) + + def stop(self): + for w in self._ws: + w.shutdown() + diff --git a/sysom_server/sysom_offline_push/app/query.py b/sysom_server/sysom_offline_push/app/query.py new file mode 100644 index 0000000000000000000000000000000000000000..fcbcd0898fc8d3f2be8d64ce694d7aa99ccb2332 --- /dev/null +++ b/sysom_server/sysom_offline_push/app/query.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- # +""" +Time 2023/09/19 15:41 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File schemas.py +Description: +""" +from typing import Optional +from app import models +from sysom_utils import BaseQueryParams + + +# class PersonQueryParams(BaseQueryParams): + +# # 1. 指定要查询的模型 +# __modelclass__ = models.Person + +# # 2. 定义排序字段 +# sort: str = "-created_at" + +# # 3. 定义支持用于过滤的参数 +# name: Optional[str] = None +# age: Optional[str] = None + +# # 4. 指定哪些字段是枚举类型,并且指明对应的枚举类 +# __enum_fields__ = { +# } \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/app/routers/health.py b/sysom_server/sysom_offline_push/app/routers/health.py new file mode 100644 index 0000000000000000000000000000000000000000..cd614ff7637f43188dd74f6d413ffc3812b9f485 --- /dev/null +++ b/sysom_server/sysom_offline_push/app/routers/health.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File health.py +Description: +""" +from fastapi import APIRouter + + +router = APIRouter() + + +@router.get("/check") +async def get_channel_config(): + return { + "code": 0, + "err_msg": "", + "data": "" + } diff --git a/sysom_server/sysom_offline_push/app/routers/person.py b/sysom_server/sysom_offline_push/app/routers/person.py new file mode 100644 index 0000000000000000000000000000000000000000..ebf6e39c0c84575adb4cb345bd3a3b9f983bf0d2 --- /dev/null +++ b/sysom_server/sysom_offline_push/app/routers/person.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File health.py +Description: +""" +from fastapi import APIRouter, Depends +from sqlalchemy.orm import Session +from app.query import PersonQueryParams +from app.database import get_db +from app.crud import get_person_list, get_person_by_name +from app.schemas import Person +from sysom_utils import StandardListResponse, StandardResponse + + +router = APIRouter() + + +@router.get("/get") +async def get_specific_person( + person_name: str, db: Session = Depends(get_db) +): + person = get_person_by_name(db, person_name) + return StandardResponse(person, Person) + +@router.get("/list") +async def get_persons( + query_params: PersonQueryParams = Depends(), db: Session = Depends(get_db) +): + person_list = get_person_list(db, query_params) + return StandardListResponse(person_list, Person) diff --git a/sysom_server/sysom_offline_push/app/schemas.py b/sysom_server/sysom_offline_push/app/schemas.py new file mode 100644 index 0000000000000000000000000000000000000000..4e8bdef4fae252d625a9c1fa6501a8c802417f26 --- /dev/null +++ b/sysom_server/sysom_offline_push/app/schemas.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File schemas.py +Description: +""" +from pydantic import BaseModel +from datetime import datetime + +########################################################################### +# Define schemas here +########################################################################### + +# @reference https://fastapi.tiangolo.com/zh/tutorial/response-model/ +# class Person(BaseModel): +# id: int +# name: str +# age: int +# created_at: datetime + +# class Config: +# orm_mode = True \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/conf/common.py b/sysom_server/sysom_offline_push/conf/common.py new file mode 100644 index 0000000000000000000000000000000000000000..a176ddb032acd47bf38f01781ad9c2527f8cedcf --- /dev/null +++ b/sysom_server/sysom_offline_push/conf/common.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File common.py +Description: +""" +from pathlib import Path +from sysom_utils import ConfigParser, SysomFramework + +BASE_DIR = Path(__file__).resolve().parent.parent + +################################################################## +# Load yaml config first +################################################################## +YAML_GLOBAL_CONFIG_PATH = f"{BASE_DIR.parent.parent}/conf/config.yml" +YAML_SERVICE_CONFIG_PATH = f"{BASE_DIR}/config.yml" + +YAML_CONFIG = ConfigParser(YAML_GLOBAL_CONFIG_PATH, YAML_SERVICE_CONFIG_PATH) + +mysql_config = YAML_CONFIG.get_server_config().db.mysql +service_config = YAML_CONFIG.get_service_config() + +SysomFramework.init(YAML_CONFIG) + +################################################################## +# fastapi config +################################################################## +SQLALCHEMY_DATABASE_URL = ( + f"{mysql_config.dialect}+{mysql_config.engine}://{mysql_config.user}:{mysql_config.password}@" + f"{mysql_config.host}:{mysql_config.port}/{mysql_config.database}" +) \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/conf/develop.py b/sysom_server/sysom_offline_push/conf/develop.py new file mode 100644 index 0000000000000000000000000000000000000000..af922d74d8b665a6b89579078826fe30583825fa --- /dev/null +++ b/sysom_server/sysom_offline_push/conf/develop.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File develoop.py +Description: +""" +from .common import * + +''' +开发环境配置项 +''' + +DEBUG = True \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/conf/gunicorn.py b/sysom_server/sysom_offline_push/conf/gunicorn.py new file mode 100644 index 0000000000000000000000000000000000000000..b769d25892fc4331becfb269c0c236eafb585cea --- /dev/null +++ b/sysom_server/sysom_offline_push/conf/gunicorn.py @@ -0,0 +1,23 @@ +''' +Channel Service Gunicorn Settings +''' +from conf.common import YAML_CONFIG + +bind = YAML_CONFIG.get_service_config().get("bind", "127.0.0.1") +port = YAML_CONFIG.get_service_config().get("port", "80") + +workers = 2 # 指定工作进程数 + +threads = 3 + +bind = f'{bind}:{port}' + +worker_class = 'uvicorn.workers.UvicornWorker' # 工作模式线程, 默认为sync模式 + +max_requests = 2000 # 设置最大并发数量为2000 (每个worker处理请求的工作线程) + +accesslog = '/var/log/sysom/sysom-offline_push-access.log' + +loglevel = 'error' + +proc_name = 'sysom_offline_push_service' diff --git a/sysom_server/sysom_offline_push/conf/product.py b/sysom_server/sysom_offline_push/conf/product.py new file mode 100644 index 0000000000000000000000000000000000000000..efce0452b49b6295ae314d64fda555412944029d --- /dev/null +++ b/sysom_server/sysom_offline_push/conf/product.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File product.py +Description: +""" +from .common import * + +''' +生产环境配置项 +''' + +DEBUG = False diff --git a/sysom_server/sysom_offline_push/conf/settings.py b/sysom_server/sysom_offline_push/conf/settings.py new file mode 100644 index 0000000000000000000000000000000000000000..40550d5e5d8d9f7d47322cd54c3983eca4321388 --- /dev/null +++ b/sysom_server/sysom_offline_push/conf/settings.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File settings.py +Description: +""" +import os + +env = os.environ.get("env", "product") + + +if env == "develop": + from .develop import * +elif env == "testing": + from .testing import * +elif env == "product": + from .product import * \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/conf/testing.py b/sysom_server/sysom_offline_push/conf/testing.py new file mode 100644 index 0000000000000000000000000000000000000000..4bfc178d31f8cfe7ec4a77218286784fcf0d3471 --- /dev/null +++ b/sysom_server/sysom_offline_push/conf/testing.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/19 14:22 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File testing.py +Description: +""" +from .common import * + +''' +测试环境配置项 +''' +DEBUG = True diff --git a/sysom_server/sysom_offline_push/config.yml b/sysom_server/sysom_offline_push/config.yml new file mode 100644 index 0000000000000000000000000000000000000000..46e21269dbbb3b9d3f455a692d6263b9296a7c5e --- /dev/null +++ b/sysom_server/sysom_offline_push/config.yml @@ -0,0 +1,37 @@ +vars: + SERVICE_NAME: &SERVICE_NAME sysom_offline_push + SERVICE_CONSUMER_GROUP: + !concat &SERVICE_CONSUMER_GROUP [*SERVICE_NAME, "_consumer_group"] + +sysom_server: + cec: + consumer_group: *SERVICE_CONSUMER_GROUP + +sysom_service: + service_name: *SERVICE_NAME + service_dir: *SERVICE_NAME + protocol: http + host: 127.0.0.1 + bind: 127.0.0.1 + port: 7021 + SYSOM_REGION: cn-zhangjiakou + SYSOM_ADDITION: McuLmtVHGGXMjeffE2ABOYq32JWg2SNyMlTwMggMSWy2cjewzAZ8XK4hRxnsItzHQuruOsqO/qtnvN4dpSuDhvPLJ+hYS7AhxFgVXV543q4= + framework: + gcache: + protocol: redis + node_dispatch: + cmg: + tags: + - offline_push + - FastApi + # Metadata of service + metadata: + check: + type: http + url: "/api/v1/offline_push/health/check" + interval: 10 + timeout: 10 + deregister: 25 + header: + tls_skip_verify: false + diff --git a/sysom_server/sysom_offline_push/lib/README.md b/sysom_server/sysom_offline_push/lib/README.md new file mode 100644 index 0000000000000000000000000000000000000000..3ec74424ba6ecde5d035d0c113bafb0ddc6e4cfa --- /dev/null +++ b/sysom_server/sysom_offline_push/lib/README.md @@ -0,0 +1 @@ +The current directory holds the public libraries or utils needed for microservices \ No newline at end of file diff --git a/sysom_server/sysom_offline_push/libaddenv.so b/sysom_server/sysom_offline_push/libaddenv.so new file mode 100644 index 0000000000000000000000000000000000000000..dac2fabf19b8762eb4acd35f79eb970414f54927 Binary files /dev/null and b/sysom_server/sysom_offline_push/libaddenv.so differ diff --git a/sysom_server/sysom_offline_push/main.py b/sysom_server/sysom_offline_push/main.py new file mode 100644 index 0000000000000000000000000000000000000000..d183fae6c0b32c77c9f9279b1aeaf6347f730ad6 --- /dev/null +++ b/sysom_server/sysom_offline_push/main.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- # +""" +Time 2024/01/17 14:00 +Author: mingfeng (SunnyQjm) +Email mfeng@linux.alibaba.com +File ssh.py +Description: +""" +from clogger import logger +from fastapi import FastAPI +from app.routers import health +from app.offlinepush.sysom_offline_push import Cslscon,CorigCli, addition +from conf.settings import YAML_CONFIG +from sysom_utils import CmgPlugin, SysomFramework + + +app = FastAPI() + +app.include_router(health.router, prefix="/api/v1/offline-push/health") +# app.include_router(health.router, prefix="/api/v1/offline-push/person") + + +############################################################################# +# Write your API interface here, or add to app/routes +############################################################################# + + +def init_framwork(): + SysomFramework\ + .init(YAML_CONFIG) \ + .load_plugin_cls(CmgPlugin) \ + .start() + logger.info("SysomFramework init finished!") + + +def start_service(): + con = Cslscon(addition) + con.consume(CorigCli()) + +@app.on_event("startup") +async def on_start(): + init_framwork() + + ############################################################################# + # Perform some microservice initialization operations over here + ############################################################################# + start_service() + +@app.on_event("shutdown") +async def on_shutdown(): + pass \ No newline at end of file