Example #1
0
def handle_rpc():
    class FakeService(MessagingEnabled):
        def __init__(self, auth_token, conn):
            super(FakeService, self).__init__(auth_token=auth_token, conn=conn)

        def start(self):
            self.get_connection().connect()

    app_url = sys.argv[1]
    rpc_name = sys.argv[2]
    api_name = sys.argv[3]
    json_args = sys.argv[4]

    plugins_db = PluginsDatabase(os.path.join(get_config_path(), "db"))
    plugins_db.start()

    conn = WeaveConnection.discover()
    conn.connect()

    instance_data = get_instance_data()
    token = instance_data.app_token

    rpc_info = find_rpc(FakeService(token, conn), app_url, rpc_name)
    client = RPCClient(conn, rpc_info, token)
    client.start()

    print(
        json.dumps(client[api_name](*json.loads(json_args), _block=True),
                   indent=2))
Example #2
0
def handle_main():
    base_path = get_config_path()
    plugins_db = PluginsDatabase(os.path.join(base_path, "db"))
    plugin_manager = PluginManager(base_path)

    plugins_db.start()

    weave = LocalWeaveInstance(get_instance_data(), plugin_manager)
    try:
        weave.start()
    except WeaveException:
        weave.stop()

    signal.signal(signal.SIGTERM, lambda x, y: weave.stop())
    signal.signal(signal.SIGINT, lambda x, y: weave.stop())

    weave.wait()
Example #3
0
def handle_messaging_plugin_install():
    base_path = get_config_path()
    plugins_db = PluginsDatabase(os.path.join(base_path, "db"))
    plugin_manager = PluginManager(base_path)

    plugins_db.start()
    plugin_manager.start()

    plugin_manager.install(MESSAGING_PLUGIN_URL)

    token = "app-token-" + str(uuid4())
    instance_data = WeaveEnvInstanceData(machine_id=get_machine_id(),
                                         app_token=token)
    plugin_data = PluginData(app_url=MESSAGING_PLUGIN_URL,
                             name="WeaveServer",
                             description="Messaging",
                             enabled=True,
                             machine=instance_data)
    plugin_data.save(force_insert=True)
    instance_data.save(force_insert=True)
Example #4
0
def handle_main():
    machine_id = get_machine_id()
    base_path = get_config_path()
    plugins_db = PluginsDatabase(os.path.join(base_path, "db"))
    plugin_manager = PluginManager(base_path)

    plugins_db.start()

    try:
        instance_data = WeaveEnvInstanceData.get(
            WeaveEnvInstanceData.machine_id == machine_id)
    except DoesNotExist:
        sys.exit("Please re-install messaging plugin.")

    weave = LocalWeaveInstance(instance_data, plugin_manager)
    weave.start()

    signal.signal(signal.SIGTERM, lambda x, y: weave.stop())
    signal.signal(signal.SIGINT, lambda x, y: weave.stop())

    weave.wait()
Example #5
0
def handle_messaging_token():
    plugins_db = PluginsDatabase(os.path.join(get_config_path(), "db"))
    plugins_db.start()

    messaging_server_url = MESSAGING_PLUGIN_URL
    if sys.argv[1] == 'set':
        plugins_db.insert(app_id=url_to_plugin_id(messaging_server_url),
                          app_secret_token=sys.argv[2],
                          is_remote=True)
    elif sys.argv[1] == 'get':
        plugin_data = plugins_db.query(url_to_plugin_id(messaging_server_url))
        print(plugin_data.app_secret_token)
    else:
        print("Supported operations: 'get' and 'set'")
Example #6
0
 def setup(self, tmpdir):
     self.base_dir = Path(tmpdir.strpath)
     self.db = PluginsDatabase(":memory:")
     self.db.start()
     self.instance = WeaveEnvInstanceData(machine_id="x", app_token="y")
     self.instance.save(force_insert=True)
