# -*- coding: utf-8 -*-
"""
Storage 升级任务
"""
import utils.common.log as logger
from utils.common.exception import HCCIException
from utils.common.fic_base import StepBaseInterface
from plugins.DistributedStorage.Upgrade.scripts.impl.TC_Upgrade_Pkg import UpgradePkg
from plugins.DistributedStorage.common.ParameterGain import ParamsGain
from utils.common.message import Message


class PkgUpgrade(StepBaseInterface):
    """
    Packege Upgrade class
    """
    def __init__(self, project_id, pod_id, regionid_list, suit_id=None):
        super(PkgUpgrade, self).__init__(project_id, pod_id, regionid_list)
        self.fs_args = ParamsGain(project_id, pod_id,
                                  regionid_list).get_args(suit_id)
        self.fs_args["upgrade_type"] = "upgrade"

    def execute(self, project_id, pod_id, regionid_list=None, suit_id=None):
        """
        Perform an upgrade task
        """
        logger.info('Start packege upgrade task.')
        try:
            UpgradePkg(project_id, pod_id, self.fs_args).run()
        except HCCIException as e:
            logger.error('Upgrade pkg failed.')
            return Message(500, e)
        except Exception as e:
            logger.error('Upgrade pkg failed.')
            return Message(500, HCCIException(620009, str(e)))
        return Message(200)

    def retry(self, project_id, pod_id, regionid_list=None, suit_id=None):
        """
        标准调用接口：重试
        :return: Message对象
        """
        logger.info('Start packege upgrade retry task.')
        try:
            UpgradePkg(project_id, pod_id, self.fs_args).retry()
        except HCCIException as e:
            logger.error('Failed to upload the package again.')
            return Message(500, e)
        except Exception as e:
            logger.error('Failed to upload the package again.')
            return Message(500, HCCIException(620009, str(e)))
        return Message(200)
