|
| 1 | +# Copyright 2013 OpenStack, LLC. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +# |
| 15 | + |
| 16 | +"""Image V1 Action Implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | +import os |
| 20 | +import sys |
| 21 | + |
| 22 | +if os.name == "nt": |
| 23 | + import msvcrt |
| 24 | +else: |
| 25 | + msvcrt = None |
| 26 | + |
| 27 | +from cliff import show |
| 28 | + |
| 29 | + |
| 30 | +class CreateImage(show.ShowOne): |
| 31 | + """Create image command""" |
| 32 | + |
| 33 | + api = "image" |
| 34 | + log = logging.getLogger(__name__ + ".CreateImage") |
| 35 | + |
| 36 | + def get_parser(self, prog_name): |
| 37 | + parser = super(CreateImage, self).get_parser(prog_name) |
| 38 | + parser.add_argument( |
| 39 | + "name", |
| 40 | + metavar="<name>", |
| 41 | + help="Name of image.") |
| 42 | + parser.add_argument( |
| 43 | + "--disk_format", |
| 44 | + default="raw", |
| 45 | + metavar="<disk_format>", |
| 46 | + help="Disk format of image.") |
| 47 | + parser.add_argument( |
| 48 | + "--id", |
| 49 | + metavar="<id>", |
| 50 | + help="ID of image to reserve.") |
| 51 | + parser.add_argument( |
| 52 | + "--store", |
| 53 | + metavar="<store>", |
| 54 | + help="Store to upload image to.") |
| 55 | + parser.add_argument( |
| 56 | + "--container-format", |
| 57 | + default="bare", |
| 58 | + metavar="<container_format>", |
| 59 | + help="Container format of image.") |
| 60 | + parser.add_argument( |
| 61 | + "--owner", |
| 62 | + metavar="<tenant_id>", |
| 63 | + help="Owner of the image.") |
| 64 | + parser.add_argument( |
| 65 | + "--size", |
| 66 | + metavar="<size>", |
| 67 | + help="Size of image in bytes. Only used with --location and" |
| 68 | + " --copy-from.") |
| 69 | + parser.add_argument( |
| 70 | + "--min-disk", |
| 71 | + metavar="<disk_gb>", |
| 72 | + help="Minimum size of disk needed to boot image in gigabytes.") |
| 73 | + parser.add_argument( |
| 74 | + "--min-ram", |
| 75 | + metavar="<disk_ram>", |
| 76 | + help="Minimum amount of ram needed to boot image in megabytes.") |
| 77 | + parser.add_argument( |
| 78 | + "--location", |
| 79 | + metavar="<image_url>", |
| 80 | + help="URL where the data for this image already resides.") |
| 81 | + parser.add_argument( |
| 82 | + "--file", |
| 83 | + metavar="<file>", |
| 84 | + help="Local file that contains disk image.") |
| 85 | + parser.add_argument( |
| 86 | + "--checksum", |
| 87 | + metavar="<checksum>", |
| 88 | + help="Hash of image data used for verification.") |
| 89 | + parser.add_argument( |
| 90 | + "--copy-from", |
| 91 | + metavar="<image_url>", |
| 92 | + help="Similar to --location, but this indicates that the image" |
| 93 | + " should immediately be copied from the data store.") |
| 94 | + parser.add_argument( |
| 95 | + "--metadata", |
| 96 | + metavar="<key=value>", |
| 97 | + default=[], |
| 98 | + action="append", |
| 99 | + help="Arbitrary metadata to associate with image.") |
| 100 | + protected_group = parser.add_mutually_exclusive_group() |
| 101 | + protected_group.add_argument( |
| 102 | + "--protected", |
| 103 | + dest="protected", |
| 104 | + action="store_true", |
| 105 | + help="Prevent image from being deleted (default: False).") |
| 106 | + protected_group.add_argument( |
| 107 | + "--unprotected", |
| 108 | + dest="protected", |
| 109 | + action="store_false", |
| 110 | + default=False, |
| 111 | + help="Allow images to be deleted (default: True).") |
| 112 | + public_group = parser.add_mutually_exclusive_group() |
| 113 | + public_group.add_argument( |
| 114 | + "--public", |
| 115 | + dest="is_public", |
| 116 | + action="store_true", |
| 117 | + default=True, |
| 118 | + help="Image is accessible to the public (default).") |
| 119 | + public_group.add_argument( |
| 120 | + "--private", |
| 121 | + dest="is_public", |
| 122 | + action="store_false", |
| 123 | + help="Image is inaccessible to the public.") |
| 124 | + return parser |
| 125 | + |
| 126 | + def take_action(self, parsed_args): |
| 127 | + self.log.debug("take_action(%s)" % parsed_args) |
| 128 | + |
| 129 | + # NOTE(jk0): Since create() takes kwargs, it's easiest to just make a |
| 130 | + # copy of parsed_args and remove what we don't need. |
| 131 | + args = vars(parsed_args) |
| 132 | + args = dict(filter(lambda x: x[1] is not None, args.items())) |
| 133 | + args.pop("columns") |
| 134 | + args.pop("formatter") |
| 135 | + args.pop("prefix") |
| 136 | + args.pop("variables") |
| 137 | + |
| 138 | + args["properties"] = {} |
| 139 | + for _metadata in args.pop("metadata"): |
| 140 | + key, value = _metadata.split("=", 1) |
| 141 | + args["properties"][key] = value |
| 142 | + |
| 143 | + if "location" not in args and "copy_from" not in args: |
| 144 | + if "file" in args: |
| 145 | + args["data"] = open(args.pop("file"), "rb") |
| 146 | + else: |
| 147 | + args["data"] = None |
| 148 | + if sys.stdin.isatty() is not True: |
| 149 | + if msvcrt: |
| 150 | + msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) |
| 151 | + args["data"] = sys.stdin |
| 152 | + |
| 153 | + image_client = self.app.client_manager.image |
| 154 | + data = image_client.images.create(**args)._info.copy() |
| 155 | + |
| 156 | + return zip(*sorted(data.iteritems())) |
0 commit comments