File: __init__.py

package info (click to toggle)
freedombox 26.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 82,976 kB
  • sloc: python: 48,504; javascript: 1,736; xml: 481; makefile: 290; sh: 167; php: 32
file content (262 lines) | stat: -rw-r--r-- 9,876 bytes parent folder | download | duplicates (4)
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
260
261
262
# SPDX-License-Identifier: AGPL-3.0-or-later
"""FreedomBox app to configure Nextcloud."""

import contextlib

from django.utils.translation import gettext_lazy as _

from plinth import app as app_module
from plinth import cfg, frontpage, menu
from plinth.config import DropinConfigs
from plinth.daemon import Daemon, SharedDaemon
from plinth.modules.apache.components import (Webserver, diagnose_url,
                                              diagnose_url_on_all)
from plinth.modules.backups.components import BackupRestore
from plinth.modules.firewall.components import (Firewall,
                                                FirewallLocalProtection)
from plinth.modules.names.components import DomainName
from plinth.package import Packages
from plinth.signals import domain_added, domain_removed
from plinth.utils import format_lazy

from . import manifest, privileged

_description = [
    _('Nextcloud is a self-hosted productivity platform which provides '
      'private and secure functions for file sharing, collaborative work, '
      'and more. Nextcloud includes the Nextcloud server, client applications '
      'for desktop computers, and mobile clients. The Nextcloud server '
      'provides a well integrated web interface.'),
    _('All users of FreedomBox can use Nextcloud. To perform administrative '
      f'actions, use the <strong>"{privileged.GUI_ADMIN}"</strong> user after '
      'setting a password here.'),
    format_lazy(
        _('Please note that Nextcloud is installed and run inside a container '
          'provided by the Nextcloud community. Security, quality, privacy '
          'and legal reviews are done by the upstream project and not by '
          'Debian/{box_name}. Updates are performed following an independent '
          'cycle.'), box_name=_(cfg.box_name)),
]


class NextcloudApp(app_module.App):
    """FreedomBox app for Nextcloud."""

    app_id = 'nextcloud'

    _version = 2

    configure_when_disabled = False

    def __init__(self):
        """Create components for the app."""
        super().__init__()

        info = app_module.Info(app_id=self.app_id, version=self._version,
                               name=_('Nextcloud'), icon_filename='nextcloud',
                               description=_description,
                               manual_page='Nextcloud',
                               clients=manifest.clients, tags=manifest.tags)
        self.add(info)

        menu_item = menu.Menu('menu-nextcloud', info.name, info.icon_filename,
                              info.tags, 'nextcloud:index',
                              parent_url_name='apps')
        self.add(menu_item)

        shortcut = frontpage.Shortcut('shortcut-nextcloud', info.name,
                                      icon=info.icon_filename,
                                      url='/nextcloud/', clients=info.clients,
                                      tags=info.tags, login_required=True)
        self.add(shortcut)

        packages = Packages('packages-nextcloud', [
            'podman', 'default-mysql-server', 'python3-iso3166',
            'redis-server', 'php-redis'
        ], conflicts=['libpam-tmpdir'],
                            conflicts_action=Packages.ConflictsAction.REMOVE)
        self.add(packages)

        dropin_configs = DropinConfigs('dropin-configs-nextcloud', [
            '/etc/apache2/conf-available/nextcloud-freedombox.conf',
            '/etc/redis/conf.d/freedombox.conf',
        ])
        self.add(dropin_configs)

        firewall = Firewall('firewall-nextcloud', info.name,
                            ports=['http', 'https'], is_external=True)
        self.add(firewall)

        firewall_local_protection = FirewallLocalProtection(
            'firewall-local-protection-nextcloud', ['9000'])
        self.add(firewall_local_protection)

        webserver = Webserver('webserver-nextcloud', 'nextcloud-freedombox')
        self.add(webserver)

        daemon = SharedDaemon('shared-daemon-podman-auto-update',
                              'podman-auto-update.timer')
        self.add(daemon)

        daemon = SharedDaemon('shared-daemon-nextcloud-redis', 'redis-server',
                              listen_ports=[(6379, 'tcp4')])
        self.add(daemon)

        daemon = SharedDaemon('shared-daemon-nextcloud-mysql', 'mysql')
        self.add(daemon)

        daemon = NextcloudDaemon('daemon-nextcloud', 'nextcloud-freedombox')
        self.add(daemon)

        daemon = Daemon('daemon-nextcloud-timer',
                        'nextcloud-cron-freedombox.timer')
        self.add(daemon)

        backup_restore = NextcloudBackupRestore('backup-restore-nextcloud',
                                                **manifest.backup)
        self.add(backup_restore)

    @staticmethod
    def post_init():
        """Perform post initialization operations."""
        domain_added.connect(_on_domain_added)
        domain_removed.connect(_on_domain_removed)

    def setup(self, old_version):
        """Install and configure the app."""
        if old_version == 1:
            with _ensure_nextcloud_running():
                # Reset the value so that 'overwrite.cli.url' is set to default
                # value instead of it being missing.
                if not privileged.get_override_domain():
                    privileged.set_override_domain('')

        super().setup(old_version)
        # Drop-in configs need to be enabled for setup to succeed
        self.get_component('dropin-configs-nextcloud').enable()
        redis = self.get_component('shared-daemon-nextcloud-redis')
        mysql = self.get_component('shared-daemon-nextcloud-mysql')
        nextcloud = self.get_component('daemon-nextcloud')

        # Determine whether app should be disabled after setup
        should_disable = old_version and not nextcloud.is_enabled()

        with redis.ensure_running():
            with mysql.ensure_running():
                # Database needs to be running for successful initialization or
                # upgrade of Nextcloud database.
                privileged.setup()
                _set_trusted_domains()

        if should_disable:
            self.disable()

        if not old_version:
            self.enable()

    def uninstall(self):
        """De-configure and uninstall the app."""
        privileged.uninstall()
        super().uninstall()

    def diagnose(self):
        """Run diagnostics and return the results.

        When an override domain is set, that domain and all other addresses are
        expected to work. This is because Nextcloud will accept any Host: HTTP
        header and then override it with the provided domain name. When
        override domain is not set, only the configured trusted domains along
        with local IP addresses are allowed. Others are rejected with an error.
        """
        results = super().diagnose()

        kwargs = {'check_certificate': False}
        url = 'https://{domain}/nextcloud/login'
        domain = privileged.get_override_domain()
        if domain:
            results.append(diagnose_url(url.format(domain=domain), **kwargs))
            results += diagnose_url_on_all(url.format(domain='{host}'),
                                           **kwargs)
        else:
            local_addresses = [('localhost', '4'), ('localhost', '6'),
                               ('127.0.0.1', '4'), ('[::1]', '6')]
            for address, kind in local_addresses:
                results.append(
                    diagnose_url(url.format(domain=address), kind=kind,
                                 **kwargs))

        return results


