Beispiel #1
0
    def run_agent(self):
        self.__log.info("Starting Cartridge Agent...")

        # Start topology event receiver thread
        self.register_topology_event_listeners()

        if Config.lvs_virtual_ip is None or str(Config.lvs_virtual_ip).strip() == "":
            self.__log.debug("LVS Virtual IP is not defined")
        else:
            event_handler.create_dummy_interface()

        # request complete topology event from CC by publishing CompleteTopologyRequestEvent
        publisher.publish_complete_topology_request_event()

        # wait until complete topology message is received to get LB IP
        self.wait_for_complete_topology()

        # wait for member initialized event
        while not Config.initialized:
            self.__log.debug("Waiting for cartridge agent to be initialized...")
            time.sleep(1)

        # Start instance notifier listener thread
        self.register_instance_topic_listeners()

        # Start tenant event receiver thread
        self.register_tenant_event_listeners()

        # start application signup event listener
        self.register_application_signup_event_listeners()

        # request complete tenant event from CC by publishing CompleteTenantRequestEvent
        publisher.publish_complete_tenant_request_event()

        # Execute instance started shell script
        event_handler.on_instance_started_event()

        # Publish instance started event
        publisher.publish_instance_started_event()

        # Execute start servers extension
        try:
            event_handler.start_server_extension()
        except Exception as ex:
            self.__log.exception("Error processing start servers event: %s" % ex)

        # check if artifact management is required before publishing instance activated event
        repo_url = Config.repo_url
        if repo_url is None or str(repo_url).strip() == "":
            self.__log.info("No artifact repository found")
            publisher.publish_instance_activated_event()
            event_handler.on_instance_activated_event()
        else:
            # instance activated event will be published in artifact updated event handler
            self.__log.info(
                "Artifact repository found, waiting for artifact updated event to checkout artifacts: [repo_url] %s",
                repo_url)

        persistence_mapping_payload = Config.persistence_mappings
        if persistence_mapping_payload is not None:
            event_handler.volume_mount_extension(persistence_mapping_payload)

        # start log publishing thread
        log_publish_manager = None
        if DataPublisherConfiguration.get_instance().enabled:
            log_file_paths = Config.log_file_paths
            if log_file_paths is None:
                self.__log.exception("No valid log file paths found, no logs will be published")
            else:
                self.__log.debug("Starting Log Publisher Manager: [Log file paths] %s" % ", ".join(log_file_paths))
                log_publish_manager = LogPublisherManager(log_file_paths)
                log_publish_manager.start()

        # run until terminated
        while not self.__terminated:
            time.sleep(5)

        if DataPublisherConfiguration.get_instance().enabled:
            log_publish_manager.terminate_all_publishers()
