Skip to content

Instantly share code, notes, and snippets.

@thequailman
Last active April 4, 2021 05:16
Show Gist options
  • Save thequailman/caffade8049ccbee64113c77f42b9bbe to your computer and use it in GitHub Desktop.
Save thequailman/caffade8049ccbee64113c77f42b9bbe to your computer and use it in GitHub Desktop.
librephotos ansible

Example NGINX config

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  ssl_certificate /etc/certs/example.com.crt;
  ssl_certificate_key /etc/certs/example.com.key;
  include ssl-high.conf;
  server_name photos.example.com;
  root /opt/librephotos/frontend/build;

  proxy_buffering off;

  location / {
    try_files $uri /index.html; 
  }
  location ~ ^/(api|media) {
    proxy_pass http://photos;
    client_max_body_size 0;
    proxy_connect_timeout 300;
    proxy_http_version 1.1;
    proxy_read_timeout 300;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect http:// https://;
    proxy_set_header X-Forwarded-Ssl on;
        proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
  }
  location /protected_media {
    alias /var/lib/librephotos/protected_media/;
    disable_symlinks off;
    internal; 
  }
  location /original {
    alias /var/lib/librephotos/data/;
    disable_symlinks off;
    internal; 
  }
}

librephotos/handlers/main.yaml

- name: install librephotos-backend
  shell: |
    python3 -m venv /opt/librephotos/backend/venv 
    /opt/librephotos/backend/venv/bin/pip install --upgrade pip
    /opt/librephotos/backend/venv/bin/pip install wheel
    /opt/librephotos/backend/venv/bin/pip install torch==1.7.1+cpu torchvision==0.8.2+cpu -f https://download.pytorch.org/whl/torch_stable.html
    /opt/librephotos/backend/venv/bin/pip install -r /opt/librephotos/backend/requirements.txt
    /opt/librephotos/backend/venv/bin/python -m spacy download en_core_web_sm
    
- name: install librephotos-frontend
  shell: |
    cd /opt/librephotos/frontend
    npm install
    npm run build

- name: restart librephotos
  service:
    daemon_reload: yes
    name: librephotos
    state: restarted

- name: restart librephotos-image-similarity
  service:
    daemon_reload: yes
    name: librephotos-image-similarity
    state: restarted

- name: restart librephotos-worker
  service:
    daemon_reload: yes
    name: librephotos-worker
    state: restarted

librephotos/tasks/main.yaml

- name: install librephotos dependencies
  package:
    name:
      - build-essential
      - cmake
      - ffmpeg
      - git
      - libheif-dev
      - libpq-dev
      - libssl-dev
      - nodejs
      - npm
      - python3-dev
      - python3-venv
      - rustc
      - swig
    state: present

- name: install place365_model
  shell:
    cmd: |
      mkdir -p /var/lib/librephotos/data_models/places365
      curl -SL https://s3.eu-central-1.amazonaws.com/ownphotos-deploy/places365_model.tar.gz | tar -zxC /var/lib/librephotos/data_models/places365/
    creates: /var/lib/librephotos/data_models/places365/model

- name: install im2txt_model
  shell:
    cmd: |
      mkdir -p /var/lib/librephotos/data_models/im2txt
      curl -SL https://s3.eu-central-1.amazonaws.com/ownphotos-deploy/im2txt_model.tar.gz | tar -zxC /var/lib/librephotos/data_models/im2txt/
    creates: /var/lib/librephotos/data_models/im2txt/models

- name: install im2txt_data
  shell:
    cmd: curl -SL https://s3.eu-central-1.amazonaws.com/ownphotos-deploy/im2txt_data.tar.gz | tar -zxC /var/lib/librephotos/data_models/im2txt
    creates: /var/lib/librephotos/data_models/im2txt/data

- name: logs
  file:
    group: librephotos
    owner: librephotos
    path: /var/lib/librephotos/logs
    state: directory

