#
# Cookbook:: irrigate-apache
# Recipe:: bulkupload
#
# Copyright:: The Orchard
#

include_recipe 'irrigate-php::bulkupload'

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

    deploy_home_dir = '/home/deploy'
    user 'deploy' do
      comment 'Deploy user'
      uid '1001'
      home deploy_home_dir
      shell '/bin/bash'
    end

    group 'webdev' do
      gid '1099'
      members %w(apache deploy)
      action :create
    end

    # SSH key used by deploy processes
    ssh_authorize_key 'deploy' do
      key 'AAAAB3NzaC1yc2EAAAABIwAAAQEAzQ/4Vj3OP6uAiX6cfV5WcBlDQp5jATXMIboUSkEl/FZEbrZFGEsGxhOS5LllHvmH23wVA5rRtmoWxZdcWikiy7GOXzDsWwomBdj5837E55ScV8uL+qXyrN0iI7T8fygqJnqoe87xD742lMWUVujYl7QnQRgwd+LKblzHrvchr8yrl+0rHY1A1oxS1iiQsp+CU/DY+t35YQPFlh3CP1xEuUipcGNkzCZDY/hAI9Prv5Fzvj6RZl+Y+VzgUvoJqKB+rrdv14ZEmvMfNcgM2Tlh33QSQd66Adu2ns22ADXUfsM421nO1IgqZ5dROOblesN8DsrMVCAXM9zL6xddLX47YQ=='
      user 'deploy'
    end

    # Grant sudo access. Specific users and privileges are defined in roles.
    include_recipe 'sudo'

    # This is a php.ini in the Remi-sandboxed location
    template '/opt/remi/php56/root/etc/php.ini' do
      source 'php.ini.erb'
      mode '0644'
      owner 'root'
      group 'root'
      notifies :restart, 'service[apache2]'
    end

    # PHP opcache configuration snippet in the Remi-sandboxed location
    template '/opt/remi/php56/root/etc/php.d/10-opcache.ini' do
      source '10-opcache.ini.erb'
      mode '0644'
      owner 'root'
      group 'root'
      notifies :restart, 'service[apache2]'
    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 Prod root directory
    directory '/var/www/bulkupload/public' do
      owner 'deploy'
      group 'webdev'
      mode '0755'
      recursive true
      action :create
    end

    # This is a ssh key for the dev-deploy user
    deploy_home_dir = '/home/deploy'
    github_ssh_key_location = "#{deploy_home_dir}/.ssh/id_rsa"
    github_ssh_key = data_bag_item('secrets', 'qa_github_ssh_key')
    private_key = github_ssh_key['private_key'].chomp
    file github_ssh_key_location do
      owner 'deploy'
      mode '0400'
      content private_key
    end

    # Relax ssh host key checks or initial git clone will fail
    key_config = "Host *\n\tStrictHostKeyChecking no\n"
    file "#{deploy_home_dir}/.ssh/config" do
      owner 'deploy'
      group 'deploy'
      mode '0600'
      content key_config
    end

    cookbook_file "#{deploy_home_dir}/.ssh/id_rsa.pub" do
      source 'github.pub'
      mode '0644'
    end

    git 'bulkupload' do
      repository 'git@github.com:/theorchard/bulkupload.git'
      reference 'master'
      destination '/var/www/bulkupload/public'
      user 'deploy'
      group 'webdev'
      action :checkout
    end

    # This by default references a template with a matching name
    apache_conf 'bulkupload' 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