Beispiel #2
0
def on_artifact_updated_event(artifacts_updated_event):
    log.debug(
        "Processing artifact updated event for [tenant] %s [cluster] %s [status] %s"
        % (str(artifacts_updated_event.tenant_id), artifacts_updated_event.cluster_id, artifacts_updated_event.status))

    cluster_id_event = str(artifacts_updated_event.cluster_id).strip()
    cluster_id_payload = Config.cluster_id
    repo_url = str(artifacts_updated_event.repo_url).strip()

    if repo_url == "":
        log.error("Repository URL is empty. Failed to process artifact updated event.")
        return

    if cluster_id_payload is None or cluster_id_payload == "":
        log.error("Cluster ID in payload is empty. Failed to process artifact updated event.")
        return

    if cluster_id_payload != cluster_id_event:
        log.debug("Cluster ID in artifact updated event does not match. Skipping event handler.")
        return

    repo_password = None
    if artifacts_updated_event.repo_password is not None:
        secret = Config.cartridge_key
        repo_password = cartridgeagentutils.decrypt_password(artifacts_updated_event.repo_password, secret)

    if Config.app_path is None:
        log.error("Repository path is empty. Failed to process artifact updated event.")
        return

    if not validate_repo_path(Config.app_path):
        log.error(
            "Repository path cannot be accessed, or is invalid. Failed to process artifact updated event. [App Path] %s"
            % Config.app_path)

        return

    repo_username = artifacts_updated_event.repo_username
    tenant_id = artifacts_updated_event.tenant_id
    is_multitenant = Config.is_multiTenant
    commit_enabled = artifacts_updated_event.commit_enabled

    # create repo object
    local_repo_path = get_repo_path_for_tenant(str(tenant_id), Config.app_path, is_multitenant)
    repo_info = Repository(repo_url, repo_username, repo_password, local_repo_path, tenant_id, commit_enabled)
    log.info("Executing checkout job on artifact updated event...")

    try:
        Config.artifact_checkout_plugin.plugin_object.checkout(repo_info)
    except Exception as e:
        log.exception(
            "Checkout job on artifact updated event failed for tenant: %s %s" % (repo_info.tenant_id, e))

    # execute artifact updated extension
    plugin_values = {"ARTIFACT_UPDATED_CLUSTER_ID": artifacts_updated_event.cluster_id,
                     "ARTIFACT_UPDATED_TENANT_ID": artifacts_updated_event.tenant_id,
                     "ARTIFACT_UPDATED_REPO_URL": artifacts_updated_event.repo_url,
                     "ARTIFACT_UPDATED_REPO_PASSWORD": artifacts_updated_event.repo_password,
                     "ARTIFACT_UPDATED_REPO_USERNAME": artifacts_updated_event.repo_username,
                     "ARTIFACT_UPDATED_STATUS": artifacts_updated_event.status}

    try:
        execute_event_extendables(constants.ARTIFACT_UPDATED_EVENT, plugin_values)
    except Exception as e:
        log.exception("Could not execute plugins for artifact updated event: %s" % e)

    if not Config.activated:
        # publish instance activated event if not yet activated
        publisher.publish_instance_activated_event()
        on_instance_activated_event()

    update_artifacts = Config.read_property(constants.ENABLE_ARTIFACT_UPDATE, True)
    auto_commit = Config.is_commits_enabled
    auto_checkout = Config.is_checkout_enabled

    if update_artifacts:
        try:
            update_interval = int(Config.artifact_update_interval)
        except ValueError:
            log.debug("Invalid artifact sync interval specified: %s, defaulting to 10 seconds" % ValueError)
            update_interval = 10

        AgentGitHandler.schedule_artifact_update_task(
            repo_info,
            auto_checkout,
            auto_commit,
            update_interval)
