Exemplo n.º 1
0
    def setUp(self):
        self.app = brew_view.app.test_client()

        self.default_instance = Instance(name="default", status="RUNNING")
        self.default_command = Command(
            id="54ac18f778c4b57e963f3c18", name="command", description="foo"
        )
        self.default_system = System(
            id="54ac18f778c4b57e963f3c18",
            name="default_system",
            version="1.0.0",
            instances=[self.default_instance],
            commands=[self.default_command],
            max_instances="1",
        )
Exemplo n.º 2
0
    def _create_new_system(system_model):
        new_system = system_model

        # Assign a default 'main' instance if there aren't any instances and there can
        # only be one
        if not new_system.instances or len(new_system.instances) == 0:
            if new_system.max_instances is None or new_system.max_instances == 1:
                new_system.instances = [Instance(name="default")]
                new_system.max_instances = 1
            else:
                raise ModelValidationError(
                    "Could not create system %s-%s: Systems with "
                    "max_instances > 1 must also define their instances" %
                    (system_model.name, system_model.version))
        else:
            if not new_system.max_instances:
                new_system.max_instances = len(new_system.instances)

        return new_system, 201
Exemplo n.º 3
0
    def _update_existing_system(existing_system, system_model):
        # Raise an exception if commands already exist for this system and they differ
        # from what's already in the database in a significant way
        if (existing_system.commands and "dev" not in existing_system.version
                and existing_system.has_different_commands(
                    system_model.commands)):
            raise ModelValidationError(
                "System %s-%s already exists with different commands" %
                (system_model.name, system_model.version))
        else:
            existing_system.upsert_commands(system_model.commands)

        # Update instances
        if not system_model.instances or len(system_model.instances) == 0:
            system_model.instances = [Instance(name="default")]

        for attr in ["description", "icon_name", "display_name"]:
            value = getattr(system_model, attr)

            # If we set an attribute on the model as None, mongoengine marks the
            # attribute for deletion. We want to prevent this, so we set it to an emtpy
            # string.
            if value is None:
                setattr(existing_system, attr, "")
            else:
                setattr(existing_system, attr, value)

        # Update metadata
        new_metadata = system_model.metadata or {}
        existing_system.metadata.update(new_metadata)

        old_instances = [
            inst for inst in existing_system.instances
            if system_model.has_instance(inst.name)
        ]
        new_instances = [
            inst for inst in system_model.instances
            if not existing_system.has_instance(inst.name)
        ]
        existing_system.instances = old_instances + new_instances

        return existing_system, 200
Exemplo n.º 4
0
    def setUp(self):
        self.app = brew_view.app.test_client()

        self.default_instance = Instance(name="default", status="RUNNING")
        self.default_command = Command(id="54ac18f778c4b57e963f3c18",
                                       name="command",
                                       description="foo")
        self.default_system = System(
            id="54ac18f778c4b57e963f3c18",
            name="default_system",
            version="1.0.0",
            instances=[self.default_instance],
            commands=[self.default_command],
            max_instances="1",
        )

        self.client_mock = Mock(name="client_mock")
        self.fake_context = MagicMock(
            __enter__=Mock(return_value=self.client_mock),
            __exit__=Mock(return_value=False),
        )
Exemplo n.º 5
0
    def setUp(self):
        self.default_command = Command(name="name", description="description")
        self.default_command.save = Mock()
        self.default_command.validate = Mock()
        self.default_command.delete = Mock()

        self.default_instance = Instance(name="default")
        self.default_instance.save = Mock()
        self.default_instance.validate = Mock()
        self.default_instance.delete = Mock()

        self.default_system = System(
            id="1234",
            name="foo",
            version="1.0.0",
            instances=[self.default_instance],
            commands=[self.default_command],
        )
        self.default_system.save = Mock()
        self.default_system.validate = Mock()
        self.default_system.delete = Mock()
