Skip to content

Commit

Permalink
Merge pull request #32 from intel/systempath
Browse files Browse the repository at this point in the history
Add support for installing cmt to the system path
  • Loading branch information
dweineha authored Oct 17, 2024
2 parents c6b3437 + a7d6b0b commit bad8904
Show file tree
Hide file tree
Showing 371 changed files with 19,951 additions and 2,277 deletions.
1 change: 1 addition & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
skip_list:
- experimental
- yaml[line-length]
2 changes: 1 addition & 1 deletion .bandit
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
# IPAS Required Checkers. Do not disable these
# Additional checkers may be added if desired
tests:
[ 'B301', 'B302', 'B303', 'B304', 'B305', 'B306', 'B308', 'B309', 'B310', 'B311', 'B312', 'B313', 'B314', 'B315', 'B316', 'B317', 'B318', 'B319', 'B320', 'B321', 'B323', 'B324', 'B325', 'B401', 'B402', 'B403', 'B404', 'B405', 'B406', 'B407', 'B408', 'B409', 'B410', 'B411', 'B412', 'B413']
[ 'B301', 'B302', 'B303', 'B304', 'B305', 'B306', 'B308', 'B310', 'B311', 'B312', 'B313', 'B314', 'B315', 'B316', 'B317', 'B318', 'B319', 'B320', 'B321', 'B323', 'B324', 'B401', 'B402', 'B403', 'B404', 'B405', 'B406', 'B407', 'B408', 'B409', 'B410', 'B411', 'B412', 'B413']

