diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 00000000..135f10a1 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +library = ./ansible_library diff --git a/ansible_library/charm_metadata.py b/ansible_library/charm_metadata.py new file mode 100644 index 00000000..f4c8e356 --- /dev/null +++ b/ansible_library/charm_metadata.py @@ -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() diff --git a/roles/charm-publish/tasks/publish.yaml b/roles/charm-publish/tasks/publish.yaml index 1206cedf..8c6642cf 100644 --- a/roles/charm-publish/tasks/publish.yaml +++ b/roles/charm-publish/tasks/publish.yaml @@ -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: |