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.
 
 

118 lines
5.6 KiB

  1. # SPDX-FileCopyrightText: 2024 Slavi Pantaleev
  2. # SPDX-FileCopyrightText: 2025 Suguru Hirahara
  3. #
  4. # SPDX-License-Identifier: AGPL-3.0-or-later
  5. ---
  6. - ansible.builtin.set_fact:
  7. matrix_authentication_service_syn2mas_migrate_dry_run: "{{ matrix_authentication_service_syn2mas_migrate_dry_run | bool }}"
  8. - name: Abort, if not using Synapse
  9. when: not matrix_synapse_enabled | bool
  10. ansible.builtin.fail:
  11. msg: |-
  12. You can only use syn2mas to migrate from Synapse to Matrix Authentication Service.
  13. Other homeserver implementations are not supported.
  14. - name: Fail if required matrix-authentication-service syn2mas settings not defined
  15. ansible.builtin.fail:
  16. msg: >-
  17. You need to define a required configuration setting (`{{ item.name }}`).
  18. when: "item.when | bool and lookup('vars', item.name, default='') | string | length == 0"
  19. with_items:
  20. - {'name': 'matrix_authentication_service_syn2mas_synapse_homeserver_config_path', when: true}
  21. - name: Check if Synapse homeserver config file exists
  22. ansible.builtin.stat:
  23. path: "{{ matrix_authentication_service_syn2mas_synapse_homeserver_config_path }}"
  24. register: matrix_authentication_service_syn2mas_synapse_config_stat
  25. - name: Fail if Synapse homeserver config file does not exist
  26. ansible.builtin.fail:
  27. msg: "The Synapse homeserver config file does not exist at the specified path: {{ matrix_authentication_service_syn2mas_synapse_homeserver_config_path }}"
  28. when: not matrix_authentication_service_syn2mas_synapse_config_stat.stat.exists
  29. - name: Ensure Synapse is stopped
  30. when: not matrix_authentication_service_syn2mas_migrate_dry_run | bool
  31. ansible.builtin.service:
  32. name: matrix-synapse
  33. state: stopped
  34. daemon_reload: true
  35. register: matrix_authentication_service_synapse_ensure_stopped_result
  36. # We probably don't necessarily need to stop this, because:
  37. # - the upstream docs don't say we should.
  38. # - while a migration is in progress (see `matrix_authentication_service_migration_in_progress`),
  39. # we don't even add compatibility layer labels, so MAS would not be used anyway.
  40. #
  41. # Still, it's probably safer to stop it anyway.
  42. - name: Ensure Matrix Authentication Service is stopped
  43. when: not matrix_authentication_service_syn2mas_migrate_dry_run | bool
  44. ansible.builtin.service:
  45. name: matrix-authentication-service
  46. state: stopped
  47. register: matrix_authentication_service_mas_ensure_stopped_result
  48. # This is similar to the command found in the systemd service file.
  49. #
  50. # We cannot use `docker exec` with the existing Matrix Authentication Service container here,
  51. # because we need an additional mount (the Synapse homeserver config).
  52. - name: Generate syn2mas migration command
  53. ansible.builtin.set_fact:
  54. matrix_authentication_service_mas_cli_syn2mas_command: >-
  55. {{ devture_systemd_docker_base_host_command_docker }} run
  56. --rm
  57. --name=matrix-authentication-service-syn2mas
  58. --log-driver=none
  59. --user={{ matrix_authentication_service_uid }}:{{ matrix_authentication_service_gid }}
  60. --cap-drop=ALL
  61. --network={{ matrix_authentication_service_syn2mas_container_network }}
  62. --mount type=bind,src={{ matrix_authentication_service_config_path }}/config.yaml,dst=/config.yaml,ro
  63. --mount type=bind,src={{ matrix_authentication_service_data_keys_path }},dst=/keys,ro
  64. --mount type=bind,src={{ matrix_authentication_service_syn2mas_synapse_homeserver_config_path }},dst=/homeserver.yaml,ro
  65. {{ matrix_authentication_service_container_image }}
  66. syn2mas
  67. --synapse-config=/homeserver.yaml
  68. {{ matrix_authentication_service_syn2mas_command_extra_options | join(' ') }}
  69. {{ matrix_authentication_service_syn2mas_subcommand }}
  70. {{ '--dry-run' if matrix_authentication_service_syn2mas_migrate_dry_run and matrix_authentication_service_syn2mas_subcommand == 'migrate' else '' }}
  71. {{ matrix_authentication_service_syn2mas_subcommand_extra_options | join(' ') }}
  72. tags:
  73. - skip_ansible_lint
  74. # This is a hack.
  75. # See: https://ansibledaily.com/print-to-standard-output-without-escaping/
  76. #
  77. # We want to run `debug: msg=".."`, but that dumps it as JSON and escapes double quotes within it,
  78. # which ruins the command (`matrix_authentication_service_mas_cli_syn2mas_command`).
  79. - name: Note about syn2mas migration
  80. ansible.builtin.set_fact:
  81. dummy: true
  82. with_items:
  83. - >-
  84. Running syn2mas migration using the following command: `{{ matrix_authentication_service_mas_cli_syn2mas_command }}`.
  85. If this crashes, you can stop Synapse (`systemctl stop matrix-synapse`), start Matrix Authentication Service (`systemctl start matrix-authentication-service`) and run the command manually.
  86. - name: Perform syn2mas migration
  87. ansible.builtin.command:
  88. cmd: "{{ matrix_authentication_service_mas_cli_syn2mas_command }}"
  89. register: matrix_authentication_service_mas_cli_syn2mas_command_result
  90. changed_when: matrix_authentication_service_mas_cli_syn2mas_command_result.rc == 0
  91. - name: Print syn2mas migration command result
  92. ansible.builtin.debug:
  93. var: matrix_authentication_service_mas_cli_syn2mas_command_result
  94. - name: Ensure Synapse is started (if it previously was)
  95. when: "not matrix_authentication_service_syn2mas_migrate_dry_run and matrix_authentication_service_mas_cli_syn2mas_command_result.changed"
  96. ansible.builtin.service:
  97. name: matrix-synapse
  98. state: started
  99. - name: Ensure Matrix Authentication Service is started (if it previously was)
  100. when: "not matrix_authentication_service_syn2mas_migrate_dry_run and matrix_authentication_service_mas_ensure_stopped_result.changed"
  101. ansible.builtin.service:
  102. name: matrix-authentication-service
  103. state: started