Restructured PHP extensions

This commit is contained in:
cytopia
2022-11-29 04:39:38 +01:00
parent c991ca9eaa
commit 0fbc57c154
19 changed files with 846 additions and 1017 deletions

View File

@@ -1,3 +1,3 @@
[defaults]
roles_path = ./roles
inventory = inventory
inventory = inventory.ini

View File

@@ -1,29 +0,0 @@
ifneq (,)
.error This Makefile requires GNU Make.
endif
# Ensure additional Makefiles are present
MAKEFILES = Makefile.python
$(MAKEFILES): URL=https://raw.githubusercontent.com/devilbox/makefiles/master/$(@)
$(MAKEFILES):
@if ! (curl --fail -sS -o $(@) $(URL) || wget -O $(@) $(URL)); then \
echo "Error, curl or wget required."; \
echo "Exiting."; \
false; \
fi
include $(MAKEFILES)
# -------------------------------------------------------------------------------------------------
# Default configuration
# -------------------------------------------------------------------------------------------------
MYPY_ARGS = --strict --disable-error-code no-any-return
PYLINT_DIR = get-modules.py
PYLINT_PIP_PKGS = yamllint
PYLINT_ARGS = --disable=invalid-name get-modules.py
PYCODE_ARGS = --max-line-length=100
BLACK_LINT_ARGS = -l 100 --check --diff
BLACK_FIX_ARGS = -l 100

View File

