PageRenderTime 87ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 37ms app.codeStats 5ms

/silversupport/abstractservice.py

https://bitbucket.org/ianb/silverlining/
Python | 79 lines | 42 code | 13 blank | 24 comment | 3 complexity | 6951083b3d368a663d252e732f6a42d3 MD5 | raw file
 1from silversupport.shell import apt_install
 2
 3
 4class AbstractService(object):
 5    name = None
 6
 7    # Any deb packages that need to be installed:
 8    packages = []
 9    # This should be like {'python': ['python-mydb']}
10    # Packages that depend on the python/php platform
11    platform_packages = {}
12
13    def __init__(self, app_config, config_string, devel=False,
14                 devel_config=None, output=None):
15        self.app_config = app_config
16        self.config_string = config_string
17        self.devel = devel
18        self.devel_config = devel_config
19        if output:
20            self.output = output
21        else:
22            self.output = self.stdout_output
23        self.env = self.env_setup()
24
25    def __repr__(self):
26        return '<%s.%s for %r>' % (
27            self.__class__.__module__, self.__class__.__name__,
28            self.app_config)
29
30    def stdout_output(self, msg):
31        print msg
32
33    def install(self):
34        """Installs everything for this service.  This may have to
35        install packages, configure the service, and probably has to
36        setup a database for the application."""
37        raise NotImplementedError
38
39    def install_packages(self):
40        """Install any (deb) packages needed for this service.
41
42        This will read the class variables packages and
43        platform_packages by default.
44
45        Implementations of ``.install()`` should probably call this
46        method."""
47        packages = (self.packages +
48                    self.platform_packages.get(self.app_config.platform, []))
49        apt_install(packages)
50
51    def env_setup(self):
52        """Returns an environment dictionary of any variables that
53        should be set"""
54        raise NotImplementedError
55
56    def backup(self, output_dir):
57        """Backs up this database into a directory"""
58        raise NotImplementedError
59
60    def restore(self, input_dir):
61        """Restores this database from the given backup (unpacked)"""
62        raise NotImplementedError
63
64    def clear(self):
65        """Clears the database"""
66        raise NotImplementedError
67
68    def check_setup(self):
69        """This runs a check on the service, especially for use in
70        development.
71
72        For instance, you could connect to a database to make sure it
73        is setup and available.
74
75        This should raise an exception if there's a serious problem,
76        or return a string warning if it's a suspected or non-fatal
77        problem.
78        """
79        pass