예제 #1
0
def remove_opereto_lib():

    try:
        c = OperetoClient()
        c.modify_agent_property(c.input['opereto_agent'], 'opereto.worker',
                                False)
        return c.SUCCESS
    except Exception as e:
        print(e)
        return 2
예제 #2
0
def install_opereto_lib():

    LIB_DIR = os.path.join(os.environ['opereto_workspace'], 'opereto')
    VIRT_ENV_DIR = os.path.join(os.environ['opereto_home'], 'operetovenv')

    per_os_module_to_install = {
        'rhel7.2': {
            'paramiko': 'paramiko==2.1.2'
        }
    }

    module_to_install = {
        'yaml' : 'pyyaml==3.13',
        'pyopereto': 'pyopereto==1.0.55',
        'requests': 'requests==2.19.1',
        'boto': 'boto==2.49.0',
        'paramiko': 'paramiko==2.4.1',
        'six': 'six==1.11.0',
        'sh': 'sh==1.12.14',
        'werkzeug': 'werkzeug==0.14.1',
        'psutil': 'psutil==0.7.0',
        'dateutil': 'python-dateutil==2.7.3',
        'jsonschema': 'jsonschema==2.6.0',
        'faker_schema': 'faker-schema==0.1.4',
        'docker': 'docker==3.1.4',
        'PIL': 'Pillow==2.9.0',
        'boto3': 'boto3==1.7.39',
        'httplib2': 'httplib2==0.11.3',
        'oauth2client': 'oauth2client==4.1.2',
        'apiclient': 'google-api-python-client==1.7.4',
        'junitparser': 'junitparser==1.2.2',
        'kubernetes':'kubernetes==5.0.0'
    }


    def get_current_os():
        (name, version,id) = platform.linux_distribution()
        return (name, version,id)

    def is_ubuntu():
        (name, version,id) = get_current_os()
        if name == 'Ubuntu':
            return True
        return False

    def is_windows():
        if platform.system() == 'Windows':
            return True
        return False

    def _remove_dir(dir):
        if os.path.exists(dir):
            if is_windows():
                _local("rmdir /s /q %s"%dir)
            else:
                _local("sudo rm -rf %s"%dir)

    def _create_dir_if_not_exists(dir):
        if not os.path.exists(dir):
            if is_windows():
                _local("mkdir %s" % dir)
            else:
                _local("sudo mkdir -p %s" % dir)
                _local('sudo chmod -R 777 ' + dir)

    def _copy_opereto_venv_contents(dir):
        if is_windows():
            WIN_LIB_DIR = LIB_DIR.replace("/","\\")
            _local("xcopy %s %s /q /i /e /Y" % (WIN_LIB_DIR, os.path.join(dir, 'opereto', 'venv')))
        else:
            _local("cp -rf %s %s" % (LIB_DIR, os.path.join(dir, 'opereto')))


    def _local(cmd, ignore=False):
        global FAILURE
        print cmd
        ret = os.system(cmd)
        if int(ret)!=0 and not ignore:
            print 'Command ended with exit code: {}'.format(int(ret))
            FAILURE=True
        return ret


    def _prepare_new_virtual_env(directory):
        os_name=None
        (name, version,id) = get_current_os()

        print 'Current OS: {} {}, {}'.format(name, version,id)
        if name=='Red Hat Enterprise Linux Server' and version=='7.2':
            os_name='rhel7.2'

        if os_name:
            module_to_install.update(per_os_module_to_install[os_name])

        if is_windows():
            _local('cd %s && virtualenv venv' % (directory), ignore=False)
            for import_name, module in module_to_install.items():
                _local('cd %s && pip install %s' % (os.path.join(directory,'venv', 'Scripts'), module), ignore=False)
                if _local('cd %s && python -c "import %s"' % (os.path.join(directory, 'venv', 'Scripts'), import_name)) != 0:
                    print >> sys.stderr, 'Python module [%s] is not installed.' % module

        else:
            _local('cd %s && virtualenv venv && . venv/bin/activate && pip install --upgrade pip ; deactivate'%(directory),ignore=False)
            for import_name, module in module_to_install.items():
                _local('cd %s && . venv/bin/activate && pip install %s && deactivate'%(directory, module),ignore=True)
                if _local('cd %s && . venv/bin/activate && python -c "import %s" && deactivate'%(directory, import_name))!=0:
                    print >> sys.stderr, 'Python module [%s] is not installed.'%module


    def create_virtual_rlauto_env(current_version_dir):
        _remove_dir(current_version_dir)
        _create_dir_if_not_exists(current_version_dir)
        _copy_opereto_venv_contents(current_version_dir)
        _prepare_new_virtual_env(current_version_dir)
        if not is_windows():
            _local('sudo chmod 777 -R %s'%current_version_dir)
            _local("sudo su -c 'echo \"export PYTHONPATH=%s\" >> %s'"%(current_version_dir,os.path.join(current_version_dir,'venv/bin/activate')))

    try:
        release=os.environ.get('opereto_service_version')

        ## check prerequisites
        if sys.version_info<(2,7):
            print >> sys.stderr,'Opereto microservices lib requires python 2.7 or higher.'
            return 2

        if is_ubuntu():
            (name, version,id) = get_current_os()
            install_list = 'sudo apt-get install -qy python-six curl python-setuptools gcc build-essential python-dev python-pip libffi-dev libssl-dev'
            _local(install_list,ignore=False)
            _local('sudo pip install virtualenv', ignore=False)

        elif is_windows():
            _local('pip install virtualenv', ignore=False)
            _local('pip install pyopereto')

        else:
            _local('sudo yum install -y python-virtualenv python-setuptools gcc libffi python-devel openssl-devel')
            _local('easy_install pip')

        if not is_windows():
            _local('sudo pip install pyopereto')

        current_version_dir= os.path.join(VIRT_ENV_DIR,release)
        create_virtual_rlauto_env(current_version_dir)

        if FAILURE:
            return 3
        else:
            if os.environ.get('opereto_agent'):
                from pyopereto.client import OperetoClient
                c = OperetoClient()
                if c.input['standard_opereto_worker']:
                    c.modify_agent_property(c.input['opereto_agent'], 'opereto.worker', True)
            return 0

    except Exception, e:
        print traceback.format_exc()
        return 2
