|
| 1 | +# Copyright 2023 Red Hat. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | + |
| 16 | +import copy |
| 17 | +import datetime |
| 18 | +import logging |
| 19 | + |
| 20 | +from osc_lib.command import command |
| 21 | +from osc_lib import exceptions |
| 22 | +from osc_lib import utils |
| 23 | + |
| 24 | +from openstackclient.i18n import _ |
| 25 | + |
| 26 | + |
| 27 | +LOG = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +def _format_image_cache(cached_images): |
| 31 | + """Format image cache to make it more consistent with OSC operations.""" |
| 32 | + |
| 33 | + image_list = [] |
| 34 | + for item in cached_images: |
| 35 | + if item == "cached_images": |
| 36 | + for image in cached_images[item]: |
| 37 | + image_obj = copy.deepcopy(image) |
| 38 | + image_obj['state'] = 'cached' |
| 39 | + image_obj[ |
| 40 | + 'last_accessed' |
| 41 | + ] = datetime.datetime.utcfromtimestamp( |
| 42 | + image['last_accessed'] |
| 43 | + ).isoformat() |
| 44 | + image_obj[ |
| 45 | + 'last_modified' |
| 46 | + ] = datetime.datetime.utcfromtimestamp( |
| 47 | + image['last_modified'] |
| 48 | + ).isoformat() |
| 49 | + image_list.append(image_obj) |
| 50 | + elif item == "queued_images": |
| 51 | + for image in cached_images[item]: |
| 52 | + image = {'image_id': image} |
| 53 | + image.update( |
| 54 | + { |
| 55 | + 'state': 'queued', |
| 56 | + 'last_accessed': 'N/A', |
| 57 | + 'last_modified': 'N/A', |
| 58 | + 'size': 'N/A', |
| 59 | + 'hits': 'N/A', |
| 60 | + } |
| 61 | + ) |
| 62 | + image_list.append(image) |
| 63 | + return image_list |
| 64 | + |
| 65 | + |
| 66 | +class ListCachedImage(command.Lister): |
| 67 | + _description = _("Get Cache State") |
| 68 | + |
| 69 | + def get_parser(self, prog_name): |
| 70 | + parser = super().get_parser(prog_name) |
| 71 | + return parser |
| 72 | + |
| 73 | + def take_action(self, parsed_args): |
| 74 | + image_client = self.app.client_manager.image |
| 75 | + |
| 76 | + # List of Cache data received |
| 77 | + data = _format_image_cache(dict(image_client.get_image_cache())) |
| 78 | + columns = [ |
| 79 | + 'image_id', |
| 80 | + 'state', |
| 81 | + 'last_accessed', |
| 82 | + 'last_modified', |
| 83 | + 'size', |
| 84 | + 'hits', |
| 85 | + ] |
| 86 | + column_headers = [ |
| 87 | + "ID", |
| 88 | + "State", |
| 89 | + "Last Accessed (UTC)", |
| 90 | + "Last Modified (UTC)", |
| 91 | + "Size", |
| 92 | + "Hits", |
| 93 | + ] |
| 94 | + |
| 95 | + return ( |
| 96 | + column_headers, |
| 97 | + ( |
| 98 | + utils.get_dict_properties( |
| 99 | + image, |
| 100 | + columns, |
| 101 | + ) |
| 102 | + for image in data |
| 103 | + ), |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +class QueueCachedImage(command.Command): |
| 108 | + _description = _("Queue image(s) for caching.") |
| 109 | + |
| 110 | + def get_parser(self, prog_name): |
| 111 | + parser = super().get_parser(prog_name) |
| 112 | + parser.add_argument( |
| 113 | + "images", |
| 114 | + metavar="<image>", |
| 115 | + nargs="+", |
| 116 | + help=_("Image to display (name or ID)"), |
| 117 | + ) |
| 118 | + return parser |
| 119 | + |
| 120 | + def take_action(self, parsed_args): |
| 121 | + image_client = self.app.client_manager.image |
| 122 | + |
| 123 | + failures = 0 |
| 124 | + for image in parsed_args.images: |
| 125 | + try: |
| 126 | + image_obj = image_client.find_image( |
| 127 | + image, |
| 128 | + ignore_missing=False, |
| 129 | + ) |
| 130 | + image_client.queue_image(image_obj.id) |
| 131 | + except Exception as e: |
| 132 | + failures += 1 |
| 133 | + msg = _( |
| 134 | + "Failed to queue image with name or " |
| 135 | + "ID '%(image)s': %(e)s" |
| 136 | + ) |
| 137 | + LOG.error(msg, {'image': image, 'e': e}) |
| 138 | + |
| 139 | + if failures > 0: |
| 140 | + total = len(parsed_args.images) |
| 141 | + msg = _("Failed to queue %(failures)s of %(total)s images") % { |
| 142 | + 'failures': failures, |
| 143 | + 'total': total, |
| 144 | + } |
| 145 | + raise exceptions.CommandError(msg) |
| 146 | + |
| 147 | + |
| 148 | +class DeleteCachedImage(command.Command): |
| 149 | + _description = _("Delete image(s) from cache") |
| 150 | + |
| 151 | + def get_parser(self, prog_name): |
| 152 | + parser = super().get_parser(prog_name) |
| 153 | + parser.add_argument( |
| 154 | + "images", |
| 155 | + metavar="<image>", |
| 156 | + nargs="+", |
| 157 | + help=_("Image(s) to delete (name or ID)"), |
| 158 | + ) |
| 159 | + return parser |
| 160 | + |
| 161 | + def take_action(self, parsed_args): |
| 162 | + failures = 0 |
| 163 | + image_client = self.app.client_manager.image |
| 164 | + for image in parsed_args.images: |
| 165 | + try: |
| 166 | + image_obj = image_client.find_image( |
| 167 | + image, |
| 168 | + ignore_missing=False, |
| 169 | + ) |
| 170 | + image_client.cache_delete_image(image_obj.id) |
| 171 | + except Exception as e: |
| 172 | + failures += 1 |
| 173 | + msg = _( |
| 174 | + "Failed to delete image with name or " |
| 175 | + "ID '%(image)s': %(e)s" |
| 176 | + ) |
| 177 | + LOG.error(msg, {'image': image, 'e': e}) |
| 178 | + |
| 179 | + if failures > 0: |
| 180 | + total = len(parsed_args.images) |
| 181 | + msg = _("Failed to delete %(failures)s of %(total)s images.") % { |
| 182 | + 'failures': failures, |
| 183 | + 'total': total, |
| 184 | + } |
| 185 | + raise exceptions.CommandError(msg) |
| 186 | + |
| 187 | + |
| 188 | +class ClearCachedImage(command.Command): |
| 189 | + _description = _("Clear all images from cache, queue or both") |
| 190 | + |
| 191 | + def get_parser(self, prog_name): |
| 192 | + parser = super().get_parser(prog_name) |
| 193 | + parser.add_argument( |
| 194 | + "--cache", |
| 195 | + action="store_const", |
| 196 | + const="cache", |
| 197 | + dest="target", |
| 198 | + help=_("Clears all the cached images"), |
| 199 | + ) |
| 200 | + parser.add_argument( |
| 201 | + "--queue", |
| 202 | + action="store_const", |
| 203 | + const="queue", |
| 204 | + dest="target", |
| 205 | + help=_("Clears all the queued images"), |
| 206 | + ) |
| 207 | + return parser |
| 208 | + |
| 209 | + def take_action(self, parsed_args): |
| 210 | + image_client = self.app.client_manager.image |
| 211 | + |
| 212 | + target = parsed_args.target |
| 213 | + try: |
| 214 | + image_client.clear_cache(target) |
| 215 | + except Exception: |
| 216 | + msg = _("Failed to clear image cache") |
| 217 | + LOG.error(msg) |
| 218 | + raise exceptions.CommandError(msg) |
0 commit comments