class NextcloudDaemon(Daemon):
    """Component to manage Nextcloud container service."""

    def is_enabled(self):
        """Return if the daemon/unit is enabled."""
        return privileged.is_enabled()

    def enable(self):
        """Run operations to enable the daemon/unit."""
        super().enable()
        privileged.enable()

    def disable(self):
        """Run operations to disable the daemon/unit."""
        super().disable()
        privileged.disable()


class NextcloudBackupRestore(BackupRestore):
    """Component to backup/restore Nextcloud."""

    def backup_pre(self, packet):
        """Save database contents."""
        super().backup_pre(packet)
        with _ensure_nextcloud_running():
            privileged.dump_database()

    def restore_post(self, packet):
        """Restore database contents."""
        super().restore_post(packet)
        with _ensure_nextcloud_running():
            privileged.restore_database()


def _on_domain_added(sender, domain_type, name='', description='',
                     services=None, **kwargs):
    """Add domain to list of trusted domains."""
    app = app_module.App.get('nextcloud')
    if app.needs_setup():
        return

    _set_trusted_domains()


def _on_domain_removed(sender, domain_type, name='', **kwargs):
    """Update the list of trusted domains."""
    app = app_module.App.get('nextcloud')
    if app.needs_setup():
        return

    _set_trusted_domains()


def _set_trusted_domains() -> None:
    """Set the list of trusted domains."""
    all_domains = DomainName.list_names()
    with _ensure_nextcloud_running():
        privileged.set_trusted_domains(all_domains)


@contextlib.contextmanager
def _ensure_nextcloud_running():
    """Ensure the nextcloud is running and returns to original state."""
    app = app_module.App.get('nextcloud')
    app.get_component('dropin-configs-nextcloud').enable()
    mysql = app.get_component('shared-daemon-nextcloud-mysql')
    redis = app.get_component('shared-daemon-nextcloud-redis')
    container = app.get_component('daemon-nextcloud')
    with mysql.ensure_running():
        with redis.ensure_running():
            with container.ensure_running():
                yield