Beispiel #3
0
    def on_artifact_updated_event(self, artifacts_updated_event):
        self.__log.info("Processing Artifact update event: [tenant] %s [cluster] %s [status] %s" %
                        (str(artifacts_updated_event.tenant_id),
                         artifacts_updated_event.cluster_id,
                         artifacts_updated_event.status))

        cluster_id_event = str(artifacts_updated_event.cluster_id).strip()
        cluster_id_payload = Config.cluster_id
        repo_url = str(artifacts_updated_event.repo_url).strip()

        if (repo_url != "") and (cluster_id_payload is not None) and (cluster_id_payload == cluster_id_event):
            local_repo_path = Config.app_path

            repo_password = None
            if artifacts_updated_event.repo_password is not None:
                secret = Config.cartridge_key
                repo_password = cartridgeagentutils.decrypt_password(artifacts_updated_event.repo_password, secret)

            repo_username = artifacts_updated_event.repo_username
            tenant_id = artifacts_updated_event.tenant_id
            is_multitenant = Config.is_multiTenant
            commit_enabled = artifacts_updated_event.commit_enabled

            self.__log.info("Executing git checkout")

            if local_repo_path is None:
                raise GitRepositorySynchronizationException("Repository path is empty. Cannot perform Git operations.")

            # create repo object
            local_repo_path = self.get_repo_path_for_tenant(str(tenant_id), local_repo_path, is_multitenant)
            repo_info = Repository(repo_url, repo_username, repo_password, local_repo_path, tenant_id, commit_enabled)

            # checkout code
            subscribe_run, updated = AgentGitHandler.checkout(repo_info)

            # execute artifact updated extension
            plugin_values = {"ARTIFACT_UPDATED_CLUSTER_ID": artifacts_updated_event.cluster_id,
                             "ARTIFACT_UPDATED_TENANT_ID": artifacts_updated_event.tenant_id,
                             "ARTIFACT_UPDATED_REPO_URL": artifacts_updated_event.repo_url,
                             "ARTIFACT_UPDATED_REPO_PASSWORD": artifacts_updated_event.repo_password,
                             "ARTIFACT_UPDATED_REPO_USERNAME": artifacts_updated_event.repo_username,
                             "ARTIFACT_UPDATED_STATUS": artifacts_updated_event.status}

            try:
                self.execute_event_extendables(constants.ARTIFACT_UPDATED_EVENT, plugin_values)
            except ValueError:
                self.__log.exception("Could not execute plugins for artifact updated event.")         

            if subscribe_run:
                # publish instanceActivated
                publisher.publish_instance_activated_event(Config.health_stat_plugin)
            elif updated:
                # updated on pull
                self.on_artifact_update_scheduler_event(tenant_id)

            update_artifacts = Config.read_property(constants.ENABLE_ARTIFACT_UPDATE, False)
            auto_commit = Config.is_commits_enabled
            auto_checkout = Config.is_checkout_enabled
            self.__log.info("ADC configuration: [update_artifacts] %s, [auto-commit] %s, [auto-checkout] %s",
                            update_artifacts, auto_commit, auto_checkout)
            if update_artifacts:
                try:
                    update_interval = int(Config.artifact_update_interval)
                except ValueError:
                    self.__log.exception("Invalid artifact sync interval specified.")
                    update_interval = 10

                self.__log.info("Artifact updating task enabled, update interval: %s seconds" % update_interval)

                self.__log.info("Auto Commit is turned %s " % ("on" if auto_commit else "off"))
                self.__log.info("Auto Checkout is turned %s " % ("on" if auto_checkout else "off"))

                AgentGitHandler.schedule_artifact_update_task(
                    repo_info,
                    auto_checkout,
                    auto_commit,
                    update_interval)
Beispiel #4
0
    def on_artifact_updated_event(self, artifacts_updated_event):
        self.__log.info(
            "Processing Artifact update event: [tenant] %s [cluster] %s [status] %s"
            % (str(artifacts_updated_event.tenant_id),
               artifacts_updated_event.cluster_id,
               artifacts_updated_event.status))

        cluster_id_event = str(artifacts_updated_event.cluster_id).strip()
        cluster_id_payload = Config.cluster_id
        repo_url = str(artifacts_updated_event.repo_url).strip()

        if (repo_url != "") and (cluster_id_payload
                                 is not None) and (cluster_id_payload
                                                   == cluster_id_event):
            local_repo_path = Config.app_path

            repo_password = None
            if artifacts_updated_event.repo_password is not None:
                secret = Config.cartridge_key
                repo_password = cartridgeagentutils.decrypt_password(
                    artifacts_updated_event.repo_password, secret)

            repo_username = artifacts_updated_event.repo_username
            tenant_id = artifacts_updated_event.tenant_id
            is_multitenant = Config.is_multiTenant
            commit_enabled = artifacts_updated_event.commit_enabled

            self.__log.info("Executing git checkout")

            if local_repo_path is None:
                raise GitRepositorySynchronizationException(
                    "Repository path is empty. Cannot perform Git operations.")

            # create repo object
            local_repo_path = self.get_repo_path_for_tenant(
                str(tenant_id), local_repo_path, is_multitenant)
            repo_info = Repository(repo_url, repo_username, repo_password,
                                   local_repo_path, tenant_id, commit_enabled)

            # checkout code
            subscribe_run, updated = AgentGitHandler.checkout(repo_info)

            # execute artifact updated extension
            plugin_values = {
                "ARTIFACT_UPDATED_CLUSTER_ID":
                artifacts_updated_event.cluster_id,
                "ARTIFACT_UPDATED_TENANT_ID":
                artifacts_updated_event.tenant_id,
                "ARTIFACT_UPDATED_REPO_URL": artifacts_updated_event.repo_url,
                "ARTIFACT_UPDATED_REPO_PASSWORD":
                artifacts_updated_event.repo_password,
                "ARTIFACT_UPDATED_REPO_USERNAME":
                artifacts_updated_event.repo_username,
                "ARTIFACT_UPDATED_STATUS": artifacts_updated_event.status
            }

            try:
                self.execute_event_extendables(
                    constants.ARTIFACT_UPDATED_EVENT, plugin_values)
            except ValueError:
                self.__log.exception(
                    "Could not execute plugins for artifact updated event.")

            if subscribe_run:
                # publish instanceActivated
                publisher.publish_instance_activated_event(
                    Config.health_stat_plugin)
            elif updated:
                # updated on pull
                self.on_artifact_update_scheduler_event(tenant_id)

            update_artifacts = Config.read_property(
                constants.ENABLE_ARTIFACT_UPDATE, False)
            auto_commit = Config.is_commits_enabled
            auto_checkout = Config.is_checkout_enabled
            self.__log.info(
                "ADC configuration: [update_artifacts] %s, [auto-commit] %s, [auto-checkout] %s",
                update_artifacts, auto_commit, auto_checkout)
            if update_artifacts:
                try:
                    update_interval = int(Config.artifact_update_interval)
                except ValueError:
                    self.__log.exception(
                        "Invalid artifact sync interval specified.")
                    update_interval = 10

                self.__log.info(
                    "Artifact updating task enabled, update interval: %s seconds"
                    % update_interval)

                self.__log.info("Auto Commit is turned %s " %
                                ("on" if auto_commit else "off"))
                self.__log.info("Auto Checkout is turned %s " %
                                ("on" if auto_checkout else "off"))

                AgentGitHandler.schedule_artifact_update_task(
                    repo_info, auto_checkout, auto_commit, update_interval)
