1 0
from django.core.management.base import BaseCommand
2 0
from django.core.management import call_command
3 0
from crum import impersonate
4 0
from cyborgbackup.main.models import Policy, Client, Repository, Schedule
5 0
from django.contrib.auth import get_user_model
6 0
from cyborgbackup.main.models.settings import Setting
7

8

9 0
class Command(BaseCommand):
10
    """Create preloaded data, intended for new installs
11
    """
12 0
    help = 'Creates a preload tower data iff there is none.'
13

14 0
    def handle(self, *args, **kwargs):
15
        # Sanity check: Is there already an organization in the system?
16 0
        if Setting.objects.count():
17 0
            print('System is already configured, exiting.')
18 0
            print('(changed: False)')
19 0
            return
20

21 0
        User = get_user_model()
22 0
        User.objects.create_superuser('admin@cyborg.local', 'adminadmin')
23

24 0
        call_command("loaddata", "settings")
25

26
        # Create a default organization as the first superuser found.
27 0
        try:
28 0
            superuser = User.objects.filter(is_superuser=True).order_by('pk')[0]
29 0
        except IndexError:
30 0
            superuser = None
31 0
        with impersonate(superuser):
32 0
            r = Repository.objects.create(name='Demo Repository',
33
                                          path='/tmp/repository',
34
                                          repository_key='0123456789abcdef',
35
                                          enabled=False)
36 0
            s = Schedule.objects.create(name='Demo Schedule',
37
                                        crontab='0 5 * * MON *',
38
                                        enabled=False)
39 0
            c = Client.objects.create(hostname='localhost',
40
                                      enabled=False)
41 0
            p = Policy(name='Demo Policy',
42
                       mode_pull=False,
43
                       enabled=False,
44
                       repository=r,
45
                       schedule=s)
46 0
            p.save()
47 0
            p.clients.add(c)
48 0
        print('Demo Client, Repository, Schedule and Policy added.')
49 0
        print('(changed: True)')

Read our documentation on viewing source code .

Loading