From 15e9be0e972e3c4e3d8e65e5d7cd5808090736f4 Mon Sep 17 00:00:00 2001 From: Stamatis Katsaounis Date: Fri, 10 Apr 2020 13:26:46 +0300 Subject: [PATCH 1/6] Add management commands --- patchman/apps.py | 7 +++ patchman/management/__init__.py | 0 patchman/management/commands/__init__.py | 0 .../commands/createsuperuser_with_password.py | 33 ++++++++++++ .../management/commands/set_rdns_check.py | 17 ++++++ .../management/commands/set_secret_key.py | 52 +++++++++++++++++++ patchman/settings.py | 1 + 7 files changed, 110 insertions(+) create mode 100644 patchman/apps.py create mode 100644 patchman/management/__init__.py create mode 100644 patchman/management/commands/__init__.py create mode 100644 patchman/management/commands/createsuperuser_with_password.py create mode 100644 patchman/management/commands/set_rdns_check.py create mode 100644 patchman/management/commands/set_secret_key.py diff --git a/patchman/apps.py b/patchman/apps.py new file mode 100644 index 00000000..4a9c3f7b --- /dev/null +++ b/patchman/apps.py @@ -0,0 +1,7 @@ +from __future__ import unicode_literals + +from django.apps import AppConfig + + +class PatchmanConfig(AppConfig): + name = 'patchman' diff --git a/patchman/management/__init__.py b/patchman/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/patchman/management/commands/__init__.py b/patchman/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/patchman/management/commands/createsuperuser_with_password.py b/patchman/management/commands/createsuperuser_with_password.py new file mode 100644 index 00000000..63d3c721 --- /dev/null +++ b/patchman/management/commands/createsuperuser_with_password.py @@ -0,0 +1,33 @@ +from django.contrib.auth.management.commands import createsuperuser +from django.core.management import CommandError + + +class Command(createsuperuser.Command): + help = 'Crate a superuser, and allow password to be provided' + + def add_arguments(self, parser): + super(Command, self).add_arguments(parser) + parser.add_argument( + '--password', dest='password', default=None, + help='Specifies the password for the superuser.', + ) + + def handle(self, *args, **options): + password = options.get('password') + username = options.get('username') + database = options.get('database') + + if options['interactive']: + raise CommandError( + 'Command is required to run with --no-input option') + if password and not username: + raise CommandError( + '--username is required if specifying --password') + + super(Command, self).handle(*args, **options) + + if password: + user = self.UserModel._default_manager.db_manager( + database).get(username=username) + user.set_password(password) + user.save() diff --git a/patchman/management/commands/set_rdns_check.py b/patchman/management/commands/set_rdns_check.py new file mode 100644 index 00000000..0e5250a7 --- /dev/null +++ b/patchman/management/commands/set_rdns_check.py @@ -0,0 +1,17 @@ +from django.core.management.base import BaseCommand, CommandError +from hosts.models import Host + + +class Command(BaseCommand): + help = 'Enable/Disable rDNS check for hosts' + + def add_arguments(self, parser): + parser.add_argument( + '--disable', action='store_false', default=True, dest='rdns_check', + help='If set, disables rDNS check') + + def handle(self, *args, **options): + try: + Host.objects.all().update(check_dns=options['rdns_check']) + except Exception as e: + raise CommandError('Failed to update rDNS check', str(e)) diff --git a/patchman/management/commands/set_secret_key.py b/patchman/management/commands/set_secret_key.py new file mode 100644 index 00000000..870d1475 --- /dev/null +++ b/patchman/management/commands/set_secret_key.py @@ -0,0 +1,52 @@ +import os +import re +import sys +import codecs +from random import choice +from tempfile import NamedTemporaryFile +from shutil import copy + +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + help = 'Set SECRET_KEY of Patchman Application.' + + def add_arguments(self, parser): + parser.add_argument( + '--key', help=( + 'The SECRET_KEY to be used by Patchman. If not set, a random ' + 'key of length 50 will be created.')) + + @staticmethod + def get_random_key(): + chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' + return ''.join([choice(chars) for i in range(50)]) + + def handle(self, *args, **options): + secret_key = options.get('key', self.get_random_key()) + + if sys.prefix == '/usr': + conf_path = '/etc/patchman' + else: + conf_path = os.path.join(sys.prefix, 'etc/patchman') + # if conf_path doesn't exist, try ./etc/patchman + if not os.path.isdir(conf_path): + conf_path = './etc/patchman' + local_settings = os.path.join(conf_path, 'local_settings.py') + + settings_contents = codecs.open( + local_settings, 'r', encoding='utf-8').read() + settings_contents = re.sub( + r"(?<=SECRET_KEY = ')'", secret_key + "'", settings_contents) + + f = NamedTemporaryFile(delete=False) + temp = f.name + f.close() + + fh = codecs.open(temp, 'w+b', encoding='utf-8') + fh.write(settings_contents) + fh.close() + + copy(temp, local_settings) + os.remove(temp) diff --git a/patchman/settings.py b/patchman/settings.py index 4a943a2f..fb1af58e 100644 --- a/patchman/settings.py +++ b/patchman/settings.py @@ -93,6 +93,7 @@ 'repos.apps.ReposConfig', 'reports.apps.ReportsConfig', 'util.apps.UtilConfig', + 'patchman.apps.PatchmanConfig', ] REST_FRAMEWORK = { From 726fa74c3cf4771546c50493ea5b22fca2de7cee Mon Sep 17 00:00:00 2001 From: Stamatis Katsaounis Date: Tue, 12 May 2020 13:24:44 +0300 Subject: [PATCH 2/6] Add set_site command --- patchman/management/commands/set_site.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 patchman/management/commands/set_site.py diff --git a/patchman/management/commands/set_site.py b/patchman/management/commands/set_site.py new file mode 100644 index 00000000..659bb281 --- /dev/null +++ b/patchman/management/commands/set_site.py @@ -0,0 +1,23 @@ +from django.core.management.base import BaseCommand, CommandError +from django.contrib.sites.models import Site +from django.conf import settings + + +class Command(BaseCommand): + help = 'Set Patchman Site Name' + + def add_arguments(self, parser): + parser.add_argument( + '-n', '--name', dest='site_name', help='Site name') + parser.add_argument( + '--clear-cache', action='store_true', default=False, + dest='clear_cache', help='Clear Site cache') + + def handle(self, *args, **options): + try: + Site.objects.filter(pk=settings.SITE_ID).update( + name=options['site_name'], domain=options['site_name']) + if options['claer_cache']: + Site.objects.clear_cache() + except Exception as e: + raise CommandError('Failed to update Site name', str(e)) From 6d0b4b04a577f1150b60f3e1a11b32621e5fa20a Mon Sep 17 00:00:00 2001 From: Stamatis Katsaounis Date: Thu, 21 May 2020 10:29:25 +0300 Subject: [PATCH 3/6] Fix option --- patchman/management/commands/set_site.py | 2 +- patchman/wsgi.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/patchman/management/commands/set_site.py b/patchman/management/commands/set_site.py index 659bb281..3ad97a1b 100644 --- a/patchman/management/commands/set_site.py +++ b/patchman/management/commands/set_site.py @@ -17,7 +17,7 @@ def handle(self, *args, **options): try: Site.objects.filter(pk=settings.SITE_ID).update( name=options['site_name'], domain=options['site_name']) - if options['claer_cache']: + if options['clear_cache']: Site.objects.clear_cache() except Exception as e: raise CommandError('Failed to update Site name', str(e)) diff --git a/patchman/wsgi.py b/patchman/wsgi.py index 9a9b4b7f..bae22bf9 100644 --- a/patchman/wsgi.py +++ b/patchman/wsgi.py @@ -15,7 +15,6 @@ # along with Patchman. If not, see import os - from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'patchman.settings') # noqa From 58d36e32d50774fd84c94944a8126357abf29a3e Mon Sep 17 00:00:00 2001 From: Marcus Furlong Date: Thu, 20 Jun 2024 22:09:39 -0400 Subject: [PATCH 4/6] Update apps.py --- patchman/apps.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/patchman/apps.py b/patchman/apps.py index 4a9c3f7b..cb08e2c1 100644 --- a/patchman/apps.py +++ b/patchman/apps.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - from django.apps import AppConfig From 1c7ef233720e27b9ca67431d17304a11b28f1451 Mon Sep 17 00:00:00 2001 From: Marcus Furlong Date: Mon, 20 Oct 2025 21:15:01 -0400 Subject: [PATCH 5/6] Update patchman/management/commands/createsuperuser_with_password.py Co-authored-by: code-review-doctor[bot] <72320148+code-review-doctor[bot]@users.noreply.github.com> --- patchman/management/commands/createsuperuser_with_password.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patchman/management/commands/createsuperuser_with_password.py b/patchman/management/commands/createsuperuser_with_password.py index 63d3c721..e2d677ba 100644 --- a/patchman/management/commands/createsuperuser_with_password.py +++ b/patchman/management/commands/createsuperuser_with_password.py @@ -24,7 +24,7 @@ def handle(self, *args, **options): raise CommandError( '--username is required if specifying --password') - super(Command, self).handle(*args, **options) + super().handle(*args, **options) if password: user = self.UserModel._default_manager.db_manager( From f5b34d7e544f2c16160bd755e3406b30a0b1f3f9 Mon Sep 17 00:00:00 2001 From: Marcus Furlong Date: Mon, 20 Oct 2025 21:15:08 -0400 Subject: [PATCH 6/6] Update patchman/management/commands/createsuperuser_with_password.py Co-authored-by: code-review-doctor[bot] <72320148+code-review-doctor[bot]@users.noreply.github.com> --- patchman/management/commands/createsuperuser_with_password.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patchman/management/commands/createsuperuser_with_password.py b/patchman/management/commands/createsuperuser_with_password.py index e2d677ba..3006e25e 100644 --- a/patchman/management/commands/createsuperuser_with_password.py +++ b/patchman/management/commands/createsuperuser_with_password.py @@ -6,7 +6,7 @@ class Command(createsuperuser.Command): help = 'Crate a superuser, and allow password to be provided' def add_arguments(self, parser): - super(Command, self).add_arguments(parser) + super().add_arguments(parser) parser.add_argument( '--password', dest='password', default=None, help='Specifies the password for the superuser.',