예제 #3
0
def install_opereto_lib():

    LIB_DIR = os.path.join(os.environ['opereto_workspace'], 'opereto')
    VIRT_ENV_DIR = os.path.join(os.environ['opereto_home'], 'operetovenv')

    module_to_install = {
        'boto3': 'boto3==1.20.6',
        'yaml': 'pyyaml==6.0',
        'pyopereto': 'pyopereto==1.0.132',
        'kubernetes': 'kubernetes==19.15.0',
        'requests': 'requests==2.26.0',
        'paramiko': 'paramiko==2.8.0',
        'six': 'six==1.16.0',
        'sh': 'sh==1.14.2',
        'werkzeug': 'werkzeug==2.0.2',
        'psutil': 'psutil==5.8.0',
        'dateutil': 'python-dateutil==2.8.2',
        'jsonschema': 'jsonschema==2.6.0',
        'faker_schema': 'faker-schema==0.1.4',
        'docker': 'docker==5.0.3',
        'PIL': 'Pillow==8.4.0',
        'httplib2': 'httplib2==0.20.2',
        'oauth2client': 'oauth2client==4.1.3',
        'apiclient': 'google-api-python-client==2.30.0',
        'junitparser': 'junitparser==2.1.1'
    }

    def get_pip_command():
        if version == '20.04':
            return 'pip3'
        return 'pip3.8'

    def get_virtenv_cmd():
        return 'virtualenv'

    def get_current_os():
        out = subprocess.check_output('cat /etc/*-release',
                                      shell=True).decode('utf-8')
        name = re.search('NAME="([A-Za-z0-9\s]*)"', out)
        version = re.search('VERSION_ID=\"([\d\.\-]*)\"', out)
        os_id = re.search('ID=([A-Za-z0-9\s\"]*)\n', out)
        if name and version and os_id:
            return name.group(1), version.group(1), os_id.group(1).strip('"')
        else:
            raise Exception(
                f'cant find OS details (NAME, VERSION_ID, ID), check full os-release output:\n{out}'
            )

    def _remove_dir(dir):
        if os.path.exists(dir):
            _local("sudo rm -rf {}".format(dir))

    def _create_dir_if_not_exists(dir):
        if not os.path.exists(dir):
            _local("sudo mkdir -p {}".format(dir))
            _local('sudo chmod -R 777 ' + dir)

    def _copy_opereto_venv_contents(dir):
        _local("cp -rf {} {}".format(LIB_DIR, os.path.join(dir, 'opereto')))

    def _local(cmd, ignore=False):
        global FAILURE
        print(cmd)
        ret = os.system(cmd)
        if int(ret) != 0 and not ignore:
            print('Command ended with exit code: {}'.format(int(ret)))
            FAILURE = True
        return ret

    def _prepare_new_virtual_env(directory):
        (name, version, id) = get_current_os()
        print('Current OS: {} {}, {}'.format(name, version, id))

        _local(
            'cd {} && {} venv -p python3.6 && . venv/bin/activate && pip install --upgrade pip ; deactivate'
            .format(directory, get_virtenv_cmd()),
            ignore=False)

        for import_name, module in module_to_install.items():
            _local(
                'cd {} && . venv/bin/activate && pip install {} && deactivate'.
                format(directory, module),
                ignore=True)
            if _local(
                    'cd {} && . venv/bin/activate && python -c "import {}" && deactivate'
                    .format(directory, import_name)) != 0:
                sys.stderr.write(
                    'Python module [{}] is not installed.'.format(module))

    def create_virtual_env(current_version_dir):
        _remove_dir(current_version_dir)
        _create_dir_if_not_exists(current_version_dir)
        _copy_opereto_venv_contents(current_version_dir)
        _prepare_new_virtual_env(current_version_dir)
        _local('sudo chmod 777 -R {}'.format(current_version_dir))
        _local("sudo su -c 'echo \"export PYTHONPATH={}\" >> {}'".format(
            current_version_dir,
            os.path.join(current_version_dir, 'venv/bin/activate')))

    try:
        release = os.environ.get('opereto_service_version')
        (name, version, id) = get_current_os()

        if sys.version_info < (3, 6):
            sys.stderr.write(
                'Opereto microservices lib requires python 3.6 or higher.')
            return 2

        _local(
            'export DEBIAN_FRONTEND=noninteractive ; sudo -E apt-get update')
        install_list = 'sudo apt-get install -qy python-six curl python-setuptools gcc build-essential python3.6-dev libffi-dev libssl-dev'
        _local(install_list, ignore=False)
        _local('curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3.8')

        _local(
            'export LC_ALL=\"en_US.UTF-8\" ; export LC_CTYPE=\"en_US.UTF-8\" ; sudo {} install virtualenv==16.0.0'
            .format(get_pip_command()),
            ignore=False)
        current_version_dir = os.path.join(VIRT_ENV_DIR, release)
        create_virtual_env(current_version_dir)

        if FAILURE:
            return 3

        try:
            _local('{} install --upgrade pyopereto==1.0.124'.format(
                get_pip_command()))
            if os.environ.get('opereto_agent'):
                from pyopereto.client import OperetoClient
                c = OperetoClient()
                if c.input['standard_opereto_worker']:
                    c.modify_agent_property(c.input['opereto_agent'],
                                            'opereto.worker', True)
        except Exception as e:
            print(e)

        return 0

    except Exception as e:
        print(traceback.format_exc())
        return 2