| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import json |
| 17 |
import os |
| 18 |
import sys |
| 19 |
|
| 20 |
from oslo_utils import strutils |
| 21 |
|
| 22 |
from glanceclient._i18n import _ |
| 23 |
from glanceclient.common import progressbar |
| 24 |
from glanceclient.common import utils |
| 25 |
from glanceclient import exc |
| 26 |
from glanceclient.v2 import cache |
| 27 |
from glanceclient.v2 import image_members |
| 28 |
from glanceclient.v2 import image_schema |
| 29 |
from glanceclient.v2 import images |
| 30 |
from glanceclient.v2 import namespace_schema |
| 31 |
from glanceclient.v2 import resource_type_schema |
| 32 |
from glanceclient.v2 import tasks |
| 33 |
|
| 34 |
MEMBER_STATUS_VALUES = image_members.MEMBER_STATUS_VALUES |
| 35 |
IMAGE_SCHEMA = None |
| 36 |
DATA_FIELDS = ('location', 'copy_from', 'file', 'uri') |
| 37 |
|
| 38 |
|
| 39 |
def get_image_schema(): |
| 40 |
global IMAGE_SCHEMA |
| 41 |
if IMAGE_SCHEMA is None: |
| 42 |
schema_path = os.path.expanduser("~/.glanceclient/image_schema.json") |
| 43 |
if os.path.isfile(schema_path): |
| 44 |
with open(schema_path, "r") as f: |
| 45 |
schema_raw = f.read() |
| 46 |
IMAGE_SCHEMA = json.loads(schema_raw) |
| 47 |
else: |
| 48 |
return image_schema._BASE_SCHEMA |
| 49 |
return IMAGE_SCHEMA |
| 50 |
|
| 51 |
|
| 52 |
def get_filesize(args, image_data): |
| 53 |
filesize = getattr(args, 'size', None) or utils.get_file_size(image_data) |
| 54 |
if getattr(args, 'size', None) and getattr(args, 'file', None): |
| 55 |
actual_size = utils.get_file_size(image_data) |
| 56 |
if actual_size != args.size: |
| 57 |
raise ValueError("Size mismatch: provided size %s does not match " |
| 58 |
"the size of the image %s" % ( |
| 59 |
args.size, actual_size)) |
| 60 |
return filesize |
| 61 |
|
| 62 |
|
| 63 |
@utils.schema_args(get_image_schema, omit=['locations', 'os_hidden']) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@utils.arg('--hidden', type=strutils.bool_from_string, metavar='[True|False]', |
| 69 |
default=None, |
| 70 |
dest='os_hidden', |
| 71 |
help=("If true, image will not appear in default image list " |
| 72 |
"response.")) |
| 73 |
@utils.arg('--property', metavar="<key=value>", action='append', |
| 74 |
default=[], help=_('Arbitrary property to associate with image.' |
| 75 |
' May be used multiple times.')) |
| 76 |
@utils.arg('--file', metavar='<FILE>', |
| 77 |
help=_('Local file that contains disk image to be uploaded ' |
| 78 |
'during creation. Alternatively, the image data can be ' |
| 79 |
'passed to the client via stdin.')) |
| 80 |
@utils.arg('--size', metavar='<IMAGE_SIZE>', type=int, |
| 81 |
help=_('Size in bytes of image to be uploaded. Default is to get ' |
| 82 |
'size from provided data object but this is supported in ' |
| 83 |
'case where size cannot be inferred.'), |
| 84 |
default=None) |
| 85 |
@utils.arg('--progress', action='store_true', default=False, |
| 86 |
help=_('Show upload progress bar.')) |
| 87 |
@utils.arg('--store', metavar='<STORE>', |
| 88 |
default=utils.env('OS_IMAGE_STORE', default=None), |
| 89 |
help='Backend store to upload image to.') |
| 90 |
@utils.on_data_require_fields(DATA_FIELDS) |
| 91 |
def do_image_create(gc, args): |
| 92 |
"""Create a new image.""" |
| 93 |
schema = gc.schemas.get("image") |
| 94 |
_args = [(x[0].replace('-', '_'), x[1]) for x in vars(args).items()] |
| 95 |
|
| 96 |
fields = dict(filter(lambda x: x[1] is not None and |
| 97 |
(x[0] == 'property' or |
| 98 |
schema.is_core_property(x[0])), |
| 99 |
_args)) |
| 100 |
|
| 101 |
raw_properties = fields.pop('property', []) |
| 102 |
for datum in raw_properties: |
| 103 |
key, value = datum.split('=', 1) |
| 104 |
fields[key] = value |
| 105 |
|
| 106 |
backend = args.store |
| 107 |
|
| 108 |
file_name = fields.pop('file', None) |
| 109 |
if 'size' in fields: |
| 110 |
utils.exit( |
| 111 |
"Setting 'size' during image creation is not supported. " |
| 112 |
"Please use --size only when uploading data.") |
| 113 |
|
| 114 |
using_stdin = hasattr(sys.stdin, 'isatty') and not sys.stdin.isatty() |
| 115 |
if args.store and not (file_name or using_stdin): |
| 116 |
utils.exit("--store option should only be provided with --file " |
| 117 |
"option or stdin.") |
| 118 |
|
| 119 |
if backend: |
| 120 |
|
| 121 |
_validate_backend(backend, gc) |
| 122 |
|
| 123 |
if file_name is not None and os.access(file_name, os.R_OK) is False: |
| 124 |
utils.exit("File %s does not exist or user does not have read " |
| 125 |
"privileges to it" % file_name) |
| 126 |
image = gc.images.create(**fields) |
| 127 |
try: |
| 128 |
if utils.get_data_file(args) is not None: |
| 129 |
backend = fields.get('store', None) |
| 130 |
args.id = image['id'] |
| 131 |
do_image_upload(gc, args) |
| 132 |
image = gc.images.get(args.id) |
| 133 |
finally: |
| 134 |
utils.print_image(image) |
| 135 |
|
| 136 |
|
| 137 |
@utils.schema_args(get_image_schema, omit=['locations', 'os_hidden']) |
| 138 |
|
| 139 |
@utils.arg('--hidden', type=strutils.bool_from_string, metavar='[True|False]', |
| 140 |
default=None, |
| 141 |
dest='os_hidden', |
| 142 |
help=("If true, image will not appear in default image list " |
| 143 |
"response.")) |
| 144 |
@utils.arg('--property', metavar="<key=value>", action='append', |
| 145 |
default=[], help=_('Arbitrary property to associate with image.' |
| 146 |
' May be used multiple times.')) |
| 147 |
@utils.arg('--file', metavar='<FILE>', |
| 148 |
help=_('Local file that contains disk image to be uploaded ' |
| 149 |
'during creation. Alternatively, the image data can be ' |
| 150 |
'passed to the client via stdin.')) |
| 151 |
@utils.arg('--size', metavar='<IMAGE_SIZE>', type=int, |
| 152 |
help=_('Size in bytes of image to be uploaded. Default is to get ' |
| 153 |
'size from provided data object but this is supported in ' |
| 154 |
'case where size cannot be inferred.'), |
| 155 |
default=None) |
| 156 |
@utils.arg('--progress', action='store_true', default=False, |
| 157 |
help=_('Show upload progress bar.')) |
| 158 |
@utils.arg('--import-method', metavar='<METHOD>', |
| 159 |
default=utils.env('OS_IMAGE_IMPORT_METHOD', default=None), |
| 160 |
help=_('Import method used for Image Import workflow. ' |
| 161 |
'Valid values can be retrieved with import-info command. ' |
| 162 |
'Defaults to env[OS_IMAGE_IMPORT_METHOD] or if that is ' |
| 163 |
'undefined uses \'glance-direct\' if data is provided using ' |
| 164 |
'--file or stdin. Otherwise, simply creates an image ' |
| 165 |
'record if no import-method and no data is supplied')) |
| 166 |
@utils.arg('--uri', metavar='<IMAGE_URL>', default=None, |
| 167 |
help=_('URI to download the external image.')) |
| 168 |
@utils.arg('--remote-region', metavar='<GLANCE_REGION>', default=None, |
| 169 |
help=_('REMOTE_GLANCE_REGION to download the image.')) |
| 170 |
@utils.arg('--remote-image-id', metavar='<REMOTE_IMAGE_ID>', default=None, |
| 171 |
help=_('The IMAGE ID of the image of remote glance, which needs' |
| 172 |
'to be imported with glance-download')) |
| 173 |
@utils.arg('--remote-service-interface', metavar='<REMOTE_SERVICE_INTERFACE>', |
| 174 |
default='public', |
| 175 |
help=_('The Remote Glance Service Interface for glance-download')) |
| 176 |
@utils.arg('--store', metavar='<STORE>', |
| 177 |
default=utils.env('OS_IMAGE_STORE', default=None), |
| 178 |
help='Backend store to upload image to.') |
| 179 |
@utils.arg('--stores', metavar='<STORES>', |
| 180 |
default=utils.env('OS_IMAGE_STORES', default=None), |
| 181 |
help=_('Stores to upload image to if multi-stores import ' |
| 182 |
'available. Comma separated list. Available stores can be ' |
| 183 |
'listed with "stores-info" call.')) |
| 184 |
@utils.arg('--all-stores', type=strutils.bool_from_string, |
| 185 |
metavar='[True|False]', |
| 186 |
default=None, |
| 187 |
dest='os_all_stores', |
| 188 |
help=_('"all-stores" can be ued instead of "stores"-list to ' |
| 189 |
'indicate that image should be imported into all available ' |
| 190 |
'stores.')) |
| 191 |
@utils.arg('--allow-failure', type=strutils.bool_from_string, |
| 192 |
metavar='[True|False]', |
| 193 |
dest='os_allow_failure', |
| 194 |
default=utils.env('OS_IMAGE_ALLOW_FAILURE', default=True), |
| 195 |
help=_('Indicator if all stores listed (or available) must ' |
| 196 |
'succeed. "True" by default meaning that we allow some ' |
| 197 |
'stores to fail and the status can be monitored from the ' |
| 198 |
'image metadata. If this is set to "False" the import will ' |
| 199 |
'be reverted should any of the uploads fail. Only usable ' |
| 200 |
'with "stores" or "all-stores".')) |
| 201 |
@utils.on_data_require_fields(DATA_FIELDS) |
| 202 |
def do_image_create_via_import(gc, args): |
| 203 |
"""EXPERIMENTAL: Create a new image via image import. |
| 204 |
|
| 205 |
Use the interoperable image import workflow to create an image. This |
| 206 |
command is designed to be backward compatible with the current image-create |
| 207 |
command, so its behavior is as follows: |
| 208 |
|
| 209 |
* If an import-method is specified (either on the command line or through |
| 210 |
the OS_IMAGE_IMPORT_METHOD environment variable, then you must provide a |
| 211 |
data source appropriate to that method (for example, --file for |
| 212 |
glance-direct, or --uri for web-download). |
| 213 |
* If no import-method is specified AND you provide either a --file or |
| 214 |
data to stdin, the command will assume you are using the 'glance-direct' |
| 215 |
import-method and will act accordingly. |
| 216 |
* If no import-method is specified and no data is supplied via --file or |
| 217 |
stdin, the command will simply create an image record in 'queued' status. |
| 218 |
""" |
| 219 |
schema = gc.schemas.get("image") |
| 220 |
_args = [(x[0].replace('-', '_'), x[1]) for x in vars(args).items()] |
| 221 |
|
| 222 |
fields = dict(filter(lambda x: x[1] is not None and |
| 223 |
(x[0] == 'property' or |
| 224 |
schema.is_core_property(x[0])), |
| 225 |
_args)) |
| 226 |
|
| 227 |
raw_properties = fields.pop('property', []) |
| 228 |
for datum in raw_properties: |
| 229 |
key, value = datum.split('=', 1) |
| 230 |
fields[key] = value |
| 231 |
|
| 232 |
file_name = fields.pop('file', None) |
| 233 |
if 'size' in fields: |
| 234 |
utils.exit( |
| 235 |
"Setting 'size' during image creation is not supported. " |
| 236 |
"Please use --size only when uploading data.") |
| 237 |
|
| 238 |
using_stdin = hasattr(sys.stdin, 'isatty') and not sys.stdin.isatty() |
| 239 |
|
| 240 |
|
| 241 |
if args.import_method is None and (file_name or using_stdin): |
| 242 |
args.import_method = 'glance-direct' |
| 243 |
|
| 244 |
if args.import_method == 'copy-image': |
| 245 |
utils.exit("Import method 'copy-image' cannot be used " |
| 246 |
"while creating the image.") |
| 247 |
|
| 248 |
|
| 249 |
import_methods = gc.images.get_import_info().get('import-methods') |
| 250 |
if args.import_method and args.import_method not in import_methods.get( |
| 251 |
'value'): |
| 252 |
utils.exit("Import method '%s' is not valid for this cloud. " |
| 253 |
"Valid values can be retrieved with import-info command." % |
| 254 |
args.import_method) |
| 255 |
|
| 256 |
|
| 257 |
backend = None |
| 258 |
stores = getattr(args, "stores", None) |
| 259 |
all_stores = getattr(args, "os_all_stores", None) |
| 260 |
|
| 261 |
if (args.store and (stores or all_stores)) or (stores and all_stores): |
| 262 |
utils.exit("Only one of --store, --stores and --all-stores can be " |
| 263 |
"provided") |
| 264 |
elif args.store: |
| 265 |
backend = args.store |
| 266 |
|
| 267 |
_validate_backend(backend, gc) |
| 268 |
elif stores: |
| 269 |
|
| 270 |
|
| 271 |
fields.pop("stores") |
| 272 |
stores = str(stores).split(',') |
| 273 |
for store in stores: |
| 274 |
|
| 275 |
_validate_backend(store, gc) |
| 276 |
|
| 277 |
|
| 278 |
if args.import_method is None: |
| 279 |
if args.uri: |
| 280 |
utils.exit("You cannot use --uri without specifying an import " |
| 281 |
"method.") |
| 282 |
if args.import_method == 'glance-direct': |
| 283 |
if backend and not (file_name or using_stdin): |
| 284 |
utils.exit("--store option should only be provided with --file " |
| 285 |
"option or stdin for the glance-direct import method.") |
| 286 |
if stores and not (file_name or using_stdin): |
| 287 |
utils.exit("--stores option should only be provided with --file " |
| 288 |
"option or stdin for the glance-direct import method.") |
| 289 |
if all_stores and not (file_name or using_stdin): |
| 290 |
utils.exit("--all-stores option should only be provided with " |
| 291 |
"--file option or stdin for the glance-direct import " |
| 292 |
"method.") |
| 293 |
|
| 294 |
if args.uri: |
| 295 |
utils.exit("You cannot specify a --uri with the glance-direct " |
| 296 |
"import method.") |
| 297 |
if file_name is not None and os.access(file_name, os.R_OK) is False: |
| 298 |
utils.exit("File %s does not exist or user does not have read " |
| 299 |
"privileges to it." % file_name) |
| 300 |
if file_name is not None and using_stdin: |
| 301 |
utils.exit("You cannot use both --file and stdin with the " |
| 302 |
"glance-direct import method.") |
| 303 |
if not file_name and not using_stdin: |
| 304 |
utils.exit("You must specify a --file or provide data via stdin " |
| 305 |
"for the glance-direct import method.") |
| 306 |
if args.import_method == 'web-download': |
| 307 |
if backend and not args.uri: |
| 308 |
utils.exit("--store option should only be provided with --uri " |
| 309 |
"option for the web-download import method.") |
| 310 |
if stores and not args.uri: |
| 311 |
utils.exit("--stores option should only be provided with --uri " |
| 312 |
"option for the web-download import method.") |
| 313 |
if all_stores and not args.uri: |
| 314 |
utils.exit("--all-stores option should only be provided with " |
| 315 |
"--uri option for the web-download import method.") |
| 316 |
if not args.uri: |
| 317 |
utils.exit("URI is required for web-download import method. " |
| 318 |
"Please use '--uri <uri>'.") |
| 319 |
if file_name: |
| 320 |
utils.exit("You cannot specify a --file with the web-download " |
| 321 |
"import method.") |
| 322 |
if using_stdin: |
| 323 |
utils.exit("You cannot pass data via stdin with the web-download " |
| 324 |
"import method.") |
| 325 |
|
| 326 |
if args.import_method == 'glance-download': |
| 327 |
if not (args.remote_region and args.remote_image_id): |
| 328 |
utils.exit("REMOTE GlANCE REGION and REMOTE IMAGE ID are " |
| 329 |
"required for glance-download import method. " |
| 330 |
"Please use --remote-region <region> and " |
| 331 |
"--remote-image-id <remote-image-id>.") |
| 332 |
if args.uri: |
| 333 |
utils.exit("You cannot specify a --uri with the glance-download " |
| 334 |
"import method.") |
| 335 |
if file_name: |
| 336 |
utils.exit("You cannot specify a --file with the glance-download " |
| 337 |
"import method.") |
| 338 |
if using_stdin: |
| 339 |
utils.exit("You cannot pass data via stdin with the " |
| 340 |
"glance-download import method.") |
| 341 |
|
| 342 |
|
| 343 |
image = gc.images.create(**fields) |
| 344 |
try: |
| 345 |
args.id = image['id'] |
| 346 |
if args.import_method: |
| 347 |
if utils.get_data_file(args) is not None: |
| 348 |
do_image_stage(gc, args) |
| 349 |
args.from_create = True |
| 350 |
args.stores = stores |
| 351 |
do_image_import(gc, args) |
| 352 |
image = gc.images.get(args.id) |
| 353 |
finally: |
| 354 |
utils.print_image(image) |
| 355 |
|
| 356 |
|
| 357 |
def _validate_backend(backend, gc): |
| 358 |
try: |
| 359 |
enabled_backends = gc.images.get_stores_info().get('stores') |
| 360 |
except exc.HTTPNotFound: |
| 361 |
|
| 362 |
return |
| 363 |
|
| 364 |
if backend: |
| 365 |
valid_backend = False |
| 366 |
for available_backend in enabled_backends: |
| 367 |
if available_backend['id'] == backend: |
| 368 |
valid_backend = True |
| 369 |
break |
| 370 |
|
| 371 |
if not valid_backend: |
| 372 |
utils.exit("Store '%s' is not valid for this cloud. Valid " |
| 373 |
"values can be retrieved with stores-info command." % |
| 374 |
backend) |
| 375 |
|
| 376 |
|
| 377 |
@utils.arg('id', metavar='<IMAGE_ID>', help=_('ID of image to update.')) |
| 378 |
@utils.schema_args(get_image_schema, omit=['id', 'locations', 'tags', |
| 379 |
'os_hidden']) |
| 380 |
|
| 381 |
@utils.arg('--hidden', type=strutils.bool_from_string, metavar='[True|False]', |
| 382 |
default=None, |
| 383 |
dest='os_hidden', |
| 384 |
help=("If true, image will not appear in default image list " |
| 385 |
"response.")) |
| 386 |
@utils.arg('--property', metavar="<key=value>", action='append', |
| 387 |
default=[], help=_('Arbitrary property to associate with image.' |
| 388 |
' May be used multiple times.')) |
| 389 |
@utils.arg('--remove-property', metavar="key", action='append', default=[], |
| 390 |
help=_("Name of arbitrary property to remove from the image.")) |
| 391 |
def do_image_update(gc, args): |
| 392 |
"""Update an existing image.""" |
| 393 |
schema = gc.schemas.get("image") |
| 394 |
_args = [(x[0].replace('-', '_'), x[1]) for x in vars(args).items()] |
| 395 |
|
| 396 |
fields = dict(filter(lambda x: x[1] is not None and |
| 397 |
(x[0] in ['property', 'remove_property'] or |
| 398 |
schema.is_core_property(x[0])), |
| 399 |
_args)) |
| 400 |
|
| 401 |
raw_properties = fields.pop('property', []) |
| 402 |
for datum in raw_properties: |
| 403 |
key, value = datum.split('=', 1) |
| 404 |
fields[key] = value |
| 405 |
|
| 406 |
remove_properties = fields.pop('remove_property', None) |
| 407 |
|
| 408 |
image_id = fields.pop('id') |
| 409 |
image = gc.images.update(image_id, remove_properties, **fields) |
| 410 |
utils.print_image(image) |
| 411 |
|
| 412 |
|
| 413 |
@utils.arg('--limit', metavar='<LIMIT>', default=None, type=int, |
| 414 |
help=_('Maximum number of images to get.')) |
| 415 |
@utils.arg('--page-size', metavar='<SIZE>', default=None, type=int, |
| 416 |
help=_('Number of images to request in each paginated request.')) |
| 417 |
@utils.arg('--visibility', metavar='<VISIBILITY>', |
| 418 |
help=_('The visibility of the images to display.')) |
| 419 |
@utils.arg('--member-status', metavar='<MEMBER_STATUS>', |
| 420 |
help=_('The status of images to display.')) |
| 421 |
@utils.arg('--owner', metavar='<OWNER>', |
| 422 |
help=_('Display images owned by <OWNER>.')) |
| 423 |
@utils.arg('--property-filter', metavar='<KEY=VALUE>', |
| 424 |
help=_("Filter images by a user-defined image property."), |
| 425 |
action='append', dest='properties', default=[]) |
| 426 |
@utils.arg('--checksum', metavar='<CHECKSUM>', |
| 427 |
help=_('Displays images that match the MD5 checksum.')) |
| 428 |
@utils.arg('--hash', dest='os_hash_value', default=None, |
| 429 |
metavar='<HASH_VALUE>', |
| 430 |
help=_('Displays images that match the specified hash value.')) |
| 431 |
@utils.arg('--tag', metavar='<TAG>', action='append', |
| 432 |
help=_("Filter images by a user-defined tag.")) |
| 433 |
@utils.arg('--sort-key', default=[], action='append', |
| 434 |
choices=images.SORT_KEY_VALUES, |
| 435 |
help=_('Sort image list by specified fields.' |
| 436 |
' May be used multiple times.')) |
| 437 |
@utils.arg('--sort-dir', default=[], action='append', |
| 438 |
choices=images.SORT_DIR_VALUES, |
| 439 |
help=_('Sort image list in specified directions.')) |
| 440 |
@utils.arg('--sort', metavar='<key>[:<direction>]', default=None, |
| 441 |
help=(_("Comma-separated list of sort keys and directions in the " |
| 442 |
"form of <key>[:<asc|desc>]. Valid keys: %s. OPTIONAL." |
| 443 |
) % ', '.join(images.SORT_KEY_VALUES))) |
| 444 |
@utils.arg('--hidden', |
| 445 |
dest='os_hidden', |
| 446 |
metavar='[True|False]', |
| 447 |
default=None, |
| 448 |
type=strutils.bool_from_string, |
| 449 |
const=True, |
| 450 |
nargs='?', |
| 451 |
help="Filters results by hidden status. Default=None.") |
| 452 |
@utils.arg('--include-stores', |
| 453 |
metavar='[True|False]', |
| 454 |
default=None, |
| 455 |
type=strutils.bool_from_string, |
| 456 |
const=True, |
| 457 |
nargs='?', |
| 458 |
help="Print backend store id.") |
| 459 |
def do_image_list(gc, args): |
| 460 |
"""List images you can access.""" |
| 461 |
filter_keys = ['visibility', 'member_status', 'owner', 'checksum', 'tag', |
| 462 |
'os_hidden', 'os_hash_value'] |
| 463 |
filter_items = [(key, getattr(args, key)) for key in filter_keys] |
| 464 |
|
| 465 |
if args.properties: |
| 466 |
filter_properties = [prop.split('=', 1) for prop in args.properties] |
| 467 |
if any(len(pair) != 2 for pair in filter_properties): |
| 468 |
utils.exit('Argument --property-filter expected properties in the' |
| 469 |
' format KEY=VALUE') |
| 470 |
filter_items += filter_properties |
| 471 |
filters = dict([item for item in filter_items if item[1] is not None]) |
| 472 |
|
| 473 |
kwargs = {'filters': filters} |
| 474 |
if args.limit is not None: |
| 475 |
kwargs['limit'] = args.limit |
| 476 |
if args.page_size is not None: |
| 477 |
kwargs['page_size'] = args.page_size |
| 478 |
|
| 479 |
if args.sort_key: |
| 480 |
kwargs['sort_key'] = args.sort_key |
| 481 |
if args.sort_dir: |
| 482 |
kwargs['sort_dir'] = args.sort_dir |
| 483 |
if args.sort is not None: |
| 484 |
kwargs['sort'] = args.sort |
| 485 |
elif not args.sort_dir and not args.sort_key: |
| 486 |
kwargs['sort_key'] = 'name' |
| 487 |
kwargs['sort_dir'] = 'asc' |
| 488 |
|
| 489 |
columns = ['ID', 'Name'] |
| 490 |
|
| 491 |
if args.verbose: |
| 492 |
columns += ['Disk_format', 'Container_format', 'Size', 'Status', |
| 493 |
'Owner'] |
| 494 |
|
| 495 |
if args.include_stores: |
| 496 |
columns += ['Stores'] |
| 497 |
|
| 498 |
images = gc.images.list(**kwargs) |
| 499 |
utils.print_list(images, columns) |
| 500 |
|
| 501 |
|
| 502 |
@utils.arg('id', metavar='<IMAGE_ID>', help=_('ID of image to describe.')) |
| 503 |
@utils.arg('--human-readable', action='store_true', default=False, |
| 504 |
help=_('Print image size in a human-friendly format.')) |
| 505 |
@utils.arg('--max-column-width', metavar='<integer>', default=80, |
| 506 |
help=_('The max column width of the printed table.')) |
| 507 |
def do_image_show(gc, args): |
| 508 |
"""Describe a specific image.""" |
| 509 |
image = gc.images.get(args.id) |
| 510 |
utils.print_image(image, args.human_readable, int(args.max_column_width)) |
| 511 |
|
| 512 |
|
| 513 |
@utils.arg('id', metavar='<IMAGE_ID>', help=_('ID of image to get tasks.')) |
| 514 |
def do_image_tasks(gc, args): |
| 515 |
"""Get tasks associated with image""" |
| 516 |
columns = ['Message', 'Status', 'Updated at'] |
| 517 |
if args.verbose: |
| 518 |
columns_to_prepend = ['Image Id', 'Task Id'] |
| 519 |
columns_to_extend = ['User Id', 'Request Id', |
| 520 |
'Result', 'Owner', 'Input', 'Expires at'] |
| 521 |
columns = columns_to_prepend + columns + columns_to_extend |
| 522 |
try: |
| 523 |
tasks = gc.images.get_associated_image_tasks(args.id) |
| 524 |
utils.print_dict_list(tasks['tasks'], columns) |
| 525 |
except exc.HTTPNotFound: |
| 526 |
utils.exit('Image %s not found.' % args.id) |
| 527 |
except exc.HTTPNotImplemented: |
| 528 |
utils.exit('Server does not support image tasks API (v2.12)') |
| 529 |
|
| 530 |
|
| 531 |
def do_usage(gc, args): |
| 532 |
"""Get quota usage information.""" |
| 533 |
columns = ['Quota', 'Limit', 'Usage'] |
| 534 |
usage = gc.info.get_usage() |
| 535 |
utils.print_dict_list( |
| 536 |
[dict(v, quota=k) for k, v in usage.items()], |
| 537 |
columns) |
| 538 |
|
| 539 |
|
| 540 |
@utils.arg('--image-id', metavar='<IMAGE_ID>', required=True, |
| 541 |
help=_('Image to display members of.')) |
| 542 |
def do_member_list(gc, args): |
| 543 |
"""Describe sharing permissions by image.""" |
| 544 |
members = gc.image_members.list(args.image_id) |
| 545 |
columns = ['Image ID', 'Member ID', 'Status'] |
| 546 |
utils.print_list(members, columns) |
| 547 |
|
| 548 |
|
| 549 |
@utils.arg('image_id', metavar='<IMAGE_ID>', |
| 550 |
help=_('Image from which to display member.')) |
| 551 |
@utils.arg('member_id', metavar='<MEMBER_ID>', |
| 552 |
help=_('Project to display.')) |
| 553 |
def do_member_get(gc, args): |
| 554 |
"""Show details of an image member""" |
| 555 |
member = gc.image_members.get(args.image_id, args.member_id) |
| 556 |
utils.print_dict(member) |
| 557 |
|
| 558 |
|
| 559 |
@utils.arg('image_id', metavar='<IMAGE_ID>', |
| 560 |
help=_('Image from which to remove member.')) |
| 561 |
@utils.arg('member_id', metavar='<MEMBER_ID>', |
| 562 |
help=_('Tenant to remove as member.')) |
| 563 |
def do_member_delete(gc, args): |
| 564 |
"""Delete image member.""" |
| 565 |
if not (args.image_id and args.member_id): |
| 566 |
utils.exit('Unable to delete member. Specify image_id and member_id') |
| 567 |
else: |
| 568 |
gc.image_members.delete(args.image_id, args.member_id) |
| 569 |
|
| 570 |
|
| 571 |
@utils.arg('image_id', metavar='<IMAGE_ID>', |
| 572 |
help=_('Image from which to update member.')) |
| 573 |
@utils.arg('member_id', metavar='<MEMBER_ID>', |
| 574 |
help=_('Tenant to update.')) |
| 575 |
@utils.arg('member_status', metavar='<MEMBER_STATUS>', |
| 576 |
choices=MEMBER_STATUS_VALUES, |
| 577 |
help=(_('Updated status of member. Valid Values: %s') % |
| 578 |
', '.join(str(val) for val in MEMBER_STATUS_VALUES))) |
| 579 |
def do_member_update(gc, args): |
| 580 |
"""Update the status of a member for a given image.""" |
| 581 |
if not (args.image_id and args.member_id and args.member_status): |
| 582 |
utils.exit('Unable to update member. Specify image_id, member_id and' |
| 583 |
' member_status') |
| 584 |
else: |
| 585 |
member = gc.image_members.update(args.image_id, args.member_id, |
| 586 |
args.member_status) |
| 587 |
member = [member] |
| 588 |
columns = ['Image ID', 'Member ID', 'Status'] |
| 589 |
utils.print_list(member, columns) |
| 590 |
|
| 591 |
|
| 592 |
@utils.arg('image_id', metavar='<IMAGE_ID>', |
| 593 |
help=_('Image with which to create member.')) |
| 594 |
@utils.arg('member_id', metavar='<MEMBER_ID>', |
| 595 |
help=_('Tenant to add as member.')) |
| 596 |
def do_member_create(gc, args): |
| 597 |
"""Create member for a given image.""" |
| 598 |
if not (args.image_id and args.member_id): |
| 599 |
utils.exit('Unable to create member. Specify image_id and member_id') |
| 600 |
else: |
| 601 |
member = gc.image_members.create(args.image_id, args.member_id) |
| 602 |
member = [member] |
| 603 |
columns = ['Image ID', 'Member ID', 'Status'] |
| 604 |
utils.print_list(member, columns) |
| 605 |
|
| 606 |
|
| 607 |
@utils.arg('model', metavar='<MODEL>', help=_('Name of model to describe.')) |
| 608 |
def do_explain(gc, args): |
| 609 |
"""Describe a specific model.""" |
| 610 |
try: |
| 611 |
schema = gc.schemas.get(args.model) |
| 612 |
except exc.HTTPNotFound: |
| 613 |
utils.exit('Unable to find requested model \'%s\'' % args.model) |
| 614 |
else: |
| 615 |
formatters = {'Attribute': lambda m: m.name} |
| 616 |
columns = ['Attribute', 'Description'] |
| 617 |
utils.print_list(schema.properties, columns, formatters) |
| 618 |
|
| 619 |
|
| 620 |
def do_import_info(gc, args): |
| 621 |
"""Print import methods available from Glance.""" |
| 622 |
try: |
| 623 |
import_info = gc.images.get_import_info() |
| 624 |
except exc.HTTPNotFound: |
| 625 |
utils.exit('Target Glance does not support Image Import workflow') |
| 626 |
else: |
| 627 |
utils.print_dict(import_info) |
| 628 |
|
| 629 |
|
| 630 |
@utils.arg('--detail', default=False, action='store_true', |
| 631 |
help='Shows details of stores. Admin only.') |
| 632 |
def do_stores_info(gc, args): |
| 633 |
"""Print available backends from Glance.""" |
| 634 |
try: |
| 635 |
if args.detail: |
| 636 |
stores_info = gc.images.get_stores_info_detail() |
| 637 |
else: |
| 638 |
stores_info = gc.images.get_stores_info() |
| 639 |
except exc.HTTPNotFound: |
| 640 |
utils.exit('Multi Backend support is not enabled') |
| 641 |
else: |
| 642 |
utils.print_dict(stores_info) |
| 643 |
|
| 644 |
|
| 645 |
@utils.arg('id', metavar='<IMAGE_ID>', help=_('ID of image to update.')) |
| 646 |
@utils.arg('--store', metavar='<STORE_ID>', required=True, |
| 647 |
help=_('Store to delete image from.')) |
| 648 |
def do_stores_delete(gc, args): |
| 649 |
"""Delete image from specific store.""" |
| 650 |
try: |
| 651 |
gc.images.delete_from_store(args.store, args.id) |
| 652 |
except exc.HTTPNotFound: |
| 653 |
utils.exit('Multi Backend support is not enabled or Image/store not ' |
| 654 |
'found.') |
| 655 |
except (exc.HTTPForbidden, exc.HTTPException) as e: |
| 656 |
msg = ("Unable to delete image '%s' from store '%s'. (%s)" % ( |
| 657 |
args.id, |
| 658 |
args.store, |
| 659 |
e)) |
| 660 |
utils.exit(msg) |
| 661 |
|
| 662 |
|
| 663 |
@utils.arg('--allow-md5-fallback', action='store_true', |
| 664 |
default=utils.env('OS_IMAGE_ALLOW_MD5_FALLBACK', default=False), |
| 665 |
help=_('If os_hash_algo and os_hash_value properties are available ' |
| 666 |
'on the image, they will be used to validate the downloaded ' |
| 667 |
'image data. If the indicated secure hash algorithm is not ' |
| 668 |
'available on the client, the download will fail. Use this ' |
| 669 |
'flag to indicate that in such a case the legacy MD5 image ' |
| 670 |
'checksum should be used to validate the downloaded data. ' |
| 671 |
'You can also set the environment variable ' |
| 672 |
'OS_IMAGE_ALLOW_MD5_FALLBACK to any value to activate this ' |
| 673 |
'option.')) |
| 674 |
@utils.arg('--file', metavar='<FILE>', |
| 675 |
help=_('Local file to save downloaded image data to. ' |
| 676 |
'If this is not specified and there is no redirection ' |
| 677 |
'the image data will not be saved.')) |
| 678 |
@utils.arg('id', metavar='<IMAGE_ID>', help=_('ID of image to download.')) |
| 679 |
@utils.arg('--progress', action='store_true', default=False, |
| 680 |
help=_('Show download progress bar.')) |
| 681 |
def do_image_download(gc, args): |
| 682 |
"""Download a specific image.""" |
| 683 |
if sys.stdout.isatty() and (args.file is None): |
| 684 |
msg = ('No redirection or local file specified for downloaded image ' |
| 685 |
'data. Please specify a local file with --file to save ' |
| 686 |
'downloaded image or redirect output to another source.') |
| 687 |
utils.exit(msg) |
| 688 |
|
| 689 |
try: |
| 690 |
body = gc.images.data(args.id, |
| 691 |
allow_md5_fallback=args.allow_md5_fallback) |
| 692 |
except (exc.HTTPForbidden, exc.HTTPException) as e: |
| 693 |
msg = "Unable to download image '%s'. (%s)" % (args.id, e) |
| 694 |
utils.exit(msg) |
| 695 |
|
| 696 |
if body.wrapped is None: |
| 697 |
msg = ('Image %s has no data.' % args.id) |
| 698 |
utils.exit(msg) |
| 699 |
|
| 700 |
if args.progress: |
| 701 |
body = progressbar.VerboseIteratorWrapper(body, len(body)) |
| 702 |
|
| 703 |
utils.save_image(body, args.file) |
| 704 |
|
| 705 |
|
| 706 |
@utils.arg('--file', metavar='<FILE>', |
| 707 |
help=_('Local file that contains disk image to be uploaded.' |
| 708 |
' Alternatively, images can be passed' |
| 709 |
' to the client via stdin.')) |
| 710 |
@utils.arg('--size', metavar='<IMAGE_SIZE>', type=int, |
| 711 |
help=_('Size in bytes of image to be uploaded. Default is to get ' |
| 712 |
'size from provided data object but this is supported in ' |
| 713 |
'case where size cannot be inferred.'), |
| 714 |
default=None) |
| 715 |
@utils.arg('--progress', action='store_true', default=False, |
| 716 |
help=_('Show upload progress bar.')) |
| 717 |
@utils.arg('id', metavar='<IMAGE_ID>', |
| 718 |
help=_('ID of image to upload data to.')) |
| 719 |
@utils.arg('--store', metavar='<STORE>', |
| 720 |
default=utils.env('OS_IMAGE_STORE', default=None), |
| 721 |
help='Backend store to upload image to.') |
| 722 |
def do_image_upload(gc, args): |
| 723 |
"""Upload data for a specific image.""" |
| 724 |
backend = None |
| 725 |
if args.store: |
| 726 |
backend = args.store |
| 727 |
|
| 728 |
_validate_backend(backend, gc) |
| 729 |
|
| 730 |
image_data = utils.get_data_file(args) |
| 731 |
filesize = get_filesize(args, image_data) |
| 732 |
|
| 733 |
if args.progress: |
| 734 |
if filesize is not None: |
| 735 |
|
| 736 |
|
| 737 |
image_data = progressbar.VerboseFileWrapper(image_data, filesize) |
| 738 |
gc.images.upload(args.id, image_data, filesize, backend=backend) |
| 739 |
|
| 740 |
|
| 741 |
@utils.arg('--file', metavar='<FILE>', |
| 742 |
help=_('Local file that contains disk image to be uploaded.' |
| 743 |
' Alternatively, images can be passed' |
| 744 |
' to the client via stdin.')) |
| 745 |
@utils.arg('--size', metavar='<IMAGE_SIZE>', type=int, |
| 746 |
help=_('Size in bytes of image to be uploaded. Default is to get ' |
| 747 |
'size from provided data object but this is supported in ' |
| 748 |
'case where size cannot be inferred.'), |
| 749 |
default=None) |
| 750 |
@utils.arg('--progress', action='store_true', default=False, |
| 751 |
help=_('Show upload progress bar.')) |
| 752 |
@utils.arg('id', metavar='<IMAGE_ID>', |
| 753 |
help=_('ID of image to upload data to.')) |
| 754 |
def do_image_stage(gc, args): |
| 755 |
"""Upload data for a specific image to staging.""" |
| 756 |
image_data = utils.get_data_file(args) |
| 757 |
filesize = get_filesize(args, image_data) |
| 758 |
|
| 759 |
if args.progress: |
| 760 |
if filesize is not None: |
| 761 |
|
| 762 |
|
| 763 |
image_data = progressbar.VerboseFileWrapper(image_data, filesize) |
| 764 |
gc.images.stage(args.id, image_data, filesize) |
| 765 |
|
| 766 |
|
| 767 |
@utils.arg('--import-method', metavar='<METHOD>', default='glance-direct', |
| 768 |
help=_('Import method used for Image Import workflow. ' |
| 769 |
'Valid values can be retrieved with import-info command ' |
| 770 |
'and the default "glance-direct" is used with ' |
| 771 |
'"image-stage".')) |
| 772 |
@utils.arg('--uri', metavar='<IMAGE_URL>', default=None, |
| 773 |
help=_('URI to download the external image.')) |
| 774 |
@utils.arg('--remote-region', metavar='<REMOTE_GLANCE_REGION>', default=None, |
| 775 |
help=_('REMOTE GLANCE REGION to download the image.')) |
| 776 |
@utils.arg('--remote-image-id', metavar='<REMOTE_IMAGE_ID>', default=None, |
| 777 |
help=_('The IMAGE ID of the image of remote glance, which needs' |
| 778 |
'to be imported with glance-download')) |
| 779 |
@utils.arg('--remote-service-interface', metavar='<REMOTE_SERVICE_INTERFACE>', |
| 780 |
default='public', |
| 781 |
help=_('The Remote Glance Service Interface for glance-download')) |
| 782 |
@utils.arg('id', metavar='<IMAGE_ID>', |
| 783 |
help=_('ID of image to import.')) |
| 784 |
@utils.arg('--store', metavar='<STORE>', |
| 785 |
default=utils.env('OS_IMAGE_STORE', default=None), |
| 786 |
help='Backend store to upload image to.') |
| 787 |
@utils.arg('--stores', metavar='<STORES>', |
| 788 |
default=utils.env('OS_IMAGE_STORES', default=None), |
| 789 |
help='Stores to upload image to if multi-stores import available.') |
| 790 |
@utils.arg('--all-stores', type=strutils.bool_from_string, |
| 791 |
metavar='[True|False]', |
| 792 |
default=None, |
| 793 |
dest='os_all_stores', |
| 794 |
help=_('"all-stores" can be ued instead of "stores"-list to ' |
| 795 |
'indicate that image should be imported all available ' |
| 796 |
'stores.')) |
| 797 |
@utils.arg('--allow-failure', type=strutils.bool_from_string, |
| 798 |
metavar='[True|False]', |
| 799 |
dest='os_allow_failure', |
| 800 |
default=utils.env('OS_IMAGE_ALLOW_FAILURE', default=True), |
| 801 |
help=_('Indicator if all stores listed (or available) must ' |
| 802 |
'succeed. "True" by default meaning that we allow some ' |
| 803 |
'stores to fail and the status can be monitored from the ' |
| 804 |
'image metadata. If this is set to "False" the import will ' |
| 805 |
'be reverted should any of the uploads fail. Only usable ' |
| 806 |
'with "stores" or "all-stores".')) |
| 807 |
def do_image_import(gc, args): |
| 808 |
"""Initiate the image import taskflow.""" |
| 809 |
backend = getattr(args, "store", None) |
| 810 |
stores = getattr(args, "stores", None) |
| 811 |
all_stores = getattr(args, "os_all_stores", None) |
| 812 |
allow_failure = getattr(args, "os_allow_failure", True) |
| 813 |
uri = getattr(args, "uri", None) |
| 814 |
remote_region = getattr(args, "remote_region", None) |
| 815 |
remote_image_id = getattr(args, "remote_image_id", None) |
| 816 |
remote_service_interface = getattr(args, "remote_service_interface", None) |
| 817 |
|
| 818 |
if not getattr(args, 'from_create', False): |
| 819 |
if (args.store and (stores or all_stores)) or (stores and all_stores): |
| 820 |
utils.exit("Only one of --store, --stores and --all-stores can be " |
| 821 |
"provided") |
| 822 |
elif args.store: |
| 823 |
backend = args.store |
| 824 |
|
| 825 |
_validate_backend(backend, gc) |
| 826 |
elif stores: |
| 827 |
stores = str(stores).split(',') |
| 828 |
|
| 829 |
|
| 830 |
if stores: |
| 831 |
for store in stores: |
| 832 |
_validate_backend(store, gc) |
| 833 |
|
| 834 |
if getattr(args, 'from_create', False): |
| 835 |
|
| 836 |
|
| 837 |
gc.images.image_import( |
| 838 |
args.id, args.import_method, args.uri, |
| 839 |
remote_region=remote_region, |
| 840 |
remote_image_id=remote_image_id, |
| 841 |
remote_service_interface=remote_service_interface, |
| 842 |
backend=backend, |
| 843 |
stores=stores, all_stores=all_stores, |
| 844 |
allow_failure=allow_failure) |
| 845 |
return |
| 846 |
|
| 847 |
|
| 848 |
try: |
| 849 |
import_methods = gc.images.get_import_info().get('import-methods') |
| 850 |
except exc.HTTPNotFound: |
| 851 |
utils.exit('Target Glance does not support Image Import workflow') |
| 852 |
|
| 853 |
if args.import_method not in import_methods.get('value'): |
| 854 |
utils.exit("Import method '%s' is not valid for this cloud. " |
| 855 |
"Valid values can be retrieved with import-info command." % |
| 856 |
args.import_method) |
| 857 |
|
| 858 |
if args.import_method == 'web-download' and not args.uri: |
| 859 |
utils.exit("Provide URI for web-download import method.") |
| 860 |
if args.uri and args.import_method != 'web-download': |
| 861 |
utils.exit("Import method should be 'web-download' if URI is " |
| 862 |
"provided.") |
| 863 |
|
| 864 |
if args.import_method == 'glance-download' and \ |
| 865 |
not (remote_region and remote_image_id): |
| 866 |
utils.exit("Provide REMOTE_IMAGE_ID and remote-region for " |
| 867 |
"'glance-download' import method.") |
| 868 |
if remote_region and args.import_method != 'glance-download': |
| 869 |
utils.exit("Import method should be 'glance-download' if " |
| 870 |
"REMOTE REGION is provided.") |
| 871 |
if remote_image_id and args.import_method != 'glance-download': |
| 872 |
utils.exit("Import method should be 'glance-download' if " |
| 873 |
"REMOTE IMAGE ID is provided.") |
| 874 |
|
| 875 |
if args.import_method == 'copy-image' and not (stores or all_stores): |
| 876 |
utils.exit("Provide either --stores or --all-stores for " |
| 877 |
"'copy-image' import method.") |
| 878 |
|
| 879 |
|
| 880 |
image = gc.images.get(args.id) |
| 881 |
container_format = image.get('container_format') |
| 882 |
disk_format = image.get('disk_format') |
| 883 |
if not (container_format and disk_format): |
| 884 |
utils.exit("The 'container_format' and 'disk_format' properties " |
| 885 |
"must be set on an image before it can be imported.") |
| 886 |
|
| 887 |
image_status = image.get('status') |
| 888 |
if args.import_method == 'glance-direct': |
| 889 |
if image_status != 'uploading': |
| 890 |
utils.exit("The 'glance-direct' import method can only be applied " |
| 891 |
"to an image in status 'uploading'") |
| 892 |
if args.import_method == 'web-download': |
| 893 |
if image_status != 'queued': |
| 894 |
utils.exit("The 'web-download' import method can only be applied " |
| 895 |
"to an image in status 'queued'") |
| 896 |
if args.import_method == 'copy-image': |
| 897 |
if image_status != 'active': |
| 898 |
utils.exit("The 'copy-image' import method can only be used on " |
| 899 |
"an image with status 'active'.") |
| 900 |
|
| 901 |
|
| 902 |
gc.images.image_import(args.id, args.import_method, uri=uri, |
| 903 |
remote_region=remote_region, |
| 904 |
remote_image_id=remote_image_id, |
| 905 |
remote_service_interface=remote_service_interface, |
| 906 |
backend=backend, stores=stores, |
| 907 |
all_stores=all_stores, allow_failure=allow_failure) |
| 908 |
|
| 909 |
image = gc.images.get(args.id) |
| 910 |
utils.print_image(image) |
| 911 |
|
| 912 |
|
| 913 |
@utils.arg('id', metavar='<IMAGE_ID>', nargs='+', |
| 914 |
help=_('ID of image(s) to delete.')) |
| 915 |
def do_image_delete(gc, args): |
| 916 |
"""Delete specified image.""" |
| 917 |
failure_flag = False |
| 918 |
for args_id in args.id: |
| 919 |
try: |
| 920 |
gc.images.delete(args_id) |
| 921 |
except exc.HTTPForbidden: |
| 922 |
msg = "You are not permitted to delete the image '%s'." % args_id |
| 923 |
utils.print_err(msg) |
| 924 |
failure_flag = True |
| 925 |
except exc.HTTPNotFound: |
| 926 |
msg = "No image with an ID of '%s' exists." % args_id |
| 927 |
utils.print_err(msg) |
| 928 |
failure_flag = True |
| 929 |
except exc.HTTPConflict: |
| 930 |
msg = "Unable to delete image '%s' because it is in use." % args_id |
| 931 |
utils.print_err(msg) |
| 932 |
failure_flag = True |
| 933 |
except exc.HTTPException as e: |
| 934 |
msg = "'%s': Unable to delete image '%s'" % (e, args_id) |
| 935 |
utils.print_err(msg) |
| 936 |
failure_flag = True |
| 937 |
if failure_flag: |
| 938 |
utils.exit() |
| 939 |
|
| 940 |
|
| 941 |
@utils.arg('id', metavar='<IMAGE_ID>', |
| 942 |
help=_('ID of image to deactivate.')) |
| 943 |
def do_image_deactivate(gc, args): |
| 944 |
"""Deactivate specified image.""" |
| 945 |
gc.images.deactivate(args.id) |
| 946 |
|
| 947 |
|
| 948 |
@utils.arg('id', metavar='<IMAGE_ID>', |
| 949 |
help=_('ID of image to reactivate.')) |
| 950 |
def do_image_reactivate(gc, args): |
| 951 |
"""Reactivate specified image.""" |
| 952 |
gc.images.reactivate(args.id) |
| 953 |
|
| 954 |
|
| 955 |
@utils.arg('image_id', metavar='<IMAGE_ID>', |
| 956 |
help=_('Image to be updated with the given tag.')) |
| 957 |
@utils.arg('tag_value', metavar='<TAG_VALUE>', |
| 958 |
help=_('Value of the tag.')) |
| 959 |
def do_image_tag_update(gc, args): |
| 960 |
"""Update an image with the given tag.""" |
| 961 |
if not (args.image_id and args.tag_value): |
| 962 |
utils.exit('Unable to update tag. Specify image_id and tag_value') |
| 963 |
else: |
| 964 |
gc.image_tags.update(args.image_id, args.tag_value) |
| 965 |
image = gc.images.get(args.image_id) |
| 966 |
image = [image] |
| 967 |
columns = ['ID', 'Tags'] |
| 968 |
utils.print_list(image, columns) |
| 969 |
|
| 970 |
|
| 971 |
@utils.arg('image_id', metavar='<IMAGE_ID>', |
| 972 |
help=_('ID of the image from which to delete tag.')) |
| 973 |
@utils.arg('tag_value', metavar='<TAG_VALUE>', |
| 974 |
help=_('Value of the tag.')) |
| 975 |
def do_image_tag_delete(gc, args): |
| 976 |
"""Delete the tag associated with the given image.""" |
| 977 |
if not (args.image_id and args.tag_value): |
| 978 |
utils.exit('Unable to delete tag. Specify image_id and tag_value') |
| 979 |
else: |
| 980 |
gc.image_tags.delete(args.image_id, args.tag_value) |
| 981 |
|
| 982 |
|
| 983 |
@utils.arg('--url', metavar='<URL>', required=True, |
| 984 |
help=_('URL of location to add.')) |
| 985 |
@utils.arg('--metadata', metavar='<STRING>', default='{}', |
| 986 |
help=_('Metadata associated with the location. ' |
| 987 |
'Must be a valid JSON object (default: %(default)s)')) |
| 988 |
@utils.arg('--checksum', metavar='<STRING>', |
| 989 |
help=_('md5 checksum of image contents')) |
| 990 |
@utils.arg('--hash-algo', metavar='<STRING>', |
| 991 |
help=_('Multihash algorithm')) |
| 992 |
@utils.arg('--hash-value', metavar='<STRING>', |
| 993 |
help=_('Multihash value')) |
| 994 |
@utils.arg('id', metavar='<IMAGE_ID>', |
| 995 |
help=_('ID of image to which the location is to be added.')) |
| 996 |
def do_location_add(gc, args): |
| 997 |
"""Add a location (and related metadata) to an image.""" |
| 998 |
validation_data = {} |
| 999 |
if args.checksum: |
| 1000 |
validation_data['checksum'] = args.checksum |
| 1001 |
if args.hash_algo: |
| 1002 |
validation_data['os_hash_algo'] = args.hash_algo |
| 1003 |
if args.hash_value: |
| 1004 |
validation_data['os_hash_value'] = args.hash_value |
| 1005 |
try: |
| 1006 |
metadata = json.loads(args.metadata) |
| 1007 |
except ValueError: |
| 1008 |
utils.exit('Metadata is not a valid JSON object.') |
| 1009 |
else: |
| 1010 |
image = gc.images.add_location(args.id, args.url, metadata, |
| 1011 |
validation_data=validation_data) |
| 1012 |
utils.print_dict(image) |
| 1013 |
|
| 1014 |
|
| 1015 |
@utils.arg('--url', metavar='<URL>', action='append', required=True, |
| 1016 |
help=_('URL of location to remove. May be used multiple times.')) |
| 1017 |
@utils.arg('id', metavar='<IMAGE_ID>', |
| 1018 |
help=_('ID of image whose locations are to be removed.')) |
| 1019 |
def do_location_delete(gc, args): |
| 1020 |
"""Remove locations (and related metadata) from an image.""" |
| 1021 |
gc.images.delete_locations(args.id, set(args.url)) |
| 1022 |
|
| 1023 |
|
| 1024 |
@utils.arg('--url', metavar='<URL>', required=True, |
| 1025 |
help=_('URL of location to update.')) |
| 1026 |
@utils.arg('--metadata', metavar='<STRING>', default='{}', |
| 1027 |
help=_('Metadata associated with the location. ' |
| 1028 |
'Must be a valid JSON object (default: %(default)s)')) |
| 1029 |
@utils.arg('id', metavar='<IMAGE_ID>', |
| 1030 |
help=_('ID of image whose location is to be updated.')) |
| 1031 |
def do_location_update(gc, args): |
| 1032 |
"""Update metadata of an image's location.""" |
| 1033 |
try: |
| 1034 |
metadata = json.loads(args.metadata) |
| 1035 |
|
| 1036 |
if metadata == {}: |
| 1037 |
print("WARNING -- The location's metadata will be updated to " |
| 1038 |
"an empty JSON object.") |
| 1039 |
except ValueError: |
| 1040 |
utils.exit('Metadata is not a valid JSON object.') |
| 1041 |
else: |
| 1042 |
image = gc.images.update_location(args.id, args.url, metadata) |
| 1043 |
utils.print_dict(image) |
| 1044 |
|
| 1045 |
|
| 1046 |
@utils.arg('--url', metavar='<URL>', required=True, |
| 1047 |
help=_('URL of location to add.')) |
| 1048 |
@utils.arg('--validation-data', metavar='<STRING>', default='{}', |
| 1049 |
help=_('Validation data containing os_hash_algo and os_hash_value ' |
| 1050 |
'only associated to the image. Must be a valid JSON object ' |
| 1051 |
'(default: %(default)s)')) |
| 1052 |
@utils.arg('id', metavar='<IMAGE_ID>', |
| 1053 |
help=_('ID of image whose location is to be added.')) |
| 1054 |
def do_add_location(gc, args): |
| 1055 |
"""Add location to an image which is in `queued` state only. """ |
| 1056 |
try: |
| 1057 |
invalid_val_data = None |
| 1058 |
validation_data = json.loads(args.validation_data) |
| 1059 |
accepted_values = ['os_hash_algo', 'os_hash_value'] |
| 1060 |
invalid_val_data = list(set(validation_data.keys()).difference( |
| 1061 |
accepted_values)) |
| 1062 |
if invalid_val_data: |
| 1063 |
utils.exit('Validation Data should contain only os_hash_algo ' |
| 1064 |
'and os_hash_value. `%s` is not allowed' % |
| 1065 |
(*invalid_val_data,)) |
| 1066 |
|
| 1067 |
allowed_hash_algo = ['sha512', 'sha256', 'sha1', 'md5'] |
| 1068 |
if validation_data and \ |
| 1069 |
validation_data['os_hash_algo'] not in allowed_hash_algo: |
| 1070 |
raise utils.exit('os_hash_algo: `%s` is incorrect, ' |
| 1071 |
'allowed hashing algorithms: %s' % |
| 1072 |
(validation_data['os_hash_algo'], |
| 1073 |
allowed_hash_algo)) |
| 1074 |
|
| 1075 |
except ValueError: |
| 1076 |
utils.exit('validation-data is not a valid JSON object.') |
| 1077 |
else: |
| 1078 |
image = gc.images.add_image_location(args.id, args.url, |
| 1079 |
validation_data=validation_data) |
| 1080 |
utils.print_image(image) |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
NAMESPACE_SCHEMA = None |
| 1085 |
|
| 1086 |
|
| 1087 |
def get_namespace_schema(): |
| 1088 |
global NAMESPACE_SCHEMA |
| 1089 |
if NAMESPACE_SCHEMA is None: |
| 1090 |
schema_path = os.path.expanduser("~/.glanceclient/" |
| 1091 |
"namespace_schema.json") |
| 1092 |
if os.path.isfile(schema_path): |
| 1093 |
with open(schema_path, "r") as f: |
| 1094 |
schema_raw = f.read() |
| 1095 |
NAMESPACE_SCHEMA = json.loads(schema_raw) |
| 1096 |
else: |
| 1097 |
return namespace_schema.BASE_SCHEMA |
| 1098 |
return NAMESPACE_SCHEMA |
| 1099 |
|
| 1100 |
|
| 1101 |
def _namespace_show(namespace, max_column_width=None): |
| 1102 |
namespace = dict(namespace) |
| 1103 |
|
| 1104 |
if 'properties' in namespace: |
| 1105 |
props = [k for k in namespace['properties']] |
| 1106 |
namespace['properties'] = props |
| 1107 |
if 'resource_type_associations' in namespace: |
| 1108 |
assocs = [assoc['name'] |
| 1109 |
for assoc in namespace['resource_type_associations']] |
| 1110 |
namespace['resource_type_associations'] = assocs |
| 1111 |
if 'objects' in namespace: |
| 1112 |
objects = [obj['name'] for obj in namespace['objects']] |
| 1113 |
namespace['objects'] = objects |
| 1114 |
|
| 1115 |
if 'tags' in namespace: |
| 1116 |
tags = [tag['name'] for tag in namespace['tags']] |
| 1117 |
namespace['tags'] = tags |
| 1118 |
|
| 1119 |
if max_column_width: |
| 1120 |
utils.print_dict(namespace, max_column_width) |
| 1121 |
else: |
| 1122 |
utils.print_dict(namespace) |
| 1123 |
|
| 1124 |
|
| 1125 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1126 |
help=_('Name of the namespace.')) |
| 1127 |
@utils.schema_args(get_namespace_schema, omit=['namespace', 'property_count', |
| 1128 |
'properties', 'tag_count', |
| 1129 |
'tags', 'object_count', |
| 1130 |
'objects', 'resource_types']) |
| 1131 |
def do_md_namespace_create(gc, args): |
| 1132 |
"""Create a new metadata definitions namespace.""" |
| 1133 |
schema = gc.schemas.get('metadefs/namespace') |
| 1134 |
_args = [(x[0].replace('-', '_'), x[1]) for x in vars(args).items()] |
| 1135 |
fields = dict(filter(lambda x: x[1] is not None and |
| 1136 |
(schema.is_core_property(x[0])), |
| 1137 |
_args)) |
| 1138 |
namespace = gc.metadefs_namespace.create(**fields) |
| 1139 |
|
| 1140 |
_namespace_show(namespace) |
| 1141 |
|
| 1142 |
|
| 1143 |
@utils.arg('--file', metavar='<FILEPATH>', |
| 1144 |
help=_('Path to file with namespace schema to import. ' |
| 1145 |
'Alternatively, namespaces schema can be passed to the ' |
| 1146 |
'client via stdin.')) |
| 1147 |
def do_md_namespace_import(gc, args): |
| 1148 |
"""Import a metadata definitions namespace from file or standard input.""" |
| 1149 |
namespace_data = utils.get_data_file(args) |
| 1150 |
if not namespace_data: |
| 1151 |
utils.exit('No metadata definition namespace passed via stdin or ' |
| 1152 |
'--file argument.') |
| 1153 |
|
| 1154 |
try: |
| 1155 |
namespace_json = json.load(namespace_data) |
| 1156 |
except ValueError: |
| 1157 |
utils.exit('Schema is not a valid JSON object.') |
| 1158 |
else: |
| 1159 |
namespace = gc.metadefs_namespace.create(**namespace_json) |
| 1160 |
_namespace_show(namespace) |
| 1161 |
|
| 1162 |
|
| 1163 |
@utils.arg('id', metavar='<NAMESPACE>', help=_('Name of namespace to update.')) |
| 1164 |
@utils.schema_args(get_namespace_schema, omit=['property_count', 'properties', |
| 1165 |
'tag_count', 'tags', |
| 1166 |
'object_count', 'objects', |
| 1167 |
'resource_type_associations']) |
| 1168 |
def do_md_namespace_update(gc, args): |
| 1169 |
"""Update an existing metadata definitions namespace.""" |
| 1170 |
schema = gc.schemas.get('metadefs/namespace') |
| 1171 |
|
| 1172 |
_args = [(x[0].replace('-', '_'), x[1]) for x in vars(args).items()] |
| 1173 |
fields = dict(filter(lambda x: x[1] is not None and |
| 1174 |
(schema.is_core_property(x[0])), |
| 1175 |
_args)) |
| 1176 |
namespace = gc.metadefs_namespace.update(args.id, **fields) |
| 1177 |
|
| 1178 |
_namespace_show(namespace) |
| 1179 |
|
| 1180 |
|
| 1181 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1182 |
help=_('Name of namespace to describe.')) |
| 1183 |
@utils.arg('--resource-type', metavar='<RESOURCE_TYPE>', |
| 1184 |
help=_('Applies prefix of given resource type associated to a ' |
| 1185 |
'namespace to all properties of a namespace.'), default=None) |
| 1186 |
@utils.arg('--max-column-width', metavar='<integer>', default=80, |
| 1187 |
help=_('The max column width of the printed table.')) |
| 1188 |
def do_md_namespace_show(gc, args): |
| 1189 |
"""Describe a specific metadata definitions namespace. |
| 1190 |
|
| 1191 |
Lists also the namespace properties, objects and resource type |
| 1192 |
associations. |
| 1193 |
""" |
| 1194 |
kwargs = {} |
| 1195 |
if args.resource_type: |
| 1196 |
kwargs['resource_type'] = args.resource_type |
| 1197 |
|
| 1198 |
namespace = gc.metadefs_namespace.get(args.namespace, **kwargs) |
| 1199 |
_namespace_show(namespace, int(args.max_column_width)) |
| 1200 |
|
| 1201 |
|
| 1202 |
@utils.arg('--resource-types', metavar='<RESOURCE_TYPES>', action='append', |
| 1203 |
help=_('Resource type to filter namespaces.')) |
| 1204 |
@utils.arg('--visibility', metavar='<VISIBILITY>', |
| 1205 |
help=_('Visibility parameter to filter namespaces.')) |
| 1206 |
@utils.arg('--page-size', metavar='<SIZE>', default=None, type=int, |
| 1207 |
help=_('Number of namespaces to request ' |
| 1208 |
'in each paginated request.')) |
| 1209 |
def do_md_namespace_list(gc, args): |
| 1210 |
"""List metadata definitions namespaces.""" |
| 1211 |
filter_keys = ['resource_types', 'visibility'] |
| 1212 |
filter_items = [(key, getattr(args, key, None)) for key in filter_keys] |
| 1213 |
filters = dict([item for item in filter_items if item[1] is not None]) |
| 1214 |
|
| 1215 |
kwargs = {'filters': filters} |
| 1216 |
if args.page_size is not None: |
| 1217 |
kwargs['page_size'] = args.page_size |
| 1218 |
|
| 1219 |
namespaces = gc.metadefs_namespace.list(**kwargs) |
| 1220 |
columns = ['namespace'] |
| 1221 |
utils.print_list(namespaces, columns) |
| 1222 |
|
| 1223 |
|
| 1224 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1225 |
help=_('Name of namespace to delete.')) |
| 1226 |
def do_md_namespace_delete(gc, args): |
| 1227 |
"""Delete specified metadata definitions namespace with its contents.""" |
| 1228 |
gc.metadefs_namespace.delete(args.namespace) |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
RESOURCE_TYPE_SCHEMA = None |
| 1233 |
|
| 1234 |
|
| 1235 |
def get_resource_type_schema(): |
| 1236 |
global RESOURCE_TYPE_SCHEMA |
| 1237 |
if RESOURCE_TYPE_SCHEMA is None: |
| 1238 |
schema_path = os.path.expanduser("~/.glanceclient/" |
| 1239 |
"resource_type_schema.json") |
| 1240 |
if os.path.isfile(schema_path): |
| 1241 |
with open(schema_path, "r") as f: |
| 1242 |
schema_raw = f.read() |
| 1243 |
RESOURCE_TYPE_SCHEMA = json.loads(schema_raw) |
| 1244 |
else: |
| 1245 |
return resource_type_schema.BASE_SCHEMA |
| 1246 |
return RESOURCE_TYPE_SCHEMA |
| 1247 |
|
| 1248 |
|
| 1249 |
@utils.arg('namespace', metavar='<NAMESPACE>', help=_('Name of namespace.')) |
| 1250 |
@utils.schema_args(get_resource_type_schema) |
| 1251 |
def do_md_resource_type_associate(gc, args): |
| 1252 |
"""Associate resource type with a metadata definitions namespace.""" |
| 1253 |
schema = gc.schemas.get('metadefs/resource_type') |
| 1254 |
_args = [(x[0].replace('-', '_'), x[1]) for x in vars(args).items()] |
| 1255 |
fields = dict(filter(lambda x: x[1] is not None and |
| 1256 |
(schema.is_core_property(x[0])), |
| 1257 |
_args)) |
| 1258 |
resource_type = gc.metadefs_resource_type.associate(args.namespace, |
| 1259 |
**fields) |
| 1260 |
utils.print_dict(resource_type) |
| 1261 |
|
| 1262 |
|
| 1263 |
@utils.arg('namespace', metavar='<NAMESPACE>', help=_('Name of namespace.')) |
| 1264 |
@utils.arg('resource_type', metavar='<RESOURCE_TYPE>', |
| 1265 |
help=_('Name of resource type.')) |
| 1266 |
def do_md_resource_type_deassociate(gc, args): |
| 1267 |
"""Deassociate resource type with a metadata definitions namespace.""" |
| 1268 |
gc.metadefs_resource_type.deassociate(args.namespace, args.resource_type) |
| 1269 |
|
| 1270 |
|
| 1271 |
def do_md_resource_type_list(gc, args): |
| 1272 |
"""List available resource type names.""" |
| 1273 |
resource_types = gc.metadefs_resource_type.list() |
| 1274 |
utils.print_list(resource_types, ['name']) |
| 1275 |
|
| 1276 |
|
| 1277 |
@utils.arg('namespace', metavar='<NAMESPACE>', help=_('Name of namespace.')) |
| 1278 |
def do_md_namespace_resource_type_list(gc, args): |
| 1279 |
"""List resource types associated to specific namespace.""" |
| 1280 |
resource_types = gc.metadefs_resource_type.get(args.namespace) |
| 1281 |
utils.print_list(resource_types, ['name', 'prefix', 'properties_target']) |
| 1282 |
|
| 1283 |
|
| 1284 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1285 |
help=_('Name of namespace the property will belong.')) |
| 1286 |
@utils.arg('--name', metavar='<NAME>', required=True, |
| 1287 |
help=_('Internal name of a property.')) |
| 1288 |
@utils.arg('--title', metavar='<TITLE>', required=True, |
| 1289 |
help=_('Property name displayed to the user.')) |
| 1290 |
@utils.arg('--schema', metavar='<SCHEMA>', required=True, |
| 1291 |
help=_('Valid JSON schema of a property.')) |
| 1292 |
@utils.arg('--type', metavar='<TYPE>', required=True, |
| 1293 |
help=_('Type of the property')) |
| 1294 |
def do_md_property_create(gc, args): |
| 1295 |
"""Create a new metadata definitions property inside a namespace.""" |
| 1296 |
try: |
| 1297 |
schema = json.loads(args.schema) |
| 1298 |
except ValueError: |
| 1299 |
utils.exit('Schema is not a valid JSON object.') |
| 1300 |
else: |
| 1301 |
fields = {'name': args.name, 'title': args.title, 'type': args.type} |
| 1302 |
fields.update(schema) |
| 1303 |
new_property = gc.metadefs_property.create(args.namespace, **fields) |
| 1304 |
utils.print_dict(new_property) |
| 1305 |
|
| 1306 |
|
| 1307 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1308 |
help=_('Name of namespace the property belongs.')) |
| 1309 |
@utils.arg('property', metavar='<PROPERTY>', help=_('Name of a property.')) |
| 1310 |
@utils.arg('--name', metavar='<NAME>', default=None, |
| 1311 |
help=_('New name of a property.')) |
| 1312 |
@utils.arg('--title', metavar='<TITLE>', default=None, |
| 1313 |
help=_('Property name displayed to the user.')) |
| 1314 |
@utils.arg('--schema', metavar='<SCHEMA>', default=None, |
| 1315 |
help=_('Valid JSON schema of a property.')) |
| 1316 |
def do_md_property_update(gc, args): |
| 1317 |
"""Update metadata definitions property inside a namespace.""" |
| 1318 |
fields = {} |
| 1319 |
if args.name: |
| 1320 |
fields['name'] = args.name |
| 1321 |
if args.title: |
| 1322 |
fields['title'] = args.title |
| 1323 |
if args.schema: |
| 1324 |
try: |
| 1325 |
schema = json.loads(args.schema) |
| 1326 |
except ValueError: |
| 1327 |
utils.exit('Schema is not a valid JSON object.') |
| 1328 |
else: |
| 1329 |
fields.update(schema) |
| 1330 |
|
| 1331 |
new_property = gc.metadefs_property.update(args.namespace, args.property, |
| 1332 |
**fields) |
| 1333 |
utils.print_dict(new_property) |
| 1334 |
|
| 1335 |
|
| 1336 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1337 |
help=_('Name of namespace the property belongs.')) |
| 1338 |
@utils.arg('property', metavar='<PROPERTY>', help=_('Name of a property.')) |
| 1339 |
@utils.arg('--max-column-width', metavar='<integer>', default=80, |
| 1340 |
help=_('The max column width of the printed table.')) |
| 1341 |
def do_md_property_show(gc, args): |
| 1342 |
"""Describe a specific metadata definitions property inside a namespace.""" |
| 1343 |
prop = gc.metadefs_property.get(args.namespace, args.property) |
| 1344 |
utils.print_dict(prop, int(args.max_column_width)) |
| 1345 |
|
| 1346 |
|
| 1347 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1348 |
help=_('Name of namespace the property belongs.')) |
| 1349 |
@utils.arg('property', metavar='<PROPERTY>', help=_('Name of a property.')) |
| 1350 |
def do_md_property_delete(gc, args): |
| 1351 |
"""Delete a specific metadata definitions property inside a namespace.""" |
| 1352 |
gc.metadefs_property.delete(args.namespace, args.property) |
| 1353 |
|
| 1354 |
|
| 1355 |
@utils.arg('namespace', metavar='<NAMESPACE>', help=_('Name of namespace.')) |
| 1356 |
def do_md_namespace_properties_delete(gc, args): |
| 1357 |
"""Delete all metadata definitions property inside a specific namespace.""" |
| 1358 |
gc.metadefs_property.delete_all(args.namespace) |
| 1359 |
|
| 1360 |
|
| 1361 |
@utils.arg('namespace', metavar='<NAMESPACE>', help=_('Name of namespace.')) |
| 1362 |
def do_md_property_list(gc, args): |
| 1363 |
"""List metadata definitions properties inside a specific namespace.""" |
| 1364 |
properties = gc.metadefs_property.list(args.namespace) |
| 1365 |
columns = ['name', 'title', 'type'] |
| 1366 |
utils.print_list(properties, columns) |
| 1367 |
|
| 1368 |
|
| 1369 |
def _object_show(obj, max_column_width=None): |
| 1370 |
obj = dict(obj) |
| 1371 |
|
| 1372 |
if 'properties' in obj: |
| 1373 |
objects = [k for k in obj['properties']] |
| 1374 |
obj['properties'] = objects |
| 1375 |
|
| 1376 |
if max_column_width: |
| 1377 |
utils.print_dict(obj, max_column_width) |
| 1378 |
else: |
| 1379 |
utils.print_dict(obj) |
| 1380 |
|
| 1381 |
|
| 1382 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1383 |
help=_('Name of namespace the object will belong.')) |
| 1384 |
@utils.arg('--name', metavar='<NAME>', required=True, |
| 1385 |
help=_('Internal name of an object.')) |
| 1386 |
@utils.arg('--schema', metavar='<SCHEMA>', required=True, |
| 1387 |
help=_('Valid JSON schema of an object.')) |
| 1388 |
def do_md_object_create(gc, args): |
| 1389 |
"""Create a new metadata definitions object inside a namespace.""" |
| 1390 |
try: |
| 1391 |
schema = json.loads(args.schema) |
| 1392 |
except ValueError: |
| 1393 |
utils.exit('Schema is not a valid JSON object.') |
| 1394 |
else: |
| 1395 |
fields = {'name': args.name} |
| 1396 |
fields.update(schema) |
| 1397 |
new_object = gc.metadefs_object.create(args.namespace, **fields) |
| 1398 |
_object_show(new_object) |
| 1399 |
|
| 1400 |
|
| 1401 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1402 |
help=_('Name of namespace the object belongs.')) |
| 1403 |
@utils.arg('object', metavar='<OBJECT>', help=_('Name of an object.')) |
| 1404 |
@utils.arg('--name', metavar='<NAME>', default=None, |
| 1405 |
help=_('New name of an object.')) |
| 1406 |
@utils.arg('--schema', metavar='<SCHEMA>', default=None, |
| 1407 |
help=_('Valid JSON schema of an object.')) |
| 1408 |
def do_md_object_update(gc, args): |
| 1409 |
"""Update metadata definitions object inside a namespace.""" |
| 1410 |
fields = {} |
| 1411 |
if args.name: |
| 1412 |
fields['name'] = args.name |
| 1413 |
if args.schema: |
| 1414 |
try: |
| 1415 |
schema = json.loads(args.schema) |
| 1416 |
except ValueError: |
| 1417 |
utils.exit('Schema is not a valid JSON object.') |
| 1418 |
else: |
| 1419 |
fields.update(schema) |
| 1420 |
|
| 1421 |
new_object = gc.metadefs_object.update(args.namespace, args.object, |
| 1422 |
**fields) |
| 1423 |
_object_show(new_object) |
| 1424 |
|
| 1425 |
|
| 1426 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1427 |
help=_('Name of namespace the object belongs.')) |
| 1428 |
@utils.arg('object', metavar='<OBJECT>', help=_('Name of an object.')) |
| 1429 |
@utils.arg('--max-column-width', metavar='<integer>', default=80, |
| 1430 |
help=_('The max column width of the printed table.')) |
| 1431 |
def do_md_object_show(gc, args): |
| 1432 |
"""Describe a specific metadata definitions object inside a namespace.""" |
| 1433 |
obj = gc.metadefs_object.get(args.namespace, args.object) |
| 1434 |
_object_show(obj, int(args.max_column_width)) |
| 1435 |
|
| 1436 |
|
| 1437 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1438 |
help=_('Name of namespace the object belongs.')) |
| 1439 |
@utils.arg('object', metavar='<OBJECT>', help=_('Name of an object.')) |
| 1440 |
@utils.arg('property', metavar='<PROPERTY>', help=_('Name of a property.')) |
| 1441 |
@utils.arg('--max-column-width', metavar='<integer>', default=80, |
| 1442 |
help=_('The max column width of the printed table.')) |
| 1443 |
def do_md_object_property_show(gc, args): |
| 1444 |
"""Describe a specific metadata definitions property inside an object.""" |
| 1445 |
obj = gc.metadefs_object.get(args.namespace, args.object) |
| 1446 |
try: |
| 1447 |
prop = obj['properties'][args.property] |
| 1448 |
prop['name'] = args.property |
| 1449 |
except KeyError: |
| 1450 |
utils.exit('Property %s not found in object %s.' % (args.property, |
| 1451 |
args.object)) |
| 1452 |
utils.print_dict(prop, int(args.max_column_width)) |
| 1453 |
|
| 1454 |
|
| 1455 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1456 |
help=_('Name of namespace the object belongs.')) |
| 1457 |
@utils.arg('object', metavar='<OBJECT>', help=_('Name of an object.')) |
| 1458 |
def do_md_object_delete(gc, args): |
| 1459 |
"""Delete a specific metadata definitions object inside a namespace.""" |
| 1460 |
gc.metadefs_object.delete(args.namespace, args.object) |
| 1461 |
|
| 1462 |
|
| 1463 |
@utils.arg('namespace', metavar='<NAMESPACE>', help=_('Name of namespace.')) |
| 1464 |
def do_md_namespace_objects_delete(gc, args): |
| 1465 |
"""Delete all metadata definitions objects inside a specific namespace.""" |
| 1466 |
gc.metadefs_object.delete_all(args.namespace) |
| 1467 |
|
| 1468 |
|
| 1469 |
@utils.arg('namespace', metavar='<NAMESPACE>', help=_('Name of namespace.')) |
| 1470 |
def do_md_object_list(gc, args): |
| 1471 |
"""List metadata definitions objects inside a specific namespace.""" |
| 1472 |
objects = gc.metadefs_object.list(args.namespace) |
| 1473 |
columns = ['name', 'description'] |
| 1474 |
column_settings = { |
| 1475 |
"description": { |
| 1476 |
"max_width": 50, |
| 1477 |
"align": "l" |
| 1478 |
} |
| 1479 |
} |
| 1480 |
utils.print_list(objects, columns, field_settings=column_settings) |
| 1481 |
|
| 1482 |
|
| 1483 |
def _tag_show(tag, max_column_width=None): |
| 1484 |
tag = dict(tag) |
| 1485 |
if max_column_width: |
| 1486 |
utils.print_dict(tag, max_column_width) |
| 1487 |
else: |
| 1488 |
utils.print_dict(tag) |
| 1489 |
|
| 1490 |
|
| 1491 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1492 |
help=_('Name of the namespace the tag will belong to.')) |
| 1493 |
@utils.arg('--name', metavar='<NAME>', required=True, |
| 1494 |
help=_('The name of the new tag to add.')) |
| 1495 |
def do_md_tag_create(gc, args): |
| 1496 |
"""Add a new metadata definitions tag inside a namespace.""" |
| 1497 |
name = args.name.strip() |
| 1498 |
if name: |
| 1499 |
new_tag = gc.metadefs_tag.create(args.namespace, name) |
| 1500 |
_tag_show(new_tag) |
| 1501 |
else: |
| 1502 |
utils.exit('Please supply at least one non-blank tag name.') |
| 1503 |
|
| 1504 |
|
| 1505 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1506 |
help=_('Name of the namespace the tags will belong to.')) |
| 1507 |
@utils.arg('--names', metavar='<NAMES>', required=True, |
| 1508 |
help=_('A comma separated list of tag names.')) |
| 1509 |
@utils.arg('--delim', metavar='<DELIM>', required=False, |
| 1510 |
help=_('The delimiter used to separate the names' |
| 1511 |
' (if none is provided then the default is a comma).')) |
| 1512 |
@utils.arg('--append', default=False, action='store_true', required=False, |
| 1513 |
help=_('Append the new tags to the existing ones instead of' |
| 1514 |
'overwriting them')) |
| 1515 |
def do_md_tag_create_multiple(gc, args): |
| 1516 |
"""Create new metadata definitions tags inside a namespace.""" |
| 1517 |
delim = args.delim or ',' |
| 1518 |
tags = [] |
| 1519 |
names_list = args.names.split(delim) |
| 1520 |
for name in names_list: |
| 1521 |
name = name.strip() |
| 1522 |
if name: |
| 1523 |
tags.append(name) |
| 1524 |
|
| 1525 |
if not tags: |
| 1526 |
utils.exit('Please supply at least one tag name. For example: ' |
| 1527 |
'--names Tag1') |
| 1528 |
|
| 1529 |
fields = {'tags': tags, 'append': args.append} |
| 1530 |
new_tags = gc.metadefs_tag.create_multiple(args.namespace, **fields) |
| 1531 |
columns = ['name'] |
| 1532 |
column_settings = { |
| 1533 |
"description": { |
| 1534 |
"max_width": 50, |
| 1535 |
"align": "l" |
| 1536 |
} |
| 1537 |
} |
| 1538 |
utils.print_list(new_tags, columns, field_settings=column_settings) |
| 1539 |
|
| 1540 |
|
| 1541 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1542 |
help=_('Name of the namespace to which the tag belongs.')) |
| 1543 |
@utils.arg('tag', metavar='<TAG>', help=_('Name of the old tag.')) |
| 1544 |
@utils.arg('--name', metavar='<NAME>', default=None, required=True, |
| 1545 |
help=_('New name of the new tag.')) |
| 1546 |
def do_md_tag_update(gc, args): |
| 1547 |
"""Rename a metadata definitions tag inside a namespace.""" |
| 1548 |
name = args.name.strip() |
| 1549 |
if name: |
| 1550 |
fields = {'name': name} |
| 1551 |
new_tag = gc.metadefs_tag.update(args.namespace, args.tag, |
| 1552 |
**fields) |
| 1553 |
_tag_show(new_tag) |
| 1554 |
else: |
| 1555 |
utils.exit('Please supply at least one non-blank tag name.') |
| 1556 |
|
| 1557 |
|
| 1558 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1559 |
help=_('Name of the namespace to which the tag belongs.')) |
| 1560 |
@utils.arg('tag', metavar='<TAG>', help=_('Name of the tag.')) |
| 1561 |
def do_md_tag_show(gc, args): |
| 1562 |
"""Describe a specific metadata definitions tag inside a namespace.""" |
| 1563 |
tag = gc.metadefs_tag.get(args.namespace, args.tag) |
| 1564 |
_tag_show(tag) |
| 1565 |
|
| 1566 |
|
| 1567 |
@utils.arg('namespace', metavar='<NAMESPACE>', |
| 1568 |
help=_('Name of the namespace to which the tag belongs.')) |
| 1569 |
@utils.arg('tag', metavar='<TAG>', help=_('Name of the tag.')) |
| 1570 |
def do_md_tag_delete(gc, args): |
| 1571 |
"""Delete a specific metadata definitions tag inside a namespace.""" |
| 1572 |
gc.metadefs_tag.delete(args.namespace, args.tag) |
| 1573 |
|
| 1574 |
|
| 1575 |
@utils.arg('namespace', metavar='<NAMESPACE>', help=_('Name of namespace.')) |
| 1576 |
def do_md_namespace_tags_delete(gc, args): |
| 1577 |
"""Delete all metadata definitions tags inside a specific namespace.""" |
| 1578 |
gc.metadefs_tag.delete_all(args.namespace) |
| 1579 |
|
| 1580 |
|
| 1581 |
@utils.arg('namespace', metavar='<NAMESPACE>', help=_('Name of namespace.')) |
| 1582 |
def do_md_tag_list(gc, args): |
| 1583 |
"""List metadata definitions tags inside a specific namespace.""" |
| 1584 |
tags = gc.metadefs_tag.list(args.namespace) |
| 1585 |
columns = ['name'] |
| 1586 |
column_settings = { |
| 1587 |
"description": { |
| 1588 |
"max_width": 50, |
| 1589 |
"align": "l" |
| 1590 |
} |
| 1591 |
} |
| 1592 |
utils.print_list(tags, columns, field_settings=column_settings) |
| 1593 |
|
| 1594 |
|
| 1595 |
@utils.arg('--target', default='both', |
| 1596 |
choices=cache.TARGET_VALUES, |
| 1597 |
help=_('Specify which target you want to clear')) |
| 1598 |
def do_cache_clear(gc, args): |
| 1599 |
"""Clear all images from cache, queue or both""" |
| 1600 |
if not gc.endpoint_provided: |
| 1601 |
utils.exit("Direct server endpoint needs to be provided. Do not use " |
| 1602 |
"loadbalanced or catalog endpoints.") |
| 1603 |
try: |
| 1604 |
gc.cache.clear(args.target) |
| 1605 |
except exc.HTTPForbidden: |
| 1606 |
msg = _("You are not permitted to delete image(s) " |
| 1607 |
"from cache.") |
| 1608 |
utils.print_err(msg) |
| 1609 |
except exc.HTTPException as e: |
| 1610 |
msg = _("'%s': Unable to delete image(s) from cache." % e) |
| 1611 |
utils.print_err(msg) |
| 1612 |
|
| 1613 |
|
| 1614 |
@utils.arg('id', metavar='<IMAGE_ID>', nargs='+', |
| 1615 |
help=_('ID of image(s) to delete from cache/queue.')) |
| 1616 |
def do_cache_delete(gc, args): |
| 1617 |
"""Delete image from cache/caching queue.""" |
| 1618 |
if not gc.endpoint_provided: |
| 1619 |
utils.exit("Direct server endpoint needs to be provided. Do not use " |
| 1620 |
"loadbalanced or catalog endpoints.") |
| 1621 |
|
| 1622 |
for args_id in args.id: |
| 1623 |
try: |
| 1624 |
gc.cache.delete(args_id) |
| 1625 |
except exc.HTTPForbidden: |
| 1626 |
msg = _("You are not permitted to delete the image '%s' " |
| 1627 |
"from cache." % args_id) |
| 1628 |
utils.print_err(msg) |
| 1629 |
except exc.HTTPException as e: |
| 1630 |
msg = _("'%s': Unable to delete image '%s' from cache." |
| 1631 |
% (e, args_id)) |
| 1632 |
utils.print_err(msg) |
| 1633 |
|
| 1634 |
|
| 1635 |
def do_cache_list(gc, args): |
| 1636 |
"""Get cache state.""" |
| 1637 |
if not gc.endpoint_provided: |
| 1638 |
utils.exit("Direct server endpoint needs to be provided. Do not use " |
| 1639 |
"loadbalanced or catalog endpoints.") |
| 1640 |
cached_images = gc.cache.list() |
| 1641 |
utils.print_cached_images(cached_images) |
| 1642 |
|
| 1643 |
|
| 1644 |
@utils.arg('id', metavar='<IMAGE_ID>', nargs='+', |
| 1645 |
help=_('ID of image(s) to queue for caching.')) |
| 1646 |
def do_cache_queue(gc, args): |
| 1647 |
"""Queue image(s) for caching.""" |
| 1648 |
if not gc.endpoint_provided: |
| 1649 |
utils.exit("Direct server endpoint needs to be provided. Do not use " |
| 1650 |
"loadbalanced or catalog endpoints.") |
| 1651 |
|
| 1652 |
for args_id in args.id: |
| 1653 |
try: |
| 1654 |
gc.cache.queue(args_id) |
| 1655 |
except exc.HTTPForbidden: |
| 1656 |
msg = _("You are not permitted to queue the image '%s' " |
| 1657 |
"for caching." % args_id) |
| 1658 |
utils.print_err(msg) |
| 1659 |
except exc.HTTPException as e: |
| 1660 |
msg = _("'%s': Unable to queue image '%s' for caching." |
| 1661 |
% (e, args_id)) |
| 1662 |
utils.print_err(msg) |
| 1663 |
|
| 1664 |
|
| 1665 |
@utils.arg('--sort-key', default='status', |
| 1666 |
choices=tasks.SORT_KEY_VALUES, |
| 1667 |
help=_('Sort task list by specified field.')) |
| 1668 |
@utils.arg('--sort-dir', default='desc', |
| 1669 |
choices=tasks.SORT_DIR_VALUES, |
| 1670 |
help=_('Sort task list in specified direction.')) |
| 1671 |
@utils.arg('--page-size', metavar='<SIZE>', default=None, type=int, |
| 1672 |
help=_('Number of tasks to request in each paginated request.')) |
| 1673 |
@utils.arg('--type', metavar='<TYPE>', |
| 1674 |
help=_('Filter tasks to those that have this type.')) |
| 1675 |
@utils.arg('--status', metavar='<STATUS>', |
| 1676 |
help=_('Filter tasks to those that have this status.')) |
| 1677 |
def do_task_list(gc, args): |
| 1678 |
"""List tasks you can access.""" |
| 1679 |
filter_keys = ['type', 'status'] |
| 1680 |
filter_items = [(key, getattr(args, key)) for key in filter_keys] |
| 1681 |
filters = dict([item for item in filter_items if item[1] is not None]) |
| 1682 |
|
| 1683 |
kwargs = {'filters': filters} |
| 1684 |
if args.page_size is not None: |
| 1685 |
kwargs['page_size'] = args.page_size |
| 1686 |
|
| 1687 |
kwargs['sort_key'] = args.sort_key |
| 1688 |
kwargs['sort_dir'] = args.sort_dir |
| 1689 |
|
| 1690 |
tasks = gc.tasks.list(**kwargs) |
| 1691 |
|
| 1692 |
columns = ['ID', 'Type', 'Status', 'Owner'] |
| 1693 |
utils.print_list(tasks, columns) |
| 1694 |
|
| 1695 |
|
| 1696 |
@utils.arg('id', metavar='<TASK_ID>', help=_('ID of task to describe.')) |
| 1697 |
def do_task_show(gc, args): |
| 1698 |
"""Describe a specific task.""" |
| 1699 |
task = gc.tasks.get(args.id) |
| 1700 |
ignore = ['self', 'schema'] |
| 1701 |
task = dict([item for item in task.items() if item[0] not in ignore]) |
| 1702 |
utils.print_dict(task) |
| 1703 |
|
| 1704 |
|
| 1705 |
@utils.arg('--type', metavar='<TYPE>', |
| 1706 |
help=_('Type of Task. Please refer to Glance schema or ' |
| 1707 |
'documentation to see which tasks are supported.')) |
| 1708 |
@utils.arg('--input', metavar='<STRING>', default='{}', |
| 1709 |
help=_('Parameters of the task to be launched')) |
| 1710 |
def do_task_create(gc, args): |
| 1711 |
"""Create a new task.""" |
| 1712 |
if not (args.type and args.input): |
| 1713 |
utils.exit('Unable to create task. Specify task type and input.') |
| 1714 |
else: |
| 1715 |
try: |
| 1716 |
input = json.loads(args.input) |
| 1717 |
except ValueError: |
| 1718 |
utils.exit('Failed to parse the "input" parameter. Must be a ' |
| 1719 |
'valid JSON object.') |
| 1720 |
|
| 1721 |
task_values = {'type': args.type, 'input': input} |
| 1722 |
task = gc.tasks.create(**task_values) |
| 1723 |
ignore = ['self', 'schema'] |
| 1724 |
task = dict([item for item in task.items() |
| 1725 |
if item[0] not in ignore]) |
| 1726 |
utils.print_dict(task) |