Beispiel #5
0
    def run(self):
        self.__log.info("Starting Cartridge Agent...")

        # Start topology event receiver thread
        self.register_topology_event_listeners()

        if Config.lvs_virtual_ip is None or str(
                Config.lvs_virtual_ip).strip() == "":
            self.__log.debug("LVS Virtual IP is not defined")
        else:
            self.__event_handler.create_dummy_interface()

        # wait until complete topology message is received to get LB IP
        self.wait_for_complete_topology()

        # wait for member initialized event
        while not Config.initialized:
            self.__log.debug(
                "Waiting for cartridge agent to be initialized...")
            time.sleep(1)

        # Start instance notifier listener thread
        self.register_instance_topic_listeners()

        # Start tenant event receiver thread
        self.register_tenant_event_listeners()

        # start application signup event listener
        self.register_application_signup_event_listeners()

        # Execute instance started shell script
        self.__event_handler.on_instance_started_event()

        # Publish instance started event
        publisher.publish_instance_started_event()

        # Execute start servers extension
        try:
            self.__event_handler.start_server_extension()
        except Exception as e:
            self.__log.exception("Error processing start servers event: %s" %
                                 e)

        # check if artifact management is required before publishing instance activated event
        repo_url = Config.repo_url
        if repo_url is None or str(repo_url).strip() == "":
            self.__log.info("No artifact repository found")
            publisher.publish_instance_activated_event()
            self.__event_handler.on_instance_activated_event()
        else:
            # instance activated event will be published in artifact updated event handler
            self.__log.info(
                "Artifact repository found, waiting for artifact updated event to checkout artifacts: [repo_url] %s",
                repo_url)

        persistence_mapping_payload = Config.persistence_mappings
        if persistence_mapping_payload is not None:
            self.__event_handler.volume_mount_extension(
                persistence_mapping_payload)

        # start log publishing thread
        if DataPublisherConfiguration.get_instance().enabled:
            log_file_paths = Config.log_file_paths
            if log_file_paths is None:
                self.__log.exception(
                    "No valid log file paths found, no logs will be published")
            else:
                self.__log.debug(
                    "Starting Log Publisher Manager: [Log file paths] %s" %
                    ", ".join(log_file_paths))
                self.__log_publish_manager = LogPublisherManager(
                    log_file_paths)
                self.__log_publish_manager.start()

        # run until terminated
        while not self.__terminated:
            time.sleep(5)

        if DataPublisherConfiguration.get_instance().enabled:
            self.__log_publish_manager.terminate_all_publishers()
