
"""
This is (cold) merge data operation job.
This job performs the following steps:
1. Prepare all volumes in chain
2. Executes qemuimg commit
3. Tears down the image
"""

from __future__ import absolute_import

import logging

from vdsm.storage import constants as sc
from vdsm.storage import guarded
from vdsm.storage import qemuimg

from . import base


class Job(base.Job):
    log = logging.getLogger('storage.sdm.merge')

    def __init__(self, job_id, subchain):
        super(Job, self).__init__(job_id, 'merge_subchain',
                                  subchain.host_id)
        self.subchain = subchain
        self.operation = None

    @property
    def progress(self):
        return getattr(self.operation, 'progress', None)

    def _run(self):
        self.log.info("Merging subchain %s", self.subchain)
        with guarded.context(self.subchain.locks):
            self.subchain.validate()
            with self.subchain.prepare(), self.subchain.volume_operation():
                self.operation = qemuimg.commit(
                    self.subchain.top_vol.getVolumePath(),
                    topFormat=sc.fmt2str(self.subchain.top_vol.getFormat()),
                    base=self.subchain.base_vol.getVolumePath())
                self.operation.run()
