diff --git a/roles/happi_db_autogen/defaults/main.yml b/roles/happi_db_autogen/defaults/main.yml new file mode 100644 index 0000000..9454511 --- /dev/null +++ b/roles/happi_db_autogen/defaults/main.yml @@ -0,0 +1,62 @@ +--- + +happi_db_autogen_directory: "/opt/happi_db_autogen" +happi_db_autogen_beamline_acronym: "tst" +happi_db_autogen_redis_url: "https://info.tst.nsls2.bnl.gov" +happi_db_autogen_redis_prefix: "" + + +# Base set of objects to instantiate + +happi_db_autogen_base_objects: + redis_md_server: + id: "redis_md_server" + name: "redis_md_server" + type: "HappiItem" + args: + - host: "{{ happi_db_autogen_redis_url}}" + active: true + last_edit: "{{ ansible_date_time.iso8601 }}" + device_class: "redis.Redis" + + runengine_metadata: + _id: "runengine_metadata" + name: "runengine_metadata" + type: "HappiItem" + args: + - "redis_md_server" + kwargs: + prefix: "{{ happi_db_autogen_redis_prefix }}" + active: true + last_edit: "{{ ansible_date_time.iso8601 }}" + device_class: "redis_json_dict.RedisJSONDict" + + RE: + _id: "RE" + name: "RE" + type: "HappiItem" + args: + - "runengine_metadata" + active: true + last_edit: "{{ ansible_date_time.iso8601 }}" + device_class: "bluesky.run_engine.RunEngine" + + tiled_writing_client: + _id: "tiled_writing_client" + name: "tiled_writing_client" + type: "HappiItem" + active: true + args: + - "{{ happi_db_autogen_beamline_acronym }}" + last_edit: "{{ ansible_date_time.iso8601 }}" + device_class: "tiled.client.from_profile" + + tiled_writer: + _id: "tiled_writer" + name: "tiled_writer" + type: "HappiItem" + active: true + args: + - "tiled_writing_client" + last_edit: "{{ ansible_date_time.iso8601 }}" + device_class: "bluesky.callbacks.tiled_writer.TiledWriter" diff --git a/roles/happi_db_autogen/tasks/add-device-specific.yml b/roles/happi_db_autogen/tasks/add-device-specific.yml new file mode 100644 index 0000000..3c37b34 --- /dev/null +++ b/roles/happi_db_autogen/tasks/add-device-specific.yml @@ -0,0 +1,41 @@ +--- + +- name: Replace and dashes with underscores in device name + ansible.builtin.set_fact: + happi_db_autogen_device_name: + "{{ happi_db_autogen_device_name | + replace('-', '_') }}" + +- name: Get device configuration + ansible.builtin.set_fact: + happi_db_autogen_device: "{{ host_config[happi_db_autogen_device_name] }}" + +- name: Check if vars file exists for device type + ansible.builtin.stat: + path: "{{ role_path }}/vars/{{ happi_db_autogen_device.type }}.yml" + register: device_type_vars_file + delegate_to: localhost + +- name: Add in device specific ophyd objects if defined + when: device_type_vars_file.stat.exists + block: + - name: Load device-specific objects + ansible.builtin.include_vars: + file: "vars/{{ happi_db_autogen_device.type }}.yml" + + - name: Create new dictionary to store device-specific objects + ansible.builtin.set_fact: + happi_db_autogen_device_objs: {} + + - name: Convert device objects to dictionary + ansible.builtin.set_fact: + happi_db_autogen_device_objs: >- + {{ happi_db_autogen_device_objs | + combine({ item._id : item }) }} + loop: "{{ happi_db_autogen_device_obj_list }}" + + - name: Merge device-specific objects into Happi database contents + ansible.builtin.set_fact: + happi_db_autogen_contents: + "{{ happi_db_autogen_contents | + combine(happi_db_autogen_device_objs) }}" diff --git a/roles/happi_db_autogen/tasks/main.yml b/roles/happi_db_autogen/tasks/main.yml new file mode 100644 index 0000000..468e397 --- /dev/null +++ b/roles/happi_db_autogen/tasks/main.yml @@ -0,0 +1,39 @@ +--- + +- name: Ensure Happi database autogen directory exists + ansible.builtin.file: + path: "{{ happi_db_autogen_directory }}" + state: directory + owner: "root" + group: "root" + mode: "0755" + +- name: Create variable to store Happi database contents + ansible.builtin.set_fact: + happi_db_autogen_contents: {} + +- name: Merge in default objects + ansible.builtin.set_fact: + happi_db_autogen_contents: + "{{ happi_db_autogen_contents | + combine(happi_db_autogen_base_objects) }}" + +- name: Get list of IOC names from loaded config + ansible.builtin.set_fact: + happi_db_autogen_ioc_list: + "{{ happi_db_autogen_component_config | + dict2items | + selectattr('value', 'mapping') | + selectattr('value.type', 'defined') | + map(attribute='key') | list }}" + +- name: Merge in device specific objects + ansible.builtin.include_tasks: add-device-specific.yml + loop: "{{ happi_db_autogen_ioc_list }}" + loop_control: + loop_var: happi_db_autogen_device_name + +- name: Write out the contents of the Happi database + ansible.builtin.copy: + dest: "{{ happi_db_autogen_directory }}/{{ happi_db_autogen_beamline_acronym }}_happi_db.json" # yamllint disable-line rule:line-length + content: "{{ happi_db_autogen_contents | to_nice_json(sort_keys=False) }}" diff --git a/roles/happi_db_autogen/vars/pandabox.yml b/roles/happi_db_autogen/vars/pandabox.yml new file mode 100644 index 0000000..4c08966 --- /dev/null +++ b/roles/happi_db_autogen/vars/pandabox.yml @@ -0,0 +1,24 @@ +--- + +happi_db_autogen_device_obj_list: + - _id: "{{ happi_db_autogen_device_name }}_path_provider" + name: "{{ happi_db_autogen_device_name }}_path_provider" + type: "HappiItem" + active: true + args: [] + kwargs: {} + last_edit: "{{ ansible_date_time.iso8601 }}" + device_class: "nslsii.ophyd_async.providers.NSLS2PathProvider" + + - _id: "{{ happi_db_autogen_device_name }}" + name: "{{ happi_db_autogen_device_name }}" + type: "OphydItem" + args: + - "{{ happi_db_autogen_device.environment.PREFIX }}" + - "{{ happi_db_autogen_device_name }}_path_provider" + kwargs: + name: "{{ happi_db_autogen_device_name }}" + prefix: "{{ happi_db_autogen_device.environment.PREFIX }}" + active: true + last_edit: "{{ ansible_date_time.iso8601 }}" + device_class: "ophyd_async.fastcs.panda.HDFPanda"