Commit 861c12f

mo khan <mo@mokhan.ca>
2015-05-02 04:00:01
finish off the nginx recipe.
1 parent 685ad1b
Changed files (2)
recipes/nginx.rb
@@ -1,7 +1,7 @@
-package 'nginx' do
-  action :install
-end
+package 'nginx'
+package 'logrotate'
 
+nginx = node['nginx']
 directory "/etc/nginx/ssl" do
   owner "root"
   group "root"
@@ -9,12 +9,12 @@ directory "/etc/nginx/ssl" do
   action :create
 end
 
-cookbook_file "/etc/nginx/ssl/#{node['nginx']['domain']}.crt" do
+cookbook_file "/etc/nginx/ssl/#{nginx['domain']}.crt" do
   source "#{node.chef_environment}.crt"
   mode "0644"
 end
 
-cookbook_file "/etc/nginx/ssl/#{node['nginx']['domain']}.key" do
+cookbook_file "/etc/nginx/ssl/#{nginx['domain']}.key" do
   source "#{node.chef_environment}.key"
   mode "0644"
 end
@@ -23,3 +23,39 @@ cookbook_file "/etc/nginx/conf.d/blacklist.conf" do
   source "blacklist.conf"
   mode "0644"
 end
+
+template "/etc/nginx/sites-available/#{nginx['domain']}" do
+  source "nginx_unicorn.erb"
+  mode "0644"
+  variables({
+    domain: nginx['domain'],
+    current_path: node['current_path'],
+    shared_path: node['shared_path'],
+    ssl_certificate: nginx['ssl_certificate'],
+    ssl_certificate_key: nginx['ssl_certificate_key'],
+    application: node['application']
+  })
+  notifies :restart, "service[nginx]"
+end
+
+link "/etc/nginx/sites-enabled/#{nginx['domain']}" do
+  to "/etc/nginx/sites-available/cakeside"
+end
+
+file "/etc/nginx/sites-enabled/default" do
+  action :delete
+end
+
+directory '/var/log/nginx' do
+  mode '0755'
+  action :create
+end
+
+template "/etc/logrotate.d/nginx" do
+  source "nginx_logrotate.erb"
+  mode "0644"
+end
+
+service "nginx" do
+  action [:enable, :start]
+end
spec/nginx_spec.rb
@@ -1,7 +1,8 @@
 describe 'mokhan-myface::nginx' do
-  let(:chef_run) do 
+  let(:domain) { "www.example.com" }
+  let(:chef_run) do
     ChefSpec::SoloRunner.new do |node|
-      node.set['nginx']['domain'] = 'www.example.com'
+      node.set['nginx']['domain'] = domain
     end.converge(described_recipe)
   end
 
@@ -17,12 +18,12 @@ describe 'mokhan-myface::nginx' do
   end
 
   it 'copies the ssl certificate' do
-    expect(chef_run).to create_cookbook_file("/etc/nginx/ssl/www.example.com.crt")
+    expect(chef_run).to create_cookbook_file("/etc/nginx/ssl/#{domain}.crt")
       .with_mode("0644")
   end
 
   it 'copies the ssl private key' do
-    expect(chef_run).to create_cookbook_file("/etc/nginx/ssl/www.example.com.key")
+    expect(chef_run).to create_cookbook_file("/etc/nginx/ssl/#{domain}.key")
       .with_mode("0644")
   end
 
@@ -30,4 +31,38 @@ describe 'mokhan-myface::nginx' do
     expect(chef_run).to create_cookbook_file("/etc/nginx/conf.d/blacklist.conf")
       .with_mode("0644")
   end
+
+  it 'adds the configuration for the website' do
+    expect(chef_run).to create_template("/etc/nginx/sites-available/#{domain}")
+      .with_mode("0644")
+  end
+
+  it 'restarts nginxj' do
+    resource = chef_run.template("/etc/nginx/sites-available/#{domain}")
+    expect(resource).to notify('service[nginx]').to(:restart).delayed
+  end
+
+  it 'starts nginx' do
+    expect(chef_run).to start_service('nginx')
+  end
+
+  it 'creates a symlink to the sites-enabled directory' do
+    expect(chef_run).to create_link("/etc/nginx/sites-enabled/#{domain}")
+  end
+
+  it 'deletes the default website' do
+    expect(chef_run).to delete_file("/etc/nginx/sites-enabled/default")
+  end
+
+  it 'installs logrotate' do
+    expect(chef_run).to install_package('logrotate')
+  end
+
+  it 'creates the log directory for nginx' do
+    expect(chef_run).to create_directory('/var/log/nginx').with_mode('0755')
+  end
+
+  it 'adds the logrotate config for rotating nginx logs' do
+    expect(chef_run).to create_template("/etc/logrotate.d/nginx")
+  end
 end