From 636142c4e0aae733373170087e6bf4681b03c09d Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Thu, 16 Mar 2023 10:19:23 +0200 Subject: [PATCH] matrix-bot-chatgpt role refactoring --- .../matrix-bot-chatgpt/defaults/main.yml | 22 ++++--- .../matrix-bot-chatgpt/tasks/install.yml | 63 +++++++++++++++++++ .../custom/matrix-bot-chatgpt/tasks/main.yml | 4 +- .../tasks/setup_install.yml | 63 ------------------- .../{setup_uninstall.yml => uninstall.yml} | 0 .../tasks/validate_config.yml | 15 ++++- .../systemd/matrix-bot-chatgpt.service.j2 | 6 +- 7 files changed, 97 insertions(+), 76 deletions(-) create mode 100644 roles/custom/matrix-bot-chatgpt/tasks/install.yml delete mode 100644 roles/custom/matrix-bot-chatgpt/tasks/setup_install.yml rename roles/custom/matrix-bot-chatgpt/tasks/{setup_uninstall.yml => uninstall.yml} (100%) diff --git a/roles/custom/matrix-bot-chatgpt/defaults/main.yml b/roles/custom/matrix-bot-chatgpt/defaults/main.yml index f34eb1e1c..16ad8588a 100644 --- a/roles/custom/matrix-bot-chatgpt/defaults/main.yml +++ b/roles/custom/matrix-bot-chatgpt/defaults/main.yml @@ -4,19 +4,22 @@ matrix_bot_chatgpt_enabled: true -matrix_bot_chatgpt_container_image_self_build: false -matrix_bot_chatgpt_docker_repo: "https://github.com/matrixgpt/matrix-chatgpt-bot" -matrix_bot_chatgpt_docker_repo_version: "{{ 'latest' if matrix_bot_chatgpt_version == 'latest' else matrix_bot_chatgpt_version }}" -matrix_bot_chatgpt_docker_src_files_path: "{{ matrix_base_data_path }}/chatgpt/docker-src" - matrix_bot_chatgpt_version: 3.0.0 -matrix_bot_chatgpt_docker_image: "{{ matrix_bot_chatgpt_docker_image_name_prefix }}matrixgpt/matrix-chatgpt-bot:{{ matrix_bot_chatgpt_version }}" -matrix_bot_chatgpt_docker_image_name_prefix: "{{ 'localhost/' if matrix_bot_chatgpt_container_image_self_build else 'ghcr.io/' }}" -matrix_bot_chatgpt_docker_image_force_pull: "{{ matrix_bot_chatgpt_docker_image.endswith(':latest') }}" + +matrix_bot_chatgpt_container_image_self_build: false +matrix_bot_chatgpt_container_image_self_build_repo: "https://github.com/matrixgpt/matrix-chatgpt-bot" +matrix_bot_chatgpt_container_image_self_build_repo_version: "{{ 'main' if matrix_bot_chatgpt_version == 'latest' else matrix_bot_chatgpt_version }}" + +matrix_bot_chatgpt_container_image: "{{ matrix_bot_chatgpt_container_image_name_prefix }}matrixgpt/matrix-chatgpt-bot:{{ matrix_bot_chatgpt_container_image_tag }}" +matrix_bot_chatgpt_container_image_name_prefix: "{{ 'localhost/' if matrix_bot_chatgpt_container_image_self_build else matrix_bot_chatgpt_container_image_registry_prefix }}" +matrix_bot_chatgpt_container_image_tag: "{{ matrix_bot_chatgpt_version }}" +matrix_bot_chatgpt_container_image_force_pull: "{{ matrix_bot_chatgpt_container_image.endswith(':latest') }}" +matrix_bot_chatgpt_container_image_registry_prefix: ghcr.io/ matrix_bot_chatgpt_base_path: "{{ matrix_base_data_path }}/chatgpt" matrix_bot_chatgpt_config_path: "{{ matrix_bot_chatgpt_base_path }}/config" matrix_bot_chatgpt_data_path: "{{ matrix_bot_chatgpt_base_path }}/data" +matrix_bot_chatgpt_container_src_path: "{{ matrix_bot_chatgpt_base_path }}/container-src" # A list of extra arguments to pass to the container matrix_bot_chatgpt_container_extra_arguments: [] @@ -78,6 +81,9 @@ matrix_bot_chatgpt_matrix_rich_text: true # MATRIX_RICH_TEXT=true # Additional environment variables to pass to the chatgpt container # +# You can discover additional environment variables from: +# https://github.com/matrixgpt/matrix-chatgpt-bot/blob/main/src/env.ts +# # Example: # matrix_bot_chatgpt_environment_variables_extension: | # chatgpt_TEXT_DONE=Done diff --git a/roles/custom/matrix-bot-chatgpt/tasks/install.yml b/roles/custom/matrix-bot-chatgpt/tasks/install.yml new file mode 100644 index 000000000..ef31df066 --- /dev/null +++ b/roles/custom/matrix-bot-chatgpt/tasks/install.yml @@ -0,0 +1,63 @@ +--- + +- name: Ensure chatgpt paths exist + ansible.builtin.file: + path: "{{ item.path }}" + state: directory + mode: 0750 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + with_items: + - {path: "{{ matrix_bot_chatgpt_config_path }}", when: true} + - {path: "{{ matrix_bot_chatgpt_data_path }}", when: true} + - {path: "{{ matrix_bot_chatgpt_container_src_path }}", when: matrix_bot_chatgpt_container_image_self_build} + when: "item.when | bool" + +- name: Ensure chatgpt environment variables file created + ansible.builtin.template: + src: "{{ role_path }}/templates/env.j2" + dest: "{{ matrix_bot_chatgpt_config_path }}/env" + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + mode: 0640 + +- name: Ensure chatgpt container image is pulled + community.docker.docker_image: + name: "{{ matrix_bot_chatgpt_container_image }}" + source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}" + force_source: "{{ matrix_bot_chatgpt_container_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}" + force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_bot_chatgpt_container_image_force_pull }}" + when: "not matrix_bot_chatgpt_container_image_self_build | bool" + register: result + retries: "{{ devture_playbook_help_container_retries_count }}" + delay: "{{ devture_playbook_help_container_retries_delay }}" + until: result is not failed + +- when: matrix_bot_chatgpt_container_image_self_build | bool + block: + - name: Ensure chatgpt repository is present on self-build + ansible.builtin.git: + repo: "{{ matrix_bot_chatgpt_container_image_self_build_repo }}" + version: "{{ matrix_bot_chatgpt_container_image_self_build_repo_version }}" + dest: "{{ matrix_bot_chatgpt_container_src_path }}" + force: "yes" + become: true + become_user: "{{ matrix_user_username }}" + register: matrix_bot_chatgpt_git_pull_results + + - name: Ensure chatgpt container image is built + community.docker.docker_image: + name: "{{ matrix_bot_chatgpt_container_image }}" + source: build + force_source: "{{ matrix_bot_chatgpt_git_pull_results.changed if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}" + force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_mailer_git_pull_results.changed }}" + build: + dockerfile: Dockerfile + path: "{{ matrix_bot_chatgpt_container_src_path }}" + pull: true + +- name: Ensure matrix-bot-chatgpt.service installed + ansible.builtin.template: + src: "{{ role_path }}/templates/systemd/matrix-bot-chatgpt.service.j2" + dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-bot-chatgpt.service" + mode: 0644 diff --git a/roles/custom/matrix-bot-chatgpt/tasks/main.yml b/roles/custom/matrix-bot-chatgpt/tasks/main.yml index 1086a62dd..3f5a7b7ec 100644 --- a/roles/custom/matrix-bot-chatgpt/tasks/main.yml +++ b/roles/custom/matrix-bot-chatgpt/tasks/main.yml @@ -11,11 +11,11 @@ ansible.builtin.include_tasks: "{{ role_path }}/tasks/validate_config.yml" - when: matrix_bot_chatgpt_enabled | bool - ansible.builtin.include_tasks: "{{ role_path }}/tasks/setup_install.yml" + ansible.builtin.include_tasks: "{{ role_path }}/tasks/install.yml" - tags: - setup-all - setup-bot-chatgpt block: - when: not matrix_bot_chatgpt_enabled | bool - ansible.builtin.include_tasks: "{{ role_path }}/tasks/setup_uninstall.yml" + ansible.builtin.include_tasks: "{{ role_path }}/tasks/uninstall.yml" diff --git a/roles/custom/matrix-bot-chatgpt/tasks/setup_install.yml b/roles/custom/matrix-bot-chatgpt/tasks/setup_install.yml deleted file mode 100644 index 5ae5ea953..000000000 --- a/roles/custom/matrix-bot-chatgpt/tasks/setup_install.yml +++ /dev/null @@ -1,63 +0,0 @@ ---- -- name: Ensure chatgpt paths exist - ansible.builtin.file: - path: "{{ item.path }}" - state: directory - mode: 0750 - owner: "{{ matrix_user_username }}" - group: "{{ matrix_user_groupname }}" - with_items: - - {path: "{{ matrix_bot_chatgpt_config_path }}", when: true} - - {path: "{{ matrix_bot_chatgpt_data_path }}", when: true} - - {path: "{{ matrix_bot_chatgpt_docker_src_files_path }}", when: matrix_bot_chatgpt_container_image_self_build} - when: "item.when | bool" - -- name: Ensure chatgpt environment variables file created - ansible.builtin.template: - src: "{{ role_path }}/templates/env.j2" - dest: "{{ matrix_bot_chatgpt_config_path }}/env" - owner: "{{ matrix_user_username }}" - group: "{{ matrix_user_groupname }}" - mode: 0640 - -- name: Ensure chatgpt image is pulled - community.docker.docker_image: - name: "{{ matrix_bot_chatgpt_docker_image }}" - source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}" - force_source: "{{ matrix_bot_chatgpt_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}" - force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_bot_chatgpt_docker_image_force_pull }}" - when: "not matrix_bot_chatgpt_container_image_self_build | bool" - register: result - retries: "{{ devture_playbook_help_container_retries_count }}" - delay: "{{ devture_playbook_help_container_retries_delay }}" - until: result is not failed - -- name: Ensure chatgpt repository is present on self-build - ansible.builtin.git: - repo: "{{ matrix_bot_chatgpt_docker_repo }}" - version: "{{ matrix_bot_chatgpt_docker_repo_version }}" - dest: "{{ matrix_bot_chatgpt_docker_src_files_path }}" - force: "yes" - become: true - become_user: "{{ matrix_user_username }}" - register: matrix_bot_chatgpt_git_pull_results - when: "matrix_bot_chatgpt_container_image_self_build | bool" - -- name: Ensure chatgpt image is built - community.docker.docker_image: - name: "{{ matrix_bot_chatgpt_docker_image }}" - source: build - force_source: "{{ matrix_bot_chatgpt_git_pull_results.changed if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}" - force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_mailer_git_pull_results.changed }}" - build: - dockerfile: Dockerfile - path: "{{ matrix_bot_chatgpt_docker_src_files_path }}" - pull: true - when: "matrix_bot_chatgpt_container_image_self_build | bool" - -- name: Ensure matrix-bot-chatgpt.service installed - ansible.builtin.template: - src: "{{ role_path }}/templates/systemd/matrix-bot-chatgpt.service.j2" - dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-bot-chatgpt.service" - mode: 0644 - register: matrix_bot_chatgpt_systemd_service_result diff --git a/roles/custom/matrix-bot-chatgpt/tasks/setup_uninstall.yml b/roles/custom/matrix-bot-chatgpt/tasks/uninstall.yml similarity index 100% rename from roles/custom/matrix-bot-chatgpt/tasks/setup_uninstall.yml rename to roles/custom/matrix-bot-chatgpt/tasks/uninstall.yml diff --git a/roles/custom/matrix-bot-chatgpt/tasks/validate_config.yml b/roles/custom/matrix-bot-chatgpt/tasks/validate_config.yml index ecc2b8d22..c6771823e 100644 --- a/roles/custom/matrix-bot-chatgpt/tasks/validate_config.yml +++ b/roles/custom/matrix-bot-chatgpt/tasks/validate_config.yml @@ -9,7 +9,6 @@ - {'name': 'matrix_bot_chatgpt_openai_api_key', when: true} - {'name': 'matrix_bot_chatgpt_matrix_bot_username', when: true} - - name: Fail if OpenAI configuration not up-to-date. ansible.builtin.fail: msg: >- @@ -20,3 +19,17 @@ - {'name': 'matrix_bot_chatgpt_openai_email'} - {'name': 'matrix_bot_chatgpt_openai_password'} - {'name': 'matrix_bot_chatgpt_openai_login_type'} + +- name: (Deprecation) Catch and report renamed ChatGPT settings + ansible.builtin.fail: + msg: >- + Your configuration contains a variable, which now has a different name. + Please change your configuration to rename the variable (`{{ item.old }}` -> `{{ item.new }}`). + when: "item.old in vars" + with_items: + - {'old': 'matrix_bot_chatgpt_docker_image', 'new': 'matrix_bot_chatgpt_container_image'} + - {'old': 'matrix_bot_chatgpt_docker_image_name_prefix', 'new': 'matrix_bot_chatgpt_container_image_name_prefix'} + - {'old': 'matrix_bot_chatgpt_docker_image_force_pull', 'new': 'matrix_bot_chatgpt_container_image_force_pull'} + - {'old': 'matrix_bot_chatgpt_docker_repo', 'new': 'matrix_bot_chatgpt_container_image_self_build_repo'} + - {'old': 'matrix_bot_chatgpt_docker_repo_version', 'new': 'matrix_bot_chatgpt_container_image_self_build_repo_version'} + - {'old': 'matrix_bot_chatgpt_docker_src_files_path', 'new': 'matrix_bot_chatgpt_container_src_path'} diff --git a/roles/custom/matrix-bot-chatgpt/templates/systemd/matrix-bot-chatgpt.service.j2 b/roles/custom/matrix-bot-chatgpt/templates/systemd/matrix-bot-chatgpt.service.j2 index 8e4a50202..c8fa06c19 100644 --- a/roles/custom/matrix-bot-chatgpt/templates/systemd/matrix-bot-chatgpt.service.j2 +++ b/roles/custom/matrix-bot-chatgpt/templates/systemd/matrix-bot-chatgpt.service.j2 @@ -16,7 +16,9 @@ Environment="HOME={{ devture_systemd_docker_base_systemd_unit_home_path }}" ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} kill matrix-bot-chatgpt 2>/dev/null || true' ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-bot-chatgpt 2>/dev/null || true' -ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-bot-chatgpt \ +ExecStart={{ devture_systemd_docker_base_host_command_docker }} run \ + --rm \ + --name=matrix-bot-chatgpt \ --log-driver=none \ --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ --cap-drop=ALL \ @@ -29,7 +31,7 @@ ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name {% for arg in matrix_bot_chatgpt_container_extra_arguments %} {{ arg }} \ {% endfor %} - {{ matrix_bot_chatgpt_docker_image }} + {{ matrix_bot_chatgpt_container_image }} ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} kill matrix-bot-chatgpt 2>/dev/null || true' ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-bot-chatgpt 2>/dev/null || true'