|
| 1 | +# Copyright 2018 Red Hat, Inc. |
| 2 | +# Copyright 2022 OVHCloud |
| 3 | +# All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +import abc |
| 18 | + |
| 19 | +import glance_store as store_api |
| 20 | +from glance_store import backend |
| 21 | +from oslo_config import cfg |
| 22 | +from oslo_log import log as logging |
| 23 | +import six |
| 24 | +from taskflow import task |
| 25 | + |
| 26 | +from glance.common import exception |
| 27 | +from glance.i18n import _, _LE |
| 28 | + |
| 29 | +LOG = logging.getLogger(__name__) |
| 30 | + |
| 31 | +CONF = cfg.CONF |
| 32 | + |
| 33 | + |
| 34 | +@six.add_metaclass(abc.ABCMeta) |
| 35 | +class BaseDownload(task.Task): |
| 36 | + |
| 37 | + default_provides = 'file_uri' |
| 38 | + |
| 39 | + def __init__(self, task_id, task_type, action_wrapper, stores, |
| 40 | + plugin_name): |
| 41 | + self.task_id = task_id |
| 42 | + self.task_type = task_type |
| 43 | + self.image_id = action_wrapper.image_id |
| 44 | + self.action_wrapper = action_wrapper |
| 45 | + self.stores = stores |
| 46 | + self._path = None |
| 47 | + self.plugin_name = plugin_name or 'Download' |
| 48 | + super(BaseDownload, self).__init__( |
| 49 | + name='%s-%s-%s' % (task_type, self.plugin_name, task_id)) |
| 50 | + |
| 51 | + # NOTE(abhishekk): Use reserved 'os_glance_staging_store' for |
| 52 | + # staging the data, the else part will be removed once old way |
| 53 | + # of configuring store is deprecated. |
| 54 | + if CONF.enabled_backends: |
| 55 | + self.store = store_api.get_store_from_store_identifier( |
| 56 | + 'os_glance_staging_store') |
| 57 | + else: |
| 58 | + if CONF.node_staging_uri is None: |
| 59 | + msg = (_("%(task_id)s of %(task_type)s not configured " |
| 60 | + "properly. Missing node_staging_uri: %(work_dir)s") % |
| 61 | + {'task_id': self.task_id, |
| 62 | + 'task_type': self.task_type, |
| 63 | + 'work_dir': CONF.node_staging_uri}) |
| 64 | + raise exception.BadTaskConfiguration(msg) |
| 65 | + |
| 66 | + self.store = self._build_store() |
| 67 | + |
| 68 | + def _build_store(self): |
| 69 | + # NOTE(flaper87): Due to the nice glance_store api (#sarcasm), we're |
| 70 | + # forced to build our own config object, register the required options |
| 71 | + # (and by required I mean *ALL* of them, even the ones we don't want), |
| 72 | + # and create our own store instance by calling a private function. |
| 73 | + # This is certainly unfortunate but it's the best we can do until the |
| 74 | + # glance_store refactor is done. A good thing is that glance_store is |
| 75 | + # under our team's management and it gates on Glance so changes to |
| 76 | + # this API will (should?) break task's tests. |
| 77 | + # TODO(abhishekk): After removal of backend module from glance_store |
| 78 | + # need to change this to use multi_backend module. |
| 79 | + conf = cfg.ConfigOpts() |
| 80 | + try: |
| 81 | + backend.register_opts(conf) |
| 82 | + except cfg.DuplicateOptError: |
| 83 | + pass |
| 84 | + |
| 85 | + conf.set_override('filesystem_store_datadir', |
| 86 | + CONF.node_staging_uri[7:], |
| 87 | + group='glance_store') |
| 88 | + |
| 89 | + # NOTE(flaper87): Do not even try to judge me for this... :( |
| 90 | + # With the glance_store refactor, this code will change, until |
| 91 | + # that happens, we don't have a better option and this is the |
| 92 | + # least worst one, IMHO. |
| 93 | + store = store_api.backend._load_store(conf, 'file') |
| 94 | + |
| 95 | + if store is None: |
| 96 | + msg = (_("%(task_id)s of %(task_type)s not configured " |
| 97 | + "properly. Could not load the filesystem store") % |
| 98 | + {'task_id': self.task_id, 'task_type': self.task_type}) |
| 99 | + raise exception.BadTaskConfiguration(msg) |
| 100 | + |
| 101 | + store.configure() |
| 102 | + return store |
| 103 | + |
| 104 | + def revert(self, result, **kwargs): |
| 105 | + LOG.error(_LE('Task: %(task_id)s failed to import image ' |
| 106 | + '%(image_id)s to the filesystem.'), |
| 107 | + {'task_id': self.task_id, |
| 108 | + 'image_id': self.image_id}) |
| 109 | + # NOTE(abhishekk): Revert image state back to 'queued' as |
| 110 | + # something went wrong. |
| 111 | + # NOTE(danms): If we failed to stage the image, then none |
| 112 | + # of the _ImportToStore() tasks could have run, so we need |
| 113 | + # to move all stores out of "importing" and into "failed". |
| 114 | + with self.action_wrapper as action: |
| 115 | + action.set_image_attribute(status='queued') |
| 116 | + action.remove_importing_stores(self.stores) |
| 117 | + action.add_failed_stores(self.stores) |
| 118 | + |
| 119 | + # NOTE(abhishekk): Deleting partial image data from staging area |
| 120 | + if self._path is not None: |
| 121 | + LOG.debug(('Deleting image %(image_id)s from staging ' |
| 122 | + 'area.'), {'image_id': self.image_id}) |
| 123 | + try: |
| 124 | + if CONF.enabled_backends: |
| 125 | + store_api.delete(self._path, None) |
| 126 | + else: |
| 127 | + store_api.delete_from_backend(self._path) |
| 128 | + except Exception: |
| 129 | + LOG.exception(_LE("Error reverting web/glance download " |
| 130 | + "task: %(task_id)s"), { |
| 131 | + 'task_id': self.task_id}) |
0 commit comments