From 9e22cfdb0f0301fbff70c766e99f1d75490aaf37 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Thu, 15 Sep 2022 12:01:46 -0700 Subject: [PATCH] Remove shebang from all python ansible modules This commit in Ansible: https://github.com/ansible/ansible/commit/9142be2f6cabbe6597c9254c5bb9186d17036d55 now allows Python modules to specify their interpreter with the shebang. We expect our roles to use the discovered python interpreter on remote nodes, and on the executor, we need them to use the virtualenv. Removing the specific shebang accomplishes this under Ansible 6, and has no effect under older versions of Ansible. Without this, for example, the log upload roles would not have access to their cloud libraries. Also update our ansible/cli check in our module files. Many of our modules can be run from the command line for ease of testing, but the check that we perform to determine if the module is being invoked from the command line or Ansible fails on Ansible 5. Update it to a check that should work in all 4 versions of Ansible that Zuul uses. Change-Id: I4e6e85156459cca032e6c3e1d8a9284be919ccca --- .../library/generate_manifest.py | 11 ++++++----- .../library/golangci_lint_parse_output.py | 2 -- roles/htmlify-logs/library/htmlify.py | 11 ++++++----- .../library/generate_all_known_hosts.py | 2 -- .../library/sshagent_remove_keys.py | 9 ++++++--- roles/sphinx/library/sphinx_check_warning_is_error.py | 2 -- roles/stage-output/library/stage_output_renames.py | 2 -- .../stage-output/library/test_stage_output_renames.py | 1 - .../library/delete_container.py | 2 -- .../library/zuul_swift_upload.py | 11 ++++++----- roles/tox/library/tox_install_sibling_packages.py | 2 -- roles/tox/library/tox_parse_output.py | 2 -- roles/upload-afs-roots/library/zuul_afs.py | 2 -- roles/upload-forge/library/forge_upload.py | 2 -- roles/upload-forge/library/test_forge_upload.py | 2 -- .../library/delete_swift_container.py | 2 -- roles/upload-logs-base/library/zuul_azure_upload.py | 11 ++++++----- .../library/zuul_google_storage_upload.py | 11 ++++++----- roles/upload-logs-base/library/zuul_ibm_upload.py | 11 ++++++----- roles/upload-logs-base/library/zuul_s3_upload.py | 11 ++++++----- roles/upload-logs-base/library/zuul_swift_upload.py | 11 ++++++----- roles/validate-host/library/zuul_debug_info.py | 2 -- roles/write-inventory/library/write_inventory.py | 2 -- 23 files changed, 54 insertions(+), 70 deletions(-) diff --git a/roles/generate-zuul-manifest/library/generate_manifest.py b/roles/generate-zuul-manifest/library/generate_manifest.py index 81fed2c9a..5e1240a2a 100644 --- a/roles/generate-zuul-manifest/library/generate_manifest.py +++ b/roles/generate-zuul-manifest/library/generate_manifest.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright 2019 Red Hat, Inc # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -131,7 +129,10 @@ def cli_main(): if __name__ == '__main__': - if sys.stdin.isatty(): - cli_main() - else: + # The zip/ansible/modules check is required for Ansible 5 because + # stdin may be a tty, but does not work in ansible 2.8. The tty + # check works on versions 2.8, 2.9, and 6. + if ('.zip/ansible/modules' in sys.argv[0] or not sys.stdin.isatty()): ansible_main() + else: + cli_main() diff --git a/roles/golangci-lint/library/golangci_lint_parse_output.py b/roles/golangci-lint/library/golangci_lint_parse_output.py index c1209630b..62efc0771 100644 --- a/roles/golangci-lint/library/golangci_lint_parse_output.py +++ b/roles/golangci-lint/library/golangci_lint_parse_output.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright 2020 VEXXHOST, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may diff --git a/roles/htmlify-logs/library/htmlify.py b/roles/htmlify-logs/library/htmlify.py index 90629c7f8..f354a6b17 100644 --- a/roles/htmlify-logs/library/htmlify.py +++ b/roles/htmlify-logs/library/htmlify.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 # Copyright 2018 Red Hat, Inc # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -137,8 +136,10 @@ def cli_main(): if __name__ == '__main__': - - if sys.stdin.isatty(): - cli_main() - else: + # The zip/ansible/modules check is required for Ansible 5 because + # stdin may be a tty, but does not work in ansible 2.8. The tty + # check works on versions 2.8, 2.9, and 6. + if ('.zip/ansible/modules' in sys.argv[0] or not sys.stdin.isatty()): ansible_main() + else: + cli_main() diff --git a/roles/multi-node-known-hosts/library/generate_all_known_hosts.py b/roles/multi-node-known-hosts/library/generate_all_known_hosts.py index fb5961b52..3cfee8dad 100644 --- a/roles/multi-node-known-hosts/library/generate_all_known_hosts.py +++ b/roles/multi-node-known-hosts/library/generate_all_known_hosts.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2017 Red Hat # # This module is free software: you can redistribute it and/or modify diff --git a/roles/remove-zuul-sshkey/library/sshagent_remove_keys.py b/roles/remove-zuul-sshkey/library/sshagent_remove_keys.py index b4f6ea699..f26322059 100644 --- a/roles/remove-zuul-sshkey/library/sshagent_remove_keys.py +++ b/roles/remove-zuul-sshkey/library/sshagent_remove_keys.py @@ -120,7 +120,10 @@ def cli_main(): if __name__ == '__main__': - if sys.stdin.isatty(): - cli_main() - else: + # The zip/ansible/modules check is required for Ansible 5 because + # stdin may be a tty, but does not work in ansible 2.8. The tty + # check works on versions 2.8, 2.9, and 6. + if ('.zip/ansible/modules' in sys.argv[0] or not sys.stdin.isatty()): ansible_main() + else: + cli_main() diff --git a/roles/sphinx/library/sphinx_check_warning_is_error.py b/roles/sphinx/library/sphinx_check_warning_is_error.py index 275d8c0a7..c057dab67 100644 --- a/roles/sphinx/library/sphinx_check_warning_is_error.py +++ b/roles/sphinx/library/sphinx_check_warning_is_error.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2017 Red Hat # # This module is free software: you can redistribute it and/or modify diff --git a/roles/stage-output/library/stage_output_renames.py b/roles/stage-output/library/stage_output_renames.py index 7e318f1b3..1233a6f7f 100644 --- a/roles/stage-output/library/stage_output_renames.py +++ b/roles/stage-output/library/stage_output_renames.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python -# # Licensed 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 diff --git a/roles/stage-output/library/test_stage_output_renames.py b/roles/stage-output/library/test_stage_output_renames.py index 67b414e8e..7fba88cb8 100644 --- a/roles/stage-output/library/test_stage_output_renames.py +++ b/roles/stage-output/library/test_stage_output_renames.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # Licensed 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 diff --git a/roles/test-upload-logs-swift/library/delete_container.py b/roles/test-upload-logs-swift/library/delete_container.py index 31e768ff7..6833936f5 100644 --- a/roles/test-upload-logs-swift/library/delete_container.py +++ b/roles/test-upload-logs-swift/library/delete_container.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright 2019 Red Hat, Inc # # Licensed under the Apache License, Version 2.0 (the "License"); you may diff --git a/roles/test-upload-logs-swift/library/zuul_swift_upload.py b/roles/test-upload-logs-swift/library/zuul_swift_upload.py index 8a1b68abd..fc2220a56 100755 --- a/roles/test-upload-logs-swift/library/zuul_swift_upload.py +++ b/roles/test-upload-logs-swift/library/zuul_swift_upload.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright 2014 Rackspace Australia # Copyright 2018 Red Hat, Inc # @@ -945,7 +943,10 @@ if __name__ == '__main__': requestsexceptions.squelch_warnings( requestsexceptions.InsecureRequestWarning) - if sys.stdin.isatty(): - cli_main() - else: + # The zip/ansible/modules check is required for Ansible 5 because + # stdin may be a tty, but does not work in ansible 2.8. The tty + # check works on versions 2.8, 2.9, and 6. + if ('.zip/ansible/modules' in sys.argv[0] or not sys.stdin.isatty()): ansible_main() + else: + cli_main() diff --git a/roles/tox/library/tox_install_sibling_packages.py b/roles/tox/library/tox_install_sibling_packages.py index fc78b69ea..86ad5eec2 100644 --- a/roles/tox/library/tox_install_sibling_packages.py +++ b/roles/tox/library/tox_install_sibling_packages.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2017 Red Hat # # This module is free software: you can redistribute it and/or modify diff --git a/roles/tox/library/tox_parse_output.py b/roles/tox/library/tox_parse_output.py index 3a6d77c44..af5ccc1e8 100644 --- a/roles/tox/library/tox_parse_output.py +++ b/roles/tox/library/tox_parse_output.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2018 Red Hat # # This module is free software: you can redistribute it and/or modify diff --git a/roles/upload-afs-roots/library/zuul_afs.py b/roles/upload-afs-roots/library/zuul_afs.py index 56f1e14b1..ccd67a50c 100644 --- a/roles/upload-afs-roots/library/zuul_afs.py +++ b/roles/upload-afs-roots/library/zuul_afs.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2016 Red Hat # # This module is free software: you can redistribute it and/or modify diff --git a/roles/upload-forge/library/forge_upload.py b/roles/upload-forge/library/forge_upload.py index d477c8ef6..424b8a1a1 100644 --- a/roles/upload-forge/library/forge_upload.py +++ b/roles/upload-forge/library/forge_upload.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - # Copyright (c) 2019 Binero # Author: Tobias Urdin # diff --git a/roles/upload-forge/library/test_forge_upload.py b/roles/upload-forge/library/test_forge_upload.py index d3b0d5de3..643c49422 100644 --- a/roles/upload-forge/library/test_forge_upload.py +++ b/roles/upload-forge/library/test_forge_upload.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - # Copyright (c) 2019 Binero # Author: Tobias Urdin # diff --git a/roles/upload-logs-base/library/delete_swift_container.py b/roles/upload-logs-base/library/delete_swift_container.py index 31e768ff7..6833936f5 100644 --- a/roles/upload-logs-base/library/delete_swift_container.py +++ b/roles/upload-logs-base/library/delete_swift_container.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright 2019 Red Hat, Inc # # Licensed under the Apache License, Version 2.0 (the "License"); you may diff --git a/roles/upload-logs-base/library/zuul_azure_upload.py b/roles/upload-logs-base/library/zuul_azure_upload.py index 61defaf24..cc36b494a 100644 --- a/roles/upload-logs-base/library/zuul_azure_upload.py +++ b/roles/upload-logs-base/library/zuul_azure_upload.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright 2014 Rackspace Australia # Copyright 2018-2019 Red Hat, Inc # Copyright 2021 Acme Gating, LLC @@ -311,7 +309,10 @@ def cli_main(): if __name__ == '__main__': - if sys.stdin.isatty(): - cli_main() - else: + # The zip/ansible/modules check is required for Ansible 5 because + # stdin may be a tty, but does not work in ansible 2.8. The tty + # check works on versions 2.8, 2.9, and 6. + if ('.zip/ansible/modules' in sys.argv[0] or not sys.stdin.isatty()): ansible_main() + else: + cli_main() diff --git a/roles/upload-logs-base/library/zuul_google_storage_upload.py b/roles/upload-logs-base/library/zuul_google_storage_upload.py index 91bdf4e2f..443f8baab 100755 --- a/roles/upload-logs-base/library/zuul_google_storage_upload.py +++ b/roles/upload-logs-base/library/zuul_google_storage_upload.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright 2014 Rackspace Australia # Copyright 2018-2019 Red Hat, Inc # @@ -332,7 +330,10 @@ def cli_main(): if __name__ == '__main__': - if sys.stdin.isatty(): - cli_main() - else: + # The zip/ansible/modules check is required for Ansible 5 because + # stdin may be a tty, but does not work in ansible 2.8. The tty + # check works on versions 2.8, 2.9, and 6. + if ('.zip/ansible/modules' in sys.argv[0] or not sys.stdin.isatty()): ansible_main() + else: + cli_main() diff --git a/roles/upload-logs-base/library/zuul_ibm_upload.py b/roles/upload-logs-base/library/zuul_ibm_upload.py index 07215fd30..fadc44a19 100755 --- a/roles/upload-logs-base/library/zuul_ibm_upload.py +++ b/roles/upload-logs-base/library/zuul_ibm_upload.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright 2014 Rackspace Australia # Copyright 2018-2019 Red Hat, Inc # Copyright 2021-2022 Acme Gating, LLC @@ -362,7 +360,10 @@ def cli_main(): if __name__ == '__main__': - if sys.stdin.isatty(): - cli_main() - else: + # The zip/ansible/modules check is required for Ansible 5 because + # stdin may be a tty, but does not work in ansible 2.8. The tty + # check works on versions 2.8, 2.9, and 6. + if ('.zip/ansible/modules' in sys.argv[0] or not sys.stdin.isatty()): ansible_main() + else: + cli_main() diff --git a/roles/upload-logs-base/library/zuul_s3_upload.py b/roles/upload-logs-base/library/zuul_s3_upload.py index 89e383b20..1d0ffd8ff 100755 --- a/roles/upload-logs-base/library/zuul_s3_upload.py +++ b/roles/upload-logs-base/library/zuul_s3_upload.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright 2014 Rackspace Australia # Copyright 2018 Red Hat, Inc # @@ -306,7 +304,10 @@ def cli_main(): if __name__ == '__main__': - if sys.stdin.isatty(): - cli_main() - else: + # The zip/ansible/modules check is required for Ansible 5 because + # stdin may be a tty, but does not work in ansible 2.8. The tty + # check works on versions 2.8, 2.9, and 6. + if ('.zip/ansible/modules' in sys.argv[0] or not sys.stdin.isatty()): ansible_main() + else: + cli_main() diff --git a/roles/upload-logs-base/library/zuul_swift_upload.py b/roles/upload-logs-base/library/zuul_swift_upload.py index 203f7db8f..d93998161 100755 --- a/roles/upload-logs-base/library/zuul_swift_upload.py +++ b/roles/upload-logs-base/library/zuul_swift_upload.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright 2014 Rackspace Australia # Copyright 2018 Red Hat, Inc # @@ -418,7 +416,10 @@ if __name__ == '__main__': requestsexceptions.squelch_warnings( requestsexceptions.InsecureRequestWarning) - if sys.stdin.isatty(): - cli_main() - else: + # The zip/ansible/modules check is required for Ansible 5 because + # stdin may be a tty, but does not work in ansible 2.8. The tty + # check works on versions 2.8, 2.9, and 6. + if ('.zip/ansible/modules' in sys.argv[0] or not sys.stdin.isatty()): ansible_main() + else: + cli_main() diff --git a/roles/validate-host/library/zuul_debug_info.py b/roles/validate-host/library/zuul_debug_info.py index 405ea6d8a..3b6543c9c 100644 --- a/roles/validate-host/library/zuul_debug_info.py +++ b/roles/validate-host/library/zuul_debug_info.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2017 Red Hat # # This module is free software: you can redistribute it and/or modify diff --git a/roles/write-inventory/library/write_inventory.py b/roles/write-inventory/library/write_inventory.py index abe707639..8784a75b1 100755 --- a/roles/write-inventory/library/write_inventory.py +++ b/roles/write-inventory/library/write_inventory.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# # Copyright 2018 Red Hat, Inc # # Licensed under the Apache License, Version 2.0 (the "License"); you may