@@ -1,198 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Generate Ansible group_vars from module definition."""
import os
import sys
from collections import OrderedDict
from typing import Dict, List, Any
import yaml
# --------------------------------------------------------------------------------------------------
# GLOBALS
# --------------------------------------------------------------------------------------------------
SCRIPT_PATH = str(os.path.dirname(os.path.realpath(__file__)))
REPOSITORY_PATH = str(os.path.dirname(os.path.dirname(SCRIPT_PATH)))
PHP_MODULE_PATH = str(os.path.join(REPOSITORY_PATH, "php_modules"))
GROUP_VARS_PATH = str(os.path.join(REPOSITORY_PATH, ".ansible", "group_vars", "all"))
# --------------------------------------------------------------------------------------------------
# HELPER FUNCTIONS
# --------------------------------------------------------------------------------------------------
def get_el_by_name(items: List[Dict[str, Any]], name: str) -> Dict[str, Any]:
"""Returns an element from a dict list by its 'name' key with given value."""
for item in items:
if item["name"] == name:
return item
print("error, key name not found by value", name, "in list: ", items)
sys.exit(1)
def load_yaml(path: str) -> Dict[str, Any]:
"""Load yaml file and return its dict()."""
with open(path, encoding="utf8") as fp:
data = yaml.safe_load(fp)
return data
# --------------------------------------------------------------------------------------------------
# MODULE FUNCTIONS
# --------------------------------------------------------------------------------------------------
def get_module_options(module_dirname: str) -> Dict[str, Any]:
"""Returns yaml dict options of a PHP module given by its absolute file path."""
return load_yaml(os.path.join(PHP_MODULE_PATH, module_dirname, "options.yml"))
def get_module_build(module_dirname: str) -> Dict[str, Any]:
"""Returns yaml dict build configuration of a PHP module given by its absolute file path."""
return load_yaml(os.path.join(PHP_MODULE_PATH, module_dirname, "build.yml"))
def get_module_test(module_dirname: str) -> Dict[str, Any]:
"""Returns yaml dict test configuration of a PHP module given by its absolute file path."""
return load_yaml(os.path.join(PHP_MODULE_PATH, module_dirname, "test.yml"))
def get_modules() -> List[Dict[str, Any]]:
"""Returns a list of PHP module directory names."""
modules = []
with os.scandir(PHP_MODULE_PATH) as it:
for item in it:
if not item.name.startswith(".") and item.is_dir():
data = get_module_options(item.name)
modules.append({"dir": item.name, "name": data["name"], "deps": data["requires"]})
# Convert list of deps into dict(dir, name, deps)
items = []
for module in modules:
if module["deps"]:
deps = []
for dep in module["deps"]:
deps.append(get_el_by_name(modules, dep))
module["deps"] = deps
items.append(module)
else:
items.append(module)
return sorted(items, key=lambda item: item["dir"])
def get_module_dependency_tree(modules: List[Dict[str, Any]]) -> OrderedDict[str, Any]:
"""Returns dictionary of module dependency tree."""
module_tree = OrderedDict() # type: OrderedDict[str, Any]
for module in modules:
mod_name = module["name"]
mod_deps = module["deps"]
module_tree[mod_name] = {}
# Do we have module requirements?
if len(mod_deps) > 0:
module_tree[mod_name] = get_module_dependency_tree(mod_deps)
return module_tree
def resolve_module_dependency_tree(tree: OrderedDict[str, Any]) -> List[str]:
"""Returns sorted list of resolved dependencies."""
resolved = []
for key, _ in tree.items():
# Has dependenies
if tree[key]:
childs = resolve_module_dependency_tree(tree[key])
for child in childs:
if child not in resolved:
resolved.append(child)
# Add current node, if not already available
if key not in resolved:
resolved.append(key)
return resolved
# --------------------------------------------------------------------------------------------------
# PRINT FUNCTIONS
# --------------------------------------------------------------------------------------------------
def print_modules(modules: List[Dict[str, Any]]) -> None:
"""Print directory modules."""
for module in modules:
print(module["dir"] + "/")
print(" name:", module["name"])
print(" deps:", end=" ")
for dep in module["deps"]:
print(dep["name"], end=", ")
print()
def print_dependency_tree(tree: Dict[str, Any], lvl: int = 0) -> None:
"""Print dependency tree of modules."""
for key, value in tree.items():
print(" " * lvl, "-", key)
if value:
print_dependency_tree(tree[key], lvl + 2)
# --------------------------------------------------------------------------------------------------
# WRITE ANSIBLE GROUP_VARS FUNCTIONS
# --------------------------------------------------------------------------------------------------
def write_group_vars(module_names: List[str]) -> None:
"""Write mods.yml group_vars for ansible."""
group_vars = os.path.join(GROUP_VARS_PATH, "mods.yml")
with open(group_vars, "w", encoding="utf8") as fp:
fp.write("---\n\n")
fp.write("# Do not alter this file, it is autogenerated\n\n")
fp.write("modules:\n")
for name in module_names:
fp.write(" - " + name + "\n")
# --------------------------------------------------------------------------------------------------
# MAIN FUNCTION
# --------------------------------------------------------------------------------------------------
def main() -> None:
"""Main entrypoint."""
# Get modules in order of dependencies
modules = get_modules()
module_tree = get_module_dependency_tree(modules)
names = resolve_module_dependency_tree(module_tree)
print("#", "-" * 78)
print("# Paths")
print("#", "-" * 78)
print("Repository: ", REPOSITORY_PATH)
print("PHP Module: ", PHP_MODULE_PATH)
print("Group Vars: ", GROUP_VARS_PATH)
print()
print("#", "-" * 78)
print("# Module directories")
print("#", "-" * 78)
print_modules(modules)
print()
print("#", "-" * 78)
print("# Build Dependency Tree")
print("#", "-" * 78)
print_dependency_tree(module_tree)
print()
print("#", "-" * 78)
print("# Build order")
print("#", "-" * 78)
print(names)
# Create group_vars file mods.yml
write_group_vars(names)
if __name__ == "__main__":
main()

View File

@@ -42,13 +42,13 @@ php_all_versions:
# -------------------------------------------------------------------------------------------------
template_dockerfiles:
- src: DOCKERFILES/Dockerfile-base.j2
dst: "../../Dockerfiles/base/Dockerfile-{{ php_version }}"
dst: "../Dockerfiles/base/Dockerfile-{{ php_version }}"
- src: DOCKERFILES/Dockerfile-mods.j2
dst: "../../Dockerfiles/mods/Dockerfile-{{ php_version }}"
dst: "../Dockerfiles/mods/Dockerfile-{{ php_version }}"
- src: DOCKERFILES/Dockerfile-prod.j2
dst: "../../Dockerfiles/prod/Dockerfile-{{ php_version }}"
dst: "../Dockerfiles/prod/Dockerfile-{{ php_version }}"
- src: DOCKERFILES/Dockerfile-work.j2
dst: "../../Dockerfiles/work/Dockerfile-{{ php_version }}"
dst: "../Dockerfiles/work/Dockerfile-{{ php_version }}"
# -------------------------------------------------------------------------------------------------
@@ -57,23 +57,23 @@ template_dockerfiles:
template_configurations:
# php.ini
- src: CONFIGURATIONS/php.ini.j2
dst: "../../Dockerfiles/base/data/php-ini.d/php-{{ php_version }}.ini"
dst: "../Dockerfiles/base/data/php-ini.d/php-{{ php_version }}.ini"
cfg: "{{ php_settings_ini }}"
key: base
alt: base
- src: CONFIGURATIONS/php.ini.j2
dst: "../../Dockerfiles/work/data/php-ini.d/php-{{ php_version }}.ini"
dst: "../Dockerfiles/work/data/php-ini.d/php-{{ php_version }}.ini"
cfg: "{{ php_settings_ini }}"
key: work
alt: base # Alternative key to use when definition is not set in 'work'
# php-fpm.conf
- src: CONFIGURATIONS/php-fpm.conf.j2
dst: "../../Dockerfiles/base/data/php-fpm.conf/php-fpm-{{ php_version }}.conf"
dst: "../Dockerfiles/base/data/php-fpm.conf/php-fpm-{{ php_version }}.conf"
cfg: "{{ php_settings_fpm }}"
key: base
alt: base
- src: CONFIGURATIONS/php-fpm.conf.j2
dst: "../../Dockerfiles/work/data/php-fpm.conf/php-fpm-{{ php_version }}.conf"
dst: "../Dockerfiles/work/data/php-fpm.conf/php-fpm-{{ php_version }}.conf"
cfg: "{{ php_settings_fpm }}"
key: work
alt: base