def on_artifact_updated_event(artifacts_updated_event):
    log.debug(
        "Processing artifact updated event for [tenant] %s [cluster] %s [status] %s"
        % (str(artifacts_updated_event.tenant_id), artifacts_updated_event.cluster_id, artifacts_updated_event.status))

    cluster_id_event = str(artifacts_updated_event.cluster_id).strip()
    cluster_id_payload = Config.cluster_id
    repo_url = str(artifacts_updated_event.repo_url).strip()

    if repo_url == "":
        log.error("Repository URL is empty. Failed to process artifact updated event.")
        return

    if cluster_id_payload is None or cluster_id_payload == "":
        log.error("Cluster ID in payload is empty. Failed to process artifact updated event.")
        return

    if cluster_id_payload != cluster_id_event:
        log.debug("Cluster ID in artifact updated event does not match. Skipping event handler.")
        return

    repo_password = None
    if artifacts_updated_event.repo_password is not None:
        secret = Config.cartridge_key
        repo_password = cartridgeagentutils.decrypt_password(artifacts_updated_event.repo_password, secret)

    if Config.app_path is None:
        log.error("Repository path is empty. Failed to process artifact updated event.")
        return

    if not validate_repo_path(Config.app_path):
        log.error(
            "Repository path cannot be accessed, or is invalid. Failed to process artifact updated event. [App Path] %s"
            % Config.app_path)

        return

    repo_username = artifacts_updated_event.repo_username
    tenant_id = artifacts_updated_event.tenant_id
    is_multitenant = Config.is_multiTenant
    commit_enabled = artifacts_updated_event.commit_enabled

    # create repo object
    local_repo_path = get_repo_path_for_tenant(str(tenant_id), Config.app_path, is_multitenant)
    repo_info = Repository(repo_url, repo_username, repo_password, local_repo_path, tenant_id, commit_enabled)
    log.info("Executing checkout job on artifact updated event...")

    try:
        Config.artifact_checkout_plugin.plugin_object.checkout(repo_info)
    except Exception as e:
        log.exception(
            "Checkout job on artifact updated event failed for tenant: %s %s" % (repo_info.tenant_id, e))

    # execute artifact updated extension
    plugin_values = {"ARTIFACT_UPDATED_CLUSTER_ID": artifacts_updated_event.cluster_id,
                     "ARTIFACT_UPDATED_TENANT_ID": artifacts_updated_event.tenant_id,
                     "ARTIFACT_UPDATED_REPO_URL": artifacts_updated_event.repo_url,
                     "ARTIFACT_UPDATED_REPO_PASSWORD": artifacts_updated_event.repo_password,
                     "ARTIFACT_UPDATED_REPO_USERNAME": artifacts_updated_event.repo_username,
                     "ARTIFACT_UPDATED_STATUS": artifacts_updated_event.status}

    try:
        execute_event_extendables(constants.ARTIFACT_UPDATED_EVENT, plugin_values)
    except Exception as e:
        log.exception("Could not execute plugins for artifact updated event: %s" % e)

    if not Config.activated:
        # publish instance activated event if not yet activated
        publisher.publish_instance_activated_event()
        on_instance_activated_event()

    update_artifacts = Config.read_property(constants.ENABLE_ARTIFACT_UPDATE, True)
    auto_commit = Config.is_commits_enabled
    auto_checkout = Config.is_checkout_enabled
    log.info("ADC configuration: [update_artifacts] %s, [auto-commit] %s, [auto-checkout] %s"
             % (update_artifacts, auto_commit, auto_checkout))

    if update_artifacts:
        try:
            update_interval = int(Config.artifact_update_interval)
        except ValueError:
            log.exception("Invalid artifact sync interval specified: %s" % ValueError)
            update_interval = 10

        log.info("Artifact updating task enabled, update interval: %s seconds" % update_interval)

        log.info("Auto Commit is turned %s " % ("on" if auto_commit else "off"))
        log.info("Auto Checkout is turned %s " % ("on" if auto_checkout else "off"))

        AgentGitHandler.schedule_artifact_update_task(
            repo_info,
            auto_checkout,
            auto_commit,
            update_interval)