-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sebastian Neumann <[email protected]>
- Loading branch information
Showing
6 changed files
with
244 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ doc/build | |
galaxy.yml | ||
osism-commons-*.tar.gz | ||
scripts/render-template.py | ||
*.swp | ||
.idea/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import os | ||
import re | ||
import urllib.request | ||
import testinfra.utils.ansible_runner | ||
|
||
|
||
def extract_url_from_variable(host, variable_name): | ||
"""Extracts a URL from a given Ansible configuration variable.""" | ||
config_value = get_variable(host, variable_name) | ||
match = re.search(r"https?://[\w./-]+", config_value) | ||
if not match: | ||
raise ValueError( | ||
f"No valid URL found in the configuration variable: '{variable_name}'" | ||
) | ||
return match.group(0) | ||
|
||
|
||
def get_ansible(): | ||
testinfra_runner = testinfra.utils.ansible_runner.AnsibleRunner( | ||
os.environ["MOLECULE_INVENTORY_FILE"] | ||
) | ||
testinfra_hosts = testinfra_runner.get_hosts("all") | ||
|
||
return testinfra_runner, testinfra_hosts | ||
|
||
|
||
def get_variable(host, name, fact=False): | ||
if fact: | ||
ansible_facts = host.ansible("setup")["ansible_facts"] | ||
|
||
if name not in ansible_facts: | ||
raise Exception(f"Fact {name} not found!") | ||
|
||
return ansible_facts[name] | ||
|
||
all_vars = host.ansible.get_variables() | ||
if name in all_vars: | ||
return all_vars[name] | ||
|
||
# debug_value = host.ansible("debug", f"var={name}")[name] | ||
# if type(debug_value) is str and not debug_value.startswith('VARIABLE IS NOT DEFINED!'): | ||
# return debug_value | ||
# elif type(debug_value) is dict: | ||
# return debug_value | ||
|
||
role_name = os.environ["ROLE_NAME"] | ||
|
||
test_vars = host.ansible( | ||
"include_vars", f"../../molecule/delegated/vars/{role_name}.yml" | ||
)["ansible_facts"] | ||
if name in test_vars: | ||
return test_vars[name] | ||
|
||
default_vars = host.ansible( | ||
"include_vars", f"../../roles/{role_name}/defaults/main.yml" | ||
)["ansible_facts"] | ||
if name in default_vars: | ||
return default_vars[name] | ||
|
||
raise Exception(f"Variable {name} not found!") | ||
|
||
|
||
def get_role_variable(host, name, filename): | ||
role_name = os.environ["ROLE_NAME"] | ||
|
||
path = f"../../roles/{role_name}/vars/{filename}" | ||
variables = host.ansible("include_vars", path)["ansible_facts"] | ||
|
||
if name not in variables: | ||
raise Exception(f"{name} not found in {path}") | ||
|
||
return variables[name] | ||
|
||
|
||
def get_family_role_variable(host, name): | ||
return get_role_variable( | ||
host, name, get_variable(host, "ansible_os_family", True) + "-family.yml" | ||
) | ||
|
||
|
||
def get_dist_role_variable(host, name): | ||
return get_role_variable( | ||
host, name, get_variable(host, "ansible_distribution", True) + "-dist.yml" | ||
) | ||
|
||
|
||
def get_from_url(url, binary=False): | ||
# Create a request object with a faked User-Agent header. | ||
# Some websites like https://pkg.osquery.io/rpm/GPG need this, otherwise they will return http 403 forbidden. | ||
req = urllib.request.Request( | ||
url, headers={"User-Agent": "Mozilla/5.0 (Linux x86_64) Chrome/103.0.0.0"} | ||
) | ||
# Open the URL with the custom request object | ||
resource = urllib.request.urlopen(req) | ||
|
||
if not binary: | ||
encoding = resource.headers.get_content_charset() | ||
if encoding is None: | ||
encoding = "utf-8" | ||
content = resource.read().decode(encoding) | ||
else: | ||
content = resource.read() | ||
|
||
return content | ||
|
||
|
||
def get_centos_repo_key(host, summary): | ||
all_keys = host.run("rpm -qa gpg-pubkey | xargs -I{} sh -c 'rpm -qi {}'").stdout | ||
split_on = "{}\nDescription :\n-----BEGIN PGP PUBLIC KEY BLOCK-----\n".format( | ||
summary | ||
) | ||
installed_key = all_keys.split(split_on)[1].split( | ||
"-----END PGP PUBLIC KEY BLOCK-----" | ||
)[0] | ||
installed_key = "\n".join(installed_key.split("\n")[2:]) | ||
|
||
return installed_key | ||
|
||
|
||
def jinja_replacement(original_variable, replacements): | ||
original_variable = original_variable.replace("{{ ", "{").replace(" }}", "}") | ||
original_variable = original_variable.format(**replacements) | ||
|
||
return original_variable | ||
|
||
|
||
def jinja_list_concat(original_variable, lists): | ||
if type(original_variable) is list: | ||
return original_variable | ||
|
||
return_value = [] | ||
for li in lists: | ||
return_value += li | ||
|
||
return return_value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
molecule==24.2.0 | ||
molecule-docker==2.1.0 | ||
pytest==8.1.1 | ||
pytest-testinfra==10.1.0 |