ok

Mini Shell

Direktori : /opt/cloudlinux/venv/lib64/python3.11/site-packages/lvestats/utils/
Upload File :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/lvestats/utils/v1import.py

# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

from __future__ import print_function
from __future__ import absolute_import
import os

CONFIG_PATH_v1 = '/etc/sysconfig/lvestats'
CONFIG_PATH_v2 = '/etc/sysconfig/lvestats2'

NOTIFY_PATH_v1 = '/etc/sysconfig/cloudlinux-notify'
NOTIFY_PATH_v2 = '/etc/sysconfig/lvestats.config/StatsNotifier.cfg'

IMPORT_STATUS_FILE = '/usr/share/lve-stats/utils/import-settings-status'

V1_DB_MIIGRATOR_CONFIG = '/etc/sysconfig/lvestats.config/V1DBMigrator.cfg'
V1_SQLITE_DB = 'sqlite:////var/lve/lveinfo.db'
V1_DB_MIIGRATOR_PLUGIN_SRC = '/usr/share/lve-stats/plugins.other/v1_db_migrator.py'
V1_DB_MIIGRATOR_PLUGIN_LINK = '/usr/share/lve-stats/plugins/v1_db_migrator.py'
V1_KEY = 'v1_connect_string'


def init_v1_db_migrator(config):
    if V1_KEY not in config:
        print('Cannot find db info to enable migration plugin')
        return

    if 'server_id' not in config:
        config['server_id'] = 'localhost'

    with open(V1_DB_MIIGRATOR_CONFIG, 'w') as cfg:
        cfg.write('v1_server_id=%s\nv1_connect_string=%s\n' % (config['server_id'], config[V1_KEY]))

    try:
        if not os.path.lexists(V1_DB_MIIGRATOR_PLUGIN_LINK):
            os.symlink(V1_DB_MIIGRATOR_PLUGIN_SRC, V1_DB_MIIGRATOR_PLUGIN_LINK)
    except (IOError, OSError) as e:
        print("Unable to enable db migration plugin: %s, continue: " % (str(e)))


def get_import_status():
    if os.path.exists(IMPORT_STATUS_FILE):
        f = open(IMPORT_STATUS_FILE, 'r')
        v = f.read()
        f.close()
        try:
            return int(v)
        except ValueError:
            raise Exception('Invalid status format for file %s' % IMPORT_STATUS_FILE)
    else:
        return 0


def set_import_status(flag):
    f = open(IMPORT_STATUS_FILE, 'w')
    f.write('%d' % flag)
    f.close()


def get_config_path(path):
    if os.path.exists(path):
        return path
    elif os.path.exists(path + '.rpmsave'):
        return path + '.rpmsave'
    return ''


def get_connection_string(lvestats_config):
    if 'db_type' in lvestats_config:
        if 'connect_string' in lvestats_config and lvestats_config['db_type'] in ('mysql', 'postgresql'):
            connect_string = lvestats_config['connect_string'].split(':')
            host = connect_string[0]
            database = connect_string[1]
            user = connect_string[2]
            password = connect_string[3]
            lvestats_config['connect_string'] = user + ':' + password + '@' + host + '/' + database
            lvestats_config[V1_KEY] = "{0}://{1}".format(lvestats_config['db_type'], lvestats_config['connect_string'])
        elif lvestats_config['db_type'] == 'sqlite':
            lvestats_config[V1_KEY] = V1_SQLITE_DB

Zerion Mini Shell 1.0