Mini Shell
# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2020 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENCE.TXT
#
# pylint: disable=no-absolute-import
import os
import sys
import subprocess
from enum import Enum
from functools import wraps
from jwt import exceptions
from clcommon.lib.consts import CLN_JWT_TOKEN_PATH
from clcommon.lib.jwt_token import read_jwt, decode_jwt
from clcommon.clexception import FormattedException
CL_EDITION_FILE_MARKER = '/etc/cloudlinux-edition-solo'
class SupportedEditions(Enum):
"""
Keeps supported CloudLinux editions
"""
SOLO = 'solo'
SHARED = 'shared'
SHARED_PRO = 'shared_pro'
class CLEditionDetectionError(FormattedException):
def __init__(self, message, **context):
FormattedException.__init__(self, {
'message': message,
'context': context
})
class CLEditions:
@staticmethod
def get_from_jwt(token=None):
if token is None:
token = read_jwt(CLN_JWT_TOKEN_PATH)
try:
jwt = decode_jwt(token)
except exceptions.PyJWTError as e:
raise CLEditionDetectionError(f'Unable to detect edition from jwt token: {CLN_JWT_TOKEN_PATH}. '
f'Please, make sure it is not broken, error: {e}')
try:
return jwt['edition']
except KeyError:
# fallback for old format of tokens
cl_plus = jwt.get('cl_plus')
if cl_plus is None:
raise CLEditionDetectionError(
f'Unable to detect edition from jwt token: {CLN_JWT_TOKEN_PATH}. '
f'Please, make sure it is not broken, error: not enough fields for detection')
return SupportedEditions.SHARED_PRO.value if cl_plus else SupportedEditions.SHARED.value
@staticmethod
def is_package_installed(package_name):
process = subprocess.Popen(['rpm', '-q', package_name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
out, err = process.communicate()
if process.returncode != 0 and 'is not installed' in out:
return False
elif process.returncode != 0:
raise CLEditionDetectionError(f'Unable to check is package {package_name} is installed, '
f'reason: {out}, {err}')
return True
@classmethod
def get_cl_edition(cls, skip_jwt_check=False, skip_marker_check=False,
raw_jwt=None):
"""
1. Try to detect edition from jwt token
if edition field is in token -> return edition
if edition field is not present -> recheck via cl_plus flag
if token is broken -> raise error about it
2. Try to detect from file
if edition is in file -> return edition
if file is broken -> raise about it
if file is missed -> check if meta package is present
Detection from file may be switched off using skip_marker_check
In case there is no token with correct edition,
no file we consider edition as regular CL.
"""
# skipping both jwt and file checks is not allowed
if skip_jwt_check and skip_marker_check:
raise CLEditionDetectionError(
'Unable to detect edition: neither jwt token check, no file marker check enabled')
if not skip_jwt_check and os.path.isfile(CLN_JWT_TOKEN_PATH):
edition = cls.get_from_jwt(token=raw_jwt)
if edition:
return edition
# if fallback to file is applicable
if not skip_marker_check:
# jwt has no 'edition' field -> ensure it is really solo via file
if os.path.isfile(CL_EDITION_FILE_MARKER):
return SupportedEditions.SOLO.value
return SupportedEditions.SHARED.value
def is_cl_solo_edition(skip_jwt_check=False, skip_marker_check=False):
"""
Allow skip_jwt_check ONLY if it not critical when license is not valid
Use skip_marker_check=True when validity of license is critical
and fallback to file is not applicable
"""
edition = CLEditions.get_cl_edition(skip_jwt_check=skip_jwt_check,
skip_marker_check=skip_marker_check)
return edition is not None and edition == SupportedEditions.SOLO.value
def is_cl_shared_edition(skip_jwt_check=False, skip_marker_check=False):
"""
Allow skip_jwt_check ONLY if it not critical when license is not valid
Use skip_marker_check=True when validity of license is critical
and fallback to file is not applicable
"""
edition = CLEditions.get_cl_edition(skip_jwt_check=skip_jwt_check,
skip_marker_check=skip_marker_check)
return edition is not None and edition == SupportedEditions.SHARED.value
def is_cl_shared_pro_edition(skip_jwt_check=False,
skip_marker_check=False):
"""
Allow skip_jwt_check ONLY if it not critical when license is not valid
Use skip_marker_check=True when validity of license is critical
and fallback to file is not applicable
"""
edition = CLEditions.get_cl_edition(skip_jwt_check=skip_jwt_check,
skip_marker_check=skip_marker_check)
return edition is not None and edition == SupportedEditions.SHARED_PRO.value
def skip_on_cl_solo():
try:
if is_cl_solo_edition():
print('CloudLinux Solo edition detected! \n'
'Command is skipped, because it is unsupported and unneeded on current edition')
sys.exit(0)
except CLEditionDetectionError as e:
print(f'Error: {e}')
sys.exit(1)
def lve_supported_or_exit(f):
@wraps(f)
def inner(*args, **kwargs):
skip_on_cl_solo()
return f(*args, **kwargs)
return inner
Zerion Mini Shell 1.0