This is an automated email from the ASF dual-hosted git repository.

aw pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/yetus.git


The following commit(s) were added to refs/heads/main by this push:
     new c8116a6  YETUS-1131. Add ansible-lint support (#231)
c8116a6 is described below

commit c8116a6023de81fff8cf318810af7dfd2eb6d493
Author: Allen Wittenauer <[email protected]>
AuthorDate: Sun Oct 31 23:34:58 2021 -0700

    YETUS-1131. Add ansible-lint support (#231)
---
 .../in-progress/precommit/index.html.md            |   1 +
 .../precommit/plugins/ansiblelint.html.md          |  50 +++++++
 precommit/src/main/shell/plugins.d/ansiblelint.sh  | 149 +++++++++++++++++++++
 .../src/main/shell/test-patch-docker/Dockerfile    |   5 +
 4 files changed, 205 insertions(+)

diff --git 
a/asf-site-src/source/documentation/in-progress/precommit/index.html.md 
b/asf-site-src/source/documentation/in-progress/precommit/index.html.md
index 2320ee8..5a326f7 100644
--- a/asf-site-src/source/documentation/in-progress/precommit/index.html.md
+++ b/asf-site-src/source/documentation/in-progress/precommit/index.html.md
@@ -134,6 +134,7 @@ Compiler Support:
 
 Language Support, Licensing, and more:
 
+* [ansible-lint](plugins/ansiblelint)
 * [Apache Creadur Rat](plugins/asflicense)
 * [buf](plugins/buf)
 * [checkmake](plugins/checkmake)
diff --git 
a/asf-site-src/source/documentation/in-progress/precommit/plugins/ansiblelint.html.md
 
b/asf-site-src/source/documentation/in-progress/precommit/plugins/ansiblelint.html.md
new file mode 100644
index 0000000..dd5ea93
--- /dev/null
+++ 
b/asf-site-src/source/documentation/in-progress/precommit/plugins/ansiblelint.html.md
@@ -0,0 +1,50 @@
+<!---
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+
+# Name
+
+ansiblelint
+
+# Category
+
+Test
+
+# Description
+
+When [Ansible](https://www.ansible.com/) playbooks are detected (a file 
matching the pattern
+`playbooks/*.yml` or `playbooks/*.yaml`), runs
+[ansiblelint](https://ansible-lint.readthedocs.io) against those files.
+
+# Environment Variables
+
+None
+
+# Options
+
+| Option | Notes |
+|:---------|:------|
+| `--ansiblelint=<path>` |  Location of `ansible-lint` executable |
+
+# Docker Notes
+
+None
+
+# Developer Notes
+
+None
diff --git a/precommit/src/main/shell/plugins.d/ansiblelint.sh 
b/precommit/src/main/shell/plugins.d/ansiblelint.sh
new file mode 100755
index 0000000..6188ad2
--- /dev/null
+++ b/precommit/src/main/shell/plugins.d/ansiblelint.sh
@@ -0,0 +1,149 @@
+#!/usr/bin/env bash
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# SHELLDOC-IGNORE
+
+add_test_type ansiblelint
+
+ANSIBLELINT_TIMER=0
+
+ANSIBLELINT=${ANSIBLELINT:-$(command -v ansible-lint 2>/dev/null)}
+
+function ansiblelint_usage
+{
+  yetus_add_option "--ansiblelint=<path>" "path to ansible-lint executable"
+}
+
+function ansiblelint_parse_args
+{
+  declare i
+
+  for i in "$@"; do
+    case ${i} in
+    --ansiblelint=*)
+      ANSIBLELINT=${i#*=}
+    ;;
+    esac
+  done
+}
+
+function ansiblelint_filefilter
+{
+  declare filename=$1
+
+  if [[ ${filename} =~ playbooks/.*\.yml$ ]] || [[ ${filename} =~ 
playbooks/.*\.yaml$ ]]; then
+    add_test ansiblelint
+  fi
+}
+
+function ansiblelint_precheck
+{
+  if ! verify_command ansiblelint "${ANSIBLELINT}"; then
+    add_vote_table 0 ansiblelint "ansiblelint was not available."
+    delete_test ansiblelint
+  fi
+}
+
+function ansiblelint_logic
+{
+  declare repostatus=$1
+  declare i
+  declare -a ansiblelint_params
+
+  pushd "${BASEDIR}" >/dev/null || return 1
+
+  ansiblelint_params=(--nocolor)
+  ansiblelint_params+=(-f plain)
+  ansiblelint_params+=(--parseable-severity)
+  ansiblelint_params+=(--show-relpath)
+
+  if [[ "${OFFLINE}" == "true" ]]; then
+    ansiblelint_params+=(--offline)
+  fi
+
+  for i in "${CHANGED_FILES[@]}"; do
+    if [[ ${i} =~ playbooks/.*\.yaml$ && -f ${i} ]] ||
+       [[ ${i} =~ playbooks/.*\.yml$ && -f ${i} ]]; then
+
+      # unfortunately, ansible-lint pollutes stderr with a pointless message
+      # about how to skip tests so there is no point in trying to process it. 
:(
+      "${ANSIBLELINT}" \
+        "${ansiblelint_params[@]}" \
+        "${i}" \
+        >> "${PATCH_DIR}/${repostatus}-ansiblelint-result.txt" \
+        2> "${PATCH_DIR}/${repostatus}-ansiblelint-stderr.txt"
+    fi
+  done
+  popd > /dev/null || return 1
+}
+
+
+function ansiblelint_preapply
+{
+  if ! verify_needed_test ansiblelint; then
+    return 0
+  fi
+
+  big_console_header "ansiblelint plugin: ${PATCH_BRANCH}"
+
+  start_clock
+
+  ansiblelint_logic branch
+
+  # keep track of how much as elapsed for us already
+  ANSIBLELINT_TIMER=$(stop_clock)
+  return 0
+}
+
+function ansiblelint_postapply
+{
+  declare version
+
+  if ! verify_needed_test ansiblelint; then
+    return 0
+  fi
+
+  big_console_header "ansiblelint plugin: ${BUILDMODE}"
+
+  start_clock
+
+  # add our previous elapsed to our new timer
+  # by setting the clock back
+  offset_clock "${ANSIBLELINT_TIMER}"
+
+  ansiblelint_logic patch
+
+  # shellcheck disable=SC2016
+  version=$("${ANSIBLELINT}" --version | "${AWK}" '{print $NF}')
+
+  add_version_data ansiblelint "v${version}"
+
+  root_postlog_compare \
+    ansiblelint \
+    "${PATCH_DIR}/branch-ansiblelint-result.txt" \
+    "${PATCH_DIR}/patch-ansiblelint-result.txt"
+}
+
+function ansiblelint_postcompile
+{
+  declare repostatus=$1
+
+  if [[ "${repostatus}" = branch ]]; then
+    ansiblelint_preapply
+  else
+    ansiblelint_postapply
+  fi
+}
\ No newline at end of file
diff --git a/precommit/src/main/shell/test-patch-docker/Dockerfile 
b/precommit/src/main/shell/test-patch-docker/Dockerfile
index 9842b92..fdb6c59 100644
--- a/precommit/src/main/shell/test-patch-docker/Dockerfile
+++ b/precommit/src/main/shell/test-patch-docker/Dockerfile
@@ -311,6 +311,8 @@ RUN apt-get -q update && apt-get -q install 
--no-install-recommends -y \
 # Install python3 and pylint3
 # astroid and pylint go hand-in-hand.  Upgrade both at the same time.
 ######
+ARG PY3_ANSIBLE_VERSION=4.7.0
+ARG PY3_ANSIBLELINT_VERSION=5.2.1
 ARG PY3_ASTROID_VERSION=2.8.0
 ARG PY3_CODESPELL_VERSION=2.1.0
 ARG PY3_DETECT_SECRETS=1.0.3
@@ -341,12 +343,15 @@ RUN apt-get -q update && apt-get -q install 
--no-install-recommends -y \
     && python3 /tmp/get-pip.py \
     && rm /usr/local/bin/pip /tmp/get-pip.py \
     && pip3 install --no-cache-dir -v \
+        ansible==$PY3_ANSIBLE_VERSION \
+        ansible-lint==$PY3_ANSIBLELINT_VERSION \
         astroid==$PY3_ASTROID_VERSION \
         codespell==$PY3_CODESPELL_VERSION \
         detect-secrets==$PY3_DETECT_SECRETS \
         docker-compose==$PY3_DOCKER_COMPOSE \
         pylint==$PY3_PYLINT_VERSION \
         yamllint==$PY3_YAMLLINT_VERSION \
+
     && rm -rf /root/.cache \
     && mv /usr/local/bin/pylint /usr/local/bin/pylint3 \
     && ln -s /usr/local/bin/pylint3 /usr/local/bin/pylint \

Reply via email to