Merge "Add module to read metadata from downloaded artifact" into main

This commit is contained in:
Zuul 2024-01-29 10:27:19 +00:00 committed by Gerrit Code Review
commit 18d5e2fbb3
3 changed files with 54 additions and 17 deletions

2
ansible.cfg Normal file
View File

@ -0,0 +1,2 @@
[defaults]
library = ./ansible_library

View File

@ -0,0 +1,48 @@
#!/usr/bin/env python3
# Copyright 2024 Canonical Ltd.
import pathlib
import zipfile
import yaml
from ansible.module_utils.basic import AnsibleModule
DOCUMENTATION = r"""
module: charm_metadata
author: Canonical
description: Read charm metadata from a charm artifact
options:
path:
description: path to charm artifact
required: yes
"""
EXAMPLES = r"""
- name: Read charm metadata
charm_metadata:
charm: /tmp/keystone.charm
"""
def run_module():
module = AnsibleModule(
argument_spec=dict(
path=dict(type="str", required=True),
)
)
path: str = module.params["path"] # type: ignore
charm_path = pathlib.Path(path)
if not charm_path.exists():
module.fail_json(msg=f"charm {path} not found on disk")
with zipfile.ZipFile(charm_path, "r") as charm_zip:
metadata = charm_zip.read("metadata.yaml")
metadata_dict = yaml.safe_load(metadata)
module.exit_json(changed=False, metadata=metadata_dict)
if __name__ == "__main__":
run_module()

View File

@ -20,27 +20,14 @@
name: ensure-docker
- name: Get metadata.yaml information
stat:
path: "{{ zuul.executor.work_root+'/'+zuul.project.src_dir+'/charms/'+charm_build_name+'/metadata.yaml' }}"
register: metadata_file
- name: Get charmcraft.yaml information
stat:
path: "{{ zuul.executor.work_root+'/'+zuul.project.src_dir+'/charms/'+charm_build_name+'/charmcraft.yaml' }}"
register: charmcraft_file
- name: Register resource file
set_fact:
resource_file: "{{ metadata_file.stat.exists | ternary(metadata_file.stat.path, charmcraft_file.stat.path) }}"
- name: Which metadata file is used
debug:
msg: "{{ resource_file }}"
charm_metadata:
path: "{{ zuul.project.src_dir }}/{{ charm_build_name }}.charm"
register: charm_metadata
- name: Upload oci-image to charmhub
register: upload_oci_image_output
vars:
metadata: "{{ lookup('file', resource_file) | from_yaml }}"
metadata: "{{ charm_metadata['metadata'] }}"
args:
executable: /bin/bash
shell: |