From 0f7a2d36d158c07ceaca122b45f2b5707e4ff7ec Mon Sep 17 00:00:00 2001 From: Alexandr Reshetnikov Date: Fri, 3 Mar 2023 20:49:46 +0400 Subject: [PATCH] ansible dockerfile (ansible-lint 6.9.1 using ansible 2.13.6), ansible gnu (ansible-lint 6.13.1 using ansible 2.14.1) (#45) --- .github/workflows/ansible.yml | 19 +++++++++- .github/workflows/build-docker.yml | 31 ++++++++++++++++ .github/workflows/test-docker.yml | 34 +++++++++++++++++ .gitignore | 5 ++- ansible/build_docker.sh | 41 +++++++++++++++++++++ ansible/build_gnu.sh | 26 +++++++++++-- ansible/release/__main__ansible-lint.py | 7 ++++ ansible/release/__main__ansible-playbook.py | 7 ++++ ansible/release/__main__ansible.py | 5 --- ansible/release/_ansible | 3 ++ ansible/release/_ansible-config | 3 ++ ansible/release/_ansible-playbook | 3 ++ ansible/release/ansible-lint.sh | 3 ++ ansible/release/ansible-playbook.sh | 3 ++ ansible/test_docker.sh | 26 +++++++++++++ ansible/test_linux.sh | 25 ++++++++++++- 16 files changed, 229 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/build-docker.yml create mode 100644 .github/workflows/test-docker.yml create mode 100755 ansible/build_docker.sh create mode 100644 ansible/release/__main__ansible-lint.py create mode 100644 ansible/release/__main__ansible-playbook.py create mode 100755 ansible/release/_ansible create mode 100755 ansible/release/_ansible-config create mode 100755 ansible/release/_ansible-playbook create mode 100755 ansible/release/ansible-lint.sh create mode 100755 ansible/release/ansible-playbook.sh create mode 100755 ansible/test_docker.sh diff --git a/.github/workflows/ansible.yml b/.github/workflows/ansible.yml index c174769bc..24dae6086 100644 --- a/.github/workflows/ansible.yml +++ b/.github/workflows/ansible.yml @@ -18,6 +18,13 @@ jobs: workflow: ${{ github.workflow }} toolset: gnu + ubuntu-docker: + needs: prejob + if: ${{ needs.prejob.outputs.workflow == github.workflow }} + uses: ./.github/workflows/build-docker.yml + with: + workflow: ${{ github.workflow }} + test-ubuntu: needs: alpine-gnu uses: ./.github/workflows/test-ubuntu.yml @@ -32,14 +39,22 @@ jobs: workflow: ${{ github.workflow }} artifact: build-gnu + test-ubuntu-docker: + needs: ubuntu-docker + uses: ./.github/workflows/test-docker.yml + with: + workflow: ${{ github.workflow }} + artifact: build-docker + release: - needs: [test-alpine, test-ubuntu] + needs: [test-alpine, test-ubuntu, test-ubuntu-docker] uses: ./.github/workflows/release.yml with: workflow: ${{ github.workflow }} tool_version: '7.1.0' prepare_body: | + bsdtar -Oxf ./build-docker/build-docker.tar.gz build-docker.md >> body.md bsdtar -Oxf ./build-gnu/build-gnu.tar.gz build-gnu.md >> body.md - artifacts: ./build-gnu/build-gnu.tar.gz + artifacts: ./build-docker/build-docker.tar.gz, ./build-gnu/build-gnu.tar.gz secrets: token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/build-docker.yml b/.github/workflows/build-docker.yml new file mode 100644 index 000000000..8b66f9f27 --- /dev/null +++ b/.github/workflows/build-docker.yml @@ -0,0 +1,31 @@ +on: + workflow_call: + inputs: + workflow: + required: true + type: string + toolset: + required: false + type: string + default: 'docker' + version: + required: false + type: string + default: '22.04' + +jobs: + build-ubuntu: + runs-on: ubuntu-latest + container: ubuntu:${{ inputs.version }} + steps: + - uses: actions/checkout@v3 + - name: build ${{ inputs.workflow }}-ubuntu-${{ inputs.toolset }} + run: | + apt update + apt install -y docker.io + ./${{ inputs.workflow }}/build_${{ inputs.toolset }}.sh + + - uses: actions/upload-artifact@v3 + with: + name: build-${{ inputs.toolset }} + path: ./${{ inputs.workflow }}/release/build-${{ inputs.toolset }}.tar.gz diff --git a/.github/workflows/test-docker.yml b/.github/workflows/test-docker.yml new file mode 100644 index 000000000..3695ce1d6 --- /dev/null +++ b/.github/workflows/test-docker.yml @@ -0,0 +1,34 @@ +on: + workflow_call: + inputs: + workflow: + required: true + type: string + artifact: + required: true + type: string + version: + required: false + type: string + default: 'latest' + +jobs: + test-docker: + runs-on: ubuntu-latest + container: ubuntu:${{ inputs.version }} + + steps: + - uses: actions/checkout@v3 + - uses: actions/download-artifact@v3 + with: + name: ${{ inputs.artifact }} + path: . + + - name: Test ${{ inputs.artifact }} build on ubuntu:${{ inputs.version }} + run: | + apt update + apt install -y docker.io + tar -xf ./${{ inputs.artifact }}.tar.gz -C ./bin + cd ./${{ inputs.workflow }} + ./test_docker.sh + diff --git a/.gitignore b/.gitignore index bea1edcc9..e61dc63c7 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ bin/ .tmp/ *.tar.gz +*.tar install-poetry*.py -poetry/poetry-* \ No newline at end of file +poetry/poetry-* + +/ansible/Dockerfile diff --git a/ansible/build_docker.sh b/ansible/build_docker.sh new file mode 100755 index 000000000..d6a98eec9 --- /dev/null +++ b/ansible/build_docker.sh @@ -0,0 +1,41 @@ +#!/bin/bash +dp0="$(realpath "$(dirname "$0")")" +set -e + +alpine_version="3.17.2" +self_name="ansible-alpine-$alpine_version" +image_name="$self_name:latest" + +release_version_dirpath="$dp0/release/build" +mkdir -p "$release_version_dirpath" + +full_archive_path="$release_version_dirpath/$self_name.tar.gz" + +echo "generating Dockerfile ..." +{ printf 'FROM alpine:%s +RUN apk add --no-cache ansible ansible-lint +' "$alpine_version" +} > "$dp0/Dockerfile" + +echo "building image '$image_name' ..." +docker build -t "$image_name" "$dp0/." + +echo "saving image '$image_name' to '$full_archive_path'" +docker image save "$image_name" | gzip > "$full_archive_path" + +cd "$release_version_dirpath" + +{ printf '### build-docker.tar.gz +docker image: %s +%s + +%s + +' "$image_name" \ + "$(docker run --rm "$image_name" ansible --version)" \ + "$(docker run --rm "$image_name" ansible-lint --version)" +} > build-docker.md + +cat build-docker.md + +tar -czvf ../build-docker.tar.gz . diff --git a/ansible/build_gnu.sh b/ansible/build_gnu.sh index 771aa40ad..f3bc92b0a 100755 --- a/ansible/build_gnu.sh +++ b/ansible/build_gnu.sh @@ -25,13 +25,15 @@ export LC_ALL=en_US.UTF-8 tool_name="ansible" tool_version="7.1.0" +tool_core_version="2.14.1" +tool_lint_version="6.13.1" python_self_name="python-3.11.1" self_name="$tool_name-$tool_version" release_version_dirpath="$dp0/release/$self_name" echo "::set-output name=tool_name::$tool_name" echo "::set-output name=tool_version::$tool_version" -mkdir -p "$release_version_dirpath" && cd "$dp0/release" +mkdir -p "$release_version_dirpath/Scripts/bin" && cd "$dp0/release" echo "download python install script ..." python_bin_download_url="https://github.com/hemnstill/StandaloneTools/releases/download/$python_self_name/build-gnu.tar.gz" @@ -55,15 +57,26 @@ echo "install ansbile ..." "python3" -m ensurepip "python3" -m pip install --target="$cpython_lib_path" cffi +"$cpython_bin" -m pip install "ansible-core==$tool_core_version" "$cpython_bin" -m pip install "$tool_name==$tool_version" +"$cpython_bin" -m pip install "ansible-lint==$tool_lint_version" echo "prepare build artifacts ..." cp -f "$dp0/release/ansible.sh" "$release_version_dirpath/" cp -f "$dp0/release/__main__ansible.py" "$release_version_dirpath/" +cp -f "$dp0/release/_ansible" "$release_version_dirpath/Scripts/bin/ansible" cp -f "$dp0/release/ansible-config.sh" "$release_version_dirpath/" cp -f "$dp0/release/__main__ansible-config.py" "$release_version_dirpath/" +cp -f "$dp0/release/_ansible-config" "$release_version_dirpath/Scripts/bin/ansible-config" + +cp -f "$dp0/release/ansible-playbook.sh" "$release_version_dirpath/" +cp -f "$dp0/release/__main__ansible-playbook.py" "$release_version_dirpath/" +cp -f "$dp0/release/_ansible-playbook" "$release_version_dirpath/Scripts/bin/ansible-playbook" + +cp -f "$dp0/release/ansible-lint.sh" "$release_version_dirpath/" +cp -f "$dp0/release/__main__ansible-lint.py" "$release_version_dirpath/" echo "creating archive ..." @@ -71,12 +84,19 @@ cd "$release_version_dirpath" ./"$tool_name.sh" --version -{ printf '%s +{ printf '### build-gnu.tar.gz +%s + +%s + +%s %s ' "$(./"$tool_name.sh" --version)" \ - "$("./ansible-config.sh" --version | head -1)" + "$(./"ansible-config.sh" --version | head -1)" \ + "$(./"ansible-playbook.sh" --version | head -1)" \ + "$(./"ansible-lint.sh" --version)" } > build-gnu.md cat build-gnu.md diff --git a/ansible/release/__main__ansible-lint.py b/ansible/release/__main__ansible-lint.py new file mode 100644 index 000000000..cb3d758cd --- /dev/null +++ b/ansible/release/__main__ansible-lint.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +import re +import sys +from ansiblelint.__main__ import _run_cli_entrypoint +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(_run_cli_entrypoint()) diff --git a/ansible/release/__main__ansible-playbook.py b/ansible/release/__main__ansible-playbook.py new file mode 100644 index 000000000..c31dad982 --- /dev/null +++ b/ansible/release/__main__ansible-playbook.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +import re +import sys +from ansible.cli.playbook import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/ansible/release/__main__ansible.py b/ansible/release/__main__ansible.py index 7aef5d1d9..20d40ae3f 100644 --- a/ansible/release/__main__ansible.py +++ b/ansible/release/__main__ansible.py @@ -1,11 +1,6 @@ # -*- coding: utf-8 -*- import re import sys -import os - -_current_script_path: str = os.path.dirname(os.path.realpath(__file__)) -sys.path.append(os.path.join(_current_script_path, 'Lib', 'site-packages')) - from ansible.cli.adhoc import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) diff --git a/ansible/release/_ansible b/ansible/release/_ansible new file mode 100755 index 000000000..7ff3e38f1 --- /dev/null +++ b/ansible/release/_ansible @@ -0,0 +1,3 @@ +#!/bin/bash +dp0="$(realpath "$(dirname "$0")")" +"$dp0/python3" "$dp0/../../__main__ansible.py" "$@" diff --git a/ansible/release/_ansible-config b/ansible/release/_ansible-config new file mode 100755 index 000000000..ebad99125 --- /dev/null +++ b/ansible/release/_ansible-config @@ -0,0 +1,3 @@ +#!/bin/bash +dp0="$(realpath "$(dirname "$0")")" +"$dp0/python3" "$dp0/../../__main__ansible-config.py" "$@" diff --git a/ansible/release/_ansible-playbook b/ansible/release/_ansible-playbook new file mode 100755 index 000000000..5cb9d6377 --- /dev/null +++ b/ansible/release/_ansible-playbook @@ -0,0 +1,3 @@ +#!/bin/bash +dp0="$(realpath "$(dirname "$0")")" +"$dp0/python3" "$dp0/../../__main__ansible-playbook.py" "$@" diff --git a/ansible/release/ansible-lint.sh b/ansible/release/ansible-lint.sh new file mode 100755 index 000000000..c0790dbae --- /dev/null +++ b/ansible/release/ansible-lint.sh @@ -0,0 +1,3 @@ +#!/bin/bash +dp0="$(realpath "$(dirname "$0")")" +"$dp0/Scripts/bin/python3" "$dp0/__main__ansible-lint.py" "$@" diff --git a/ansible/release/ansible-playbook.sh b/ansible/release/ansible-playbook.sh new file mode 100755 index 000000000..512225196 --- /dev/null +++ b/ansible/release/ansible-playbook.sh @@ -0,0 +1,3 @@ +#!/bin/bash +dp0="$(realpath "$(dirname "$0")")" +"$dp0/Scripts/bin/python3" "$dp0/__main__ansible-playbook.py" "$@" diff --git a/ansible/test_docker.sh b/ansible/test_docker.sh new file mode 100755 index 000000000..354e63fc5 --- /dev/null +++ b/ansible/test_docker.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +alpine_version="3.17.2" +self_name="ansible-alpine-$alpine_version" +image_name="$self_name:latest" + +docker load --input "../bin/$self_name.tar.gz" + +test_version() { + assertEquals "ansible [core 2.13.6] + config file = None + configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] + ansible python module location = /usr/lib/python3.10/site-packages/ansible + ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections + executable location = /usr/bin/ansible + python version = 3.10.10 (main, Feb 9 2023, 02:08:14) [GCC 12.2.1 20220924] + jinja version = 3.1.2 + libyaml = True" "$(docker run --rm $image_name ansible --version)" +} + +test_ansible_lint_version() { + assertEquals "ansible-lint 6.9.1 using ansible 2.13.6" "$(docker run --rm $image_name ansible-lint --version | head -1)" +} + +# Load and run shUnit2. +source "../.tests/shunit2/shunit2" diff --git a/ansible/test_linux.sh b/ansible/test_linux.sh index a2c839913..449b5387d 100755 --- a/ansible/test_linux.sh +++ b/ansible/test_linux.sh @@ -1,9 +1,11 @@ #!/bin/bash is_alpine_os=false && [[ -f "/etc/alpine-release" ]] && is_alpine_os=true +is_ubuntu_os=false && [[ -f "/etc/lsb-release" ]] && is_ubuntu_os=true + if [[ "$is_alpine_os" == true ]]; then apk update - apk add --no-cache gcompat + apk add --no-cache gcompat git { printf '%s' "-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApZ2u1KJKUu/fW4A25y9m @@ -23,6 +25,11 @@ Zvo9GI2e2MaZyo9/lvb+LbLEJZKEQckqRj4P26gmASrZEPStwc+yqy1ShHLA0j6m export LC_ALL=en_US.UTF-8 fi +if [[ "$is_ubuntu_os" == true ]]; then + apt update + apt install -y git +fi + test_version() { assertEquals "ansible [core 2.14.1] config file = None @@ -35,5 +42,21 @@ test_version() { libyaml = True" "$(../bin/ansible.sh --version)" } +test_bin_version() { + assertEquals "ansible [core 2.14.1]" "$(../bin/Scripts/bin/ansible --version | head -1)" +} + +test_ansible_config_version() { + assertEquals "ansible-config [core 2.14.1]" "$(../bin/Scripts/bin/ansible-config --version | head -1)" +} + +test_ansible_playbook_version() { + assertEquals "ansible-playbook [core 2.14.1]" "$(../bin/Scripts/bin/ansible-playbook --version | head -1)" +} + +test_ansible_lint_version() { + assertEquals "ansible-lint 6.13.1 using ansible 2.14.1" "$(../bin/ansible-lint.sh --version | head -1)" +} + # Load and run shUnit2. source "../.tests/shunit2/shunit2"