#
# Cookbook:: irrigate-apache
# Recipe:: qa_webservice
#
# Copyright:: The Orchard
#
# Description: This recipe manages a sandboxed PHP 5.6 stack
# with all extensions and configuration to run a QA environment.
# Deployments are managed with git, sensitive info is served from
# encrypted data bags, and vhosts are programtically generated.
# This recipe follows Debian-style Apache snippet files and commands.

include_recipe 'irrigate-php::qa_webservice'

if platform_family?('rhel')
  if node['platform_version'].start_with?('7')

    # There are extra required packages that are provided by the base
    # and/or apache-specific cookbooks. This array is purpose-specific.
    packages = %w(
      autoconf
      bzip2-devel
      freetype-devel
      gcc-c++
      glibc
      gmp-devel
      httpd-devel
      ImageMagick
      ImageMagick-devel
      libcurl-devel
      libdb4-devel
      libevent
      libjpeg-turbo
      libjpeg-turbo-devel
      libmcrypt
      libmcrypt-devel
      libmemcached
      libpng-devel
      libtiff
      libtiff-devel
      libxml2-devel
      make
      memcached
      mhash
      mhash-devel
      mod_ssl
      openssl-devel
      zip
    )

    packages.each do |name|
      package name
    end

    service 'memcached' do
      supports restart: true, status: true, reload: false
      action [:enable, :start]
    end

    # PHP memcached configuration snippet in the Remi-sandboxed location
    template '/opt/remi/php56/root/etc/php.d/50-memcached.ini' do
      source '50-memcached.ini.erb'
      mode '0644'
      owner 'root'
      group 'root'
      notifies :restart, 'service[apache2]'
    end

    # The QA root directory

    directory '/var/www/html_qa' do
      owner node['php']['deploy_user']
      group 'webdev'
      mode '0755'
      action :create
    end

    git 'Webservice' do
      repository 'git@github.com:/theorchard/direct_delivery.git'
      reference 'master'
      checkout_branch 'master'
      enable_checkout false
      destination '/var/www/html_qa/webservice'
      user node['php']['deploy_user']
      group 'webdev'
      action :checkout
    end

    # Populate healthcheck
    file '/var/www/html_qa/webservice/public/healthchk.php' do
      content '<?php http_response_code(200);'
      owner node['php']['deploy_user']
      group 'webdev'
      mode '644'
      action :create
    end

    # This by default references a template with a matching name
    apache_conf 'webservice' do
      enable true
    end

    # Ensure the default vhost is disabled
    apache_site 'default' do
      enabled false
    end

    # Explicitly disable the default site if it is active, as a failsafe.
    execute 'a2dissite default' do
      only_if do
        File.symlink?('/etc/httpd/sites-enabled/default.conf')
      end
      notifies :restart, 'service[apache2]'
    end

    # Vhost attributes are contained in roles
    node['webapps'].each do |vhost, vhost_info|
      web_app vhost do
        template 'vhost.conf.erb'
        port vhost_info['port']
        server_admin vhost_info['server_admin']
        server_name vhost_info['server_name']
        if defined?(vhost_info['server_aliases'])
          server_aliases vhost_info['server_aliases']
        end
        docroot vhost_info['docroot']
        if defined?(vhost_info['directory_options'])
          directory_options vhost_info['directory_options']
        end
        allow_override vhost_info['allow_override']
        setenv setenv_params if defined?(vhost_info['setenv'])
        newrelic vhost_info['newrelic'] if defined?(vhost_info['newrelic'])
      end
    end
  end
end
