feature: made new static path variables and dictionaries containing paths
This commit is contained in:
50
whereami.py
50
whereami.py
@@ -2,25 +2,61 @@
|
|||||||
Library of path constants to be used or referenced elsewhere.
|
Library of path constants to be used or referenced elsewhere.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from custtypes import ExecutedPath, Roles, Scopes, AnsibleScopes
|
from custtypes import ExecutedPath, Roles, Scopes, AnsibleScopes, NodeType, UserName
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from configparser import ConfigParser as cfg
|
||||||
|
from itertools import chain
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
USER_PATH: ExecutedPath = Path.home()
|
def get_home(node: NodeType = NodeType.control, home: UserName | str | None = None) -> ExecutedPath:
|
||||||
|
if node == NodeType.control:
|
||||||
|
return Path.home()
|
||||||
|
else:
|
||||||
|
if home is None:
|
||||||
|
return Path("~")
|
||||||
|
else:
|
||||||
|
if isinstance(home, UserName):
|
||||||
|
return Path("/home") / home.name
|
||||||
|
else:
|
||||||
|
return Path(home)
|
||||||
|
|
||||||
|
USER_PATH: Callable = get_home
|
||||||
PROJ_ROOT: ExecutedPath = Path(__file__).parent.resolve()
|
PROJ_ROOT: ExecutedPath = Path(__file__).parent.resolve()
|
||||||
PROJ_ROLES: ExecutedPath = PROJ_ROOT / "roles"
|
|
||||||
|
|
||||||
|
config = cfg()
|
||||||
|
ANSIBLE_CONFIG = PROJ_ROOT / "ansible.cfg"
|
||||||
|
|
||||||
|
if ANSIBLE_CONFIG.exists():
|
||||||
|
config.read(str(ANSIBLE_CONFIG))
|
||||||
|
role_candidates = config["defaults"]["roles_path"].split(":")
|
||||||
|
role_candidates = map(lambda s: PROJ_ROOT / s, role_candidates)
|
||||||
|
role_paths = list(filter(lambda p: p.exists(), role_candidates))
|
||||||
|
|
||||||
|
inv_candidates = config["defaults"]["inventory"].split(",")
|
||||||
|
inv_candidates = map(lambda s: PROJ_ROOT / s, inv_candidates)
|
||||||
|
inv_paths = list(filter(lambda p: p.exists(), inv_candidates))
|
||||||
|
else:
|
||||||
|
role_paths = [PROJ_ROOT / "roles"]
|
||||||
|
inv_paths = PROJ_ROOT.glob("hosts*.*")
|
||||||
|
PROJ_ROLES: list[ExecutedPath] = role_paths
|
||||||
|
PROJ_INVENTORIES: list[ExecutedPath] = inv_paths
|
||||||
|
|
||||||
|
proj_paths = map(lambda r: r.glob("**/files"), PROJ_ROLES)
|
||||||
|
proj_paths = list(chain.from_iterable(proj_paths))
|
||||||
|
template_paths = map(lambda r: r.glob("**/templates"), PROJ_ROLES)
|
||||||
|
template_paths = list(chain.from_iterable(template_paths))
|
||||||
APP_ROOTS = {
|
APP_ROOTS = {
|
||||||
Roles.CONF.name.lower(): {
|
Roles.CONF.name.lower(): {
|
||||||
Scopes.SYS.name.lower(): Path("/etc"),
|
Scopes.SYS.name.lower(): Path("/etc"),
|
||||||
Scopes.USER.name.lower(): USER_PATH / ".config",
|
Scopes.USER.name.lower(): USER_PATH(NodeType.remote) / ".config",
|
||||||
Scopes.SHARED.name.lower(): Path("/usr/share"),
|
Scopes.SHARED.name.lower(): Path("/usr/share"),
|
||||||
Scopes.PROJ.name.lower(): tuple(list(PROJ_ROLES.glob("**/files")) + list(PROJ_ROLES.glob("**/templates")))
|
Scopes.PROJ.name.lower(): tuple(proj_paths + template_paths)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ANSIBLE_ROOTS = {
|
ANSIBLE_ROOTS = {
|
||||||
AnsibleScopes.GROUPVARS.name.lower(): PROJ_ROOT / "group_vars",
|
AnsibleScopes.GROUPVARS.name.lower(): PROJ_ROOT / "group_vars",
|
||||||
AnsibleScopes.HOSTVARS.name.lower(): PROJ_ROOT / "host_vars",
|
AnsibleScopes.HOSTVARS.name.lower(): PROJ_ROOT / "host_vars",
|
||||||
AnsibleScopes.INVENTORY.name.lower(): None,
|
AnsibleScopes.INVENTORY.name.lower(): tuple(PROJ_INVENTORIES),
|
||||||
AnsibleScopes.ROLE.name.lower(): None
|
AnsibleScopes.ROLE.name.lower(): tuple(PROJ_ROLES)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user