---
{%- from "netbox/map.jinja" import netbox with context %}
netbox-virtualenv-pkg:
  pip.installed:
    - name: virtualenv
    - bin_env: /usr/bin/pip3

netbox-archive-extract:
  archive.extracted:
    - name: {{ netbox.release_dir }}
    - source: {{ netbox.archive_url }}/v{{ netbox.version }}.tar.gz
    - skip_verify: true

netbox-install-link:
  file.symlink:
    - name: {{ netbox.install_dir }}/netbox
    - target: {{ netbox.release_dir }}/netbox-{{ netbox.version }}
    - watch_in:
      - service: netbox-service
    - require:
      - archive: netbox-archive-extract

netbox-virtualenv:
  virtualenv.managed:
    - name: {{ netbox.install_dir }}/netbox
    - requirements: {{ netbox.install_dir }}/netbox/requirements.txt
    - python: /usr/bin/python3
    - require:
      - pip: netbox-virtualenv-pkg

netbox-gunicorn-pkg:
  pip.installed:
    - name: gunicorn
    - bin_env: {{ netbox.install_dir }}/netbox/bin/pip3
    - require:
      - virtualenv: netbox-virtualenv

netbox-gunicorn-execfile:
  file.managed:
    - name: {{ netbox.install_dir }}/netbox/gunicorn.py
    - source: salt://netbox/templates/gunicorn.py.j2
    - user: root
    - group: root
    - mode: 0644
    - template: jinja

netbox-config-file:
  file.managed:
    - name: {{ netbox.install_dir }}/netbox/netbox/netbox/configuration.py
    - source: salt://netbox/templates/configuration.py.j2
    - user: root
    - group: root
    - mode: 0644
    - template: jinja

netbox-migration:
  module.run:
    - name: django.migrate
    - settings_module: netbox.settings
    - pythonpath: {{ netbox.install_dir }}/netbox/netbox
    - bin_env: {{ netbox.install_dir }}/netbox
    - require:
      - virtualenv: netbox-virtualenv

netbox-install-static-files:
  module.run:
    - name: django.collectstatic
    - settings_module: netbox.settings
    - pythonpath: {{ netbox.install_dir }}/netbox/netbox
    - bin_env: {{ netbox.install_dir }}/netbox
    - require:
      - virtualenv: netbox-virtualenv