GitLab upgraded to 13.11.4-ee.0 - changelog: https://gitlab.com/gitlab-org/gitlab/blob/master/CHANGELOG-EE.md

Commit 791f4076 authored by Sander van Zoest's avatar Sander van Zoest
Browse files

Initial work on custom resources #414, need chef#4741 guidance

parent a1eaa3d9
......@@ -8,11 +8,9 @@ notifications:
on_failure: always # options: [always|never|change] default: always
on_start: false # default: false
rvm:
- 2.1.8
- 2.3.1
bundler_args: --without integration development
before_script:
- bundle exec berks
script:
- bundle exec foodcritic -f any . --tags ~FC007 --tags ~FC015 --tags ~FC023
- bundle exec rubocop
......
if defined?(ChefSpec)
ChefSpec.define_matcher :apache_conf
def create_apache_conf(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:apache_conf, :create, resource_name)
end
def delete_apache_conf(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:apache_conf, :delete, resource_name)
end
ChefSpec.define_matcher :apache_config
def create_apache_config(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:apache_config, :create, resource_name)
end
def delete_apache_config(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:apache_config, :delete, resource_name)
end
ChefSpec.define_matcher :apache_mod
def create_apache_mod(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:apache_mod, :create, resource_name)
end
def delete_apache_mod(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:apache_mod, :delete, resource_name)
end
ChefSpec.define_matcher :apache_module
def create_apache_module(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:apache_module, :create, resource_name)
end
def delete_apache_module(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:apache_module, :delete, resource_name)
end
ChefSpec.define_matcher :apache_site
def create_apache_site(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:apache_site, :create, resource_name)
end
def delete_apache_site(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:apache_site, :delete, resource_name)
end
ChefSpec.define_matcher :web_app
def create_web_app(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:web_app, :create, resource_name)
end
def delete_web_app(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:web_app, :delete, resource_name)
end
end
......@@ -19,3 +19,5 @@ supports 'freebsd'
supports 'suse'
supports 'opensuse'
supports 'arch'
depends 'compat_resource'
......@@ -17,19 +17,34 @@
# limitations under the License.
#
define :apache_conf, :enable => true do
include_recipe 'apache2::default'
resource_name :apache_conf
provides :apache_conf
conf_name = "#{params[:name]}.conf"
params[:conf_path] = params[:conf_path] || "#{node['apache']['dir']}/conf-available"
property :name, String, name_property: true
property :conf_path, String
property :enable, kind_of: [TrueClass, FalseClass], default: true
property :source, String
property :cookbook, String
file "#{params[:conf_path]}/#{params[:name]}" do
default_action :create
action :create do
conf_name = "#{new_resource.name}.conf"
new_resource.conf_path = new_resource.conf_path || "#{node['apache']['dir']}/conf-available"
# declare_resource(:service, 'apache2', run_context: Chef.run_context, create_if_missing: true) do
# action :nothing
# end
file "#{new_resource.conf_path}/#{new_resource.name}" do
action :delete
end
template "#{params[:conf_path]}/#{conf_name}" do
source params[:source] || "#{conf_name}.erb"
cookbook params[:cookbook] if params[:cookbook]
has_cookbook_property = property_is_set?(:cookbook)
template "#{new_resource.conf_path}/#{conf_name}" do
source new_resource.source || "#{conf_name}.erb"
cookbook new_resource.cookbook if has_cookbook_property
owner 'root'
group node['apache']['root_group']
backup false
......@@ -37,8 +52,8 @@ define :apache_conf, :enable => true do
notifies :restart, 'service[apache2]', :delayed
end
if params[:enable]
apache_config params[:name] do
if new_resource.enable
apache_config new_resource.name do
enable true
end
end
......
#
# Cookbook Name:: apache2
# Definition:: apache_config
# Resource:: apache_config
#
# Copyright 2008-2013, Chef Software, Inc.
# Copyright 2016, Alexander van Zoest
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -16,20 +16,27 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
resource_name :apache_config
provides :apache_config
define :apache_config, :enable => true do
include_recipe 'apache2::default'
property :conf_name, String, name_property: true
property :conf_path, String, default: "#{node['apache']['dir']}/conf-available"
property :enable, kind_of: [TrueClass, FalseClass], default: true
conf_name = "#{params[:name]}.conf"
params[:conf_path] = params[:conf_path] || "#{node['apache']['dir']}/conf-available"
default_action :create
if params[:enable]
action :create do
# declare_resource(:service, 'apache2', run_context: Chef.run_context, create_if_missing: true) do
# action :nothing
# end
if new_property.enable
execute "a2enconf #{conf_name}" do
command "/usr/sbin/a2enconf #{conf_name}"
notifies :restart, 'service[apache2]', :delayed
not_if do
::File.symlink?("#{node['apache']['dir']}/conf-enabled/#{conf_name}") &&
(::File.exist?(params[:conf_path]) ? ::File.symlink?("#{node['apache']['dir']}/conf-enabled/#{conf_name}") : true)
(::File.exist?(conf_path) ? ::File.symlink?("#{node['apache']['dir']}/conf-enabled/#{conf_name}") : true)
end
end
else
......@@ -40,3 +47,15 @@ define :apache_config, :enable => true do
end
end
end
action :delete do
# declare_resource(:service, 'apache2', run_context: Chef.run_context, create_if_missing: true) do
# action :nothing
# end
execute "a2disconf #{conf_name}" do
command "/usr/sbin/a2disconf #{conf_name}"
notifies :reload, 'service[apache2]', :delayed
only_if { ::File.symlink?("#{node['apache']['dir']}/conf-enabled/#{conf_name}") }
end
end
#
# Cookbook Name:: apache2
# Definition:: apache_mod
# Resource:: apache_mod
#
# Copyright 2008-20013, Chef Software, Inc.
# Copyright 2016, Alexander van Zoest
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -16,12 +16,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
resource_name :apache_mod
provides :apache_mod
define :apache_mod do
include_recipe 'apache2::default'
property :name, String, name_property: true
template "#{node['apache']['dir']}/mods-available/#{params[:name]}.conf" do
source "mods/#{params[:name]}.conf.erb"
default_action :create
action :create do
# declare_resource(:service, 'apache2', run_context: Chef.run_context, create_if_missing: true) do
# action :nothing
# end
template "#{node['apache']['dir']}/mods-available/#{new_resource.name}.conf" do
source "mods/#{new_resource.name}.conf.erb"
mode '0644'
notifies :reload, 'service[apache2]', :delayed
end
......
#
# Cookbook Name:: apache2
# Definition:: apache_module
# Resource:: apache_module
#
# Copyright 2008-2013, Chef Software, Inc.
# Copyright 2016, Alexander van Zoest
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -17,42 +17,56 @@
# limitations under the License.
#
define :apache_module, :enable => true, :conf => false, :restart => false do
include_recipe 'apache2::default'
resource_name :apache_module
provides :apache_module
params[:filename] = params[:filename] || "mod_#{params[:name]}.so"
params[:module_path] = params[:module_path] || "#{node['apache']['libexec_dir']}/#{params[:filename]}"
params[:identifier] = params[:identifier] || "#{params[:name]}_module"
property :module_name, String, name_property: true
property :enable, kind_of: [TrueClass, FalseClass], default: true
property :conf, kind_of: [TrueClass, FalseClass], default: false
property :restart, kind_of: [TrueClass, FalseClass], default: false
property :filename, String
property :module_path, String
property :identifier, String
apache_mod params[:name] if params[:conf]
default_action :create
file "#{node['apache']['dir']}/mods-available/#{params[:name]}.load" do
content "LoadModule #{params[:identifier]} #{params[:module_path]}\n"
mode '0644'
action :create do
new_resource.filename = "mod_#{module_name}.so" unless property_is_set?(:filename)
new_resource.module_path = "#{node['apache']['libexec_dir']}/#{new_resource.filename}" unless property_is_set?(:module_path)
new_resource.identifier = "#{new_resource.module_name}_module" unless property_is_set?(:identifier)
apache_mod new_resource.module_name if new_resource.conf
# declare_resource(:service, 'apache2', run_context: Chef.run_context, create_if_missing: true) do
# action :nothing
# end
file "#{node['apache']['dir']}/mods-available/#{new_resource.module_name}.load" do
content "LoadModule #{new_resource.identifier} #{new_resource.module_path}\n"
mode 0o644
end
if params[:enable]
execute "a2enmod #{params[:name]}" do
command "/usr/sbin/a2enmod #{params[:name]}"
if params[:restart]
if new_resource.enable
execute "a2enmod #{new_resource.module_name}" do
command "/usr/sbin/a2enmod #{new_resource.module_name}"
if new_resource.restart
notifies :restart, 'service[apache2]', :delayed
else
notifies :reload, 'service[apache2]', :delayed
end
not_if do
::File.symlink?("#{node['apache']['dir']}/mods-enabled/#{params[:name]}.load") &&
(::File.exist?("#{node['apache']['dir']}/mods-available/#{params[:name]}.conf") ? ::File.symlink?("#{node['apache']['dir']}/mods-enabled/#{params[:name]}.conf") : true)
::File.symlink?("#{node['apache']['dir']}/mods-enabled/#{new_resource.module_name}.load") &&
(::File.exist?("#{node['apache']['dir']}/mods-available/#{new_resource.module_name}.conf") ? ::File.symlink?("#{node['apache']['dir']}/mods-enabled/#{new_resource.module_name}.conf") : true)
end
end
else
execute "a2dismod #{params[:name]}" do
command "/usr/sbin/a2dismod #{params[:name]}"
if params[:restart]
execute "a2dismod #{new_resource.module_name}" do
command "/usr/sbin/a2dismod #{new_resource.module_name}"
if new_resource.restart
notifies :restart, 'service[apache2]', :delayed
else
notifies :reload, 'service[apache2]', :delayed
end
only_if { ::File.symlink?("#{node['apache']['dir']}/mods-enabled/#{params[:name]}.load") }
only_if { ::File.symlink?("#{node['apache']['dir']}/mods-enabled/#{new_resource.module_name}.load") }
end
end
end
......@@ -16,28 +16,39 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
resource_name :apache_site
provides :apache_site
define :apache_site, :enable => true do
include_recipe 'apache2::default'
conf_name = "#{params[:name]}.conf"
property :site_name, String, name_property: true
property :enable, kind_of: [TrueClass, FalseClass], default: true
property :conf_name, String
if params[:enable]
execute "a2ensite #{conf_name}" do
command "/usr/sbin/a2ensite #{conf_name}"
default_action :create
action :create do
new_resource.conf_name = "#{new_resource.site_name}.conf" unless property_is_set?(:conf_name)
# declare_resource(:service, 'apache2', run_context: Chef.run_context, create_if_missing: true) do
# action :nothing
# end
if new_resource.enable
execute "a2ensite #{new_resource.conf_name}" do
command "/usr/sbin/a2ensite #{new_resource.conf_name}"
notifies :reload, 'service[apache2]', :delayed
not_if do
::File.symlink?("#{node['apache']['dir']}/sites-enabled/#{conf_name}") ||
::File.symlink?("#{node['apache']['dir']}/sites-enabled/000-#{conf_name}")
::File.symlink?("#{node['apache']['dir']}/sites-enabled/#{new_resource.conf_name}") ||
::File.symlink?("#{node['apache']['dir']}/sites-enabled/000-#{new_resource.conf_name}")
end
only_if { ::File.exist?("#{node['apache']['dir']}/sites-available/#{conf_name}") }
only_if { ::File.exist?("#{node['apache']['dir']}/sites-available/#{new_resource.conf_name}") }
end
else
execute "a2dissite #{conf_name}" do
command "/usr/sbin/a2dissite #{conf_name}"
execute "a2dissite #{new_resource.conf_name}" do
command "/usr/sbin/a2dissite #{new_resource.conf_name}"
notifies :reload, 'service[apache2]', :delayed
only_if do
::File.symlink?("#{node['apache']['dir']}/sites-enabled/#{conf_name}") ||
::File.symlink?("#{node['apache']['dir']}/sites-enabled/000-#{conf_name}")
::File.symlink?("#{node['apache']['dir']}/sites-enabled/#{new_resource.conf_name}") ||
::File.symlink?("#{node['apache']['dir']}/sites-enabled/000-#{new_resource.conf_name}")
end
end
end
......
#
# Cookbook Name:: apache2
# Definition:: web_app
# Resource:: web_app
#
# Copyright 2008-2013, Chef Software, Inc.
# Copyright 2016, Alexander van Zoest
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -17,21 +17,31 @@
# limitations under the License.
#
define :web_app, :template => 'web_app.conf.erb', :local => false, :enable => true, :server_port => 80 do
application_name = params[:name]
resource_name :web_app
provides :web_app
include_recipe 'apache2::default'
include_recipe 'apache2::mod_rewrite'
include_recipe 'apache2::mod_deflate'
include_recipe 'apache2::mod_headers'
property :application_name, String, name_property: true
property :template, String, default: 'web_app.conf.erb'
property :local, kind_of: [TrueClass, FalseClass], default: false
property :enable, kind_of: [TrueClass, FalseClass], default: true
property :server_port, Integer, default: 80
property :cookbook, String
property :params, kind_of: Hash, default: {}
default_action :create
action :create do
# declare_resource(:service, 'apache2', run_context: Chef.run_context, create_if_missing: true) do
# action :nothing
# end
template "#{node['apache']['dir']}/sites-available/#{application_name}.conf" do
source params[:template]
local params[:local]
source new_resource.template
local new_resource.local
owner 'root'
group node['apache']['root_group']
mode '0644'
cookbook params[:cookbook] if params[:cookbook]
mode 0o644
cookbook new_resource.cookbook if property_is_set?(:cookbook)
variables(
:application_name => application_name,
:params => params
......@@ -41,8 +51,7 @@ define :web_app, :template => 'web_app.conf.erb', :local => false, :enable => tr
end
end
site_enabled = params[:enable]
apache_site params[:name] do
enable site_enabled
apache_site new_resource.application_name do
enable new_resource.enable
end
end
......@@ -19,7 +19,7 @@ describe 'apache2::default' do
context 'with valid apache configuration' do
before(:context) do
@chef_run = ChefSpec::SoloRunner.new(:platform => platform, :version => version)
@chef_run = ChefSpec::SoloRunner.new(step_into: ['apache_conf'], :platform => platform, :version => version)
stub_command("#{property[:apache][:binary]} -t").and_return(true)
@chef_run.converge(described_recipe)
end
......@@ -134,6 +134,9 @@ describe 'apache2::default' do
end
%w(security charset).each do |config|
it "creates apache conf #{config}" do
expect(chef_run).to create_apache_conf(config)
end
it "deletes #{property[:apache][:dir]}/conf-available/#{config}" do
expect(chef_run).to delete_file("#{property[:apache][:dir]}/conf-available/#{config}")
end
......
......@@ -9,7 +9,7 @@ aaa_modules_without_config.each do |mod|
property = load_platform_properties(:platform => platform, :platform_version => version)
before(:context) do
@chef_run = ChefSpec::SoloRunner.new(:platform => platform, :version => version)
@chef_run = ChefSpec::SoloRunner.new(step_into: [:apache_mod, :apache_module], :platform => platform, :version => version)
stub_command("#{property[:apache][:binary]} -t").and_return(true)
@chef_run.converge(described_recipe)
end
......
......@@ -3,10 +3,6 @@ RSpec.shared_examples 'an apache2 module' do |a2module, a2conf, a2filename = nil
allow(::File).to receive(:symlink?).and_return(true)
end
it 'includes the `apache2::default` recipe' do
expect(chef_run).to include_recipe('apache2::default')
end
module_name = a2module
module_enable = true
module_conf = a2conf
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment