From fbeae282e8a0c58cd48f377e97ee8cb61472cff3 Mon Sep 17 00:00:00 2001
From: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>
Date: Tue, 14 Jul 2020 18:44:13 +0200
Subject: [PATCH 1/2] Use pulpcore-manager

This requires at least Pulp 3.2.0, but since
f59265ebb9fbe9cceddad37400849f8935538d1e that's been the required
minimum version.
---
 manifests/admin.pp            | 14 +++-----------
 manifests/database.pp         |  4 ++--
 spec/acceptance/basic_spec.rb |  2 +-
 spec/classes/pulpcore_spec.rb | 10 ++--------
 spec/defines/admin_spec.rb    | 28 +++++++---------------------
 5 files changed, 15 insertions(+), 43 deletions(-)

diff --git a/manifests/admin.pp b/manifests/admin.pp
index c1e2e070..8b8c5403 100644
--- a/manifests/admin.pp
+++ b/manifests/admin.pp
@@ -1,4 +1,4 @@
-# @summary Run a python3-django-admin command
+# @summary Run a pulpcore-manager command
 #
 # @param command
 #   The command to run
@@ -15,9 +15,6 @@
 #   The path to look for commands.
 #
 # @param pulp_settings
-#   The pulp settings file to use
-#
-# @param static_root
 #   Root directory for static content
 #
 # @see exec
@@ -27,16 +24,11 @@
   Optional[String] $unless = undef,
   Array[Stdlib::Absolutepath] $path = ['/usr/bin'],
   Stdlib::Absolutepath $pulp_settings = $pulpcore::settings_file,
