summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Admin/Syncdb.py
blob: 72d3d469e584f011877089a90ec1ee83c9e7433a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Bcfg2.settings
import Bcfg2.Options
import Bcfg2.Server.Admin
from Bcfg2.Server.SchemaUpdater import update_database, UpdaterError
from django.core.management import setup_environ

class Syncdb(Bcfg2.Server.Admin.Mode):
    __shorthelp__ = ("Sync the Django ORM with the configured database")
    __longhelp__ = __shorthelp__ + "\n\nbcfg2-admin syncdb"
    __usage__ = "bcfg2-admin syncdb"
    options = {'configfile': Bcfg2.Options.CFILE,
               'repo': Bcfg2.Options.SERVER_REPOSITORY}

    def __init__(self, setup):
        Bcfg2.Server.Admin.Mode.__init__(self, setup)

    def __call__(self, args):
        import Bcfg2.Server.Admin
        Bcfg2.Server.Admin.Mode.__call__(self, args)

        # Parse options
        self.opts = Bcfg2.Options.OptionParser(self.options)
        self.opts.parse(args)

        # we have to set up the django environment before we import
        # the syncdb command, but we have to wait to set up the
        # environment until we've read the config, which has to wait
        # until we've parsed options.  it's a windy, twisting road.
        Bcfg2.settings.read_config(cfile=self.opts['configfile'],
                                   repo=self.opts['repo'])
        setup_environ(Bcfg2.settings)
        import Bcfg2.Server.models
        Bcfg2.Server.models.load_models(cfile=self.opts['configfile'])

        try:
            update_database()
        except UpdaterError:
            print("Update failed")
            raise SystemExit(-1)