diff --git a/manifests/admin.pp b/manifests/admin.pp new file mode 100644 index 00000000..8c264723 --- /dev/null +++ b/manifests/admin.pp @@ -0,0 +1,38 @@ +# @summary Run a django-admin command +# +# @param command +# The command to run +# +# @param refreshonly +# The command should only be run as a refresh mechanism for when a dependent +# object is changed. +# +# @param unless +# A test command that checks the state of the target system and restricts +# when the exec can run. +# +# @param path +# The path to look for commands. +# +# @param pulp_settings +# The pulp settings file to use +# +# @see exec +define pulpcore::admin( + String $command = $title, + Boolean $refreshonly = false, + Optional[String] $unless = undef, + Array[Stdlib::Absolutepath] $path = ['/usr/bin'], + Stdlib::Absolutepath $pulp_settings = $pulpcore::settings_file, +) { + File <| title == $pulp_settings |> + -> exec { "django-admin ${title}": + path => $path, + environment => [ + 'DJANGO_SETTINGS_MODULE=pulpcore.app.settings', + "PULP_SETTINGS=${pulp_settings}", + ], + refreshonly => $refreshonly, + unless => $unless, + } +} diff --git a/manifests/config.pp b/manifests/config.pp index 19201caf..459d9d64 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -20,12 +20,7 @@ mode => '0755', } - exec { 'django-admin collectstatic --noinput': - path => ['/usr/local/bin', '/usr/bin'], - environment => [ - 'DJANGO_SETTINGS_MODULE=pulpcore.app.settings', - "PULP_SETTINGS=${pulpcore::settings_file}", - ], + pulpcore::admin { 'collectstatic --noinput': refreshonly => true, subscribe => File[$pulpcore::settings_file], } diff --git a/manifests/database.pp b/manifests/database.pp index c754b11b..9fe7edfd 100644 --- a/manifests/database.pp +++ b/manifests/database.pp @@ -8,14 +8,10 @@ password => postgresql_password($pulpcore::user, $pulpcore::postgresql_db_password), } - exec { 'django-admin migrate --noinput': - path => ['/usr/local/bin', '/usr/bin'], - environment => [ - 'DJANGO_SETTINGS_MODULE=pulpcore.app.settings', - "PULP_SETTINGS=${pulpcore::settings_file}", - ], - require => Postgresql::Server::Db[$pulpcore::postgresql_db_name], + pulpcore::admin { 'migrate --noinput': unless => 'django-admin migrate --plan | grep "No planned migration operations"', + refreshonly => false, + require => Postgresql::Server::Db[$pulpcore::postgresql_db_name], } include redis diff --git a/spec/defines/admin_spec.rb b/spec/defines/admin_spec.rb new file mode 100644 index 00000000..ed6d0912 --- /dev/null +++ b/spec/defines/admin_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +describe 'pulpcore::admin' do + on_supported_os.each do |os, os_facts| + context "on #{os}" do + let(:facts) { os_facts } + let(:title) { 'help' } + + context 'with a fixed pulp_settings' do + let(:params) { { pulp_settings: '/etc/pulpcore/settings.py' } } + + context 'default parameters' do + it do + is_expected.to compile.with_all_deps + is_expected.to contain_exec('django-admin help') + .with_environment(['DJANGO_SETTINGS_MODULE=pulpcore.app.settings', 'PULP_SETTINGS=/etc/pulpcore/settings.py']) + .with_refreshonly(false) + .with_unless(nil) + end + end + + context 'default parameters' do + let(:params) do + super().merge( + command: 'migrate --noinput', + refreshonly: true, + unless: '/usr/bin/false', + ) + end + + it do + is_expected.to compile.with_all_deps + is_expected.to contain_exec('django-admin help') + .with_environment(['DJANGO_SETTINGS_MODULE=pulpcore.app.settings', 'PULP_SETTINGS=/etc/pulpcore/settings.py']) + .with_refreshonly(true) + .with_unless('/usr/bin/false') + end + end + end + + context 'with inheritance' do + let(:pre_condition) { 'include pulpcore' } + + context 'default parameters' do + it do + is_expected.to compile.with_all_deps + is_expected.to contain_pulpcore__admin('help').with_pulp_settings('/etc/pulp/settings.py') + is_expected.to contain_file('/etc/pulp/settings.py') + is_expected.to contain_exec('django-admin help') + .with_environment(['DJANGO_SETTINGS_MODULE=pulpcore.app.settings', 'PULP_SETTINGS=/etc/pulp/settings.py']) + .with_refreshonly(false) + .with_unless(nil) + .that_requires('File[/etc/pulp/settings.py]') + end + end + end + end + end +end