From 57dc12725f67f731002a18899131e56d79b93b1e Mon Sep 17 00:00:00 2001 From: BrazenWillow <> Date: Tue, 3 Sep 2024 10:57:30 -0500 Subject: [PATCH] fix: refactor and add docker tests --- .gitignore | 4 ++ .vscode/settings.json | 3 - README.md | 89 +++++++++++++++--------- defaults/main.yml | 12 ++-- inventory | 1 - meta/main.yml | 88 ++++++++++------------- playbook.yml | 7 -- requirements.yml | 5 ++ roles/ansible-role-docker | 1 - tasks/build-images.yml | 4 +- tasks/docker-check.yml | 15 ++++ tasks/install-docker.yml | 15 ---- tasks/main.yml | 142 +++++++++++++++++++++----------------- tests/inventory | 2 - tests/test.yml | 5 -- 15 files changed, 205 insertions(+), 188 deletions(-) create mode 100644 .gitignore delete mode 100644 .vscode/settings.json delete mode 100644 inventory delete mode 100644 playbook.yml create mode 100644 requirements.yml delete mode 160000 roles/ansible-role-docker create mode 100644 tasks/docker-check.yml delete mode 100644 tasks/install-docker.yml delete mode 100644 tests/inventory delete mode 100644 tests/test.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..52098a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode + +inventory +inventory.yml \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index ae64258..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "ansible.python.interpreterPath": "/bin/python3.10" -} \ No newline at end of file diff --git a/README.md b/README.md index 81a101a..3a187a6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -ansible-simplex-chat-server +ansible-role-simplex-chat-server ========= Spin up a SimpleX SMP/XFTP server, optionally hosted/proxied with Tor @@ -6,55 +6,78 @@ Spin up a SimpleX SMP/XFTP server, optionally hosted/proxied with Tor Requirements ------------ -Docker +None. Role Variables -------------- +```yaml +######################################################## - ######################################################## - DOCKER_DIR: /opt/docker - ######################################################## +DOCKER_DIR: /opt/docker - ######################################################## - SIMPLEX_UID: 1000 - SIMPLEX_GID: 1000 +######################################################## - SIMPLEX_SMP_SERVER_ENABLED: true - SIMPLEX_SMP_IMAGE: simplexchat/smp-server:latest - SIMPLEX_SMP_PORT: 5223 - SIMPLEX_SMP_ADDR: changeme.internal - SIMPLEX_SMP_PASSWORD: +SIMPLEX_UID: 1000 +SIMPLEX_GID: 1000 - SIMPLEX_XFTP_SERVER_ENABLED: true - SIMPLEX_XFTP_IMAGE: simplexchat/xftp-server:latest - SIMPLEX_XFTP_PORT: 5233 - SIMPLEX_XFTP_QUOTA: 150gb - SIMPLEX_XFTP_ADDR: changeme.internal - SIMPLEX_XFTP_EXPIRE_FILE_HOURS: 48 - ######################################################## +SIMPLEX_SMP_SERVER_ENABLED: true +SIMPLEX_SMP_IMAGE: simplexchat/smp-server:latest +SIMPLEX_SMP_PORT: 5223 +SIMPLEX_SMP_ADDR: changeme.internal +SIMPLEX_SMP_PASSWORD: - ######################################################## - TOR_ENABLED: true - TOR_PROXY_ENABLED: true - TOR_SINGLE_HOP_MODE_ENABLED: true - TOR_NON_ANONYMOUS_MODE_ENABLED: true - ######################################################## +SIMPLEX_XFTP_SERVER_ENABLED: true +SIMPLEX_XFTP_IMAGE: simplexchat/xftp-server:latest +SIMPLEX_XFTP_PORT: 5233 +SIMPLEX_XFTP_QUOTA: 150gb +SIMPLEX_XFTP_ADDR: changeme.internal +SIMPLEX_XFTP_EXPIRE_FILE_HOURS: 48 - ######################################################## - WATCHTOWER_ENABLED: true - ######################################################## +######################################################## +TOR_ENABLED: true +TOR_PROXY_ENABLED: false +TOR_SINGLE_HOP_MODE_ENABLED: true +TOR_NON_ANONYMOUS_MODE_ENABLED: true + +######################################################## + +WATCHTOWER_ENABLED: true + +######################################################## +``` Dependencies ------------ -- geerlingguy.docker +```yaml +collections: + - name: community.docker +roles: + - src: geerlingguy.docker +``` Example Playbook ---------------- - - hosts: server - roles: - - ansible-simplex-chat-server +```yaml +--- + +- hosts: vps + become: yes + gather_facts: true + + pre_tasks: + - ansible.builtin.include_tasks: tasks/docker-check.yml + + roles: + - role: ansible-simplex-chat-server + vars: + SIMPLEX_UID: 5000 + SIMPLEX_GID: 5000 + SIMPLEX_XFTP_QUOTA: 1gb + SIMPLEX_SMP_ADDR: example.org + SIMPLEX_XFTP_ADDR: example.org +``` License ------- diff --git a/defaults/main.yml b/defaults/main.yml index 79d4166..a2ad08b 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,11 +1,10 @@ --- -# defaults file for ansible-simplex-chat-server - ######################################################## + DOCKER_DIR: /opt/docker -######################################################## ######################################################## + SIMPLEX_UID: 1000 SIMPLEX_GID: 1000 @@ -21,15 +20,16 @@ SIMPLEX_XFTP_PORT: 5233 SIMPLEX_XFTP_QUOTA: 150gb SIMPLEX_XFTP_ADDR: changeme.internal SIMPLEX_XFTP_EXPIRE_FILE_HOURS: 48 -######################################################## ######################################################## + TOR_ENABLED: true -TOR_PROXY_ENABLED: true +TOR_PROXY_ENABLED: false TOR_SINGLE_HOP_MODE_ENABLED: true TOR_NON_ANONYMOUS_MODE_ENABLED: true -######################################################## ######################################################## + WATCHTOWER_ENABLED: true + ######################################################## \ No newline at end of file diff --git a/inventory b/inventory deleted file mode 100644 index d18580b..0000000 --- a/inventory +++ /dev/null @@ -1 +0,0 @@ -localhost \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml index 7a9f14d..80297ff 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,51 +1,39 @@ +--- +dependencies: + - role: geerlingguy.docker + when: docker_installed is false or docker_compose_installed is false + galaxy_info: - author: your name - description: your role description - company: your company (optional) - - # If the issue tracker for your role is not on github, uncomment the - # next line and provide a value - # issue_tracker_url: http://example.com/issue/tracker - - # Choose a valid license ID from https://spdx.org - some suggested licenses: - # - BSD-3-Clause (default) - # - MIT - # - GPL-2.0-or-later - # - GPL-3.0-only - # - Apache-2.0 - # - CC-BY-4.0 - license: GPL-3.0-or-later - - min_ansible_version: 2.1 - - # If this a Container Enabled role, provide the minimum Ansible Container version. - # min_ansible_container_version: - - # - # Provide a list of supported platforms, and for each platform a list of versions. - # If you don't wish to enumerate all versions for a particular platform, use 'all'. - # To view available platforms and versions (or releases), visit: - # https://galaxy.ansible.com/api/v1/platforms/ - # - # platforms: - # - name: Fedora - # versions: - # - all - # - 25 - # - name: SomePlatform - # versions: - # - all - # - 1.0 - # - 7 - # - 99.99 - - galaxy_tags: [] - # List tags for your role here, one per line. A tag is a keyword that describes - # and categorizes the role. Users find roles by searching for tags. Be sure to - # remove the '[]' above, if you add tags to this list. - # - # NOTE: A tag is limited to a single word comprised of alphanumeric characters. - # Maximum 20 tags per role. - -dependencies: [] - # - community.docker \ No newline at end of file + author: BrazenWillow + description: Spin up a SimpleX SMP/XFTP server, optionally hosted/proxied with Tor + company: "" + issue_tracker_url: https://codeberg.org/BrazenWillow/ansible-role-simplex-chat-server/issues + license: AGPL-3.0-or-later + min_ansible_version: "2.10" + platforms: + - name: Fedora + versions: + - all + - name: Debian + versions: + - buster + - bullseye + - bookworm + - name: Ubuntu + versions: + - bionic + - focal + - jammy + - noble + - name: Alpine + version: + - all + - name: ArchLinux + versions: + - all + galaxy_tags: + - simplex + - simplex-chat + - simplex-chat-smp + - simplex-chat-xftp + - simplex-chat-server \ No newline at end of file diff --git a/playbook.yml b/playbook.yml deleted file mode 100644 index be5a835..0000000 --- a/playbook.yml +++ /dev/null @@ -1,7 +0,0 @@ ---- - -- hosts: localhost - become: yes - gather_facts: true - roles: - - ansible-simplex-chat-server diff --git a/requirements.yml b/requirements.yml new file mode 100644 index 0000000..39ae9ca --- /dev/null +++ b/requirements.yml @@ -0,0 +1,5 @@ +--- +collections: + - name: community.docker +roles: + - src: geerlingguy.docker \ No newline at end of file diff --git a/roles/ansible-role-docker b/roles/ansible-role-docker deleted file mode 160000 index ae17f06..0000000 --- a/roles/ansible-role-docker +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ae17f0641b9de1eaded93302da5bfcf513b3543d diff --git a/tasks/build-images.yml b/tasks/build-images.yml index e7570bb..8a2da4b 100644 --- a/tasks/build-images.yml +++ b/tasks/build-images.yml @@ -13,7 +13,7 @@ ansible.builtin.shell: cmd: "git clone https://github.com/simplex-chat/simplexmq" chdir: "{{ DOCKER_DIR }}/simplex/" - when: simplex_repo_stat.exists is false + when: simplex_repo_stat.stat.exists is false - name: Checkout stable branch of SimpleX chat repo ansible.builtin.shell: @@ -24,7 +24,7 @@ ansible.builtin.shell: cmd: "git pull" chdir: "{{ DOCKER_DIR }}/simplex/simplexmq" - when: simplex_repo_stat.exists is true + when: simplex_repo_stat.stat.exists is true - name: Build SimpleX Chat SMP server image community.docker.docker_image_build: diff --git a/tasks/docker-check.yml b/tasks/docker-check.yml new file mode 100644 index 0000000..a3f1686 --- /dev/null +++ b/tasks/docker-check.yml @@ -0,0 +1,15 @@ +--- + +- name: Check if Docker is installed + shell: "docker --version" + ignore_errors: true + register: docker_installed_check + +- name: Check if Docker Compose is installed + shell: "docker compose version" + ignore_errors: true + register: docker_compose_installed_check + +- ansible.builtin.set_fact: + docker_installed: "{{ docker_installed_check.failed is false }}" + docker_compose_installed: "{{ docker_compose_installed_check.failed is false }}" \ No newline at end of file diff --git a/tasks/install-docker.yml b/tasks/install-docker.yml deleted file mode 100644 index 162f3c5..0000000 --- a/tasks/install-docker.yml +++ /dev/null @@ -1,15 +0,0 @@ ---- -- name: Check if Docker is installed - shell: "docker --version" - ignore_errors: true - register: docker_installed - -- name: Check if Docker Compose is installed - shell: "docker compose version" - ignore_errors: true - register: docker_compose_installed - -- name: geerlingguy - ansible.builtin.include_role: - name: ansible-role-docker - when: docker_installed.failed or docker_compose_installed.failed diff --git a/tasks/main.yml b/tasks/main.yml index 45235a8..ce9fb5b 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,32 +1,27 @@ --- -# tasks file for ansible-simplex-chat-server - -# - ansible.builtin.set_fact: -# TEST: '' - -# - debug: -# msg: "{{ ',' + TEST if TEST | default('') | length > 1 else '' }}" - -# - meta: end_play - -- include_tasks: install-docker.yml - -- block: - - include_tasks: build-images.yml - - - ansible.builtin.set_fact: - SIMPLEX_SMP_IMAGE: local/smp-server - SIMPLEX_XFTP_IMAGE: local/xftp-server - simplex_image_source: local - - when: ansible_architecture != 'x86_64' - - name: Ensure Docker Working Directory Exists ansible.builtin.file: path: "{{ DOCKER_DIR }}" state: directory mode: "0755" +- name: Create SimpleX directory for docker config + ansible.builtin.file: + path: "{{ DOCKER_DIR }}/simplex/" + state: directory + +- name: Check if images need to be built manually + block: + - ansible.builtin.include_tasks: build-images.yml + + - name: Store updated variables after building + ansible.builtin.set_fact: + SIMPLEX_SMP_IMAGE: local/smp-server + SIMPLEX_XFTP_IMAGE: local/xftp-server + simplex_image_source: local + + when: ansible_architecture != 'x86_64' + - name: Create docker storage for SimpleX containers ansible.builtin.file: path: "{{ DOCKER_DIR }}/simplex/{{ item }}" @@ -50,7 +45,7 @@ - "smp" - "xftp" -- name: Generate env file for SimpleX Docker +- name: Generate .env file for SimpleX Docker ansible.builtin.template: src: "env.j2" dest: "{{ DOCKER_DIR }}/simplex/.env" @@ -62,7 +57,7 @@ dest: "{{ DOCKER_DIR }}/simplex/docker-compose.yaml" mode: "0755" -- name: Add Tor hosting option for SimpleX Server +- name: Add Tor hosting option for SimpleX SMP/XFTP Server block: - name: Create tor-data directories for tor docker container ansible.builtin.file: @@ -92,7 +87,7 @@ when: TOR_ENABLED is true -- name: Add Tor proxy option for SimpleX Server +- name: Add Tor proxy option for SimpleX SMP Server block: - name: Create tor-proxy directories for tor docker container ansible.builtin.file: @@ -132,57 +127,75 @@ cmd: docker compose up -d chdir: "{{ DOCKER_DIR }}/simplex" -- name: Wait for SMP server to generate fingerprint - ansible.builtin.wait_for: - path: "{{ DOCKER_DIR }}/simplex/smp/config/fingerprint" - timeout: 30 - -- name: Wait for XFTP server to generate fingerprint - ansible.builtin.wait_for: - path: "{{ DOCKER_DIR }}/simplex/xftp/config/fingerprint" - timeout: 30 - -- block: - - name: Read SimpleX SMP fingerprint - ansible.builtin.set_fact: - smp_fingerprint: "{{ lookup('file', DOCKER_DIR + '/simplex/smp/config/fingerprint') }}" - - - name: Wait for tor to generate onion for SMP server +- name: Gather SMP server info + block: + - name: Wait for SMP server to generate fingerprint ansible.builtin.wait_for: - path: "{{ DOCKER_DIR }}/simplex/tor-data/simplex-smp/hostname" + path: "{{ DOCKER_DIR }}/simplex/smp/config/fingerprint" timeout: 30 - - ansible.builtin.shell: - cmd: "cat {{ DOCKER_DIR }}/simplex/tor-data/simplex-smp/hostname" - register: smp_onion_cmd + - name: cat SMP fingerprint + ansible.builtin.shell: + cmd: "cat {{ DOCKER_DIR }}/simplex/smp/config/fingerprint" + register: smp_fingerprint_cmd - - name: Read SimpleX SMP onion hostname + - name: Gather SimpleX SMP fingerprint ansible.builtin.set_fact: - smp_onion: "{{ smp_onion_cmd.stdout }}" + smp_fingerprint: "{{ smp_fingerprint_cmd.stdout }}" + + - name: Gather onion info for SMP server + block: + - name: Wait for tor to generate onion for SMP server + ansible.builtin.wait_for: + path: "{{ DOCKER_DIR }}/simplex/tor-data/simplex-smp/hostname" + timeout: 30 + + - name: cat XFTP fingerprint + ansible.builtin.shell: + cmd: "cat {{ DOCKER_DIR }}/simplex/tor-data/simplex-smp/hostname" + register: smp_onion_cmd + + - name: Gather SimpleX SMP onion hostname + ansible.builtin.set_fact: + smp_onion: "{{ smp_onion_cmd.stdout }}" when: TOR_ENABLED is true - - name: Display SimpleX SMP URL + - name: Gather SimpleX SMP URL ansible.builtin.set_fact: smp_server: "smp://{{ smp_fingerprint }}@{{ SIMPLEX_SMP_ADDR }}{{ ',' + smp_onion if smp_onion | default ('') | length > 1 }}:{{ SIMPLEX_SMP_PORT }}" when: SIMPLEX_SMP_SERVER_ENABLED is true -- block: - - name: Read SimpleX XFTP fingerprint +- name: Gather XFTP server info + block: + - name: Wait for XFTP server to generate fingerprint + ansible.builtin.wait_for: + path: "{{ DOCKER_DIR }}/simplex/xftp/config/fingerprint" + timeout: 30 + + - name: cat XFTP fingerprint + ansible.builtin.shell: + cmd: "cat {{ DOCKER_DIR }}/simplex/xftp/config/fingerprint" + register: xftp_fingerprint_cmd + + - name: Gather SimpleX XFTP fingerprint ansible.builtin.set_fact: - xftp_fingerprint: "{{ lookup('file', DOCKER_DIR + '/simplex/xftp/config/fingerprint') }}" + xftp_fingerprint: "{{ xftp_fingerprint_cmd.stdout }}" - - ansible.builtin.shell: - cmd: "cat {{ DOCKER_DIR }}/simplex/tor-data/simplex-xftp/hostname" - register: xftp_onion_cmd + - name: Gather onion info for XFTP server + block: + - name: cat XFTP .onion hostname + ansible.builtin.shell: + cmd: "cat {{ DOCKER_DIR }}/simplex/tor-data/simplex-xftp/hostname" + register: xftp_onion_cmd + when: TOR_ENABLED is true + + - name: Gather SimpleX XFTP onion hostname + ansible.builtin.set_fact: + xftp_onion: "{{ xftp_onion_cmd.stdout }}" when: TOR_ENABLED is true - - name: Read SimpleX XFTP onion hostname - ansible.builtin.set_fact: - xftp_onion: "{{ xftp_onion_cmd.stdout }}" - when: TOR_ENABLED is true - - - name: Display SimpleX XFTP URL + - name: Gather SimpleX XFTP URL ansible.builtin.set_fact: xftp_server: "xftp://{{ xftp_fingerprint }}@{{ SIMPLEX_XFTP_ADDR }}{{',' + xftp_onion if xftp_onion | default('') | length > 1 }}:{{ SIMPLEX_XFTP_PORT }}" @@ -190,9 +203,12 @@ - name: Configure socks_proxy in smp-server.ini ansible.builtin.include_tasks: configure-tor-proxy-conf.yml + when: TOR_PROXY_ENABLED is defined -- debug: +- name: Display server URLs + debug: msg: "{{ item }}" loop: - - "{{ xftp_server }}" - - "{{ smp_server }}" + - "{{ smp_server | default ('') }}" + - "{{ xftp_server | default ('') }}" + when: item diff --git a/tests/inventory b/tests/inventory deleted file mode 100644 index 878877b..0000000 --- a/tests/inventory +++ /dev/null @@ -1,2 +0,0 @@ -localhost - diff --git a/tests/test.yml b/tests/test.yml deleted file mode 100644 index 3a478a2..0000000 --- a/tests/test.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -- hosts: localhost - remote_user: root - roles: - - ansible-simplex-chat-server