Skip to content

Commit 7c6b37e

Browse files
committed
Add management commands
1 parent 56d381a commit 7c6b37e

File tree

7 files changed

+110
-0
lines changed

7 files changed

+110
-0
lines changed

patchman/apps.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from __future__ import unicode_literals
2+
3+
from django.apps import AppConfig
4+
5+
6+
class PatchmanConfig(AppConfig):
7+
name = 'patchman'

patchman/management/__init__.py

Whitespace-only changes.

patchman/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from django.contrib.auth.management.commands import createsuperuser
2+
from django.core.management import CommandError
3+
4+
5+
class Command(createsuperuser.Command):
6+
help = 'Crate a superuser, and allow password to be provided'
7+
8+
def add_arguments(self, parser):
9+
super(Command, self).add_arguments(parser)
10+
parser.add_argument(
11+
'--password', dest='password', default=None,
12+
help='Specifies the password for the superuser.',
13+
)
14+
15+
def handle(self, *args, **options):
16+
password = options.get('password')
17+
username = options.get('username')
18+
database = options.get('database')
19+
20+
if options['interactive']:
21+
raise CommandError(
22+
'Command is required to run with --no-input option')
23+
if password and not username:
24+
raise CommandError(
25+
'--username is required if specifying --password')
26+
27+
super(Command, self).handle(*args, **options)
28+
29+
if password:
30+
user = self.UserModel._default_manager.db_manager(
31+
database).get(username=username)
32+
user.set_password(password)
33+
user.save()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.core.management.base import BaseCommand, CommandError
2+
from hosts.models import Host
3+
4+
5+
class Command(BaseCommand):
6+
help = 'Enable/Disable rDNS check for hosts'
7+
8+
def add_arguments(self, parser):
9+
parser.add_argument(
10+
'--disable', action='store_false', default=True, dest='rdns_check',
11+
help='If set, disables rDNS check')
12+
13+
def handle(self, *args, **options):
14+
try:
15+
Host.objects.all().update(check_dns=options['rdns_check'])
16+
except Exception as e:
17+
raise CommandError('Failed to update rDNS check', str(e))
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
import re
3+
import sys
4+
import codecs
5+
from random import choice
6+
from tempfile import NamedTemporaryFile
7+
from shutil import copy
8+
9+
from django.core.management.base import BaseCommand
10+
11+
12+
class Command(BaseCommand):
13+
help = 'Set SECRET_KEY of Patchman Application.'
14+
15+
def add_arguments(self, parser):
16+
parser.add_argument(
17+
'--key', help=(
18+
'The SECRET_KEY to be used by Patchman. If not set, a random '
19+
'key of length 50 will be created.'))
20+
21+
@staticmethod
22+
def get_random_key():
23+
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
24+
return ''.join([choice(chars) for i in range(50)])
25+
26+
def handle(self, *args, **options):
27+
secret_key = options.get('key', self.get_random_key())
28+
29+
if sys.prefix == '/usr':
30+
conf_path = '/etc/patchman'
31+
else:
32+
conf_path = os.path.join(sys.prefix, 'etc/patchman')
33+
# if conf_path doesn't exist, try ./etc/patchman
34+
if not os.path.isdir(conf_path):
35+
conf_path = './etc/patchman'
36+
local_settings = os.path.join(conf_path, 'local_settings.py')
37+
38+
settings_contents = codecs.open(
39+
local_settings, 'r', encoding='utf-8').read()
40+
settings_contents = re.sub(
41+
r"(?<=SECRET_KEY = ')'", secret_key + "'", settings_contents)
42+
43+
f = NamedTemporaryFile(delete=False)
44+
temp = f.name
45+
f.close()
46+
47+
fh = codecs.open(temp, 'w+b', encoding='utf-8')
48+
fh.write(settings_contents)
49+
fh.close()
50+
51+
copy(temp, local_settings)
52+
os.remove(temp)

patchman/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
'repos.apps.ReposConfig',
9090
'reports.apps.ReportsConfig',
9191
'util.apps.UtilConfig',
92+
'patchman.apps.PatchmanConfig',
9293
]
9394

9495
REST_FRAMEWORK = {

0 commit comments

Comments
 (0)