psi-4ward / psitransfer

Simple open source self-hosted file sharing solution.
BSD 2-Clause "Simplified" License
1.45k stars 211 forks source link

Ansible (Debian-only) #309

Open martin-braun opened 1 month ago

martin-braun commented 1 month ago

The "Manual, from source" steps could be incorporated into an Ansible script. I used to do this for Directus deployments and it should be pretty straight forward if we limit ourselves to Debian.

Reasons for this:

Cons for this:

I'm making this issue to see if someone else is interested in this? (You could assign me to this issue right away)

lpirl commented 1 month ago

Great idea.

It is probably hard to create a role that fits the taste of everyone, but if we don't try, we don't know.

I use the following quick-and-dirty role for updating a small deployment. Initial installation would need some more steps, as installing npm, etc.

# file: <role_name>/defaults/main.yml

file_transfer_app_user_name: file-transfer
# file: <role_name>/tasks/main.yml

- name: create user for file transfer app
  register: create_file_transfer_app_user_result
  user:
    name: "{{ file_transfer_app_user_name }}"
    comment: "user for running the file transfer Web app"
    group: www-data

- name: reset PsiTransfer sources
  register: git_checkout
  command: git checkout .
  args:
    chdir: >-
      {{ create_file_transfer_app_user_result.home }}/psitransfer
  changed_when: "'Updated 0 paths' not in git_checkout.stderr"
  become_user: "{{ file_transfer_app_user_name }}"

- name: update PsiTransfer sources
  register: git_pull
  command: git pull
  args:
    chdir: >-
      {{ create_file_transfer_app_user_result.home }}/psitransfer
  changed_when: "'is up to date' not in git_pull.stdout"
  become_user: "{{ file_transfer_app_user_name }}"

- name: build
  command: "{{ item.cmd }}"
  args:
    chdir: "{{ item.dir }}"
  with_items:
    - cmd: npm install
      dir: "{{ create_file_transfer_app_user_result.home }}/psitransfer/app"
    - cmd: npm run build
      dir: "{{ create_file_transfer_app_user_result.home }}/psitransfer/app"
    - cmd: npm install
      dir: "{{ create_file_transfer_app_user_result.home }}/psitransfer"

  become_user: "{{ file_transfer_app_user_name }}"
  when: git_pull.changed

- name: restart file transfer app
  service:
    name: psitransfer
    state: restarted
  when: git_pull.changed

Could maybe serve as a starting point.

martin-braun commented 1 month ago

@lpirl Thanks, that's great, but there is obviously more to it, especially on Debian you need to install node through nodesource. Luckily, I have those things all prepared so it really is just a copy-over. What also need to be part of it is a proxy (such as nginx), and configuration that can be adjusted beforehand. Ansible supports jinja and I went through the pain to setup such things already. Lastly, we need a service to run the app, and systemd is probably the most straight-forward method here when targeting Debian only, for Alpine this would be a different story. But, I just don't like to leverage things like pm2, because I hate to learn service runners for each and every language out there. I like to use what I already have available.

I'm glad that you share your starting point, because it gives me an important thing to answer: How to deploy?

When it comes to node projects, I always use Ansible's synchronize module (rsync) to deploy the files and then build it on the target to cover native code dependencies that needs to be built on the target platform. git clone is also a nice way of solving things, although my perspective is that you have to clone the project to get the necessary playbook and all the configuration. You then need to setup your inventory.cfg, might as well upload your local project instead of doing git clone on the target. This would also allow to deploy with local modifications.

How do you think about that?


Here is how I did this approach with a Deno application: https://github.com/martin-braun/cronmon/blob/master/playbook.yml

The ansible folder contains configuration templates. Would love to have some feedback on my approach. :)