# -*- coding: utf-8 -*-
"""
Storage 上传软件包
"""
import utils.common.log as logger
from utils.common.exception import HCCIException
from utils.common.fic_base import StepBaseInterface
from utils.common.message import Message
from plugins.DistributedStorage.Upgrade.scripts.impl.TC_Update_UVP_Pkg import \
    UpdateUVPPkg
import traceback


class UVPPkgUpdate(StepBaseInterface):
    """
    upgrade class
    """
    def __init__(self, project_id, pod_id, regionid_list, suit_id=None):
        super(UVPPkgUpdate, self).__init__(project_id, pod_id, regionid_list)
        self.fs_args = dict()
        self.fs_args["pkg_regular"] = "^OceanStor-Pacific_8.[1-9].([0-9._]|SPH|SPC|RC|HP)+UVP.tar.gz$"
        if suit_id == 'suit01':
            self.fs_args["upload_uvp"] = True
        else:
            self.fs_args["upload_uvp"] = False
            logger.info("current suit %s" % suit_id)

    def execute(self, project_id, pod_id, regionid_list=None, suit_id=None):
        """
        Perform an upgrade task
        """
        logger.info('Start distribute package.')
        try:
            logger.info('try Start update UVP package.')
            UpdateUVPPkg(project_id, pod_id, self.fs_args).procedure()
        except HCCIException as e:
            logger.error('update UVP package %s.' % (str(e)))
            return Message(500, e)
        except Exception as e:
            exc = traceback.format_exc()
            logger.error('update UPV package.%s' % (str(exc)))
            return Message(500, HCCIException(620006, str(e)))
        logger.info('Start distribute package.')
        return Message(200)

    def retry(self, project_id, pod_id, regionid_list=None, suit_id=None):
        """
        标准调用接口：重试
        :return: Message对象
        """
        return self.execute(project_id, pod_id, regionid_list, suit_id)