# (optional) list skipped test IDs here, eg '[B101, B406]':
# The following checkers are not required but be added to tests list if desired
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ parsers/BUNDLE.yaml
tests/testpaths
tests/testlogs
tests/modified_repo
tests/*.py
tests/clustermanagementtoolkit
.coverage
coverage.json
htmlcov
26 changes: 0 additions & 26 deletions .yamllint

This file was deleted.

100 changes: 42 additions & 58 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ python_executables = \
cmt-install \
cmtinv \
cmu
python_data_coverage = \
helptexts.py \
recommended_permissions.py \
pvtypes.py
python_test_executables = \
tests/async_fetch \
tests/ansibletests \
Expand All @@ -19,7 +15,10 @@ python_test_executables = \
tests/cmtlibtests \
tests/cnitests \
tests/coverage_stats \
tests/cnitests \
tests/cursestests \
tests/datatests \
tests/dgtests \
tests/dump_cluster \
tests/dump_logs \
tests/fgtests \
Expand All @@ -46,6 +45,7 @@ python_unit_tests = \
tests/cmtlibtests \
tests/cnitests \
tests/cursestests \
tests/datatests \
tests/dgtests \
tests/fgtests \
tests/fmttests \
Expand All @@ -59,38 +59,7 @@ python_unit_tests = \
tests/ogtests \
tests/typetests \
tests/validatortests
test_lib_symlinks = \
about.py \
ansible_helper.py \
ansithemeprint.py \
checks.py \
cmtio.py \
cmtio_yaml.py \
cmtlib.py \
cmtpaths.py \
cmttypes.py \
cmtvalidators.py \
cluster_actions.py \
cni_data.py \
commandparser.py \
curses_helper.py \
datagetters.py \
fieldgetters.py \
formatters.py \
generators.py \
helptexts.py \
infogetters.py \
itemgetters.py \
kubernetes_helper.py \
kubernetes_resources.py \
listgetters.py \
listgetters_async.py \
logparser.py \
networkio.py \
objgetters.py \
pvtypes.py \
recommended_permissions.py \
reexecutor.py
test_libs_symlink = clustermanagementtoolkit

# F841 is the warning about unused assignments.
# flake8 doesn't recognise "_<variable>" to capture unused return values;
Expand Down Expand Up @@ -125,11 +94,6 @@ coverage: setup_tests
@cmd=python3-coverage ;\
printf -- "\n\n Running: tests/atptests --include-clear\n\n" ;\
$$cmd run --branch --append tests/atptests --include-clear --end-at 0 || exit 1 ;\
printf -- "\n\nRunning python3-coverage to check test coverage on data\n" ;\
for test in $(python_data_coverage); do \
printf -- "\n\n Running: $$test\n\n" ;\
$$cmd run --branch --append $$test || exit 1 ;\
done ;\
printf -- "\n\nRunning python3-coverage to check test coverage\n" ;\
for test in $(python_unit_tests); do \
printf -- "\n\n Running: $$test\n\n" ;\
Expand Down Expand Up @@ -231,7 +195,7 @@ bandit:
exit 0 ;\
fi ;\
printf -- "\n\nRunning bandit to check for common security issues in Python code\n\n" ;\
$$cmd -c .bandit $(python_executables) $(python_test_executables) *.py
$$cmd -c .bandit $(python_executables) $(python_test_executables) clustermanagementtoolkit/*.py

ruff:
@cmd=ruff ;\
Expand All @@ -240,7 +204,7 @@ ruff:
exit 0 ;\
fi ;\
printf -- "\n\nRunning $$cmd to check Python code quality\n\n" ;\
for file in $(python_executables) *.py; do \
for file in $(python_executables) clustermanagementtoolkit/*.py; do \
case $$file in \
'cmtlog.py'|'noxfile.py') \
continue;; \
Expand All @@ -256,7 +220,7 @@ pylint:
exit 0 ;\
fi ;\
printf -- "\n\nRunning pylint to check Python code quality\n\n" ;\
for file in $(python_executables) *.py; do \
for file in $(python_executables) clustermanagementtoolkit/*.py; do \
case $$file in \
'cmtlog.py'|'noxfile.py') \
continue;; \
Expand All @@ -272,7 +236,7 @@ pylint-markdown:
exit 0 ;\
fi ;\
printf -- "\n\nRunning pylint to generate Pylint Markdown output\n\n" ;\
for file in $(python_executables) *.py; do \
for file in $(python_executables) clustermanagementtoolkit/*.py; do \
case $$file in \
'cmtlog.py'|'noxfile.py') \
continue;; \
Expand All @@ -298,7 +262,7 @@ flake8:
exit 0 ;\
fi ;\
printf -- "\n\nRunning flake8 to check Python code quality\n\n" ;\
$$cmd --ignore $(FLAKE8_IGNORE) --max-line-length 100 --statistics $(python_executables) *.py && printf -- "OK\n\n" ;\
$$cmd --ignore $(FLAKE8_IGNORE) --max-line-length 100 --statistics $(python_executables) clustermanagementtoolkit/*.py && printf -- "OK\n\n" ;\
printf -- "\n\nRunning flake8 to check Python test case code quality\n\n" ;\
$$cmd --ignore $(FLAKE8_IGNORE) --max-line-length 100 --statistics $(python_test_executables) && printf -- "OK\n\n"

Expand All @@ -312,7 +276,7 @@ regexploit:
printf -- "Checking executables\n" ;\
$$cmd $(python_executables) $(python_test_executables) &&\
printf -- "\nChecking libraries\n" ;\
$$cmd *.py
$$cmd clustermanagementtoolkit/*.py

yamllint:
@cmd=yamllint ;\
Expand All @@ -335,7 +299,7 @@ mypy:
exit 0; \
fi; \
printf -- "\n\nRunning mypy to check Python typing\n\n"; \
for file in $(python_executables) $(python_test_executables) *.py; do \
for file in $(python_executables) $(python_test_executables) clustermanagementtoolkit/*.py; do \
$$cmd --ignore-missing --disallow-untyped-calls --disallow-untyped-defs --disallow-incomplete-defs --check-untyped-defs --disallow-untyped-decorators $$file || true; \
done

Expand All @@ -348,7 +312,7 @@ mypy-markdown:
exit 0; \
fi; \
printf -- "\n\nRunning mypy to check Python typing\n\n"; \
for file in $(python_executables) $(python_test_executables) *.py; do \
for file in $(python_executables) $(python_test_executables) clustermanagementtoolkit/*.py; do \
case $$file in \
'cmtlog.py'|'noxfile.py') \
continue;; \
Expand Down Expand Up @@ -393,16 +357,10 @@ parser_bundle:
done

remove_test_symlinks:
@(cd tests ;\
for file in $(test_lib_symlinks); do \
rm -f $$file; \
done)
@(cd tests; rm -f $(test_libs_symlink))

create_test_symlinks:
@(cd tests ;\
for file in $(test_lib_symlinks); do \
test -L $$file || ln -s ../$$file . ;\
done)
@(cd tests; test -L $(test_libs_symlink) || ln -s ../$(test_libs_symlink) .)

setup_tests: create_test_symlinks
@(cd tests ;\
Expand Down Expand Up @@ -477,5 +435,31 @@ check_theme_use: setup_tests
for theme in themes/*.yaml; do \
printf -- "\nChecking against theme file $$theme:\n" ;\
printf -- "---\n" ;\
./tests/check_theme_use $$theme $(python_executables) $(python_test_executables) *.py ;\
./tests/check_theme_use $$theme $(python_executables) $(python_test_executables) clustermanagementtoolkit/*.py ;\
done

build:
./build.py views/templates views/variables views

# This rule is used when making a system-wide install
INSTALL := install --mode=755
INSTALL_DATA := install --mode=644
INSTALL_DIRECTORY := install -d
BASH_COMPLETION_DIR := /usr/share/bash-completion/completions
CMT_CONFIG_DIR := /etc/cmt
CMT_CONFIGLET_DIR := $(CMT_CONFIG_DIR)/cmt.yaml.d
CMT_DATA_DIR := /usr/share/cluster-management-toolkit
DIST_PACKAGE_DIR := /usr/lib/python3/dist-packages
BINDIR := /usr/bin

install:
@$(INSTALL_DIRECTORY) $(DESTDIR)$(BASH_COMPLETION_DIR) &&\
$(INSTALL_DIRECTORY) $(DESTDIR)$(CMT_CONFIGLET_DIR) &&\
$(INSTALL_DIRECTORY) $(DESTDIR)$(CMT_DATA_DIR) &&\
$(INSTALL_DIRECTORY) $(DESTDIR)$(DIST_PACKAGE_DIR) &&\
$(INSTALL_DIRECTORY) $(DESTDIR)$(BINDIR) &&\
$(INSTALL_DATA) cmt.yaml $(DESTDIR)$(CMT_CONFIG_DIR) &&\
$(INSTALL) cmt cmtadm cmtinv cmu $(DESTDIR)$(BINDIR) &&\
tar cf - --exclude=.*.swp clustermanagementtoolkit | (cd $(DESTDIR)$(DIST_PACKAGE_DIR); tar xf -) &&\
tar cf - --exclude=.*.swp parsers playbooks sources themes | (cd $(DESTDIR)$(CMT_DATA_DIR); tar xf -) &&\
cp views/*.yaml $(DESTDIR)$(CMT_DATA_DIR)/views || printf -- "Installation failed.\n"
2 changes: 1 addition & 1 deletion bash-completion/cmt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ add_option_if_not_in_list() {
_cmt() {
commands=""
cri_list="docker-shim containerd cri-o"
known_list="known unknown not-known list not-list info not-info"
known_list="known unknown not-known updated list not-list info not-info"
kubernetes_distro_list="kubeadm rke2"
sort_columns="name apiversion namespaced kind"

Expand Down
1 change: 1 addition & 0 deletions bash-completion/cmtadm
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ _cmtadm() {
upgrade-control-plane)
commands=$(add_option_if_not_in_list "--override" ${commands})
commands=$(add_option_if_not_in_list "--reinstall" ${commands})
commands=$(add_option_if_not_in_list "--ignore-feature-gates" ${commands})
esac

case "${COMP_WORDS[1]}" in
Expand Down
82 changes: 82 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#! /bin/sh
# vim: ts=4 filetype=python expandtab shiftwidth=4 softtabstop=4 syntax=python
# pylint: disable-next=anomalous-backslash-in-string
''''eval version=$( ls /usr/bin/python3.* | \
grep '.*[0-9]$' | sort -nr -k2 -t. | head -n1 ) && \
version=${version##/usr/bin/python3.} && [ ${version} ] && \
[ ${version} -ge 8 ] && exec /usr/bin/python3.${version} "$0" "$@" || \
exec /usr/bin/env python3 "$0" "$@"' #'''
# The above hack is to handle distros where /usr/bin/python3
# doesn't point to the latest version of python3 they provide

# Requires: python3 (>= 3.8)
# Requires: python3-jinja2
import os
from pathlib import Path, PosixPath
import sys
import yaml

try: # pragma: no cover
from jinja2 import Environment, FileSystemLoader
except ModuleNotFoundError: # pragma: no cover
sys.exit("ModuleNotFoundError: Could not import jinja2; "
"you may need to (re-)run `cmt-install` or `pip3 install jinja2`; aborting.")


def main() -> None:
"""
Main function for the program.
"""

# Before doing anything else, make sure that the user is not running as root
if os.geteuid() == 0:
sys.exit("CRITICAL: This program should not be run as the root user; aborting.")

# This program should be called with the path to the directory to process .j2 files in
# as well as a path to the directory that holds the variables to use in substitutions.
if not (2 < len(sys.argv) < 5):
sys.exit(f"Usage: build.py TEMPLATE_DIRECTORY VARIABLE_DIRECTORY [OUTPUT_DIRECTORY]")
template_path = sys.argv[1]
variable_path = sys.argv[2]
if len(sys.argv) > 3:
output_path = sys.argv[3]
else:
output_path = template_path
if template_path == variable_path:
sys.exit(f"TEMPLATE_DIRECTORY cannot be same as VARIABLE_DIRECTORY; aborting.")
template_entry = Path(template_path)
variable_entry = Path(variable_path)
output_entry = Path(output_path)

if not template_entry.is_dir():
sys.exit(f"{template_path} is not a directory; aborting.")
if not variable_entry.is_dir():
sys.exit(f"{variable_path} is not a directory; aborting.")
if not output_entry.is_dir():
sys.exit(f"{output_path} is not a directory; aborting.")

# Load variables
context = {}
for filepath in variable_entry.iterdir():
if not filepath.is_file() or not filepath.name.endswith(".var"):
continue
string: str = ""
with open(filepath, "r", encoding="utf-8", errors="replace") as f:
tmp = f.read()
context[filepath.name[:-len(".var")]] = tmp[:-1]

# Initialise Jinja2
environment = Environment(loader=FileSystemLoader(template_path), keep_trailing_newline=True)

for filepath in template_entry.iterdir():
if not filepath.name.endswith(".j2"):
continue
template = environment.get_template(filepath.name)
dest_filename = filepath.name[:-len(".j2")]
with open(str(PosixPath(output_path).joinpath(dest_filename)),
mode="w", encoding="utf-8") as f:
f.write(template.render(context))


if __name__ == "__main__":
main()
10 changes: 5 additions & 5 deletions about.py → clustermanagementtoolkit/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
PROGRAM_SUITE_VERSION = "0.8.4"

UI_PROGRAM_NAME = "cmu"
UI_PROGRAM_VERSION = "0.5.1"
UI_PROGRAM_VERSION = "0.5.2"

TOOL_PROGRAM_NAME = "cmt"
TOOL_PROGRAM_VERSION = "0.6.5"
TOOL_PROGRAM_VERSION = "0.6.6"

INSTALL_PROGRAM_NAME = "cmt-install"
INSTALL_PROGRAM_VERSION = "0.13.6"
INSTALL_PROGRAM_VERSION = "0.13.7"

ADMIN_PROGRAM_NAME = "cmtadm"
ADMIN_PROGRAM_VERSION = "0.9.2"
ADMIN_PROGRAM_VERSION = "0.9.3"

INVENTORY_PROGRAM_NAME = "cmtinv"
INVENTORY_PROGRAM_VERSION = "0.4.5"
INVENTORY_PROGRAM_VERSION = "0.4.6"

# We don't support Python-versions older than 3.8
version_info = sys.version_info
Expand Down
Loading

0 comments on commit bad8904

Please sign in to comment.