Exemplo n.º 1
0
    def _job(self):
        get_conf = db.get().server_config_get
        is_applied = lambda cfg: not cfg or (cfg and cfg["applied"])

        no_changes_msg = "Deployer: no changes in config detected."

        try:
            if is_applied(get_conf()):
                LOG.info(no_changes_msg)
            else:
                with eslock.Glock("update_config"):
                    config = get_conf()  # Refresh config after lock
                    if not is_applied(config):
                        LOG.info("Deployer detect new config: "
                                 "Updating deployment")
                        clients = db.get().clients_get()

                        # TODO(boris-42): Add support of multi drivers
                        new_clients = StaticDeployer().redeploy(
                            config["config"]["deployment"]["static"], clients)

                        db.get().clients_set(new_clients)
                        db.get().server_config_apply(config["id"])
                        return True
                    else:
                        LOG.info(no_changes_msg)

        except exceptions.GlobalLockException:
            pass  # can't accuire lock, someone else is working on it

        except Exception:
            LOG.exception("Deployer update failed")
Exemplo n.º 2
0
    def _job(self):
        get_conf = db.get().server_config_get
        is_meshed = lambda cfg: (not cfg or (cfg and not cfg["applied"]) or
                                 (cfg and cfg["meshed"]))
        try:
            if is_meshed(get_conf()):
                LOG.info(self.no_changes_msg)
            else:
                with eslock.Glock("update_config"):
                    # TODO(boris-42): Alogrithm should be a bit smarter
                    # even if it is meshed try to update all not configured
                    # clients.
                    config = get_conf()
                    if not is_meshed(config):
                        LOG.info(self.new_config_msg)
                        for c in self._mesh(config["config"]):
                            # TODO(boris-42): Run this in parallel
                            self._update_client(c[0], c[1])
                        db.get().server_config_meshed(config["id"])
                    else:
                        LOG.info(self.no_changes_msg)

        except exceptions.GlobalLockException:
            pass  # can't accuire lock, someone else is working on it

        except Exception:
            LOG.exception(self.update_failed_msg)
Exemplo n.º 3
0
    def test_lock_failed(self, mock_get):
        db_ = db.DB()
        db_.own_url = "upsis"
        db_.elastic = mock.MagicMock()
        db_.elastic.indices.create.side_effect = (
            elasticsearch.exceptions.ElasticsearchException)
        mock_get.return_value = db_

        g = eslock.Glock("some_name")
        self.assertRaises(exceptions.GlobalLockException, g.__enter__)
Exemplo n.º 4
0
    def test_lock_acquired(self, mock_get):
        db_ = db.DB()
        db_.elastic = mock.MagicMock()
        db_.own_url = "upsis"
        mock_get.return_value = db_

        g = eslock.Glock("some_name")
        with g:
            self.assertTrue(g.acquired)
            self.assertRaises(exceptions.GlobalLockException, g.__enter__)

        self.assertFalse(g.acquired)
Exemplo n.º 5
0
    def refresh_client(self, host, port):
        lock_acuired = False
        attempts = 0

        while not lock_acuired and attempts < 3:
            try:
                with eslock.Glock("update_config"):
                    config = db.get().server_config_get()
                    if not (config["applied"] and config["meshed"]):
                        return False, 404, "Configuration not found"

                    for c in self._mesh(config["config"]):
                        if c[0]["host"] == host and c[0]["port"] == port:
                            return self._update_client(c[0], c[1])

                    return False, 404, "Client not found"

            except exceptions.GlobalLockException:
                attempts += 1
                self._death.wait(0.1)

        return False, 500, "Couldn't accuire lock"
Exemplo n.º 6
0
 def test_init(self):
     g = eslock.Glock("some_name")
     self.assertEqual("some_name", g.name)
     self.assertEqual(10, g.ttl)
     self.assertFalse(g.acquired)