File: bootstrap.py

package info (click to toggle)
errbot 6.1.7+ds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,712 kB
  • sloc: python: 13,831; makefile: 164; sh: 97
file content (259 lines) | stat: -rw-r--r-- 9,844 bytes parent folder | download
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import importlib
import logging
import sys
from os import makedirs, path

from errbot.backend_plugin_manager import BackendPluginManager
from errbot.core import ErrBot
from errbot.logs import format_logs
from errbot.plugin_manager import BotPluginManager
from errbot.repo_manager import BotRepoManager
from errbot.storage.base import StoragePluginBase
from errbot.utils import PLUGINS_SUBDIR

log = logging.getLogger(__name__)

HERE = path.dirname(path.abspath(__file__))
CORE_BACKENDS = path.join(HERE, "backends")
CORE_STORAGE = path.join(HERE, "storage")

PLUGIN_DEFAULT_INDEX = "https://errbot.io/repos.json"


def bot_config_defaults(config):
    if not hasattr(config, "ACCESS_CONTROLS_DEFAULT"):
        config.ACCESS_CONTROLS_DEFAULT = {}
    if not hasattr(config, "ACCESS_CONTROLS"):
        config.ACCESS_CONTROLS = {}
    if not hasattr(config, "HIDE_RESTRICTED_COMMANDS"):
        config.HIDE_RESTRICTED_COMMANDS = False
    if not hasattr(config, "HIDE_RESTRICTED_ACCESS"):
        config.HIDE_RESTRICTED_ACCESS = False
    if not hasattr(config, "BOT_PREFIX_OPTIONAL_ON_CHAT"):
        config.BOT_PREFIX_OPTIONAL_ON_CHAT = False
    if not hasattr(config, "BOT_PREFIX"):
        config.BOT_PREFIX = "!"
    if not hasattr(config, "BOT_ALT_PREFIXES"):
        config.BOT_ALT_PREFIXES = ()
    if not hasattr(config, "BOT_ALT_PREFIX_SEPARATORS"):
        config.BOT_ALT_PREFIX_SEPARATORS = ()
    if not hasattr(config, "BOT_ALT_PREFIX_CASEINSENSITIVE"):
        config.BOT_ALT_PREFIX_CASEINSENSITIVE = False
    if not hasattr(config, "DIVERT_TO_PRIVATE"):
        config.DIVERT_TO_PRIVATE = ()
    if not hasattr(config, "DIVERT_TO_THREAD"):
        config.DIVERT_TO_THREAD = ()
    if not hasattr(config, "MESSAGE_SIZE_LIMIT"):
        config.MESSAGE_SIZE_LIMIT = None  # No user limit declared.
    if not hasattr(config, "GROUPCHAT_NICK_PREFIXED"):
        config.GROUPCHAT_NICK_PREFIXED = False
    if not hasattr(config, "AUTOINSTALL_DEPS"):
        config.AUTOINSTALL_DEPS = True
    if not hasattr(config, "SUPPRESS_CMD_NOT_FOUND"):
        config.SUPPRESS_CMD_NOT_FOUND = False
    if not hasattr(config, "BOT_ASYNC"):
        config.BOT_ASYNC = True
    if not hasattr(config, "BOT_ASYNC_POOLSIZE"):
        config.BOT_ASYNC_POOLSIZE = 10
    if not hasattr(config, "CHATROOM_PRESENCE"):
        config.CHATROOM_PRESENCE = ()
    if not hasattr(config, "CHATROOM_RELAY"):
        config.CHATROOM_RELAY = ()
    if not hasattr(config, "REVERSE_CHATROOM_RELAY"):
        config.REVERSE_CHATROOM_RELAY = ()
    if not hasattr(config, "CHATROOM_FN"):
        config.CHATROOM_FN = "Errbot"
    if not hasattr(config, "TEXT_DEMO_MODE"):
        config.TEXT_DEMO_MODE = True
    if not hasattr(config, "BOT_ADMINS"):
        raise ValueError("BOT_ADMINS missing from config.py.")
    if not hasattr(config, "TEXT_COLOR_THEME"):
        config.TEXT_COLOR_THEME = "light"
    if not hasattr(config, "BOT_ADMINS_NOTIFICATIONS"):
        config.BOT_ADMINS_NOTIFICATIONS = config.BOT_ADMINS


