Пример #1
0
    def sync_to(self, request, secret, query):
        from freenasUI.failover.models import CARP, Failover
        from freenasUI.failover.utils import (
            delete_pending_pairing,
            get_pending_pairing,
        )
        from freenasUI.middleware.notifier import notifier
        from freenasUI.network.models import Interfaces

        self._check_version(request)

        update_ip = False
        if Failover.objects.all().count() == 0:
            # Pairing
            pairing = get_pending_pairing()
            if not pairing:
                return False
            if secret != pairing['secret'] or pairing['verified'] is False:
                return False
            update_ip = True

            CARP.objects.all().delete()
            Interfaces.objects.filter(
                int_interface__startswith='carp').delete()

            # Order is important, Interfaces first
            for kind, model in (
                ('Interfaces', Interfaces),
                ('CARP', CARP),
            ):
                for entry in pairing['data'][kind]:
                    instance = model()
                    for name, value in entry.iteritems():
                        if name == 'id':
                            continue
                        setattr(instance, name, value)
                    instance.save()

            notifier().start('network')

            delete_pending_pairing()
        else:
            self._authenticated(secret)

        rv = self._conn.dump_recv(query)
        # If this is a pairing action we need to update the IP
        if update_ip and rv:
            Failover.objects.filter(secret=secret).update(
                ipaddress=pairing['ip'])
        return rv
Пример #2
0
    def pairing_receive(self, request, secret):
        from freenasUI.failover.models import CARP, Failover
        from freenasUI.failover.utils import (
            delete_pending_pairing,
            get_pending_pairing,
        )
        from freenasUI.middleware.notifier import notifier
        from freenasUI.storage.models import Volume

        pairing = get_pending_pairing()
        if pairing is None:
            return False
        if secret != pairing.get('secret'):
            return False

        delete_pending_pairing()

        carp = CARP.objects.get(pk=pairing['carp'])
        volume = Volume.objects.get(pk=pairing['volume'])

        if Failover.objects.all().exists():
            raise ValueError("Failover already exists")

        failover = Failover()
        failover.volume = volume
        failover.carp = carp
        failover.ipaddress = pairing['ip']
        failover.secret = pairing['secret']
        failover.save()

        notifier().restart('ix-devd')

        try:
            return notifier().failover_sync_peer(fid=failover.id, fromto='to')
        except ValueError:
            return False