Skip to content
Snippets Groups Projects
Commit 22890ea3 authored by Gabriel Mazetto's avatar Gabriel Mazetto
Browse files

Refactor postgresql::bin recipe extracting helper to omnibus_helper

parent 936e1508
2 merge requests!51Validate shift of Heptapod 0.25 to oldstable series,!41GitLab 13.12
......@@ -74,6 +74,21 @@
expected_user?(file, user) && expected_group?(file, group)
end
# Checks whether a specific resource exist in runtime
#
# @example usage
# omnibus_helper.is_resource_available?('runit_service[postgresql]')
#
# @param [String] name of the resource
# @return [Boolean]
def is_resource_available?(name)
node.run_context.resource_collection.find(name)
true
rescue Chef::Exceptions::ResourceNotFound
false
end
def is_deprecated_praefect_config?
return unless node['praefect']['storage_nodes'].is_a?(Array)
......
......@@ -21,20 +21,6 @@
include_recipe 'postgresql::directory_locations'
# This recipe will also be called standalone so the resource
# won't exist for resource collection.
# We only have ourselves to blame here, we want DRY code this is what we get.
# The block below is cleanest solution and
# was found at https://gist.github.com/scalp42/7606857#gistcomment-1618630
resource_exists = proc do |name|
begin
resources name
true
rescue Chef::Exceptions::ResourceNotFound
false
end
end
main_db_version = pg_helper.database_version if Services.enabled?('postgresql')
geo_db_version = geo_pg_helper.database_version if Services.enabled?('geo_postgresql')
db_version = node['postgresql']['version'] || main_db_version || geo_db_version
......@@ -44,6 +30,7 @@
block do
LoggingHelper.warning("We do not ship client binaries for PostgreSQL #{db_version}, defaulting to #{pg_helper.version.major}")
end
not_if { node['postgresql']['version'].nil? || db_path }
end
......@@ -54,6 +41,7 @@
To upgrade, please see: https://docs.gitlab.com/omnibus/settings/database.html#upgrade-packaged-postgresql-server
))
end
not_if { node['postgresql']['version'].nil? || node['postgresql']['version'].to_f >= 12 }
end
......@@ -68,9 +56,10 @@
FileUtils.ln_sf(pg_bin, "#{node['package']['install-dir']}/embedded/bin/#{File.basename(pg_bin)}")
end
end
only_if do
!File.exist?(File.join(node['postgresql']['data_dir'], "PG_VERSION")) || \
pg_helper.version.major !~ /^#{pg_helper.database_version}/ || \
(Services.enabled?('geo_postgresql') && geo_pg_helper.version.major !~ /^#{geo_pg_helper.database_version}/) || \
!node['postgresql']['version'].nil?
end
......@@ -71,8 +60,11 @@
only_if do
!File.exist?(File.join(node['postgresql']['data_dir'], "PG_VERSION")) || \
pg_helper.version.major !~ /^#{pg_helper.database_version}/ || \
(Services.enabled?('geo_postgresql') && geo_pg_helper.version.major !~ /^#{geo_pg_helper.database_version}/) || \
!node['postgresql']['version'].nil?
end
notifies :restart, 'runit_service[postgresql]', :immediately if omnibus_helper.should_notify?("postgresql") && resource_exists['runit_service[postgresql]']
# This recipe will also be called standalone so the resource won't exist in some circunstances
# This is why we check whether it is defined in runtime or not
notifies :restart, 'runit_service[postgresql]', :immediately if omnibus_helper.should_notify?("postgresql") && omnibus_helper.is_resource_available?('runit_service[postgresql]')
end
# frozen_string_literal: true
require 'chef_helper'
RSpec.describe OmnibusHelper do
cached(:chef_run) { ChefSpec::SoloRunner.converge('gitlab::default') }
subject(:omnibus_helper) { described_class.new(chef_run.node) }
describe '.resource_is_available?' do
it 'returns false for a resource that exists but has not been loaded in runtime' do
expect(omnibus_helper.is_resource_available?('runit_service[geo-logcursor]')).to be_falsey
end
it 'returns true for a resource that exists and is loaded in runtime' do
expect(omnibus_helper.is_resource_available?('runit_service[logrotated]'))
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment