Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
tvault-rubrik / setup.py
Size: Mime:
# Copyright 2021 TrilioData Inc.
# All Rights Reserved.

import os
import sys
import shutil
import atexit
import pwd
import pkg_resources
from setuptools.command.install import install
from distutils.sysconfig import get_python_lib

# NOTE: Because of the way the packages are built, we
# need to occasionally override which toolstack is used
# to build the package. By default (i.e. for PIP) we go
# to the setuptools variant -- but this tries to be too
# clever when we are building debian packages.
setup = os.getenv("__SETUP", "setuptools")
if setup == "setuptools":
    from setuptools import setup, find_packages
elif setup == "distutils":
    from distutils.core import setup, find_packages
else:
    raise Exception("Unknown __SETUP tools specified.")

ROOT = os.path.dirname(os.path.realpath(__file__))
PIP_REQUIRES = os.path.join(ROOT, "requirements.txt")
# 'source' SHOULD BE PRESENT
lst_of_files = [
    {'file_name': 'backup_target_gate.py', 'dest': 'workloadmgr/backup_target_gate.py'},
    {'file_name': 'api.py', 'dest': 'workloadmgr/workloads/api.py'},
    {'file_name': 'manager.py', 'dest': 'workloadmgr/workloads/manager.py'},
    {'file_name': 'workload_utils.py', 'dest': 'workloadmgr/workloads/workload_utils.py'},
    {'file_name': 'qemuimages.py', 'dest': 'workloadmgr/virt/qemuimages.py'},
    {'file_name': 'clean_admin_creds.yml',
     'dest': 'tvault_configurator/ansible-play/roles/ansible-workloadmgr/tasks/clean_admin_creds.yml'},
    {'file_name': 'configure.yml',
     'dest': 'tvault_configurator/ansible-play/roles/ansible-workloadmgr/tasks/configure.yml'},
    {'file_name': 'main.yml',
     'dest': 'tvault_configurator/ansible-play/roles/ansible-populate-variables/templates/roles/ansible-workloadmgr/defaults/main.yml'},
    {'file_name': 'tvault_config_bottle.py', 'dest': 'tvault_configurator/tvault_config_bottle.py'},
    {'file_name': 'workloadmgr.conf.j2',
     'dest': 'tvault_configurator/ansible-play/roles/ansible-workloadmgr/templates/workloadmgr.conf.j2'},
]
conf_destination = '/etc/workloadmgr/'


def get_package():
    return os.getenv('TVAULT_PACKAGE', "tvault-rubrik")


def get_module_name():
    return 'tvault_rubrik'


def read_file_list(filename):
    with open(filename) as f:
        return [line.strip() for line in f.readlines() if len(line.strip()) > 0]


def store_base_file(path):
    backup_path = path + "_backup"
    shutil.copy(path, backup_path)


def get_user_info(user_name):
    try:
        user_info = pwd.getpwnam(user_name)
        u_id = user_info.pw_uid
        g_id = user_info.pw_gid
    except KeyError:
        u_id = 162  # nova u_id
        g_id = 162  # nova g_id
    return u_id, g_id


def _chown(s_dir, u_id, g_id):
    os.chown(s_dir, u_id, g_id)
    for root, dirs, files in os.walk(s_dir):
        for dir in dirs:
            os.chown(os.path.join(root, dir), u_id, g_id)
        for file in files:
            os.chown(os.path.join(root, file), u_id, g_id)


def _post_install():
    source_location = get_python_lib() + f'/{MODULE_NAME}'
    for files in lst_of_files:
        dest_location = os.path.join(get_python_lib(), files.get('dest'))
        if files.get('dest') != 'workloadmgr/backup_target_gate.py':
            store_base_file(dest_location)
        shutil.copy(os.path.join(f'./{MODULE_NAME}', files.get('file_name')), dest_location)

    try:
        if not os.path.exists(os.path.dirname(conf_destination)):
            os.makedirs(os.path.dirname(conf_destination))
        shutil.move('./etc/rubrik.conf', conf_destination)

    except Exception as ex:
        raise ex
    nova_u_id, nova_g_id = get_user_info('nova')
    u_id, g_id = get_user_info('root')
    _chown(os.path.join(conf_destination, 'rubrik.conf'), u_id, nova_g_id)
    os.chmod(os.path.join(conf_destination, 'rubrik.conf'), 0o640)


def check_installed_package():
    pkg_to_check = ['workloadmgr', 'tvault-configurator']
    installed_packages = pkg_resources.working_set
    installed_packages_list = sorted(["%s" % (i.key) for i in installed_packages])
    pkg_exist = [x for x in installed_packages_list if x in pkg_to_check]
    pkg_missing = set(pkg_to_check) - set(pkg_exist)
    return pkg_missing


class PostInstallCommand(install):
    """Post-installation for installation mode."""

    def __init__(self, *args, **kwargs):
        super(PostInstallCommand, self).__init__(*args, **kwargs)
        if not check_installed_package():
            atexit.register(_post_install)
        else:
            sys.exit(f'{check_installed_package()} is not installed')


INSTALL_REQUIRES = read_file_list(PIP_REQUIRES)
PACKAGE = get_package()
MODULE_NAME = get_module_name()

setup(
    name=PACKAGE,
    version=os.getenv('TVAULT_VERSION', '4.2.64'),
    description="TVault rubrik integration",
    author="TrilioData",
    author_email="support@triliodata.com",
    url="http://www.triliodata.com/",
    license="http://www.triliodata.com",
    classifiers=[
        "Environment :: Console",
        "Environment :: OpenStack",
        "Intended Audience :: System Administrators",
        "Intended Audience :: Information Technology",
        "License :: www.triliodata.com",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
    ],
    cmdclass={'install': PostInstallCommand},
    install_requires=INSTALL_REQUIRES,
    packages=find_packages(),
    include_package_data=True,
    scripts=[f'{MODULE_NAME}/rubrikutil']

)