| 1 |
# Copyright (c) 2021, Felix Fontein <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
name: collection_version |
| 9 |
author: Felix Fontein (@felixfontein) |
| 10 |
version_added: "4.0.0" |
| 11 |
short_description: Retrieves the version of an installed collection |
| 12 |
description: |
| 13 |
- This lookup allows to query the version of an installed collection, and to determine whether a collection is installed |
| 14 |
at all. |
| 15 |
- By default it returns V(none) for non-existing collections and V(*) for collections without a version number. The latter |
| 16 |
should only happen in development environments, or when installing a collection from git which has no version in its C(galaxy.yml). |
| 17 |
This behavior can be adjusted by providing other values with O(result_not_found) and O(result_no_version). |
| 18 |
options: |
| 19 |
_terms: |
| 20 |
description: |
| 21 |
- The collections to look for. |
| 22 |
- For example V(community.general). |
| 23 |
type: list |
| 24 |
elements: str |
| 25 |
required: true |
| 26 |
result_not_found: |
| 27 |
description: |
| 28 |
- The value to return when the collection could not be found. |
| 29 |
- By default, V(none) is returned. |
| 30 |
type: string |
| 31 |
default: ~ |
| 32 |
result_no_version: |
| 33 |
description: |
| 34 |
- The value to return when the collection has no version number. |
| 35 |
- This can happen for collections installed from git which do not have a version number in C(galaxy.yml). |
| 36 |
- By default, V(*) is returned. |
| 37 |
type: string |
| 38 |
default: '*' |
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = r""" |
| 42 |
- name: Check version of community.general |
| 43 |
ansible.builtin.debug: |
| 44 |
msg: "community.general version {{ lookup('community.general.collection_version', 'community.general') }}" |
| 45 |
""" |
| 46 |
|
| 47 |
RETURN = r""" |
| 48 |
_raw: |
| 49 |
description: |
| 50 |
- The version number of the collections listed as input. |
| 51 |
- If a collection can not be found, it returns the value provided in O(result_not_found). By default, this is V(none). |
| 52 |
- If a collection can be found, but the version not identified, it returns the value provided in O(result_no_version). |
| 53 |
By default, this is V(*). This can happen for collections installed from git which do not have a version number in V(galaxy.yml). |
| 54 |
type: list |
| 55 |
elements: str |
| 56 |
""" |
| 57 |
|
| 58 |
import json |
| 59 |
import os |
| 60 |
import re |
| 61 |
from importlib import import_module |
| 62 |
|
| 63 |
import yaml |
| 64 |
from ansible.errors import AnsibleLookupError |
| 65 |
from ansible.plugins.lookup import LookupBase |
| 66 |
|
| 67 |
from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms |
| 68 |
|
| 69 |
FQCN_RE = re.compile(r"^[A-Za-z0-9_]+\.[A-Za-z0-9_]+$") |
| 70 |
|
| 71 |
|
| 72 |
def load_collection_meta_manifest(manifest_path): |
| 73 |
with open(manifest_path, "rb") as f: |
| 74 |
meta = json.load(f) |
| 75 |
return { |
| 76 |
"version": meta["collection_info"]["version"], |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
def load_collection_meta_galaxy(galaxy_path, no_version="*"): |
| 81 |
with open(galaxy_path, "rb") as f: |
| 82 |
meta = yaml.safe_load(f) |
| 83 |
return { |
| 84 |
"version": meta.get("version") or no_version, |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
def load_collection_meta(collection_pkg, no_version="*"): |
| 89 |
path = os.path.dirname(collection_pkg.__file__) |
| 90 |
|
| 91 |
# Try to load MANIFEST.json |
| 92 |
manifest_path = os.path.join(path, "MANIFEST.json") |
| 93 |
if os.path.exists(manifest_path): |
| 94 |
return load_collection_meta_manifest(manifest_path) |
| 95 |
|
| 96 |
# Try to load galaxy.yml |
| 97 |
galaxy_path = os.path.join(path, "galaxy.yml") |
| 98 |
if os.path.exists(galaxy_path): |
| 99 |
return load_collection_meta_galaxy(galaxy_path, no_version=no_version) |
| 100 |
|
| 101 |
return {} |
| 102 |
|
| 103 |
|
| 104 |
class LookupModule(LookupBase): |
| 105 |
def run(self, terms, variables=None, **kwargs): |
| 106 |
result = [] |
| 107 |
self.set_options(var_options=variables, direct=kwargs) |
| 108 |
check_for_wrong_terms(self, direct=kwargs) |
| 109 |
not_found = self.get_option("result_not_found") |
| 110 |
no_version = self.get_option("result_no_version") |
| 111 |
|
| 112 |
for term in terms: |
| 113 |
if not FQCN_RE.match(term): |
| 114 |
raise AnsibleLookupError(f'"{term}" is not a FQCN') |
| 115 |
|
| 116 |
try: |
| 117 |
collection_pkg = import_module(f"ansible_collections.{term}") |
| 118 |
except ImportError: |
| 119 |
# Collection not found |
| 120 |
result.append(not_found) |
| 121 |
continue |
| 122 |
|
| 123 |
try: |
| 124 |
data = load_collection_meta(collection_pkg, no_version=no_version) |
| 125 |
except Exception as exc: |
| 126 |
raise AnsibleLookupError(f"Error while loading metadata for {term}: {exc}") from exc |
| 127 |
|
| 128 |
result.append(data.get("version", no_version)) |
| 129 |
|
| 130 |
return result |