#
# Cookbook:: irrigate-selenium
# Recipe:: node
#
# Copyright:: (C) 2016 The Orchard
#

# Make sure the firewall is installed and enabled
firewall 'default' do
  log_level :high
  action :install
end

firewall_rule 'selenium_node_port' do
  port 5555
  protocol :tcp
  position 1
  command :allow
end

firewall_rule 'ssh' do
  port 22
  protocol :tcp
  position 2
  command :allow
end

yum_repository 'google-chrome' do
  description 'Google Chrome Repo'
  baseurl 'http://dl.google.com/linux/chrome/rpm/stable/$basearch'
  gpgkey 'https://dl-ssl.google.com/linux/linux_signing_key.pub'
  action :create
end

packages = %w(
  firefox
  gcc
  google-chrome-stable
  java-1.8.0-openjdk
  java-1.8.0-openjdk-devel
  unzip
)

packages.each do |name|
  package name
end

include_recipe 'xvfb'

include_recipe 'selenium::node'

remote_file 'download chromedriver' do
  path "#{node['selenium']['unix']['home']}/chromedriver-#{node['selenium']['node']['chromedriver_version']}.zip"
  source "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/#{node['selenium']['node']['chromedriver_version']}/linux64/chromedriver-linux64.zip"
  use_etag true
  use_conditional_get true
  notifies :run, 'execute[unzip]', :immediately
end

execute 'unzip' do
  command "unzip -j -o #{node['selenium']['unix']['home']}/chromedriver-#{node['selenium']['node']['chromedriver_version']}.zip -d #{node['selenium']['unix']['home']}/chromedriver && chmod -R 0755 #{node['selenium']['unix']['home']}/chromedriver"
  action :nothing
end

link '/usr/bin/chromedriver' do
  to "#{node['selenium']['unix']['home']}/chromedriver/chromedriver"
end

# Upgrade xdg-utils package to avoid xdg-open dialog pop-ups in Chrome
remote_file 'xdg-utils-rpm' do
  path "#{node['selenium']['unix']['home']}/xdg-utils.rpm"
  source node['selenium']['node']['xdg-utils']['source']
  action :create
end

rpm_package 'xdg-utils' do
  source "#{node['selenium']['unix']['home']}/xdg-utils.rpm"
  action :install
end

# Set Chrome as default application for web protocols
template "#{node['selenium']['unix']['home']}/xdg-mime-chrome.sh" do
  source 'xdg-mime-chrome.sh.erb'
  owner 'selenium'
  group 'selenium'
  mode '0755'
  action :create
end

# Only run the script if mimeapps.list does not exist
# It is necessary to explicitly set environment variables since Chef does not
# correctly set the home directory otherwise.
execute 'selenium-xdg-mime-update' do
  command "/bin/bash #{node['selenium']['unix']['home']}/xdg-mime-chrome.sh"
  environment(
    'HOME' => node['selenium']['node']['user_home_dir'],
    'USER' => node['selenium']['node']['user']
  )
  user node['selenium']['node']['user']
  creates "#{node['selenium']['node']['user_home_dir']}/.config/mimeapps.list"
end

# Script to kill unclosed processes for chromedriver and chrome
cookbook_file '/usr/local/bin/kill_chrome.py' do
  source 'kill_chrome.py'
  owner 'root'
  group 'root'
  mode '0744'
  action :create
end

# Cron job for script
cron 'kill_chrome' do
  minute '*/5'
  command '/usr/bin/python /usr/local/bin/kill_chrome.py'
  user 'root'
  action :create
  only_if { ::File.exist?('/usr/local/bin/kill_chrome.py') }
end
