## puppet_cd::r10k::webhook.pp
# Module name: puppet_cd
# Author: Arne Teuke (arne_teuke@confdroid)
# @summary  Class manages r10k webhook settings for the puppet_cd module.
###############################################################################
class puppet_cd::r10k::webhook (

) inherits puppet_cd::params {
  if ($pt_pm_fqdn == $fqdn) and ($pt_use_r10k_webhook == true) {
    # create the webhook binary
    file { $pt_r10k_hook_file:
      ensure   => file,
      owner    => 'root',
      group    => 'root',
      mode     => '0755',
      selrange => s0,
      selrole  => object_r,
      seltype  => bin_t,
      seluser  => unconfined_u,
      source   => 'puppet:///modules/puppet_cd/webhook',
    }

    # create the webhook config dir
    file { $pt_r10k_webhook_dir:
      ensure   => directory,
      owner    => 'root',
      group    => 'root',
      mode     => '0755',
      selrange => s0,
      selrole  => object_r,
      seltype  => etc_t,
      seluser  => system_u,
    }

    # create webhook config
    file { $pt_r10k_hook_config_file:
      owner    => 'root',
      group    => 'root',
      mode     => '0644',
      selrange => s0,
      selrole  => object_r,
      seltype  => etc_t,
      seluser  => system_u,
      content  => template($pt_r10k_hook_config_erb),
      notify   => Service['webhook'],
    }

    # create service config
    file { $pt_r10k_hook_service_file:
      owner    => 'root',
      group    => 'root',
      mode     => '0644',
      selrange => s0,
      selrole  => object_r,
      seltype  => systemd_unit_file_t,
      seluser  => system_u,
      content  => template($pt_r10k_hook_service_erb),
      notify   => Service['webhook'],
    }

    # manage service
    service { 'webhook':
      ensure     => 'running',
      hasstatus  => true,
      hasrestart => true,
      enable     => true,
    }
  }
}
