include <tunables/global>

# Docker overlay
@{docker_root}=/docker/ /var/lib/docker/
@{fs_root}=/ @{docker_root}/overlay2/*/diff/
@{do_etc}=@{fs_root}/etc/
@{do_opt}=@{fs_root}/opt/
@{do_run}=@{fs_root}/{run,var/run}/
@{do_usr}=@{fs_root}/usr/
@{do_var}=@{fs_root}/var/

# Systemd Journal location
@{journald}=/{run,var}/log/journal/{,**}

profile hassio_promtail flags=(attach_disconnected,mediate_deleted) {
  include <abstractions/base>
  include <abstractions/bash>

  # Send signals to child services
  signal (send) peer=@{profile_name}//*,

  # Network access
  network tcp,
  network udp,

  # S6-Overlay
  /init                                   rix,
  /bin/**                                 rix,
  @{do_usr}/bin/**                        rix,
  @{do_usr}/sbin/**                       rix,
  @{do_usr}/share/{,**}                   r,
  @{do_usr}/lib/locale/{,**}              r,
  @{do_etc}/*                             rw,
  @{do_etc}/s6*/**                        r,
  @{do_etc}/fix-attrs.d/{,**}             r,
  @{do_etc}/cont-{init,finish}.d/{,**}    rwix,
  @{do_etc}/services.d/{,**}              rwix,
  @{do_etc}/ssl/openssl.cnf               r,
  @{do_etc}/{group,hosts,passwd}          r,
  @{do_etc}/{host,nsswitch,resolv}.conf   r,
  @{do_run}/{s6,s6-rc*,service}/**        rix,
  @{do_run}/{,**}                         rwk,
  /var/cache/{,**}                        rw,
  /dev/tty                                rw,
  /dev/null                               k,
  /command/**                             rix,
  /package/**                             rix,

 
  # Bashio
  @{do_usr}/lib/bashio/**                 ix,
  /tmp/**                                 rw,

  # Options.json & addon data
  /data                                   r,
  /data/**                                rw,

  # Files needed for setup
  @{do_etc}/promtail/{,**}                rw,
  /config/promtail/{,**}                  r,
  /{share,ssl}/{,**}                      r,
  @{journald}                             r,

  # Programs
  /usr/bin/promtail                       cx -> promtail_profile,
  /usr/bin/yq                             Cx -> yq_profile,
  /usr/sbin/dpkg-reconfigure              Cx -> dpkg_reconfigure_profile,

  profile promtail_profile flags=(attach_disconnected,mediate_deleted) {
    include <abstractions/base>

    # Receive signals from s6
    signal (receive) peer=*_promtail,

    # Network access
    network tcp,
    network udp,
    network netlink raw,
    network unix dgram,

    # Temp files
    /tmp/.positions.yaml*                 rw,

    # Addon data
    /data/**                              r,
    /data/promtail/**                     rwk,

    # Config & log data
    @{do_etc}/promtail/config.yaml        r,
    /config/promtail/{,**}                r,
    /{share,ssl}/**                       r,
    @{journald}                           r,

    # Runtime usage
    /usr/bin/promtail                     rm,
    @{do_etc}/{hosts,passwd}              r,
    @{do_etc}/{resolv,nsswitch}.conf      r,
    @{PROC}/sys/net/core/somaxconn        r,
    @{sys}/kernel/mm/transparent_hugepage/hpage_pmd_size r,
    /dev/null                             k,
    @{do_etc}/ssl/certs/**                r,
  }

  profile yq_profile flags=(attach_disconnected,mediate_deleted) {
    include <abstractions/base>

    # Config files
    @{do_etc}/promtail/*                  rw,
    /config/promtail/{,**}                r,
    /share/**                             r,

    # Runtime usage
    /usr/bin/yq                           rm,
    @{sys}/kernel/mm/transparent_hugepage/hpage_pmd_size r,
    /dev/null                             k,
  }

  profile dpkg_reconfigure_profile flags=(attach_disconnected,mediate_deleted) {
    include <abstractions/base>

    /**                                   rwlkmix,
  }
}