authentik.blueprints.management.commands.apply_blueprint

Apply blueprint from commandline

 1"""Apply blueprint from commandline"""
 2
 3from argparse import ArgumentParser
 4from sys import exit as sys_exit
 5
 6from django.core.management.base import BaseCommand, no_translations
 7from structlog.stdlib import get_logger
 8
 9from authentik.blueprints.models import BlueprintInstance
10from authentik.blueprints.v1.importer import Importer
11
12LOGGER = get_logger()
13
14
15class Command(BaseCommand):
16    """Apply blueprint from commandline"""
17
18    @no_translations
19    def handle(self, *args, blueprints: list[str], dry_run: bool, **options):
20        """Apply all blueprints in order, abort when one fails to import"""
21        for blueprint_path in blueprints:
22            content = BlueprintInstance(path=blueprint_path).retrieve()
23            importer = Importer.from_string(content)
24            valid, logs = importer.validate()
25            if not valid:
26                self.stderr.write("Blueprint invalid")
27                for log in logs:
28                    self.stderr.write(f"\t{log.logger}: {log.event}: {log.attributes}")
29                sys_exit(1)
30            if not dry_run:
31                importer.apply()
32            else:
33                LOGGER.info("Dry-run enabled, not applying blueprint")
34
35    def add_arguments(self, parser: ArgumentParser):
36        parser.add_argument("--dry-run", action="store_true", default=False)
37        parser.add_argument("blueprints", nargs="+", type=str)
LOGGER = <BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
class Command(django.core.management.base.BaseCommand):
16class Command(BaseCommand):
17    """Apply blueprint from commandline"""
18
19    @no_translations
20    def handle(self, *args, blueprints: list[str], dry_run: bool, **options):
21        """Apply all blueprints in order, abort when one fails to import"""
22        for blueprint_path in blueprints:
23            content = BlueprintInstance(path=blueprint_path).retrieve()
24            importer = Importer.from_string(content)
25            valid, logs = importer.validate()
26            if not valid:
27                self.stderr.write("Blueprint invalid")
28                for log in logs:
29                    self.stderr.write(f"\t{log.logger}: {log.event}: {log.attributes}")
30                sys_exit(1)
31            if not dry_run:
32                importer.apply()
33            else:
34                LOGGER.info("Dry-run enabled, not applying blueprint")
35
36    def add_arguments(self, parser: ArgumentParser):
37        parser.add_argument("--dry-run", action="store_true", default=False)
38        parser.add_argument("blueprints", nargs="+", type=str)

Apply blueprint from commandline

def handle(*args, **kwargs):
106    def wrapper(*args, **kwargs):
107        from django.utils import translation
108
109        saved_locale = translation.get_language()
110        translation.deactivate_all()
111        try:
112            res = handle_func(*args, **kwargs)
113        finally:
114            if saved_locale is not None:
115                translation.activate(saved_locale)
116        return res

The type of the None singleton.

def add_arguments(self, parser: argparse.ArgumentParser):
36    def add_arguments(self, parser: ArgumentParser):
37        parser.add_argument("--dry-run", action="store_true", default=False)
38        parser.add_argument("blueprints", nargs="+", type=str)

Entry point for subclassed commands to add custom arguments.