- name: clone backend
  git:
    dest: /opt/librephotos/backend
    force: yes
    repo: https://github.com/librephotos/librephotos.git
    version: "{{ librephotos.commit.backend }}"
  notify:
    - install librephotos-backend
    - restart librephotos-backend

- name: clone frontend
  git:
    dest: /opt/librephotos/frontend
    force: yes
    repo: https://github.com/librephotos/librephotos-frontend.git
    version: "{{ librephotos.commit.frontend }}"
  notify:
    - install librephotos-frontend
    - restart librephotos-frontend

- name: librephotos config 
  copy:
    content: |
      BACKEND_HOST={{ librephotos.hostname }}
      BASE_DATA=/var/lib/librephotos
      BASE_LOGS=/var/lib/librephotos/logs
      DB_BACKEND=postgresql
      DB_HOST={{ librephotos.postgresql.hostname }}
      DB_PORT=5432
      DB_NAME={{ librephotos.postgresql.username }}
      DB_USER={{ librephotos.postgresql.username }}
      DB_PASS={{ librephotos.postgresql.password }}
      REDIS_HOST={{ librephotos.redis.hostname }}
      REDIS_PASS={{ librephotos.redis.password }}
      REDIS_PORT={{ librephotos.redis.port }}
      SECRET_KEY={{ librephotos.secret_key }}
      TIME_ZONE={{ librephotos.time_zone }}
    dest: /etc/librephotos.env
    mode: "0400"
  notify: restart librephotos

- name: librephotos service
  copy:
    content: |
      # ansible/media/librephotos

      [Unit]
      Description=Librephotos
      After=network.target

      [Service]
      Type=simple
      User=librephotos
      Group=librephotos
      EnvironmentFile=/etc/librephotos.env
      ExecStartPre=/opt/librephotos/backend/venv/bin/python manage.py migrate
      ExecStartPre=/opt/librephotos/backend/venv/bin/python manage.py clear_cache
      ExecStart=/opt/librephotos/backend/venv/bin/gunicorn --worker-class=gevent --timeout 3600 --bind 0.0.0.0:8001 --log-level=info ownphotos.wsgi
      Restart=always
      RestartSec=5
      StartLimitInterval=0
      WorkingDirectory=/opt/librephotos/backend

      [Install]
      WantedBy=multi-user.target
    dest: /etc/systemd/system/librephotos.service
  notify: restart librephotos

- name: librephotos service
  service:
    enabled: yes
    name: librephotos
    state: started

- name: librephotos-image-similarity service
  copy:
    content: |
      # ansible/media/librephotos

      [Unit]
      Description=Librephotos Image Similarity
      After=network.target

      [Service]
      Type=simple
      User=librephotos
      Group=librephotos
      Environment=BASE_LOGS=/var/lib/librephotos/logs
      ExecStart=/opt/librephotos/backend/venv/bin/python image_similarity/main.py
      Restart=always
      RestartSec=5
      StartLimitInterval=0
      WorkingDirectory=/opt/librephotos/backend

      [Install]
      WantedBy=multi-user.target
    dest: /etc/systemd/system/librephotos-image-similarity.service
  notify: restart librephotos-image-similarity

- name: librephotos-image-simlarity service
  service:
    enabled: yes
    name: librephotos-image-similarity
    state: started


- name: librephotos-worker service
  copy:
    content: |
      # ansible/media/librephotos

      [Unit]
      Description=Librephotos Worker
      After=network.target

      [Service]
      Type=simple
      User=librephotos
      Group=librephotos
      EnvironmentFile=/etc/librephotos.env
      ExecStart=/opt/librephotos/backend/venv/bin/python manage.py rqworker default
      Restart=always
      RestartSec=5
      StartLimitInterval=0
      WorkingDirectory=/opt/librephotos/backend

      [Install]
      WantedBy=multi-user.target
    dest: /etc/systemd/system/librephotos-worker.service
  notify: restart librephotos-worker

- name: librephotos-worker service
  service:
    enabled: yes
    name: librephotos-worker
    state: started
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment