arrow_backAll Posts·5 Min Read·2024-10-20

Yuyu Billing in OpenStack Horizon

OpenStackApachePython

Yuyu is an OpenStack billing plugin that collects usage data from OpenStack events. It lets administrators define prices for flavors, images, networking resources, and volumes.

Key benefits include:

  • Accurate reporting: Usage costs are calculated automatically, reducing the need for manual billing calculations.
  • Real-time management: Review and manage usage without waiting for a fixed reporting window.
  • Better cost control: Precise usage data makes it easier to identify savings opportunities.
  • Install the required dependencies, then clone the Horizon source repository.

    bash
    root@controller:~# apt update
    root@controller:~# apt install python3 python3-dev python3-venv python3-distutils apache2 libapache2-mod-wsgi-py3 memcached libmemcached-tools python3-setuptools python3-virtualenv -y
    root@controller:~# cd /var/www/html
    
    root@controller:~/html# git clone https://opendev.org/openstack/horizon -b stable/2023.1 --depth=1
    root@controller:~/html# cd horizon
    
  • Install the Horizon dependencies. Keep the pinned version of python-memcached in the requirements file; changing it can cause logout failures.

    bash
    root@controller:~/horizon# pip install -U pip
    root@controller:~/horizon# wget https://opendev.org/openstack/requirements/raw/branch/stable/2023.1/upper-constraints.txt 
    root@controller:~/horizon# sed -i 's/horizon===23.1.1/horizon===0.0.0/g' upper-constraints.txt
    root@controller:~/horizon# pip install -c upper-constraints.txt .
    root@controller:~/horizon# pip install -r requirements.txt
    root@controller:~/horizon# pip install python-cinderclient
    
  • Copy the sample local_settings.py file and configure it as shown below.

    bash
    root@controller:~/horizon# cp openstack_dashboard/local/local_settings.py.example \
    openstack_dashboard/local/local_settings.py
    root@controller:~/horizon# nano openstack_dashboard/local/local_settings.py
    
    python
    # Set the debug value.
    DEBUG = False
    
    # Set allowed hosts.
    ALLOWED_HOSTS = ['*']
    
    # Configure the cache backend.
    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
            'LOCATION': '{IP-OPENSTACK}:11211', # replace {IP-OPENSTACK} with the VM IP address
        },
    }
    
    # Configure the OpenStack host and Keystone.
    OPENSTACK_HOST = "{IP-VIP-OPENSTACK}:5000" # replace {IP-VIP-OPENSTACK} with the Keystone API endpoint
    OPENSTACK_KEYSTONE_URL = "http://%s/v3" % OPENSTACK_HOST
    
    # Configure themes.
    AVAILABLE_THEMES = [
      ('default', 'Default', 'themes/default'),
      ('material', 'Material', 'themes/material'),
      ('example', 'Example', 'themes/example'),
    ]
    
  • Create a Python 3.10 symlink so Horizon uses the expected interpreter.

    bash
    root@controller:~/horizon# ln -s /usr/bin/python3.10 /usr/bin/python
    
  • Generate the static CSS, JavaScript, image, and font assets.

    bash
    root@controller:~/horizon# ./manage.py collectstatic
    
  • Generate the WSGI file.

    bash
    root@controller:~/horizon# ./manage.py make_web_conf --wsgi
    
  • Generate the Apache configuration for the Horizon web service.

    bash
    root@controller:~/horizon# ./manage.py make_web_conf --apache > /etc/apache2/sites-available/horizon.conf
    
  • Enable the Horizon site and disable Apache's default site.

    bash
    root@controller:~/horizon# a2ensite horizon.conf && a2dissite 000-default.conf
    
  • Set the required permissions for the static directory and WSGI file.

    bash
    root@controller:~/horizon# chown -R www-data:www-data openstack_dashboard/local/.secret_key_store
    root@controller:~/horizon# chown -R www-data:www-data static/
    root@controller:~/horizon# chmod a+x openstack_dashboard/horizon_wsgi.py
    
    <br>
  • Update the following OpenStack service configuration files so they publish events to RabbitMQ for Yuyu to consume:

    • Nova: /etc/kolla/nova-scheduler/nova.conf.

      bash
      root@controller:~# nano /etc/kolla/nova-scheduler/nova.conf
      
      yaml
      [oslo_messaging_notifications]
      driver = messagingv2 
      topics = notifications
      
      [notifications]
      notify_on_state_change = vm_and_task_state
      notification_format = unversioned
      
    • Cinder: /etc/kolla/cinder-scheduler/cinder.conf.

      bash
      root@controller:~# nano /etc/kolla/cinder-scheduler/cinder.conf
      
      yaml
      [oslo_messaging_notifications]
      driver = messagingv2
      topics = notifications
      
    • Neutron: /etc/kolla/neutron-server/neutron.conf.

      bash
      root@controller:~# nano /etc/kolla/neutron-server/neutron.conf
      
      yaml
      [oslo_messaging_notifications]
      driver = messagingv2 
      topics = notifications
      
    • Keystone: /etc/kolla/keystone/keystone.conf.

      bash
      root@controller:~# nano /etc/kolla/keystone/keystone.conf
      
      yaml
      [oslo_messaging_notifications]
      driver = messagingv2 
      topics = notifications
      
  • Restart the configured service containers.

    bash
    root@controller:~# docker restart nova_scheduler cinder_scheduler neutron_server keystone
    
  • Clone the Yuyu API and event-monitor repositories.

    bash
    root@controller:~# cd /var
    root@controller:~/var# git clone https://github.com/btechpt/yuyu.git
    root@controller:~/yuyu# cd yuyu
    
  • Create a Python virtual environment, activate it, and install the required libraries.

    bash
    root@controller:~/yuyu# virtualenv env
    root@controller:~/yuyu# source env/bin/activate
    (env) root@controller:~/yuyu# pip install -r requirements.txt
    
  • Retrieve the RabbitMQ URL from the Neutron configuration. You will use it in Yuyu's local_settings.py.

    bash
    (env) root@controller:~/yuyu# cat /etc/kolla/neutron-server/neutron.conf | grep transport_url
    
  • Copy yuyu/local_settings.py.sample to yuyu/local_settings.py and configure it.

    bash
    (env) root@controller:~/yuyu# cp yuyu/local_settings.py.sample yuyu/local_settings.py
    (env) root@controller:~/yuyu# vim yuyu/local_settings.py
    
    python
    YUYU_NOTIFICATION_URL = "rabbit://openstack:password@127.0.0.1:5672//"
    YUYU_NOTIFICATION_TOPICS = ["notifications"]
    
  • Run the database migrations.

    bash
    (env) root@controller:~/yuyu# python manage.py migrate
    
  • Set up and start the Yuyu API and event-monitor services.

    bash
    (env) root@controller:~/yuyu# ./bin/setup_api.sh
    (env) root@controller:~/yuyu# systemctl enable yuyu_api
    (env) root@controller:~/yuyu# systemctl start yuyu_api
    (env) root@controller:~/yuyu# systemctl status yuyu_api
    
    bash
    (env) root@controller:~/yuyu# ./bin/setup_event_monitor.sh
    (env) root@controller:~/yuyu# systemctl enable yuyu_event_monitor
    (env) root@controller:~/yuyu# systemctl start yuyu_event_monitor
    (env) root@controller:~/yuyu# systemctl status yuyu_event_monitor
    
  • Create a cron entry to run the processing script on a schedule.

    bash
    (env) root@controller:~/yuyu# crontab -e
    
    no crontab for root - using an empty one
    Select an editor. To change later, run 'select-editor'.
    1. /bin/nano          <---- easiest
    2. /usr/bin/vim.basic
    3. /usr/bin/vim.tiny
    4. /bin/ed
    Choose 1-4 [1]: [choose an editor]
    
  • Add the following line to the generated crontab file, then save it.

    bash
    1 0 1 * * /var/yuyu/bin/process_invoice.sh
    
  • Deactivate the Python virtual environment.

    bash
    (env) root@controller:~/yuyu# deactivate
    
    <br>
  • Clone the Yuyu Dashboard repository.

    bash
    root@controller:~# cd /var
    root@controller:~/var# git clone https://github.com/btechpt/yuyu_dashboard.git
    root@controller:~/yuyu_dashboard# cd yuyu_dashboard
    
  • Run the Yuyu Dashboard setup. When prompted for the Horizon directory, use the correct path, such as /var/www/html/horizon.

    bash
    root@controller:~/yuyu_dashboard# ./setup_yuyu.sh
    
    Enter horizon location and press ENTER.
    
    /var/www/html/horizon
    
  • Install the Python libraries required by the Yuyu Dashboard.

    bash
    root@controller:~/yuyu_dashboard# pip3 install -r requirements.txt
    
  • Add the Yuyu settings to Horizon's local_settings.py. Bind the dashboard to localhost because both services run on the same host.

    bash
    root@controller:~/yuyu_dashboard# vim /var/www/html/horizon/openstack_dashboard/local/local_settings.py
    
    python
    YUYU_URL="http://127.0.0.1:8182"
    CURRENCIES = ('IDR',)
    DEFAULT_CURRENCY = "IDR"
    
  • Restart Apache and Memcached.

    bash
    root@controller:~/yuyu_dashboard# systemctl restart apache2
    root@controller:~/yuyu_dashboard# systemctl restart memcached
    
<br>

Sign in to the dashboard with the appropriate credentials. Horizon login Horizon home page

Administrators can manage billing settings. Billing settings

Resource prices can be configured for the selected currency. Resource pricing

Usage costs can be reviewed for a specific project. Project usage cost

Usage-cost data can also be downloaded as a file. Download usage cost

Invoices are available through the Horizon page. Horizon invoice page

Invoices can also be downloaded as files. Downloaded invoice