Matrix Docker Ansible eploy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

131 line
5.5 KiB

  1. # SPDX-FileCopyrightText: 2024 MDAD Team and contributors
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. - ansible.builtin.set_fact:
  6. matrix_appservice_discord_requires_restart: false
  7. - when: "matrix_appservice_discord_database_engine == 'postgres'"
  8. block:
  9. - name: Check if an SQLite database already exists
  10. ansible.builtin.stat:
  11. path: "{{ matrix_appservice_discord_sqlite_database_path_local }}"
  12. register: matrix_appservice_discord_sqlite_database_path_local_stat_result
  13. - when: "matrix_appservice_discord_sqlite_database_path_local_stat_result.stat.exists | bool"
  14. block:
  15. - ansible.builtin.include_role:
  16. name: galaxy/postgres
  17. tasks_from: migrate_db_to_postgres
  18. vars:
  19. postgres_db_migration_request:
  20. src: "{{ matrix_appservice_discord_sqlite_database_path_local }}"
  21. dst: "{{ matrix_appservice_discord_database_connString }}"
  22. caller: "{{ role_path | basename }}"
  23. engine_variable_name: 'matrix_appservice_discord_database_engine'
  24. engine_old: 'sqlite'
  25. systemd_services_to_stop: ['matrix-appservice-discord.service']
  26. - ansible.builtin.set_fact:
  27. matrix_appservice_discord_requires_restart: true
  28. - name: Ensure Appservice Discord image is pulled
  29. community.docker.docker_image:
  30. name: "{{ matrix_appservice_discord_docker_image }}"
  31. source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
  32. force_source: "{{ matrix_appservice_discord_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
  33. force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_appservice_discord_docker_image_force_pull }}"
  34. register: result
  35. retries: "{{ devture_playbook_help_container_retries_count }}"
  36. delay: "{{ devture_playbook_help_container_retries_delay }}"
  37. until: result is not failed
  38. - name: Ensure AppService Discord paths exist
  39. ansible.builtin.file:
  40. path: "{{ item }}"
  41. state: directory
  42. mode: 0750
  43. owner: "{{ matrix_user_username }}"
  44. group: "{{ matrix_user_groupname }}"
  45. with_items:
  46. - "{{ matrix_appservice_discord_base_path }}"
  47. - "{{ matrix_appservice_discord_config_path }}"
  48. - "{{ matrix_appservice_discord_data_path }}"
  49. - name: Check if an old database file already exists
  50. ansible.builtin.stat:
  51. path: "{{ matrix_appservice_discord_base_path }}/discord.db"
  52. register: matrix_appservice_discord_stat_db
  53. - name: (Data relocation) Ensure matrix-appservice-discord.service is stopped
  54. ansible.builtin.service:
  55. name: matrix-appservice-discord
  56. state: stopped
  57. enabled: false
  58. daemon_reload: true
  59. failed_when: false
  60. when: "matrix_appservice_discord_stat_db.stat.exists"
  61. - name: (Data relocation) Move AppService Discord discord.db file to ./data directory
  62. ansible.builtin.command:
  63. cmd: "mv {{ matrix_appservice_discord_base_path }}/{{ item }} {{ matrix_appservice_discord_data_path }}/{{ item }}"
  64. creates: "{{ matrix_appservice_discord_data_path }}/{{ item }}"
  65. removes: "{{ matrix_appservice_discord_base_path }}/{{ item }}"
  66. with_items:
  67. - discord.db
  68. - user-store.db
  69. - room-store.db
  70. when: "matrix_appservice_discord_stat_db.stat.exists"
  71. - name: Ensure AppService Discord config.yaml installed
  72. ansible.builtin.copy:
  73. content: "{{ matrix_appservice_discord_configuration | to_nice_yaml(indent=2, width=999999) }}"
  74. dest: "{{ matrix_appservice_discord_config_path }}/config.yaml"
  75. mode: 0644
  76. owner: "{{ matrix_user_username }}"
  77. group: "{{ matrix_user_groupname }}"
  78. - name: Ensure AppService Discord registration.yaml installed
  79. ansible.builtin.copy:
  80. content: "{{ matrix_appservice_discord_registration | to_nice_yaml(indent=2, width=999999) }}"
  81. dest: "{{ matrix_appservice_discord_config_path }}/registration.yaml"
  82. mode: 0644
  83. owner: "{{ matrix_user_username }}"
  84. group: "{{ matrix_user_groupname }}"
  85. # If `matrix_appservice_discord_client_id` hasn't changed, the same invite link would be generated.
  86. # We intentionally suppress Ansible changes.
  87. - name: Generate AppService Discord invite link
  88. ansible.builtin.shell: >-
  89. {{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-appservice-discord-link-gen
  90. --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
  91. --cap-drop=ALL
  92. --mount type=bind,src={{ matrix_appservice_discord_config_path }},dst=/cfg
  93. -w /cfg
  94. {{ matrix_appservice_discord_docker_image }}
  95. /bin/sh -c "node /build/tools/addbot.js > /cfg/invite_link"
  96. changed_when: false
  97. - name: Ensure matrix-appservice-discord container network is created
  98. community.general.docker_network:
  99. enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
  100. name: "{{ matrix_appservice_discord_container_network }}"
  101. driver: bridge
  102. driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"
  103. - name: Ensure matrix-appservice-discord.service installed
  104. ansible.builtin.template:
  105. src: "{{ role_path }}/templates/systemd/matrix-appservice-discord.service.j2"
  106. dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-appservice-discord.service"
  107. mode: 0644
  108. register: matrix_appservice_discord_systemd_service_result
  109. - name: Ensure matrix-appservice-discord.service restarted, if necessary
  110. ansible.builtin.service:
  111. name: "matrix-appservice-discord.service"
  112. state: restarted
  113. daemon_reload: true
  114. when: "matrix_appservice_discord_requires_restart | bool"