Example #7
0
class TestPluginLifecycle(object):
    def get_test_id(self, plugin_dir):
        return url_to_plugin_id(self.get_test_plugin_path(plugin_dir))

    def get_test_plugin_path(self, plugin_dir):
        testdata = os.path.join(os.path.dirname(__file__), "testdata")
        return os.path.join(testdata, plugin_dir)

    def get_test_plugin(self, dir_name):
        path = self.get_test_plugin_path(dir_name)
        return path, dir_name, "description"

    def list_plugins(self):
        pattern = os.path.join(os.path.dirname(__file__), "testdata/*/")
        plugin_dirs = [os.path.basename(x.rstrip('/')) for x in glob(pattern)]
        return [self.get_test_plugin(x) for x in plugin_dirs]

    @classmethod
    def setup_class(cls):
        cls.messaging_service = MessagingService()
        cls.messaging_service.service_start()
        cls.messaging_service.wait_for_start(15)

        cls.conn = WeaveConnection.local()
        cls.conn.connect()

        cls.fake_service = DummyEnvService(cls.messaging_service.test_token,
                                           cls.conn)

    @classmethod
    def teardown_class(cls):
        cls.conn.close()
        cls.messaging_service.service_stop()

    @pytest.fixture(autouse=True)
    def setup(self, tmpdir):
        self.base_dir = Path(tmpdir.strpath)
        self.db = PluginsDatabase(":memory:")
        self.db.start()
        self.instance = WeaveEnvInstanceData(machine_id="x", app_token="y")
        self.instance.save(force_insert=True)

    def teardown(self):
        shutil.rmtree(self.base_dir)

    def test_plugin_listing(self, *args):
        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()

        expected = [{
            "name": "plugin1",
            "description": "description",
            "id": self.get_test_id('plugin1'),
            "enabled": False,
            "installed": False,
            "active": False,
            "remote_url": self.get_test_plugin_path('plugin1'),
        }]

        assert pm.list() == expected

    def test_plugin_install(self, *args):
        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()
        res = pm.install(self.get_test_plugin_path('plugin1'))

        expected = {
            "name": "plugin1",
            "description": "description",
            "id": self.get_test_id('plugin1'),
            "enabled": False,
            "installed": True,
            "active": False,
            "remote_url": self.get_test_plugin_path('plugin1'),
        }

        assert res == expected

    def test_plugin_double_install(self, *args):
        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()
        res1 = pm.install(self.get_test_plugin_path('plugin1'))

        expected = {
            "name": "plugin1",
            "description": "description",
            "id": self.get_test_id('plugin1'),
            "enabled": False,
            "installed": True,
            "active": False,
            "remote_url": self.get_test_plugin_path('plugin1'),
        }

        res2 = pm.install(self.get_test_plugin_path('plugin1'))
        assert res1 == res2 == expected

    def test_plugin_uninstall(self, *args):
        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()
        res1 = pm.install(self.get_test_plugin_path('plugin1'))
        res2 = pm.uninstall(self.get_test_plugin_path('plugin1'))
        expected = {
            "name": "plugin1",
            "description": "description",
            "id": self.get_test_id('plugin1'),
            "enabled": False,
            "installed": False,
            "active": False,
            "remote_url": self.get_test_plugin_path('plugin1'),
        }

        assert res2 == expected

    def test_plugin_bad_uninstall(self, *args):
        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()
        res = pm.uninstall(self.get_test_plugin_path('plugin1'))
        assert not res["installed"]

    def test_uninstall_enabled_plugin(self, *args):
        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()
        plugin_url = self.get_test_plugin_path('plugin1')
        pm.install(plugin_url)
        pm.enable(plugin_url)

        with pytest.raises(PluginLoadError,
                           match="Must disable the plugin .*"):
            pm.uninstall(self.get_test_plugin_path('plugin1'))

        pm.stop()

    def test_load_plugin_not_enabled(self, *args):
        plugin_url = self.get_test_plugin_path('plugin1')
        db_plugin = PluginData(name="plugin1",
                               description="x",
                               app_url=plugin_url,
                               machine=self.instance)
        db_plugin.save(force_insert=True)

        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()

        assert pm.plugin_state(plugin_url) == {
            "name": "plugin1",
            "description": "description",
            "id": self.get_test_id('plugin1'),
            "enabled": False,
            "installed": False,
            "active": False,
            "remote_url": self.get_test_plugin_path('plugin1'),
        }

    def test_load_plugin_enabled(self, *args):
        plugin_url = self.get_test_plugin_path('plugin1')
        plugin_id = self.get_test_id('plugin1')
        db_plugin = PluginData(name="plugin1",
                               description="x",
                               enabled=True,
                               app_url=plugin_url,
                               machine=self.instance)
        install_dir = self.base_dir / "plugins" / plugin_id
        venv_dir = self.base_dir / "venv" / plugin_id

        db_plugin.save(force_insert=True)
        shutil.copytree(plugin_url, str(install_dir))

        VirtualEnvManager(venv_dir).install(install_dir / "requirements.txt")

        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()

        expected = {
            "name": "plugin1",
            "description": "description",
            "id": plugin_id,
            "enabled": True,
            "installed": True,
            "active": True,
            "remote_url": plugin_url,
        }
        assert pm.plugin_state(plugin_url) == expected

        pm.stop()

    def test_activate(self, *args):
        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()

        plugin_url = self.get_test_plugin_path('plugin1')
        pm.install(plugin_url)
        pm.enable(plugin_url)
        result = pm.activate(plugin_url)

        expected = {
            "name": "plugin1",
            "description": "description",
            "id": self.get_test_id('plugin1'),
            "enabled": True,
            "installed": True,
            "active": True,
            "remote_url": self.get_test_plugin_path('plugin1'),
        }
        assert result == expected

        result = pm.deactivate(plugin_url)
        expected = {
            "name": "plugin1",
            "description": "description",
            "id": self.get_test_id('plugin1'),
            "enabled": True,
            "installed": True,
            "active": False,
            "remote_url": self.get_test_plugin_path('plugin1'),
        }
        assert result == expected

        pm.stop()

    def test_activate_disabled_plugin(self, *args):
        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()

        plugin_url = self.get_test_plugin_path('plugin1')
        plugin = pm.install(plugin_url)

        with pytest.raises(PluginLoadError, match=".*not enabled.*"):
            pm.activate(plugin_url)

    def test_activate_remote_plugin(self, *args):
        pm = PluginManager(self.base_dir,
                           self.instance,
                           self.fake_service,
                           lister_fn=self.list_plugins)
        pm.start()

        plugin_url = self.get_test_plugin_path('plugin1')
        with pytest.raises(PluginLoadError, match=".*not installed.*"):
            pm.activate(plugin_url)