# Configures Phabricator.
#
# @summary Configures Arcanist, libphutil and Phabricator.
# @private
#
class phabricator::config {
  assert_private()

  $notify = Class['php::fpm::service']

# NOTE: Group 'phabricator' managed in basics/init.pp
  user {
    default:
      ensure     => 'present',
      managehome => false,
      system     => true;

    $phabricator::daemon_user:
      gid        => $phabricator::group,
      comment => 'Phabricator Daemons',
      home    => "${phabricator::install_dir}/phabricator/support/empty",
      shell   => '/usr/sbin/nologin';

    $phabricator::vcs_user:
      gid        => $phabricator::group,
      comment => 'Phabricator VCS',
      home    => $phabricator::repo_dir,
      shell   => '/bin/sh';
  }
  
  file {
      default:
          group => $phabricator::group;

      $phabricator::logs_dir:
          owner  => $phabricator::www_user,
          ensure => 'directory',
          mode   => '0775';

      $phabricator::repo_dir:
          ensure => 'directory',
          owner  => $phabricator::www_user,
          mode   => '0750';
      
      $phabricator::install_dir:  
          ensure => 'directory',
          owner =>  $phabricator::admin_user;
          
      $phabricator::storage_dir:
          ensure => 'directory',
          owner =>  $phabricator::www_user;
          
      $phabricator::dumps_dir:  
          ensure => 'directory',
          owner =>  $phabricator::admin_user;
      
      "/home/${phabricator::admin_user}/.ssh/bitbucket_id_rsa":
          owner => 'xreate',
          mode => 'u=rw,g=,o=',
          source => "${phabricator::secrets_dir}bitbucket_id_rsa",
          ensure => file;
          
      'phabricator/conf/local.json':
          ensure  => 'file',
          path    => "${phabricator::install_dir}/phabricator/conf/local/local.json",
          content => inline_template("<%= scope['phabricator::config'].to_json %>"),
          mode    => '0640',
          notify  => $notify,
          owner => 'root',
          group => $phabricator::group,
          require => Vcsrepo['phabricator'];
       
      #Xreate Docs    
      "${phabricator::install_dir}phabricator/webroot/rsrc/css/xreate-docs":
          ensure => link,
          target  => "${phabricator::install_dir}/xreate-docs/css",
          owner => $phabricator::daemon_user,
          group => $phabricator::group,
          require => Vcsrepo['phabricator'];
      
      ["${phabricator::install_dir}xreate-docs/conf", $phabricator::xreate_docs_path]:
        ensure => 'directory',
        owner=>$phabricator::admin_user,
        require=>Vcsrepo['xreate-docs'];
        
      "${phabricator::install_dir}xreate-docs/conf/local.json":
        ensure  => 'file',
        content => inline_template("<%= scope['phabricator::xreate_docs_config'].to_json %>"),
        require => [Vcsrepo['xreate-docs']];
  }
  
#Configure Bitbucket:
  sshkey {
  'bitbucket.org':
    type => 'ssh-rsa',
    key => 'AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw=='
  }
  
  class { 'ssh::client':
    options => {
      "Host *" => {
        'HashKnownHosts' => false,
      }
    }
  }
  
  ::ssh::client::config::user { 
    $phabricator::admin_user:
      ensure => present,
        options => {
          'Host bitbucket.org' => {
            'Hostname' => 'bitbucket.org',
            'User' => 'git',
            'IdentityFile' => '~/.ssh/bitbucket_id_rsa'
        }
      }
  }
  
  
  logrotate::rule { 'phabricator':
      ensure        => 'present',
      path          => [$phabricator::log_access_path, "${phabricator::logs_dir}ssh.log"],
      compress      => true,
      delaycompress => true,
      ifempty       => false,
      missingok     => true,
      rotate_every  => 'week',
      rotate        => 40
  }

  # TODO: We should be able to tighten these permissions as follows:
  #
  # - `/usr/bin/git`, `/usr/bin/git-receive-pack`, `/usr/bin/git-upload-pack`
  #   and `/usr/lib/git-core/git-http-backend` should only be required if the
  #   node is //hosting// Diffusion repositories.
  # - `/usr/bin/ssh` should only be required if the node is //serving// (either
  #   directly or by proxy) Diffusion repositories.
  #
  if $phabricator::manage_diffusion {
    # lint:ignore:strict_indent
    sudo::conf { "${phabricator::vcs_user}:${phabricator::daemon_user}":
      ensure  => 'present',
      content => sprintf(
        '%s ALL=(%s) SETENV: NOPASSWD: %s',
        $phabricator::vcs_user,
        "${phabricator::daemon_user}:${phabricator::group}",
        join([
          '/usr/bin/git',
          '/usr/bin/git-receive-pack',
          '/usr/bin/git-upload-pack',
          '/usr/bin/ssh',
        ], ', '),
      ),
    }
    # lint:endignore

    #PHP
    include php::params

    # lint:ignore:strict_indent
      sudo::conf { "${php::params::fpm_user}:${phabricator::daemon_user}":
        ensure  => 'present',
        content => sprintf(
          '%s ALL=(%s) SETENV: NOPASSWD: %s',
          $php::params::fpm_user,
          "${phabricator::daemon_user}:${phabricator::group}",
          join([
            '/usr/bin/git',
            '/usr/bin/ssh',
            '/usr/lib/git-core/git-http-backend',
          ], ', '),
        ),
      }
    # lint:endignore

    # lint:ignore:strict_indent
    ssh::server::config::setting { $phabricator::vcs_user:
      key   => "Match User ${phabricator::vcs_user}",

      # TODO: This seems quite hacky.
      value => join([
        '',
        "AuthorizedKeysCommand ${phabricator::install_dir}/phabricator/bin/ssh-auth",
        "AuthorizedKeysCommandUser ${phabricator::vcs_user}",
      ], "\n  "),
    }
    # lint:endignore

  }
}
