From 41c8be93a058f34f23e80ae69a0051d85031639d Mon Sep 17 00:00:00 2001 From: Setyo Nugroho Date: Fri, 18 Mar 2022 13:04:43 +0700 Subject: [PATCH] project YuYu --- .gitignore | 1 + README.md | 52 ++++++++++----------- api/serializers.py | 7 ++- bin/setup_api.sh | 18 ++++--- bin/setup_event_monitor.sh | 17 ++++--- core/event_endpoint.py | 2 +- core/management/commands/event_monitor.py | 10 ++-- core/management/commands/process_invoice.py | 4 +- rintik/__init__.py => logs/.gitkeep | 0 manage.py | 2 +- rintik/local_settings.py.sample | 7 --- script/rintik_api.service | 12 ----- script/rintik_event_monitor.service | 12 ----- script/yuyu_api.service | 12 +++++ script/yuyu_event_monitor.service | 12 +++++ yuyu/__init__.py | 0 {rintik => yuyu}/asgi.py | 4 +- yuyu/local_settings.py.sample | 7 +++ {rintik => yuyu}/settings.py | 6 +-- {rintik => yuyu}/urls.py | 2 +- {rintik => yuyu}/wsgi.py | 4 +- 21 files changed, 103 insertions(+), 88 deletions(-) rename rintik/__init__.py => logs/.gitkeep (100%) delete mode 100644 rintik/local_settings.py.sample delete mode 100644 script/rintik_api.service delete mode 100644 script/rintik_event_monitor.service create mode 100644 script/yuyu_api.service create mode 100644 script/yuyu_event_monitor.service create mode 100644 yuyu/__init__.py rename {rintik => yuyu}/asgi.py (74%) create mode 100644 yuyu/local_settings.py.sample rename {rintik => yuyu}/settings.py (96%) rename {rintik => yuyu}/urls.py (96%) rename {rintik => yuyu}/wsgi.py (74%) diff --git a/.gitignore b/.gitignore index 8f9d944..4d6b4f0 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ share/python-wheels/ .installed.cfg *.egg MANIFEST +logs/* # PyInstaller # Usually these files are written by a python script from a template diff --git a/README.md b/README.md index 97c704f..5b36bc1 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ -# Rintik +# Yuyu -Rintik provide ability to manage openstack billing by listening to every openstack event. Rintik is a required component to use Rintik Dashboard. There are 3 main component in Rintik: API, Cron, Event Monitor +Yuyu provide ability to manage openstack billing by listening to every openstack event. Yuyu is a required component to use Yuyu Dashboard. There are 3 main component in Yuyu: API, Cron, Event Monitor -## Rintik API -Main component to communicate with Rintik Dashboard. +## Yuyu API +Main component to communicate with Yuyu Dashboard. -## Rintik Cron +## Yuyu Cron Provide invoice calculation and rolling capabilities that needed to run every month. -## Rintik Event Monitor +## Yuyu Event Monitor Monitor event from openstack to calculate billing spent. # System Requirement @@ -21,7 +21,7 @@ Monitor event from openstack to calculate billing spent. # Pre-Installation ### Virtualenv -Make sure you installed virtualenv before installing Rintik +Make sure you installed virtualenv before installing Yuyu ```bash pip3 install virtualenv @@ -62,12 +62,12 @@ If you using Kolla, please add configuration above on all service container. For # Installation -Clone the latest source code and put it on any directory you want. Here i assume you put it on `/var/rintik/` +Clone the latest source code and put it on any directory you want. Here i assume you put it on `/var/yuyu/` ```bash -cd /var/rintik/ +cd /var/yuyu/ git clone {repository} -cd rintik +cd yuyu ``` Then create virtualenv and activate it @@ -80,7 +80,7 @@ pip install -r requirements.txt Then create a configuration file, just copy from sample file and modify as your preference. ```bash -cp core/local_settings.py.sample core/local_settings.py +cp yuyu/local_settings.py.sample yuyu/local_settings.py ``` Please read [Local Setting Configuration](#local-setting-configuration) to get to know about what configuration you should change. @@ -99,20 +99,20 @@ python manage.py createsuperuser ## Local Setting Configuration -### RINTIK_NOTIFICATION_URL (required) +### YUYU_NOTIFICATION_URL (required) A Messaging Queue URL that used by Openstack, usually it is a RabbitMQ URL. Example: ``` -RINTIK_NOTIFICATION_URL = "rabbit://openstack:password@127.0.0.1:5672/" +YUYU_NOTIFICATION_URL = "rabbit://openstack:password@127.0.0.1:5672/" ``` -### RINTIK_NOTIFICATION_TOPICS (required) +### YUYU_NOTIFICATION_TOPICS (required) A list of topic notification topic that is configured on each openstack service Example: ``` -RINTIK_NOTIFICATION_TOPICS = ["notifications"] +YUYU_NOTIFICATION_TOPICS = ["notifications"] ``` @@ -124,37 +124,37 @@ By default, it will use Sqlite. If you want to change it to other database pleas ## API Installation -To install Rintik API, you need to execute this command. +To install Yuyu API, you need to execute this command. ```bash ./bin/setup_api.sh ``` -This will install `rintik_api` service +This will install `yuyu_api` service To start the service use this command ```bash -systemctl enable rintik_api -systemctl start rintik_api +systemctl enable yuyu_api +systemctl start yuyu_api ``` An API server will be open on port `8182`. ## Event Monitor Installation -To install Rintik API, you need to execute this command. +To install Yuyu API, you need to execute this command. ```bash ./bin/setup_event_montor.sh ``` -This will install `rintik_event_monitor` service +This will install `yuyu_event_monitor` service To start the service use this command ```bash -systemctl enable rintik_event_monitor -systemctl start rintik_event_monitor +systemctl enable yuyu_event_monitor +systemctl start yuyu_event_monitor ``` ## Cron Installation @@ -166,10 +166,10 @@ To install it, you can use `crontab -e`. Put this expression on the crontab ``` -1 0 1 * * $rintik_dir/bin/process_invoice.sh +1 0 1 * * $yuyu_dir/bin/process_invoice.sh ``` -Replace $rintik_dir with the directory of where rintik is located. Example +Replace $yuyu_dir with the directory of where yuyu is located. Example ``` -1 0 1 * * /etc/rintik/bin/process_invoice.sh +1 0 1 * * /etc/yuyu/bin/process_invoice.sh ``` \ No newline at end of file diff --git a/api/serializers.py b/api/serializers.py index 1f79ce0..307cf9f 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -31,6 +31,8 @@ def generate_invoice_component_serializer(model): class InvoiceSerializer(serializers.ModelSerializer): subtotal = MoneyField(max_digits=10, decimal_places=0) subtotal_currency = serializers.CharField(source="subtotal.currency") + total = MoneyField(max_digits=10, decimal_places=0) + total_currency = serializers.CharField(source="total.currency") def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -45,7 +47,10 @@ class InvoiceSerializer(serializers.ModelSerializer): class SimpleInvoiceSerializer(serializers.ModelSerializer): subtotal = MoneyField(max_digits=10, decimal_places=0) subtotal_currency = serializers.CharField(source="subtotal.currency") + total = MoneyField(max_digits=10, decimal_places=0) + total_currency = serializers.CharField(source="total.currency") class Meta: model = Invoice - fields = ['id', 'start_date', 'end_date', 'state', 'tax', 'subtotal', 'subtotal_currency', 'total'] + fields = ['id', 'start_date', 'end_date', 'state', 'tax', 'subtotal', 'subtotal_currency', 'total', + 'total_currency'] diff --git a/bin/setup_api.sh b/bin/setup_api.sh index 3a46645..12deb5f 100755 --- a/bin/setup_api.sh +++ b/bin/setup_api.sh @@ -1,12 +1,16 @@ #!/bin/bash -echo "Installing Rintik API Service" -rintik_dir=`pwd -P` +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +cd $SCRIPT_DIR || exit +cd .. -echo "Riktik dir is $rintik_dir" +echo "Installing Yuyu API Service" +yuyu_dir=`pwd -P` -rintik_dir_sub=${rintik_dir//\//\\\/} -sed "s/{{rintik_dir}}/$rintik_dir_sub/g" "$rintik_dir"/script/rintik_api.service > /etc/systemd/system/rintik_api.service +echo "Yuyu dir is $yuyu_dir" -echo "Rintik API Service Installed on /etc/systemd/system/rintik_api.service" -echo "Done! you can enable Rintik API with systemctl start rintik_api" \ No newline at end of file +yuyu_dir_sub=${yuyu_dir//\//\\\/} +sed "s/{{yuyu_dir}}/$yuyu_dir_sub/g" "$yuyu_dir"/script/yuyu_api.service > /etc/systemd/system/yuyu_api.service + +echo "Yuyu API Service Installed on /etc/systemd/system/yuyu_api.service" +echo "Done! you can enable Yuyu API with systemctl start yuyu_api" \ No newline at end of file diff --git a/bin/setup_event_monitor.sh b/bin/setup_event_monitor.sh index 1fe1f0d..d652d62 100755 --- a/bin/setup_event_monitor.sh +++ b/bin/setup_event_monitor.sh @@ -1,12 +1,17 @@ #!/bin/bash +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +cd $SCRIPT_DIR || exit +cd .. + + echo "Installing Rintik API Service" -rintik_dir=`pwd -P` +yuyu_dir=`pwd -P` -echo "Riktik dir is $rintik_dir" +echo "Riktik dir is $yuyu_dir" -rintik_dir_sub=${rintik_dir//\//\\\/} -sed "s/{{rintik_dir}}/$rintik_dir_sub/g" "$rintik_dir"/script/rintik_event_monitor.service > /etc/systemd/system/rintik_event_monitor.service +yuyu_dir_sub=${yuyu_dir//\//\\\/} +sed "s/{{yuyu_dir}}/$yuyu_dir_sub/g" "$yuyu_dir"/script/yuyu_event_monitor.service > /etc/systemd/system/yuyu_event_monitor.service -echo "Rintik API Service Installed on /etc/systemd/system/rintik_event_monitor.service" -echo "Done! you can enable Rintik API with systemctl start rintik_event_monitor" \ No newline at end of file +echo "Yuyu API Service Installed on /etc/systemd/system/yuyu_event_monitor.service" +echo "Done! you can enable Rintik API with systemctl start yuyu_event_monitor" \ No newline at end of file diff --git a/core/event_endpoint.py b/core/event_endpoint.py index 735740a..1ec8b61 100644 --- a/core/event_endpoint.py +++ b/core/event_endpoint.py @@ -5,7 +5,7 @@ from oslo_messaging import NotificationResult from core.component import component from core.utils.dynamic_setting import get_dynamic_settings, BILLING_ENABLED, get_dynamic_setting -LOG = logging.getLogger("rintik_notification") +LOG = logging.getLogger("yuyu_notification") class EventEndpoint(object): diff --git a/core/management/commands/event_monitor.py b/core/management/commands/event_monitor.py index 64006cb..a891403 100644 --- a/core/management/commands/event_monitor.py +++ b/core/management/commands/event_monitor.py @@ -12,7 +12,7 @@ from django.core.management.base import BaseCommand from core.event_endpoint import EventEndpoint -LOG = logging.getLogger("rintik_notification") +LOG = logging.getLogger("yuyu_notification") class SignalExit(SystemExit): @@ -22,7 +22,7 @@ class SignalExit(SystemExit): class Command(BaseCommand): - help = 'Start Rintik Event Monitor' + help = 'Start Yuyu Event Monitor' def signal_handler(self, signum, frame): raise SignalExit(signum) @@ -53,19 +53,19 @@ class Command(BaseCommand): self.run_server(transport, server) def handle(self, *args, **options): - url = settings.RINTIK_NOTIFICATION_URL + url = settings.YUYU_NOTIFICATION_URL transport = messaging.get_notification_transport(cfg.CONF, url=url) # oslo.config defaults cfg.CONF.heartbeat_interval = 5 cfg.CONF.prog = os.path.basename(__file__) - cfg.CONF.project = 'rintik' + cfg.CONF.project = 'yuyu' signal.signal(signal.SIGTERM, self.signal_handler) signal.signal(signal.SIGINT, self.signal_handler) self.notify_server( transport=transport, - topics=settings.RINTIK_NOTIFICATION_TOPICS, + topics=settings.YUYU_NOTIFICATION_TOPICS, ) diff --git a/core/management/commands/process_invoice.py b/core/management/commands/process_invoice.py index 6dc2d7c..6a1baf9 100644 --- a/core/management/commands/process_invoice.py +++ b/core/management/commands/process_invoice.py @@ -8,11 +8,11 @@ from core.models import Invoice, InvoiceComponentMixin from core.component import component, labels from core.utils.dynamic_setting import get_dynamic_setting, BILLING_ENABLED, INVOICE_TAX -LOG = logging.getLogger("rintik_new_invoice") +LOG = logging.getLogger("yuyu_new_invoice") class Command(BaseCommand): - help = 'Rintik New Invoice' + help = 'Yuyu New Invoice' def handle(self, *args, **options): print("Processing Invoice") diff --git a/rintik/__init__.py b/logs/.gitkeep similarity index 100% rename from rintik/__init__.py rename to logs/.gitkeep diff --git a/manage.py b/manage.py index 0bfbf48..8efd21e 100755 --- a/manage.py +++ b/manage.py @@ -6,7 +6,7 @@ import sys def main(): """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rintik.settings') + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yuyu.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/rintik/local_settings.py.sample b/rintik/local_settings.py.sample deleted file mode 100644 index 912fbe5..0000000 --- a/rintik/local_settings.py.sample +++ /dev/null @@ -1,7 +0,0 @@ -RINTIK_NOTIFICATION_URL = "rabbit://openstack:password@127.0.0.1:5672/" -RINTIK_NOTIFICATION_TOPICS = ["notifications"] - -# Currency Configuration -CURRENCIES = ('IDR', 'USD') -DEFAULT_CURRENCY = "IDR" - diff --git a/script/rintik_api.service b/script/rintik_api.service deleted file mode 100644 index 628794d..0000000 --- a/script/rintik_api.service +++ /dev/null @@ -1,12 +0,0 @@ -[Unit] -Description=rintik api daemon -After=network.target - -[Service] -User=root -Group=root -WorkingDirectory={{rintik_dir}} -ExecStart={{rintik_dir}}/env/bin/gunicorn rintik.wsgi --workers 2 --bind 127.0.0.1:8000 --log-file=logs/gunicorn.log - -[Install] -WantedBy=multi-user.target diff --git a/script/rintik_event_monitor.service b/script/rintik_event_monitor.service deleted file mode 100644 index 9a522a3..0000000 --- a/script/rintik_event_monitor.service +++ /dev/null @@ -1,12 +0,0 @@ -[Unit] -Description=rintik event monitor daemon -After=network.target - -[Service] -User=root -Group=root -WorkingDirectory={{rintik_dir}} -ExecStart={{rintik_dir}}/env/bin/python manage.py event_monitor - -[Install] -WantedBy=multi-user.target diff --git a/script/yuyu_api.service b/script/yuyu_api.service new file mode 100644 index 0000000..6eb184a --- /dev/null +++ b/script/yuyu_api.service @@ -0,0 +1,12 @@ +[Unit] +Description=yuyu api daemon +After=network.target + +[Service] +User=root +Group=root +WorkingDirectory={{yuyu_dir}} +ExecStart={{yuyu_dir}}/env/bin/gunicorn yuyu.wsgi --workers 2 --bind 127.0.0.1:8182 --log-file=logs/gunicorn.log + +[Install] +WantedBy=multi-user.target diff --git a/script/yuyu_event_monitor.service b/script/yuyu_event_monitor.service new file mode 100644 index 0000000..ab9e150 --- /dev/null +++ b/script/yuyu_event_monitor.service @@ -0,0 +1,12 @@ +[Unit] +Description=yuyu event monitor daemon +After=network.target + +[Service] +User=root +Group=root +WorkingDirectory={{yuyu_dir}} +ExecStart={{yuyu_dir}}/env/bin/python manage.py event_monitor + +[Install] +WantedBy=multi-user.target diff --git a/yuyu/__init__.py b/yuyu/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/rintik/asgi.py b/yuyu/asgi.py similarity index 74% rename from rintik/asgi.py rename to yuyu/asgi.py index bd80560..36e7049 100644 --- a/rintik/asgi.py +++ b/yuyu/asgi.py @@ -1,5 +1,5 @@ """ -ASGI config for rintik project. +ASGI config for yuyu project. It exposes the ASGI callable as a module-level variable named ``application``. @@ -11,6 +11,6 @@ import os from django.core.asgi import get_asgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rintik.settings') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yuyu.settings') application = get_asgi_application() diff --git a/yuyu/local_settings.py.sample b/yuyu/local_settings.py.sample new file mode 100644 index 0000000..70c3a4e --- /dev/null +++ b/yuyu/local_settings.py.sample @@ -0,0 +1,7 @@ +YUYU_NOTIFICATION_URL = "rabbit://openstack:password@127.0.0.1:5672/" +YUYU_NOTIFICATION_TOPICS = ["notifications"] + +# Currency Configuration +CURRENCIES = ('IDR', 'USD') +DEFAULT_CURRENCY = "IDR" + diff --git a/rintik/settings.py b/yuyu/settings.py similarity index 96% rename from rintik/settings.py rename to yuyu/settings.py index 9a85454..d27f481 100644 --- a/rintik/settings.py +++ b/yuyu/settings.py @@ -1,5 +1,5 @@ """ -Django settings for rintik project. +Django settings for yuyu project. Generated by 'django-admin startproject' using Django 3.2.6. @@ -53,7 +53,7 @@ MIDDLEWARE = [ 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] -ROOT_URLCONF = 'rintik.urls' +ROOT_URLCONF = 'yuyu.urls' TEMPLATES = [ { @@ -72,7 +72,7 @@ TEMPLATES = [ }, ] -WSGI_APPLICATION = 'rintik.wsgi.application' +WSGI_APPLICATION = 'yuyu.wsgi.application' # Database diff --git a/rintik/urls.py b/yuyu/urls.py similarity index 96% rename from rintik/urls.py rename to yuyu/urls.py index 2ff83b6..a82f32e 100644 --- a/rintik/urls.py +++ b/yuyu/urls.py @@ -1,4 +1,4 @@ -"""rintik URL Configuration +"""yuyu URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.2/topics/http/urls/ diff --git a/rintik/wsgi.py b/yuyu/wsgi.py similarity index 74% rename from rintik/wsgi.py rename to yuyu/wsgi.py index 87f5186..c152968 100644 --- a/rintik/wsgi.py +++ b/yuyu/wsgi.py @@ -1,5 +1,5 @@ """ -WSGI config for rintik project. +WSGI config for yuyu project. It exposes the WSGI callable as a module-level variable named ``application``. @@ -11,6 +11,6 @@ import os from django.core.wsgi import get_wsgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rintik.settings') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yuyu.settings') application = get_wsgi_application()