def setup_bot(backend_name: str, logger, config, restore=None) -> ErrBot:
    # from here the environment is supposed to be set (daemon / non daemon,
    # config.py in the python path )

    bot_config_defaults(config)

    if hasattr(config, "BOT_LOG_FORMATTER"):
        format_logs(formatter=config.BOT_LOG_FORMATTER)
    else:
        format_logs(theme_color=config.TEXT_COLOR_THEME)

        if config.BOT_LOG_FILE:
            hdlr = logging.FileHandler(config.BOT_LOG_FILE)
            hdlr.setFormatter(
                logging.Formatter("%(asctime)s %(levelname)-8s %(name)-25s %(message)s")
            )
            logger.addHandler(hdlr)

    if hasattr(config, "BOT_LOG_SENTRY") and config.BOT_LOG_SENTRY:
        sentry_integrations = []

        try:
            import sentry_sdk
            from sentry_sdk.integrations.logging import LoggingIntegration

        except ImportError:
            log.exception(
                "You have BOT_LOG_SENTRY enabled, but I couldn't import modules "
                "needed for Sentry integration. Did you install sentry-sdk? "
                "(See https://docs.sentry.io/platforms/python for installation instructions)"
            )
            exit(-1)

        sentry_logging = LoggingIntegration(
            level=config.SENTRY_LOGLEVEL, event_level=config.SENTRY_EVENTLEVEL
        )

        sentry_integrations.append(sentry_logging)

        if hasattr(config, "BOT_LOG_SENTRY_FLASK") and config.BOT_LOG_SENTRY_FLASK:
            try:
                from sentry_sdk.integrations.flask import FlaskIntegration
            except ImportError:
                log.exception(
                    "You have BOT_LOG_SENTRY enabled, but I couldn't import modules "
                    "needed for Sentry integration. Did you install sentry-sdk[flask]? "
                    "(See https://docs.sentry.io/platforms/python/flask for installation instructions)"
                )
                exit(-1)

            sentry_integrations.append(FlaskIntegration())

        try:
            if hasattr(config, "SENTRY_TRANSPORT") and isinstance(
                config.SENTRY_TRANSPORT, tuple
            ):
                mod = importlib.import_module(config.SENTRY_TRANSPORT[1])
                transport = getattr(mod, config.SENTRY_TRANSPORT[0])

                sentry_sdk.init(
                    dsn=config.SENTRY_DSN,
                    integrations=sentry_integrations,
                    transport=transport,
                )
            else:
                sentry_sdk.init(dsn=config.SENTRY_DSN, integrations=sentry_integrations)
        except ImportError:
            log.exception(
                f"Unable to import selected SENTRY_TRANSPORT - {config.SENTRY_TRANSPORT}"
            )
            exit(-1)

    logger.setLevel(config.BOT_LOG_LEVEL)

    storage_plugin = get_storage_plugin(config)

    # init the botplugin manager
    botplugins_dir = path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR)
    if not path.exists(botplugins_dir):
        makedirs(botplugins_dir, mode=0o755)

    plugin_indexes = getattr(config, "BOT_PLUGIN_INDEXES", (PLUGIN_DEFAULT_INDEX,))
    if isinstance(plugin_indexes, str):
        plugin_indexes = (plugin_indexes,)

    # Extra backend is expected to be a list type, convert string to list.
    extra_backend = getattr(config, "BOT_EXTRA_BACKEND_DIR", [])
    if isinstance(extra_backend, str):
        extra_backend = [extra_backend]

    backendpm = BackendPluginManager(
        config, "errbot.backends", backend_name, ErrBot, CORE_BACKENDS, extra_backend
    )

    log.info(f"Found Backend plugin: {backendpm.plugin_info.name}")

    repo_manager = BotRepoManager(storage_plugin, botplugins_dir, plugin_indexes)

    try:
        bot = backendpm.load_plugin()
        botpm = BotPluginManager(
            storage_plugin,
            config.BOT_EXTRA_PLUGIN_DIR,
            config.AUTOINSTALL_DEPS,
            getattr(config, "CORE_PLUGINS", None),
            lambda name, clazz: clazz(bot, name),
            getattr(config, "PLUGINS_CALLBACK_ORDER", (None,)),
        )
        bot.attach_storage_plugin(storage_plugin)
        bot.attach_repo_manager(repo_manager)
        bot.attach_plugin_manager(botpm)
        bot.initialize_backend_storage()

        # restore the bot from the restore script
        if restore:
            # Prepare the context for the restore script
            if "repos" in bot:
                log.fatal("You cannot restore onto a non empty bot.")
                sys.exit(-1)
            log.info(f"**** RESTORING the bot from {restore}")
            restore_bot_from_backup(restore, bot=bot, log=log)
            print("Restore complete. You can restart the bot normally")
            sys.exit(0)

        errors = bot.plugin_manager.update_plugin_places(
            repo_manager.get_all_repos_paths()
        )
        if errors:
            startup_errors = "\n".join(errors.values())
            log.error("Some plugins failed to load:\n%s", startup_errors)
            bot._plugin_errors_during_startup = startup_errors
        return bot
    except Exception:
        log.exception("Unable to load or configure the backend.")
        exit(-1)


def restore_bot_from_backup(backup_filename, *, bot, log):
    """Restores the given bot by executing the 'backup' script.

    The backup file is a python script which manually execute a series of commands on the bot
    to restore it to its previous state.

    :param backup_filename: the full path to the backup script.
    :param bot: the bot instance to restore
    :param log: logger to use during the restoration process
    """
    with open(backup_filename) as f:
        exec(f.read(), {"log": log, "bot": bot})
    bot.close_storage()


def get_storage_plugin(config):
    """
    Find and load the storage plugin
    :param config: the bot configuration.
    :return: the storage plugin
    """
    storage_name = getattr(config, "STORAGE", "Shelf")
    extra_storage_plugins_dir = getattr(config, "BOT_EXTRA_STORAGE_PLUGINS_DIR", None)
    spm = BackendPluginManager(
        config,
        "errbot.storage",
        storage_name,
        StoragePluginBase,
        CORE_STORAGE,
        extra_storage_plugins_dir,
    )
    log.info(f"Found Storage plugin: {spm.plugin_info.name}.")
    return spm.load_plugin()


def bootstrap(bot_class, logger, config, restore=None):
    """
    Main starting point of Errbot.

    :param bot_class: The backend class inheriting from Errbot you want to start.
    :param logger: The logger you want to use.
    :param config: The config.py module.
    :param restore: Start Errbot in restore mode (from a backup).
    """
    bot = setup_bot(bot_class, logger, config, restore)
    log.debug(f"Start serving commands from the {bot.mode} backend.")
    bot.serve_forever()