#
# Cookbook:: irrigate-apache
# Recipe:: corpsite
#
# Copyright:: The Orchard
#
# Description: This recipe manages apache for corpsite

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

    # Add MySQL community repo
    yum_repository 'mysql56-community' do
      description 'MySQL 5.6 Community Server'
      baseurl 'http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/'
      gpgkey 'https://repo.mysql.com/RPM-GPG-KEY-mysql'
      action :create
    end

    # A few extra packages
    packages = %w(
      mysql-community-client
      mysql-devel
      unzip
      zip
    )

    packages.each do |name|
      package name
    end

    deploy_user = node['php']['deploy_user']
    deploy_home_dir = node['php']['deploy_home_dir']
    # 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_user
      group deploy_user
      mode '0600'
      content key_config
    end

    # This is a deploy key in the theorchard.com github repo
    github_ssh_key_location = "#{deploy_home_dir}/.ssh/id_rsa"
    github_ssh_key = data_bag_item('secrets', 'prod_wpcorpsite_github_key')
    private_key = github_ssh_key['private_key'].chomp
    file github_ssh_key_location do
      owner deploy_user
      mode '0400'
      content private_key
    end

    wp_install_path = node['wp']['install_location']

    directory wp_install_path do
      owner deploy_user
      group deploy_user
      mode '0755'
      action :create
    end

    link 'wp-content' do
      target_file "#{wp_install_path}/wp-content"
      to '/mnt/efs/wp-content'
      action [:create]
      only_if "test -d #{wp_install_path}"
    end

    # Because this repo has the wp-content subdirectory, the wp root is correct
    git 'theorchard' do
      repository 'git@github.com:/theorchard/theorchard.com.git'
      reference 'master'
      destination wp_install_path
      user deploy_user
      group deploy_user
      action :checkout
    end

    execute 'wp-install' do
      command "/usr/local/bin/wp core download --path=#{wp_install_path} "\
              "&& touch #{deploy_home_dir}/.wp_install"
      creates "#{deploy_home_dir}/.wp_install"
      cwd '/var/www'
      user deploy_user
      group 'webdev'
    end

    execute 'wp-config' do
      command "/usr/local/bin/wp core config --path=#{wp_install_path} "\
              '--dbname=tempdb --dbuser=tempuser --dbpass=temppassword '\
              "--skip-check && touch #{deploy_home_dir}/.wp_config"
      creates "#{deploy_home_dir}/.wp_config"
      cwd '/var/www'
      user deploy_user
      group 'webdev'
    end

    # Read database credentials from encrypted data bags
    wp_db_user = data_bag_item('secrets', 'prod_corpsite_wp_db_user')
    db_user = wp_db_user['user'].chomp

    wp_db_password = data_bag_item('secrets', 'prod_corpsite_wp_db_password')
    db_password = wp_db_password['password'].chomp

    db_host = node['wp']['db']['host']
    db_name = node['wp']['db']['name']

    # wp-config file from template.
    template "#{wp_install_path}/wp-config.php" do
      source 'wp-config.php.erb'
      mode '0640'
      owner deploy_user
      group 'apache'
      variables ({
        db_name: db_name,
        db_user: db_user,
        db_password: db_password,
        db_host: db_host,
      })
      notifies :restart, 'service[apache2]'
    end

    directory "#{wp_install_path}/wp-content/uploads" do
      mode '0755'
      owner 'apache'
      group 'webdev'
    end

    # This by default references a template with a matching name
    apache_conf 'corpsite' 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['docroot_rewrites'])
          docroot_rewrites vhost_info['docroot_rewrites']
        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
