Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Build and Install cortx py utils

rohit-k-dwivedi edited this page Feb 1, 2022 · 5 revisions

CORTX Python Utilities

Codacy Badge

A common utils framework which includes common modules across components


Prerequisites for build

sudo yum install -y gcc rpm-build python36 python36-pip python36-devel python36-setuptools openssl-devel libffi-devel python36-dbus

For Rocky Linux, python36-pip, python36-setuptools, python36-dbus are not available, use the command below

sudo yum install -y gcc rpm-build  openssl-devel libffi-devel python36 python3-pip python3-devel python3-setuptools python3-dbus

Clone

git clone --recursive https://github.com/Seagate/cortx-utils -b main

Build

It will create cortx-py-utils-1.0.0-1_<git-version>.noarch.rpm by default. One can change the version by passing extra -v <version_string> parameter. Below command passes version string as 2.0.0 and build number 2, which creates cortx-py-utils-2.0.0-2_<git-version>.noarch.rpm Run below command from repo root (cortx-utils).

$ ./jenkins/build.sh -v 2.0.0 -b 2

Installation

Note : The rpm package installation will fail if any dependent python package is not installed. Please refer to WIKI (https://github.com/Seagate/cortx-utils/wiki/%22cortx-py-utils%22-single-node-manual-provisioning)

$ cd ./py-utils/dist
$ sudo yum install -y cortx-py-utils-*.noarch.rpm

Uninstallation

$ yum remove cortx-py-utils

Update new dependency package

  • Add package in python_requirements.txt.

Usage

After cortx package is installed, it can be used the common way as any other Python module, E.g.:

from cortx.utils.security.cipher import Cipher

Security

Cipher

All Cipher methods are static, parameters contain all the neccessary information to perform tasks.

Use generate_key method to create an encryption key. The method requires two strings (could be considered as salt and password), but user can pass more strings if neccessary:

key = Cipher.generate_key('salt','pass','more','strings')

Use the obtained key to encrypt or decrypt your data. Note, that all arguments must be bytes:

encrypted = Cipher.encrypt(key, b'secret')
decrypted = Cipher.decrypt(key, encrypted)

Note that decrypt method might raise a CipherInvalidToken exception if the key is not valid. User might need to handle the exception gracefully if not sure about the key:

try:
    decrypted = Cipher.decrypt(key, encrypted)
except CipherInvalidToken:
    print('Key is wrong')

Utility

Validator

The validator modules can be used to perform checks and validate a wide range of things. For example, if a certain service is running, certain packages are installed or not, examine network connectivity etc. For the remote checks, there is an optional hostname usage which will cause ssh connection to remote host, and the ssh must be set up without password beforehand otherwise operation will fail.

  • Package validator: Currently two types of package validators are supported. One for checking rpm packages and the other one for pip3 packages. These APIs can be used to check if certain packages are installed on a remote host as well.

    -- To check if rpm packages are installed, use command "rpms", and pass a list of package names.

from cortx.utils.validator.v_pkg import PkgV
try:
	PkgV().validate('rpms', ["lvm2-2.02.186-7.el7", "openldap-server"])
except Exception as e:
	print("one or more pkgs are not installed on this machine")

Note The second example below shows how to check if packages are installed on a remote host specified by "remote_hostname".

	PkgV().validate('rpms', ["lvm2-2.02.186-7.el7", "openldap-server"], "remote_hostname")
-- To check if pip3 packages are installed, use command "pip3", and pass a list of package names.
	PkgV().validate('pip3', ["toml", "salt"])
	PkgV().validate('pip3', ["toml", "salt"], "remote_hostname") # for checking remote pip3 packages
  • Service validator: This can be used to check if certain services are running on this host or on a remote host. Use command "isrunning" to check, pass a list of name of services which needs to be checked.
from cortx.utils.validator.v_service import ServiceV
try:
	ServiceV().validate('isrunning', ["rabbitmq-server", "sshd"])
except Exception as e:
	print("one or more services are not running on this machine")

Note The second example below shows how to check if given services are running on a remote host specified by "remote_hostname".

	ServiceV().validate('isrunning', ["rabbitmq-server", "sshd"], "remote_hostname")
  • Path validator: This can be used to check if certain paths and their types are as expected. Use command "exists" to check, pass a list of colon separated types followed by absolute paths. e.g. ["dir:/", "file:/etc/hosts", "device:/dev/loop9"]
from cortx.utils.validator.v_path import PathV
try:
	PathV().validate('exists', ["dir:/", "file:/etc/hosts", "device:/dev/loop9"])
except Exception as e:
	print("one or more path checks failed")

Note The second example below shows how to check if given paths are ok on a remote host specified by "remote_hostname".

	PathV().validate('exists', ["dir:/", "file:/etc/hosts", "device:/dev/loop9"], "remote_hostname")
  • Confstore key validator: This can be used to check if an preloaded index on confstore has the requested keys present or not. Use command "exists" to check, pass the preloaded index and a list of 'keys'.
from cortx.utils.validator.v_confkeys import ConfKeysV
try:
	ConfKeysV().validate('exists', index, ['bridge', 'bridge>namei'])
except Exception as e:
	if "key missing" not in f"{e}":
		raise Exception(f"Unexpected exception: {e}")
	print("One or more keys are missing.")