#
# Cookbook Name:: rubygems-app
# Recipe:: nginx
#

node.default['nginx']['server_tokens'] = 'off'
node.default['nginx']['default_site_enabled'] = false
node.default['nginx']['repo_source'] = 'nginx'

include_recipe 'chef_nginx'

query = "roles:balancer AND chef_environment:#{node.chef_environment}"
lb_ip = search(:node, query)[0]['ipaddress']

template "#{node['nginx']['dir']}/sites-available/rubygems" do
  source 'nginx.conf.erb'
  owner  'root'
  group  'root'
  mode   '0644'
  variables(
    rails_env:  node.chef_environment,
    rails_root: '/applications/rubygems',
    load_balancer_ip: lb_ip,
    unicorn_port: 3000,
    nginx_port: 9000
  )
  notifies :reload, 'service[nginx]'
end

nginx_site 'rubygems'

file '/etc/nginx/conf.d/default.conf' do
  action :delete
  notifies :reload, 'service[nginx]'
end

logrotate_app 'nginx' do
  path "#{node['nginx']['log_dir']}/*.log"
  daily
  size '1G'
  rotate 15
  options %w(missingok compress delaycompress notifempty sharedscripts)
  postrotate "    [ -f #{node['nginx']['pid']} ] && kill -USR1 `cat #{node['nginx']['pid']}`"
  create '0640 www-data adm'
end

node.default['datadog']['nginx']['instances'] = [{ 'nginx_status_url' => 'http://localhost/nginx_status/' }]
include_recipe 'datadog::nginx'
