from parallels.core.logging import get_logger
from parallels.core.utils.common import safe_string_repr
from parallels.plesk.source.web import messages
from parallels.plesk.source.web.actions.base.site_migration import SiteMigrationAction
from parallels.plesk.source.web.utils.status_storage import StatusStorage

logger = get_logger(__name__)


class DeployWebRPCAgent(SiteMigrationAction):
    """Deploy web RPC agent to the site and check connection to it"""

    def get_description(self):
        """Get short description of action as string

        :rtype: str | unicode
        """
        return messages.ACTION_DEPLOY_WEB_RPC_AGENT_DESCRIPTION

    def get_failure_message(self, global_context):
        """Get message for situation when action failed

        :type global_context: parallels.core.global_context.GlobalMigrationContext
        :rtype: str | unicode
        """
        return messages.ACTION_DEPLOY_WEB_RPC_AGENT_FAILURE

    def run(self, global_context):
        """
        :type global_context: parallels.plesk.source.web.global_context.WebGlobalMigrationContext
        """
        try:
            source = global_context.conn.get_source()
            source.connect_by_web_rpc_agent()
        except Exception as e:
            global_context.status_storage.set_operation_failure(
                StatusStorage.OPERATION_SCAN_CONTENT,
                safe_string_repr(e)
            )
            raise
