Example #1
0
def unlink_servers(server_id, link_server_id):
    collection = mongo.get_collection('servers')

    spec = {
        '_id': {'$in': [server_id, link_server_id]},
    }
    project = {
        '_id': True,
        'status': True,
    }

    for doc in collection.find(spec, project):
        if doc['status'] == ONLINE:
            raise ServerLinkOnlineError('Server must be offline to unlink')

    tran = transaction.Transaction()
    collection = tran.collection('servers')

    collection.update({
        '_id': server_id,
    }, {'$pull': {
        'links': {'server_id': link_server_id},
    }})

    collection.update({
        '_id': link_server_id,
    }, {'$pull': {
        'links': {'server_id': server_id},
    }})

    tran.commit()
Example #2
0
    def commit(self, *args, **kwargs):
        tran = None

        if self.network != self._orig_network:
            tran = transaction.Transaction()
            if self.network_lock:
                raise ServerNetworkLocked('Server network is locked', {
                    'server_id': self.id,
                    'lock_id': self.network_lock,
                })
            else:
                queue_ip_pool = queue.start(
                    'assign_ip_pool',
                    transaction=tran,
                    server_id=self.id,
                    network=self.network,
                    old_network=self._orig_network,
                )
                self.network_lock = queue_ip_pool.id
        elif self._orgs_changed:
            # TODO update ip pool
            pass

        mongo.MongoObject.commit(self, transaction=tran, *args, **kwargs)

        if tran:
            messenger.publish('queue', 'queue_updated', transaction=tran)
            tran.commit()
Example #3
0
    def commit(self, *args, **kwargs):
        tran = None

        if 'network' in self.loaded_fields and \
                self.network != self._orig_network:
            tran = transaction.Transaction()
            if self.network_lock:
                raise ServerNetworkLocked('Server network is locked', {
                    'server_id': self.id,
                    'lock_id': self.network_lock,
                })
            else:
                queue_ip_pool = queue.start(
                    'assign_ip_pool',
                    transaction=tran,
                    server_id=self.id,
                    network=self.network,
                    old_network=self._orig_network,
                )
                self.network_lock = queue_ip_pool.id

        for org_id in self._orgs_added:
            self.ip_pool.assign_ip_pool_org(org_id)

        for org_id in self._orgs_removed:
            self.ip_pool.unassign_ip_pool_org(org_id)

        mongo.MongoObject.commit(self, transaction=tran, *args, **kwargs)

        if tran:
            messenger.publish('queue', 'queue_updated', transaction=tran)
            tran.commit()
Example #4
0
    def commit(self, init=False):
        from pritunl import messenger
        from pritunl import transaction

        docs = []
        has_docs = False
        transaction = transaction.Transaction()
        collection = transaction.collection(self.collection.name_str)

        for group in self.groups:
            group_cls = getattr(self, group)
            if group_cls.type != GROUP_MONGO:
                continue

            doc = group_cls.get_commit_doc(init)

            if doc:
                has_docs = True
                collection.bulk().find({
                    '_id': doc['_id'],
                }).upsert().update({
                    '$set': doc,
                })
                docs.append(doc)

        messenger.publish('setting', docs, transaction=transaction)

        if not has_docs:
            return

        collection.bulk_execute()
        transaction.commit()
Example #5
0
def _check_thread():
    collection = mongo.get_collection('transaction')

    while True:
        try:
            spec = {
                'ttl_timestamp': {
                    '$lt': utils.now()
                },
            }

            for doc in collection.find(spec).sort('priority'):
                logger.info(
                    'Transaction timeout retrying...',
                    'runners',
                    doc=doc,
                )

                try:
                    tran = transaction.Transaction(doc=doc)
                    tran.run()
                except:
                    logger.exception(
                        'Failed to run transaction',
                        'runners',
                        transaction_id=doc['_id'],
                    )

            yield interrupter_sleep(settings.mongo.tran_ttl)
        except GeneratorExit:
            raise
        except:
            logger.exception('Error in transaction runner thread', 'runners')
            time.sleep(0.5)
Example #6
0
def link_servers(server_id, link_server_id, use_local_address=False):
    if server_id == link_server_id:
        raise TypeError('Server id must be different then link server id')

    collection = mongo.get_collection('servers')

    count = 0
    spec = {
        '_id': {
            '$in': [server_id, link_server_id]
        },
    }
    project = {
        '_id': True,
        'status': True,
    }

    for doc in collection.find(spec, project):
        if doc['status'] == ONLINE:
            raise ServerLinkOnlineError('Server must be offline to link')
        count += 1
    if count != 2:
        raise ServerLinkError('Link server not found')

    tran = transaction.Transaction()
    collection = tran.collection('servers')

    collection.update(
        {
            '_id': server_id,
            'links.server_id': {
                '$ne': link_server_id
            },
        }, {
            '$push': {
                'links': {
                    'server_id': link_server_id,
                    'user_id': None,
                    'use_local_address': use_local_address,
                },
            }
        })

    collection.update(
        {
            '_id': link_server_id,
            'links.server_id': {
                '$ne': server_id
            },
        }, {
            '$addToSet': {
                'links': {
                    'server_id': server_id,
                    'user_id': None,
                    'use_local_address': use_local_address,
                },
            }
        })

    tran.commit()
