From bccaf9352e5020b47000de67f7b46d1bee8d5676 Mon Sep 17 00:00:00 2001 From: Istvan Orban Date: Mon, 3 Jul 2023 19:07:40 +0100 Subject: [PATCH] upgrade molecule wrapper --- .moleculew/ansible_lint_version | 1 + .moleculew/ansible_version | 2 +- .moleculew/flake8_version | 1 + .moleculew/molecule_version | 2 +- .moleculew/python_version | 2 +- .moleculew/testinfra_version | 1 + moleculew | 342 ++++++++++++++++++++++++-------- tasks/configure-groups.yml | 1 + 8 files changed, 266 insertions(+), 86 deletions(-) create mode 100644 .moleculew/ansible_lint_version create mode 100644 .moleculew/flake8_version create mode 100644 .moleculew/testinfra_version diff --git a/.moleculew/ansible_lint_version b/.moleculew/ansible_lint_version new file mode 100644 index 0000000..e198586 --- /dev/null +++ b/.moleculew/ansible_lint_version @@ -0,0 +1 @@ +4.3.5 diff --git a/.moleculew/ansible_version b/.moleculew/ansible_version index dedcc7d..b630701 100644 --- a/.moleculew/ansible_version +++ b/.moleculew/ansible_version @@ -1 +1 @@ -2.9.1 +2.10.7 diff --git a/.moleculew/flake8_version b/.moleculew/flake8_version new file mode 100644 index 0000000..1454f6e --- /dev/null +++ b/.moleculew/flake8_version @@ -0,0 +1 @@ +4.0.1 diff --git a/.moleculew/molecule_version b/.moleculew/molecule_version index 4699fb0..3ad0595 100644 --- a/.moleculew/molecule_version +++ b/.moleculew/molecule_version @@ -1 +1 @@ -2.22 +3.1.5 diff --git a/.moleculew/python_version b/.moleculew/python_version index f24054f..d20cc2b 100644 --- a/.moleculew/python_version +++ b/.moleculew/python_version @@ -1 +1 @@ -2.7.15 +3.8.10 diff --git a/.moleculew/testinfra_version b/.moleculew/testinfra_version new file mode 100644 index 0000000..c7cb131 --- /dev/null +++ b/.moleculew/testinfra_version @@ -0,0 +1 @@ +5.3.1 diff --git a/moleculew b/moleculew index 952183b..d0e31b1 100755 --- a/moleculew +++ b/moleculew @@ -28,12 +28,16 @@ set -e -WRAPPER_VERSION=0.9.12 +WRAPPER_VERSION=1.2.0 VERSION_DIR='.moleculew' PYTHON_VERSION_FILE="$VERSION_DIR/python_version" ANSIBLE_VERSION_FILE="$VERSION_DIR/ansible_version" MOLECULE_VERSION_FILE="$VERSION_DIR/molecule_version" +YAMLLINT_VERSION_FILE="$VERSION_DIR/yamllint_version" +ANSIBLE_LINT_VERSION_FILE="$VERSION_DIR/ansible_lint_version" +FLAKE8_VERSION_FILE="$VERSION_DIR/flake8_version" +TESTINFRA_VERSION_FILE="$VERSION_DIR/testinfra_version" BUILD_DEPENDENCIES_INSTALLLED=false PYENV_INSTALLED=false @@ -41,6 +45,10 @@ PYENV_INSTALLED=false ANSIBLE_VERSION='' MOLECULE_VERSION='' PYTHON_VERSION='' +YAMLLINT_VERSION='' +ANSIBLE_LINT_VERSION='' +FLAKE8_VERSION='' +TESTINFRA_VERSION='' USE_SYSTEM_DEPENDENCIES=false PRE_ARGS=() @@ -139,13 +147,7 @@ pyenv_present() { PYENV_INSTALLED=true } -query_latest_python_version2() { - pyenv_present - - PYTHON_VERSION="$(~/.pyenv/plugins/python-build/bin/python-build --definitions | grep --color=never '^2\.' | grep --invert-match '\-dev$' | tail -1)" -} - -query_latest_python_version3() { +query_latest_python_version() { pyenv_present PYTHON_VERSION="$(~/.pyenv/plugins/python-build/bin/python-build --definitions | grep --color=never '^3\.' | grep --invert-match '\-dev$' | tail -1)" @@ -179,7 +181,7 @@ docker_present() { return fi if [[ $USE_SYSTEM_DEPENDENCIES == true ]]; then - echo 'Error: docker is not installed.' >&2 + echo 'Error: Docker is not installed.' >&2 exit 1 fi @@ -194,15 +196,17 @@ docker_present() { python_present() { if [[ $PYTHON_VERSION == system ]]; then - if [[ ! -x "$(command -v python)" ]]; then + if [[ ! -x "$(command -v python3)" ]] && + [[ ! -x "$(command -v python)" ]]; then echo 'Error: python is not installed.' >&2 exit 1 fi - if [[ ! -x "$(command -v pip)" ]]; then + if [[ ! -x "$(command -v pip3)" ]] && + [[ ! -x "$(command -v pip)" ]]; then echo 'Error: pip is not installed.' >&2 exit 1 fi - PYTHON_EXE="$(command -v python)" + PYTHON_EXE="$(command -v python3 || command -v python)" else if [[ ! -x "$(command -v git)" ]]; then echo 'Error: git is not installed.' >&2 @@ -227,11 +231,19 @@ python_present() { virtualenv_presant() { if [[ ! -x "$(command -v virtualenv)" ]]; then banner "Installing virtualenv for user $USER" - pip install --user virtualenv + "$PYTHON_EXE" -m pip install --user virtualenv echo '' fi } +install_rich() { + # Workaround breaking changes in rich 11 by installing version 10 + local RICH_VERSION='10.16.2' + banner "Installing Rich $RICH_VERSION into virtualenv $VIRTUAL_ENV" + pip install "rich==$RICH_VERSION" + echo '' +} + install_ansible() { banner "Installing Ansible $ANSIBLE_VERSION into virtualenv $VIRTUAL_ENV" pip install "ansible==$ANSIBLE_VERSION" @@ -241,13 +253,38 @@ install_ansible() { install_molecule() { banner "Installing Molecule $MOLECULE_VERSION into virtualenv $VIRTUAL_ENV" - # Workaround https://github.com/ansible-community/molecule/issues/2676 - pip install 'sh==1.12.14' - pip install "molecule[docker]==$MOLECULE_VERSION" echo '' } +install_yamllint() { + banner "Installing YamlLint $YAMLLINT_VERSION into virtualenv $VIRTUAL_ENV" + + pip install "yamllint==$YAMLLINT_VERSION" + echo '' +} + +install_ansible_lint() { + banner "Installing Anssible Lint $ANSIBLE_LINT_VERSION into virtualenv $VIRTUAL_ENV" + + pip install "ansible-lint==$ANSIBLE_LINT_VERSION" + echo '' +} + +install_flake8() { + banner "Installing Flake8 $FLAKE8_VERSION into virtualenv $VIRTUAL_ENV" + + pip install "flake8==$FLAKE8_VERSION" + echo '' +} + +install_testinfra() { + banner "Installing Testinfra $TESTINFRA_VERSION into virtualenv $VIRTUAL_ENV" + + pip install "testinfra==$TESTINFRA_VERSION" + echo '' +} + wrapper_clean() { local MOLECULE_WRAPPER_HOME="$HOME/.moleculew" read -r -p "Delete ${MOLECULE_WRAPPER_HOME} (y/n)? " yn @@ -287,6 +324,10 @@ print_versions() { echo "Python: $PYTHON_VERSION" echo "Ansible: $ANSIBLE_VERSION" echo "Molecule: $MOLECULE_VERSION" + echo "YamlLint: $YAMLLINT_VERSION" + echo "Ansible Lint: $ANSIBLE_LINT_VERSION" + echo "Flake8: $FLAKE8_VERSION" + echo "Testinfra: $TESTINFRA_VERSION" } wrapper_versions() { @@ -306,6 +347,10 @@ wrapper_freeze() { echo "$PYTHON_VERSION" > "$PYTHON_VERSION_FILE" echo "$ANSIBLE_VERSION" > "$ANSIBLE_VERSION_FILE" echo "$MOLECULE_VERSION" > "$MOLECULE_VERSION_FILE" + echo "$YAMLLINT_VERSION" > "$YAMLLINT_VERSION_FILE" + echo "$ANSIBLE_LINT_VERSION" > "$ANSIBLE_LINT_VERSION_FILE" + echo "$FLAKE8_VERSION" > "$FLAKE8_VERSION_FILE" + echo "$TESTINFRA_VERSION" > "$TESTINFRA_VERSION_FILE" print_versions @@ -324,6 +369,19 @@ wrapper_unfreeze() { if [[ -f "$MOLECULE_VERSION_FILE" ]]; then rm --verbose "$MOLECULE_VERSION_FILE" fi + if [[ -f "$YAMLLINT_VERSION_FILE" ]]; then + rm --verbose "$YAMLLINT_VERSION_FILE" + fi + if [[ -f "$ANSIBLE_LINT_VERSION_FILE" ]]; then + rm --verbose "$ANSIBLE_LINT_VERSION_FILE" + fi + if [[ -f "$FLAKE8_VERSION_FILE" ]]; then + rm --verbose "$FLAKE8_VERSION_FILE" + fi + if [[ -f "$TESTINFRA_VERSION_FILE" ]]; then + rm --verbose "$TESTINFRA_VERSION_FILE" + fi + exit } @@ -335,10 +393,18 @@ wrapper_upgrade_versions() { local CURRENT_PYTHON_VERSION="$PYTHON_VERSION" local CURRENT_ANSIBLE_VERSION="$ANSIBLE_VERSION" local CURRENT_MOLECULE_VERSION="$MOLECULE_VERSION" + local CURRENT_YAMLLINT_VERSION="$YAMLLINT_VERSION" + local CURRENT_ANSIBLE_LINT_VERSION="$ANSIBLE_LINT_VERSION" + local CURRENT_FLAKE8_VERSION="$FLAKE8_VERSION" + local CURRENT_TESTINFRA_VERSION="$TESTINFRA_VERSION" - query_latest_python_version2 + query_latest_python_version query_latest_package_version ANSIBLE_VERSION ansible query_latest_package_version MOLECULE_VERSION molecule + query_latest_package_version YAMLLINT_VERSION yamllint + query_latest_package_version ANSIBLE_LINT_VERSION ansible-lint + query_latest_package_version FLAKE8_VERSION flake8 + query_latest_package_version TESTINFRA_VERSION testinfra echo '' echo 'New versions:' @@ -359,6 +425,31 @@ wrapper_upgrade_versions() { else echo "Molecule: $CURRENT_MOLECULE_VERSION -> $MOLECULE_VERSION" fi + + if [[ "$CURRENT_YAMLLINT_VERSION" == "$YAMLLINT_VERSION" ]]; then + echo "YamlLint: $CURRENT_YAMLLINT_VERSION (no change)" + else + echo "YamlLint: $CURRENT_YAMLLINT_VERSION -> $YAMLLINT_VERSION" + fi + + if [[ "$CURRENT_ANSIBLE_LINT_VERSION" == "$ANSIBLE_LINT_VERSION" ]]; then + echo "Ansible Lint: $CURRENT_ANSIBLE_LINT_VERSION (no change)" + else + echo "Ansible Lint: $CURRENT_ANSIBLE_LINT_VERSION -> $ANSIBLE_LINT_VERSION" + fi + + if [[ "$CURRENT_FLAKE8_VERSION" == "$FLAKE8_VERSION" ]]; then + echo "Flake8: $CURRENT_FLAKE8_VERSION (no change)" + else + echo "Flake8: $CURRENT_FLAKE8_VERSION -> $FLAKE8_VERSION" + fi + + if [[ "$CURRENT_TESTINFRA_VERSION" == "$TESTINFRA_VERSION" ]]; then + echo "Testinfra: $CURRENT_TESTINFRA_VERSION (no change)" + else + echo "Testinfra: $CURRENT_TESTINFRA_VERSION -> $TESTINFRA_VERSION" + fi + echo '' wrapper_freeze @@ -376,6 +467,10 @@ Additional options: --ansible VERSION Use the specified version of Ansible --molecule VERSION Use the specified version of Molecule --python VERSION Use the specified version of Python + --yamllint VERSION Use the specified version of YamlLint + --ansible-lint VERSION Use the specified version of Ansible Lint + --flake8 VERSION Use the specified version of Flake8 + --testinfra VERSION Use the specified version of Testinfra --use-system-dependencies Use system dependencies Additional commands: @@ -425,12 +520,12 @@ query_package_versions() { wrapper_options_ansible() { echo 'latest' - query_package_versions 'ansible' '2.7' + query_package_versions 'ansible' '2.8' } wrapper_options_molecule() { echo 'latest' - query_package_versions 'molecule' '2.20' + query_package_versions 'molecule' '3.1.5' } wrapper_options_python() { @@ -441,7 +536,7 @@ wrapper_options_python() { pyenv_present > /dev/null - local min_version='2.7' + local min_version='3.6' echo 'latest' @@ -457,67 +552,35 @@ wrapper_options_python() { done } -wrapper_options_scenario() { - if [ -f 'moleculew' ]; then - activate_virtualenv > /dev/null - fi - python << EOF -import os -import sys - -import six -import yaml - - -molecule_dir = 'molecule' -if not os.path.isdir(molecule_dir): - sys.exit() - -scenarios = [] -default = False - -for filename in os.listdir(molecule_dir): - scenario_dir = os.path.join(molecule_dir, filename) - if not os.path.isdir(scenario_dir): - continue - - molecule_yaml = os.path.join(scenario_dir, 'molecule.yml') - if not os.path.isfile(molecule_yaml): - continue - - with open(molecule_yaml, 'r') as stream: - try: - contents = yaml.safe_load(stream) - except yaml.YAMLError as exc: - continue - - if not isinstance(contents, dict): - continue - - scenario = contents.get('scenario') - if scenario is None: - continue - if not isinstance(scenario, dict): - continue +wrapper_options_yamllint() { + echo 'latest' + query_package_versions 'yamllint' '1.26.3' +} - name = scenario.get('name') - if name is None: - continue - if not isinstance(name, six.string_types): - continue +wrapper_options_ansible_lint() { + echo 'latest' + query_package_versions 'ansible_lint' '5.4.0' +} - if name == 'default': - default = True - else: - scenarios.append(name) +wrapper_options_flake8() { + echo 'latest' + query_package_versions 'flake8' '4.0.1' +} -scenarios.sort() -if default: - scenarios.append('default') +wrapper_options_testinfra() { + echo 'latest' + query_package_versions 'testinfra' '5.3.1' +} -for scenario in scenarios: - print(scenario) -EOF +wrapper_options_scenario() { + ( + cd molecule > /dev/null && + for d in *; do + if [ -d "$d" ]; then + echo "$d" + fi + done + ) } wrapper_virtualenv() { @@ -559,6 +622,42 @@ parse_args() { MOLECULE_VERSION="$1" shift ;; + --yamllint=*) + YAMLLINT_VERSION="${1#*=}" + shift + ;; + --yamllint) + shift + YAMLLINT_VERSION="$1" + shift + ;; + --ansible-lint=*) + ANSIBLE_LINT_VERSION="${1#*=}" + shift + ;; + --ansible-lint) + shift + ANSIBLE_LINT_VERSION="$1" + shift + ;; + --flake8=*) + FLAKE8_VERSION="${1#*=}" + shift + ;; + --flake8) + shift + FLAKE8_VERSION="$1" + shift + ;; + --testinfra) + shift + TESTINFRA_VERSION="$1" + shift + ;; + --testinfra=*) + TESTINFRA_VERSION="${1#*=}" + shift + ;; --use-system-dependencies) USE_SYSTEM_DEPENDENCIES=true shift @@ -605,6 +704,18 @@ detemine_versions() { if [[ $MOLECULE_VERSION == '' ]]; then MOLECULE_VERSION="$MOLECULEW_MOLECULE" fi + if [[ $YAMLLINT_VERSION == '' ]]; then + YAMLLINT_VERSION="$MOLECULEW_YAMLLINT" + fi + if [[ $ANSIBLE_LINT_VERSION == '' ]]; then + ANSIBLE_LINT_VERSION="$MOLECULEW_ANSIBLE_LINT" + fi + if [[ $FLAKE8_VERSION == '' ]]; then + FLAKE8_VERSION="$MOLECULEW_FLAKE8" + fi + if [[ $TESTINFRA_VERSION == '' ]]; then + TESTINFRA_VERSION="$MOLECULEW_TESTINFRA" + fi if [[ $USE_SYSTEM_DEPENDENCIES == true ]]; then if [[ $PYTHON_VERSION != '' ]]; then @@ -617,12 +728,10 @@ detemine_versions() { PYTHON_VERSION=$(<"$PYTHON_VERSION_FILE") fi if [[ $PYTHON_VERSION == '' ]]; then - query_latest_python_version2 + query_latest_python_version fi - elif [[ $PYTHON_VERSION == 'latest' ]] || [[ $PYTHON_VERSION == 'latest2' ]]; then - query_latest_python_version2 - elif [[ $PYTHON_VERSION == 'latest3' ]]; then - query_latest_python_version3 + elif [[ $PYTHON_VERSION == 'latest' ]]; then + query_latest_python_version fi if [[ $ANSIBLE_VERSION == '' ]] || [[ $ANSIBLE_VERSION == 'default' ]]; then @@ -646,12 +755,56 @@ detemine_versions() { elif [[ $MOLECULE_VERSION == 'latest' ]]; then query_latest_package_version MOLECULE_VERSION molecule fi + + if [[ $YAMLLINT_VERSION == '' ]] || [[ $YAMLLINT_VERSION == 'default' ]]; then + if [[ -f $YAMLLINT_VERSION_FILE ]]; then + YAMLLINT_VERSION=$(<$YAMLLINT_VERSION_FILE) + fi + if [[ $YAMLLINT_VERSION == '' ]]; then + query_latest_package_version YAMLLINT_VERSION yamllint + fi + elif [[ $YAMLLINT_VERSION == 'latest' ]]; then + query_latest_package_version YAMLLINT_VERSION yamllint + fi + + if [[ $ANSIBLE_LINT_VERSION == '' ]] || [[ $ANSIBLE_LINT_VERSION == 'default' ]]; then + if [[ -f $ANSIBLE_LINT_VERSION_FILE ]]; then + ANSIBLE_LINT_VERSION=$(<$ANSIBLE_LINT_VERSION_FILE) + fi + if [[ $ANSIBLE_LINT_VERSION == '' ]]; then + query_latest_package_version ANSIBLE_LINT_VERSION ansible-lint + fi + elif [[ $ANSIBLE_LINT_VERSION == 'latest' ]]; then + query_latest_package_version ANSIBLE_LINT_VERSION ansible-lint + fi + + if [[ $FLAKE8_VERSION == '' ]] || [[ $FLAKE8_VERSION == 'default' ]]; then + if [[ -f $FLAKE8_VERSION_FILE ]]; then + FLAKE8_VERSION=$(<$FLAKE8_VERSION_FILE) + fi + if [[ $FLAKE8_VERSION == '' ]]; then + query_latest_package_version FLAKE8_VERSION flake8 + fi + elif [[ $FLAKE8_VERSION == 'latest' ]]; then + query_latest_package_version FLAKE8_VERSION flake8 + fi + + if [[ $TESTINFRA_VERSION == '' ]] || [[ $TESTINFRA_VERSION == 'default' ]]; then + if [[ -f $TESTINFRA_VERSION_FILE ]]; then + TESTINFRA_VERSION=$(<$TESTINFRA_VERSION_FILE) + fi + if [[ $TESTINFRA_VERSION == '' ]]; then + query_latest_package_version TESTINFRA_VERSION testinfra + fi + elif [[ $TESTINFRA_VERSION == 'latest' ]]; then + query_latest_package_version TESTINFRA_VERSION testinfra + fi } activate_virtualenv() { detemine_versions - MOLECULE_WRAPPER_ENV="$HOME/.moleculew/molecule/$MOLECULE_VERSION/ansible/$ANSIBLE_VERSION/python/$PYTHON_VERSION" + MOLECULE_WRAPPER_ENV="$HOME/.moleculew/ml-${MOLECULE_VERSION}_an-${ANSIBLE_VERSION}_py-${PYTHON_VERSION}_yl-${YAMLLINT_VERSION}_al-${ANSIBLE_LINT_VERSION}_f8-${FLAKE8_VERSION}_ti-${TESTINFRA_VERSION}" if [ ! -f "$MOLECULE_WRAPPER_ENV/bin/activate" ]; then @@ -669,9 +822,20 @@ activate_virtualenv() { source "$MOLECULE_WRAPPER_ENV/bin/activate" echo '' + # Workaround breaking changes in rich 11 by installing version 10 + install_rich + install_ansible install_molecule + + install_yamllint + + install_ansible_lint + + install_flake8 + + install_testinfra else # shellcheck disable=SC1090 source "$MOLECULE_WRAPPER_ENV/bin/activate" @@ -702,6 +866,18 @@ case $MOLECULE_CMD in wrapper-options-python) wrapper_options_python ;; + wrapper-options-yamllint) + wrapper_options_yamllint + ;; + wrapper-options-ansible-lint) + wrapper_options_ansible_lint + ;; + wrapper-options-flake8) + wrapper_options_flake8 + ;; + wrapper-options-testinfra) + wrapper_options_testinfra + ;; wrapper-options-scenario) wrapper_options_scenario ;; diff --git a/tasks/configure-groups.yml b/tasks/configure-groups.yml index 451b561..cdd779e 100644 --- a/tasks/configure-groups.yml +++ b/tasks/configure-groups.yml @@ -42,6 +42,7 @@ owner: '{{ user }}' group: '{{ user }}' recurse: yes + mode: 0750 with_items: '{{ users }}' loop_control: loop_var: user