Exemplo n.º 6
0
    def load_plugin(self, plugin_path):
        """Loads a plugin given a path to a plugin directory.

        It will use the validator to validate the plugin before registering the
        plugin in the database as well as adding an entry to the plugin map.

        :param plugin_path: The path of the plugin to load
        :return: The loaded plugin
        """
        if not self.validator.validate_plugin(plugin_path):
            self.logger.warning(
                "Not loading plugin at %s because it was invalid.",
                plugin_path)
            return False

        config = self._load_plugin_config(join(plugin_path, "beer.conf"))

        plugin_name = config["NAME"]
        plugin_version = config["VERSION"]
        plugin_entry = config["PLUGIN_ENTRY"]
        plugin_instances = config["INSTANCES"]
        plugin_args = config["PLUGIN_ARGS"]

        # If this system already exists we need to do some stuff
        plugin_id = None
        plugin_commands = []
        # TODO: replace this with a call from the EasyClient
        # plugin_system = self.easy_client.find_unique_system(
        #     name=plugin_name, version=plugin_version)
        plugin_system = System.find_unique(plugin_name, plugin_version)

        if plugin_system:
            # Remove the current instances so they aren't left dangling
            # TODO: This should be replaced with a network call
            plugin_system.delete_instances()

            # Carry these over to the new system
            plugin_id = plugin_system.id
            plugin_commands = plugin_system.commands

        plugin_system = System(
            id=plugin_id,
            name=plugin_name,
            version=plugin_version,
            commands=plugin_commands,
            instances=[
                Instance(name=instance_name)
                for instance_name in plugin_instances
            ],
            max_instances=len(plugin_instances),
            description=config.get("DESCRIPTION"),
            icon_name=config.get("ICON_NAME"),
            display_name=config.get("DISPLAY_NAME"),
            metadata=config.get("METADATA"),
        )

        # TODO: Right now, we have to save this system because the LocalPluginRunner
        # uses the database to determine status, specifically, it calls reload on the
        # instance object which we need to change to satisfy
        plugin_system.deep_save()

        plugin_list = []
        plugin_log_directory = bartender.config.plugin.local.log_directory
        for instance_name in plugin_instances:
            plugin = LocalPluginRunner(
                plugin_entry,
                plugin_system,
                instance_name,
                abspath(plugin_path),
                bartender.config.web.host,
                bartender.config.web.port,
                bartender.config.web.ssl_enabled,
                plugin_args=plugin_args.get(instance_name),
                environment=config["ENVIRONMENT"],
                requirements=config["REQUIRES"],
                plugin_log_directory=plugin_log_directory,
                url_prefix=bartender.config.web.url_prefix,
                ca_verify=bartender.config.web.ca_verify,
                ca_cert=bartender.config.web.ca_cert,
                username=bartender.config.plugin.local.auth.username,
                password=bartender.config.plugin.local.auth.password,
                log_level=config["LOG_LEVEL"],
            )

            self.registry.register_plugin(plugin)
            plugin_list.append(plugin)

        return plugin_list
Exemplo n.º 7
0
 def test_clean_bad_status(self):
     instance = Instance(status="BAD")
     self.assertRaises(ModelValidationError, instance.clean)
Exemplo n.º 8
0
 def test_repr(self):
     instance = Instance(name="name", status="RUNNING")
     self.assertNotEqual(-1, repr(instance).find("name"))
     self.assertNotEqual(-1, repr(instance).find("RUNNING"))
Exemplo n.º 9
0
 def test_str(self):
     self.assertEqual("name", str(Instance(name="name")))
Exemplo n.º 10
0
 def test_clean_fail_duplicate_instance_names(self):
     self.default_system.max_instances = 2
     self.default_system.instances.append(Instance(name="default"))
     self.assertRaises(ModelValidationError, self.default_system.clean)
Exemplo n.º 11
0
def mongo_instance(instance_dict, ts_dt):
    """An instance as a model."""
    dict_copy = copy.deepcopy(instance_dict)
    dict_copy["status_info"]["heartbeat"] = ts_dt
    return Instance(**dict_copy)