Example #7
0
def _check_thread():
    collection = mongo.get_collection('transaction')

    while True:
        spec = {
            'ttl_timestamp': {
                '$lt': datetime.datetime.utcnow()
            },
        }

        for doc in collection.find(spec).sort('priority'):
            try:
                tran = transaction.Transaction(doc=doc)
                tran.run()
            except:
                logger.exception('Failed to run transaction. %r' % {
                    'transaction_id': str(doc['id']),
                })

        time.sleep(settings.mongo.tran_ttl)
Example #8
0
def link_servers(server_id, link_server_id, use_local_address=False):
    if server_id == link_server_id:
        raise TypeError('Server id must be different then link server id')

    hosts = set()
    routes = set()
    for svr, link_id in (
        (get_by_id(server_id), link_server_id),
        (get_by_id(link_server_id), server_id),
    ):
        has_link = False
        if svr.links:
            for link in svr.links:
                if link['server_id'] == link_id:
                    has_link = True

        if not has_link:
            if not svr.links:
                svr.links = []

            svr.links.append({
                'server_id': link_id,
                'user_id': None,
                'use_local_address': use_local_address,
            })

        err, err_msg = svr.validate_conf()
        if err:
            return err, err_msg

        hosts_set = set(svr.hosts)
        if hosts & hosts_set:
            return SERVER_LINK_COMMON_HOST, SERVER_LINK_COMMON_HOST_MSG
        hosts.update(hosts_set)

        routes_set = set()
        for route in svr.get_routes():
            if route['network'] != '0.0.0.0/0':
                routes_set.add(route['network'])
        if routes & routes_set:
            return SERVER_LINK_COMMON_ROUTE, SERVER_LINK_COMMON_ROUTE_MSG
        routes.update(routes_set)

    tran = transaction.Transaction()
    collection = tran.collection('servers')

    collection.update(
        {
            '_id': server_id,
            'links.server_id': {
                '$ne': link_server_id
            },
        }, {
            '$push': {
                'links': {
                    'server_id': link_server_id,
                    'user_id': None,
                    'use_local_address': use_local_address,
                },
            }
        })

    collection.update(
        {
            '_id': link_server_id,
            'links.server_id': {
                '$ne': server_id
            },
        }, {
            '$addToSet': {
                'links': {
                    'server_id': server_id,
                    'user_id': None,
                    'use_local_address': use_local_address,
                },
            }
        })

    tran.commit()

    return None, None
Example #9
0
def link_servers(server_id, link_server_id, use_local_address=False):
    if server_id == link_server_id:
        raise TypeError('Server id must be different then link server id')

    fields = ('_id', 'status', 'hosts', 'replica_count', 'network', 'links',
              'network_start', 'network_end', 'routes', 'organizations')

    hosts = set()
    routes = set()

    for svr in (
            get_by_id(server_id, fields=fields),
            get_by_id(link_server_id, fields=fields),
    ):
        if svr.status == ONLINE:
            raise ServerLinkOnlineError('Server must be offline to link')

        if svr.replica_count > 1:
            raise ServerLinkReplicaError('Server has replicas')

        hosts_set = set(svr.hosts)
        if hosts & hosts_set:
            raise ServerLinkCommonHostError('Servers have a common host')
        hosts.update(hosts_set)

        routes_set = set()
        for route in svr.get_routes():
            routes_set.add(route['network'])
        if routes & routes_set:
            raise ServerLinkCommonRouteError('Servers have a common route')
        routes.update(routes_set)

    tran = transaction.Transaction()
    collection = tran.collection('servers')

    collection.update(
        {
            '_id': server_id,
            'links.server_id': {
                '$ne': link_server_id
            },
        }, {
            '$push': {
                'links': {
                    'server_id': link_server_id,
                    'user_id': None,
                    'use_local_address': use_local_address,
                },
            }
        })

    collection.update(
        {
            '_id': link_server_id,
            'links.server_id': {
                '$ne': server_id
            },
        }, {
            '$addToSet': {
                'links': {
                    'server_id': server_id,
                    'user_id': None,
                    'use_local_address': use_local_address,
                },
            }
        })

    tran.commit()
Example #10
0
def link_servers(server_id, link_server_id, use_local_address=False):
    if server_id == link_server_id:
        raise TypeError('Server id must be different then link server id')

    collection = mongo.get_collection('servers')

    count = 0
    spec = {
        '_id': {
            '$in': [server_id, link_server_id]
        },
    }
    project = {
        '_id': True,
        'status': True,
        'hosts': True,
        'replica_count': True,
    }

    hosts = set()
    for doc in collection.find(spec, project):
        if doc['status'] == ONLINE:
            raise ServerLinkOnlineError('Server must be offline to link')

        if doc['replica_count'] > 1:
            raise ServerLinkReplicaError('Server has replicas')

        hosts_set = set(doc['hosts'])
        if hosts & hosts_set:
            raise ServerLinkCommonHostError('Servers have a common host')
        hosts.update(hosts_set)

        count += 1
    if count != 2:
        raise ServerLinkError('Link server not found')

    tran = transaction.Transaction()
    collection = tran.collection('servers')

    collection.update(
        {
            '_id': server_id,
            'links.server_id': {
                '$ne': link_server_id
            },
        }, {
            '$push': {
                'links': {
                    'server_id': link_server_id,
                    'user_id': None,
                    'use_local_address': use_local_address,
                },
            }
        })

    collection.update(
        {
            '_id': link_server_id,
            'links.server_id': {
                '$ne': server_id
            },
        }, {
            '$addToSet': {
                'links': {
                    'server_id': server_id,
                    'user_id': None,
                    'use_local_address': use_local_address,
                },
            }
        })

    tran.commit()