-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support of IPA client A lot of work is done in the setup of the environment, support of IPA clients (used for testing as Kerberos users). Most of Bash scripts are replaced with python code. Add unit tests.
- Loading branch information
Showing
25 changed files
with
2,271 additions
and
989 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import logging | ||
from sys import stdout | ||
import colorlog | ||
|
||
|
||
handler = logging.StreamHandler(stdout) | ||
handler.setLevel(logging.DEBUG) | ||
|
||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt="%H:%M:%S") | ||
handler = colorlog.StreamHandler() | ||
formatter = colorlog.ColoredFormatter( | ||
'%(log_color)s%(module)s.%(funcName)s.%(lineno)d [%(levelname)s] %(message)s', | ||
log_colors={ | ||
'DEBUG': 'cyan', | ||
'INFO': 'green', | ||
'WARNING': 'yellow', | ||
'ERROR': 'red', | ||
'CRITICAL': 'red,bg_white'}) | ||
handler.setFormatter(formatter) | ||
|
||
# Basic logger | ||
log = logging.getLogger("base") | ||
log.setLevel(logging.DEBUG) | ||
|
||
log.addHandler(handler) | ||
base_logger = colorlog.getLogger("base") | ||
base_logger.addHandler(handler) | ||
base_logger.setLevel(colorlog.DEBUG) | ||
|
||
# Logger for environment events | ||
env_logger = logging.getLogger("env") | ||
env_logger.setLevel(logging.DEBUG) | ||
|
||
env_logger = colorlog.getLogger("env") | ||
env_logger.addHandler(handler) | ||
env_logger.setLevel(colorlog.DEBUG) | ||
|
||
|
||
def hello(): | ||
print("Hello. Just check that SCAutolib is imported") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from os.path import (dirname, abspath, join) | ||
|
||
import yaml | ||
from SCAutolib import env_logger | ||
from decouple import config | ||
|
||
DIR_PATH = dirname(abspath(__file__)) | ||
SETUP_IPA_SERVER = f"{DIR_PATH}/env/ipa-install-server.sh" | ||
|
||
|
||
def load_env(conf_file: str) -> str: | ||
""" | ||
Create .env near source files of the library. In .env file following | ||
variables expected to be present: CA_DIR, TMP, KEYS, CERTS, BACKUP. | ||
Deployment process would relay on this variables. | ||
Args: | ||
conf_file: path to YAML configuration fil | ||
Returns: | ||
Path to .env file. | ||
""" | ||
|
||
env_file = f"{DIR_PATH}/.env" | ||
with open(conf_file, "r") as f: | ||
env_logger.debug(f"Reading configurations from {conf_file}") | ||
data = yaml.load(f, Loader=yaml.FullLoader) | ||
ca_dir = data["ca_dir"] | ||
data["restore"] = [] | ||
|
||
with open(conf_file, "w") as f: | ||
yaml.dump(data, f) | ||
env_logger.debug("restore section is added to te configuration file") | ||
|
||
with open(env_file, "w") as f: | ||
f.write(f"TMP={join(ca_dir, 'tmp')}\n") | ||
f.write(f"KEYS={join(ca_dir, 'tmp', 'keys')}\n") | ||
f.write(f"CERTS={join(ca_dir, 'tmp', 'certs')}\n") | ||
f.write(f"BACKUP={join(ca_dir, 'tmp', 'backup')}\n") | ||
f.write(f"CONF={abspath(conf_file)}\n") | ||
f.write(f"CA_DIR={ca_dir}\n") | ||
env_logger.debug(f"File {env_file} is created") | ||
return env_file | ||
|
||
|
||
def read_env(item: str, *args, **kwargs): | ||
"""Just for unifying with read_conf function. Accepts all arguments that | ||
decouple.config() function takes. | ||
Args: | ||
item: variable to read from the .env file | ||
""" | ||
return config(item, *args, **kwargs) | ||
|
||
|
||
def read_config(*items) -> list or object: | ||
""" | ||
Read data from the configuration file and return require items or full | ||
content. | ||
Args: | ||
items: list of items to extracrt from the configuration file. | ||
If None, full contant would be returned | ||
Returns: | ||
list with required items | ||
""" | ||
try: | ||
with open(read_env("CONF"), "r") as file: | ||
config_data = yaml.load(file, Loader=yaml.FullLoader) | ||
assert config_data, "Data are not loaded correctly." | ||
except FileNotFoundError as e: | ||
env_logger.error(".env file is not present. Try to rerun command" | ||
"with --conf </path/to/conf.yaml> parameter") | ||
raise e | ||
|
||
if items is None: | ||
return config_data | ||
|
||
return_list = [] | ||
for item in items: | ||
parts = item.split(".") | ||
value = config_data | ||
for part in parts: | ||
if value is None: | ||
env_logger.warning( | ||
f"Key {part} not present in the configuration file. Skip.") | ||
return None | ||
|
||
value = value.get(part) | ||
if part == parts[-1]: | ||
return_list.append(value) | ||
|
||
return return_list if len(items) > 1 else return_list[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.