-  Stdlib::Absolutepath $static_root = $pulpcore::pulp_static_root,
 ) {
   Concat <| title == 'pulpcore settings' |>
-  -> exec { "python3-django-admin ${command}":
+  -> exec { "pulpcore-manager ${command}":
     path        => $path,
-    environment => [
-      'DJANGO_SETTINGS_MODULE=pulpcore.app.settings',
-      "PULP_SETTINGS=${pulp_settings}",
-      "PULP_STATIC_ROOT=${static_root}",
-    ],
+    environment => ["PULP_SETTINGS=${pulp_settings}"],
     refreshonly => $refreshonly,
     unless      => $unless,
   }
diff --git a/manifests/database.pp b/manifests/database.pp
index 9492b1b9..5e1afe4a 100644
--- a/manifests/database.pp
+++ b/manifests/database.pp
@@ -12,12 +12,12 @@
   }
 
   pulpcore::admin { 'migrate --noinput':
-    unless      => 'python3-django-admin migrate --plan | grep "No planned migration operations"',
+    unless      => 'pulpcore-manager migrate --plan | grep "No planned migration operations"',
     refreshonly => false,
   }
 
   pulpcore::admin { 'reset-admin-password --random':
-    unless      => 'python3-django-admin dumpdata auth.User | grep "auth.user"',
+    unless      => 'pulpcore-manager dumpdata auth.User | grep "auth.user"',
     refreshonly => false,
     require     => Pulpcore::Admin['migrate --noinput'],
   }
diff --git a/spec/acceptance/basic_spec.rb b/spec/acceptance/basic_spec.rb
index b6919b65..244a185b 100644
--- a/spec/acceptance/basic_spec.rb
+++ b/spec/acceptance/basic_spec.rb
@@ -66,7 +66,7 @@ class { 'pulpcore':
     its(:exit_status) { is_expected.to eq 0 }
   end
 
-  describe command("DJANGO_SETTINGS_MODULE=pulpcore.app.settings PULP_SETTINGS=/etc/pulp/settings.py python3-django-admin dumpdata auth.User") do
+  describe command("PULP_SETTINGS=/etc/pulp/settings.py pulpcore-manager dumpdata auth.User") do
     its(:stdout) { is_expected.to match(/auth\.user/) }
     its(:exit_status) { is_expected.to eq 0 }
   end
diff --git a/spec/classes/pulpcore_spec.rb b/spec/classes/pulpcore_spec.rb
index e735b5e1..4010ae00 100644
--- a/spec/classes/pulpcore_spec.rb
+++ b/spec/classes/pulpcore_spec.rb
@@ -35,9 +35,9 @@
           is_expected.to contain_class('postgresql::server')
           is_expected.to contain_postgresql__server__db('pulpcore')
           is_expected.to contain_pulpcore__admin('migrate --noinput')
-          is_expected.to contain_exec('python3-django-admin migrate --noinput')
+          is_expected.to contain_exec('pulpcore-manager migrate --noinput')
           is_expected.to contain_pulpcore__admin('reset-admin-password --random')
-          is_expected.to contain_exec('python3-django-admin reset-admin-password --random')
+          is_expected.to contain_exec('pulpcore-manager reset-admin-password --random')
         end
 
         it 'configures apache' do
@@ -249,12 +249,6 @@
         it do
           is_expected.to compile.with_all_deps
           is_expected.to contain_file('/my/custom/directory')
-          is_expected.to contain_exec('python3-django-admin collectstatic --noinput')
-            .with_environment([
-              "DJANGO_SETTINGS_MODULE=pulpcore.app.settings",
-              "PULP_SETTINGS=/etc/pulp/settings.py",
-              "PULP_STATIC_ROOT=/my/other/custom/directory"
-            ])
           is_expected.to contain_systemd__unit_file('pulpcore-api.service')
             .with_content(%r{Environment="PULP_STATIC_ROOT=/my/other/custom/directory"})
           is_expected.to contain_apache__vhost('pulpcore')
diff --git a/spec/defines/admin_spec.rb b/spec/defines/admin_spec.rb
index 4ad6157a..e880a0e4 100644
--- a/spec/defines/admin_spec.rb
+++ b/spec/defines/admin_spec.rb
@@ -6,23 +6,18 @@
       let(:facts) { os_facts }
       let(:title) { 'help' }
 
-      context 'with a fixed pulp_settings and static_root' do
+      context 'with a fixed pulp_settings' do
         let(:params) do
           {
             pulp_settings: '/etc/pulpcore/settings.py',
-            static_root: '/var/lib/pulp/assets',
           }
         end
 
         context 'default parameters' do
           it do
             is_expected.to compile.with_all_deps
-            is_expected.to contain_exec('python3-django-admin help')
-              .with_environment([
-                'DJANGO_SETTINGS_MODULE=pulpcore.app.settings',
-                'PULP_SETTINGS=/etc/pulpcore/settings.py',
-                'PULP_STATIC_ROOT=/var/lib/pulp/assets',
-              ])
+            is_expected.to contain_exec('pulpcore-manager help')
+              .with_environment(['PULP_SETTINGS=/etc/pulpcore/settings.py'])
               .with_refreshonly(false)
               .with_unless(nil)
           end
@@ -34,18 +29,13 @@
               command: 'migrate --noinput',
               refreshonly: true,
               unless: '/usr/bin/false',
-              static_root: '/var/lib/pulp/assets',
             )
           end
 
           it do
             is_expected.to compile.with_all_deps
-            is_expected.to contain_exec('python3-django-admin migrate --noinput')
-              .with_environment([
-                'DJANGO_SETTINGS_MODULE=pulpcore.app.settings',
-                'PULP_SETTINGS=/etc/pulpcore/settings.py',
-                'PULP_STATIC_ROOT=/var/lib/pulp/assets',
-              ])
+            is_expected.to contain_exec('pulpcore-manager migrate --noinput')
+              .with_environment(['PULP_SETTINGS=/etc/pulpcore/settings.py'])
               .with_refreshonly(true)
               .with_unless('/usr/bin/false')
           end
@@ -60,12 +50,8 @@
             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_concat('pulpcore settings')
-            is_expected.to contain_exec('python3-django-admin help')
-              .with_environment([
-                'DJANGO_SETTINGS_MODULE=pulpcore.app.settings',
-                'PULP_SETTINGS=/etc/pulp/settings.py',
-                'PULP_STATIC_ROOT=/var/lib/pulp/assets',
-              ])
+            is_expected.to contain_exec('pulpcore-manager help')
+              .with_environment(['PULP_SETTINGS=/etc/pulp/settings.py'])
               .with_refreshonly(false)
               .with_unless(nil)
               .that_requires('Concat[pulpcore settings]')

From 8c66bff5f2146139d2882ef75ac742235fe253b1 Mon Sep 17 00:00:00 2001
From: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>
Date: Thu, 16 Jul 2020 17:45:20 +0200
Subject: [PATCH 2/2] Run pulpcore-manager as the pulp user

---
 manifests/admin.pp         | 5 +++++
 spec/defines/admin_spec.rb | 6 +++++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/manifests/admin.pp b/manifests/admin.pp
index 8b8c5403..7a538ce8 100644
--- a/manifests/admin.pp
+++ b/manifests/admin.pp
@@ -14,6 +14,9 @@
 # @param path
 #   The path to look for commands.
 #
+# @param user
+#   The user to execute the command.
+#
 # @param pulp_settings
 #   Root directory for static content
 #
@@ -23,10 +26,12 @@
   Boolean $refreshonly = false,
   Optional[String] $unless = undef,
   Array[Stdlib::Absolutepath] $path = ['/usr/bin'],
+  String $user = $pulpcore::user,
   Stdlib::Absolutepath $pulp_settings = $pulpcore::settings_file,
 ) {
   Concat <| title == 'pulpcore settings' |>
   -> exec { "pulpcore-manager ${command}":
+    user        => $user,
     path        => $path,
     environment => ["PULP_SETTINGS=${pulp_settings}"],
     refreshonly => $refreshonly,
diff --git a/spec/defines/admin_spec.rb b/spec/defines/admin_spec.rb
index e880a0e4..bdbdab94 100644
--- a/spec/defines/admin_spec.rb
+++ b/spec/defines/admin_spec.rb
@@ -6,10 +6,11 @@
       let(:facts) { os_facts }
       let(:title) { 'help' }
 
-      context 'with a fixed pulp_settings' do
+      context 'with a fixed pulp_settings and user' do
         let(:params) do
           {
             pulp_settings: '/etc/pulpcore/settings.py',
+            user: 'pulpcore',
           }
         end
 
@@ -17,6 +18,7 @@
           it do
             is_expected.to compile.with_all_deps
             is_expected.to contain_exec('pulpcore-manager help')
+              .with_user('pulpcore')
               .with_environment(['PULP_SETTINGS=/etc/pulpcore/settings.py'])
               .with_refreshonly(false)
               .with_unless(nil)
@@ -35,6 +37,7 @@
           it do
             is_expected.to compile.with_all_deps
             is_expected.to contain_exec('pulpcore-manager migrate --noinput')
+              .with_user('pulpcore')
               .with_environment(['PULP_SETTINGS=/etc/pulpcore/settings.py'])
               .with_refreshonly(true)
               .with_unless('/usr/bin/false')
@@ -51,6 +54,7 @@
             is_expected.to contain_pulpcore__admin('help').with_pulp_settings('/etc/pulp/settings.py')
             is_expected.to contain_concat('pulpcore settings')
             is_expected.to contain_exec('pulpcore-manager help')
+              .with_user('pulp')
               .with_environment(['PULP_SETTINGS=/etc/pulp/settings.py'])
               .with_refreshonly(false)
               .with_unless(nil)