| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2018, Milan Ilic <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
# Make coding more python3-ish |
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: one_image |
| 11 |
short_description: Manages OpenNebula images |
| 12 |
description: |
| 13 |
- Manages OpenNebula images. |
| 14 |
requirements: |
| 15 |
- pyone |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.general._opennebula |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: full |
| 22 |
diff_mode: |
| 23 |
support: none |
| 24 |
options: |
| 25 |
id: |
| 26 |
description: |
| 27 |
- A O(id) of the image you would like to manage. |
| 28 |
type: int |
| 29 |
name: |
| 30 |
description: |
| 31 |
- A O(name) of the image you would like to manage. |
| 32 |
- Required if O(create=true). |
| 33 |
type: str |
| 34 |
state: |
| 35 |
description: |
| 36 |
- V(present) - state that is used to manage the image. |
| 37 |
- V(absent) - delete the image. |
| 38 |
- V(cloned) - clone the image. |
| 39 |
- V(renamed) - rename the image to the O(new_name). |
| 40 |
choices: ["present", "absent", "cloned", "renamed"] |
| 41 |
default: present |
| 42 |
type: str |
| 43 |
enabled: |
| 44 |
description: |
| 45 |
- Whether the image should be enabled or disabled. |
| 46 |
type: bool |
| 47 |
new_name: |
| 48 |
description: |
| 49 |
- A name that is assigned to the existing or new image. |
| 50 |
- In the case of cloning, by default O(new_name) is set to the name of the origin image with the prefix 'Copy of'. |
| 51 |
type: str |
| 52 |
persistent: |
| 53 |
description: |
| 54 |
- Whether the image should be persistent or non-persistent. |
| 55 |
type: bool |
| 56 |
version_added: 9.5.0 |
| 57 |
create: |
| 58 |
description: |
| 59 |
- Whether the image should be created if not present. |
| 60 |
- This is ignored if O(state=absent). |
| 61 |
type: bool |
| 62 |
version_added: 10.0.0 |
| 63 |
template: |
| 64 |
description: |
| 65 |
- Use with O(create=true) to specify image template. |
| 66 |
type: str |
| 67 |
version_added: 10.0.0 |
| 68 |
datastore_id: |
| 69 |
description: |
| 70 |
- Use with O(create=true) to specify datastore for image. |
| 71 |
type: int |
| 72 |
version_added: 10.0.0 |
| 73 |
wait_timeout: |
| 74 |
description: |
| 75 |
- Seconds to wait until image is ready, deleted or cloned. |
| 76 |
type: int |
| 77 |
default: 60 |
| 78 |
version_added: 10.0.0 |
| 79 |
author: |
| 80 |
- "Milan Ilic (@ilicmilan)" |
| 81 |
""" |
| 82 |
|
| 83 |
EXAMPLES = r""" |
| 84 |
- name: Fetch the IMAGE by id |
| 85 |
community.general.one_image: |
| 86 |
id: 45 |
| 87 |
register: result |
| 88 |
|
| 89 |
- name: Print the IMAGE properties |
| 90 |
ansible.builtin.debug: |
| 91 |
var: result |
| 92 |
|
| 93 |
- name: Rename existing IMAGE |
| 94 |
community.general.one_image: |
| 95 |
id: 34 |
| 96 |
state: renamed |
| 97 |
new_name: bar-image |
| 98 |
|
| 99 |
- name: Disable the IMAGE by id |
| 100 |
community.general.one_image: |
| 101 |
id: 37 |
| 102 |
enabled: false |
| 103 |
|
| 104 |
- name: Make the IMAGE persistent |
| 105 |
community.general.one_image: |
| 106 |
id: 37 |
| 107 |
persistent: true |
| 108 |
|
| 109 |
- name: Enable the IMAGE by name |
| 110 |
community.general.one_image: |
| 111 |
name: bar-image |
| 112 |
enabled: true |
| 113 |
|
| 114 |
- name: Clone the IMAGE by name |
| 115 |
community.general.one_image: |
| 116 |
name: bar-image |
| 117 |
state: cloned |
| 118 |
new_name: bar-image-clone |
| 119 |
register: result |
| 120 |
|
| 121 |
- name: Delete the IMAGE by id |
| 122 |
community.general.one_image: |
| 123 |
id: '{{ result.id }}' |
| 124 |
state: absent |
| 125 |
|
| 126 |
- name: Make sure IMAGE is present |
| 127 |
community.general.one_image: |
| 128 |
name: myyy-image |
| 129 |
state: present |
| 130 |
create: true |
| 131 |
datastore_id: 100 |
| 132 |
template: | |
| 133 |
PATH = "/var/tmp/image" |
| 134 |
TYPE = "OS" |
| 135 |
SIZE = 20512 |
| 136 |
FORMAT = "qcow2" |
| 137 |
PERSISTENT = "Yes" |
| 138 |
DEV_PREFIX = "vd" |
| 139 |
|
| 140 |
- name: Make sure IMAGE is present with a longer timeout |
| 141 |
community.general.one_image: |
| 142 |
name: big-image |
| 143 |
state: present |
| 144 |
create: true |
| 145 |
datastore_id: 100 |
| 146 |
wait_timeout: 900 |
| 147 |
template: |- |
| 148 |
PATH = "https://192.0.2.200/repo/tipa_image.raw" |
| 149 |
TYPE = "OS" |
| 150 |
SIZE = 82048 |
| 151 |
FORMAT = "raw" |
| 152 |
PERSISTENT = "Yes" |
| 153 |
DEV_PREFIX = "vd" |
| 154 |
""" |
| 155 |
|
| 156 |
RETURN = r""" |
| 157 |
id: |
| 158 |
description: Image ID. |
| 159 |
type: int |
| 160 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 161 |
sample: 153 |
| 162 |
name: |
| 163 |
description: Image name. |
| 164 |
type: str |
| 165 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 166 |
sample: app1 |
| 167 |
group_id: |
| 168 |
description: Image's group ID. |
| 169 |
type: int |
| 170 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 171 |
sample: 1 |
| 172 |
group_name: |
| 173 |
description: Image's group name. |
| 174 |
type: str |
| 175 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 176 |
sample: one-users |
| 177 |
owner_id: |
| 178 |
description: Image's owner ID. |
| 179 |
type: int |
| 180 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 181 |
sample: 143 |
| 182 |
owner_name: |
| 183 |
description: Image's owner name. |
| 184 |
type: str |
| 185 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 186 |
sample: ansible-test |
| 187 |
state: |
| 188 |
description: State of image instance. |
| 189 |
type: str |
| 190 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 191 |
sample: READY |
| 192 |
used: |
| 193 |
description: Is image in use. |
| 194 |
type: bool |
| 195 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 196 |
sample: true |
| 197 |
running_vms: |
| 198 |
description: Count of running vms that use this image. |
| 199 |
type: int |
| 200 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 201 |
sample: 7 |
| 202 |
permissions: |
| 203 |
description: The image's permissions. |
| 204 |
type: dict |
| 205 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 206 |
version_added: 9.5.0 |
| 207 |
contains: |
| 208 |
owner_u: |
| 209 |
description: The image's owner USAGE permissions. |
| 210 |
type: str |
| 211 |
sample: 1 |
| 212 |
owner_m: |
| 213 |
description: The image's owner MANAGE permissions. |
| 214 |
type: str |
| 215 |
sample: 0 |
| 216 |
owner_a: |
| 217 |
description: The image's owner ADMIN permissions. |
| 218 |
type: str |
| 219 |
sample: 0 |
| 220 |
group_u: |
| 221 |
description: The image's group USAGE permissions. |
| 222 |
type: str |
| 223 |
sample: 0 |
| 224 |
group_m: |
| 225 |
description: The image's group MANAGE permissions. |
| 226 |
type: str |
| 227 |
sample: 0 |
| 228 |
group_a: |
| 229 |
description: The image's group ADMIN permissions. |
| 230 |
type: str |
| 231 |
sample: 0 |
| 232 |
other_u: |
| 233 |
description: The image's other users USAGE permissions. |
| 234 |
type: str |
| 235 |
sample: 0 |
| 236 |
other_m: |
| 237 |
description: The image's other users MANAGE permissions. |
| 238 |
type: str |
| 239 |
sample: 0 |
| 240 |
other_a: |
| 241 |
description: The image's other users ADMIN permissions. |
| 242 |
type: str |
| 243 |
sample: 0 |
| 244 |
sample: |
| 245 |
owner_u: 1 |
| 246 |
owner_m: 0 |
| 247 |
owner_a: 0 |
| 248 |
group_u: 0 |
| 249 |
group_m: 0 |
| 250 |
group_a: 0 |
| 251 |
other_u: 0 |
| 252 |
other_m: 0 |
| 253 |
other_a: 0 |
| 254 |
type: |
| 255 |
description: The image's type. |
| 256 |
type: str |
| 257 |
sample: 0 |
| 258 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 259 |
version_added: 9.5.0 |
| 260 |
disk_type: |
| 261 |
description: The image's format type. |
| 262 |
type: str |
| 263 |
sample: 0 |
| 264 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 265 |
version_added: 9.5.0 |
| 266 |
persistent: |
| 267 |
description: The image's persistence status (1 means true, 0 means false). |
| 268 |
type: int |
| 269 |
sample: 1 |
| 270 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 271 |
version_added: 9.5.0 |
| 272 |
source: |
| 273 |
description: The image's source. |
| 274 |
type: str |
| 275 |
sample: /var/lib/one//datastores/100/somerandomstringxd |
| 276 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 277 |
path: |
| 278 |
description: The image's filesystem path. |
| 279 |
type: str |
| 280 |
sample: /var/tmp/hello.qcow2 |
| 281 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 282 |
version_added: 9.5.0 |
| 283 |
fstype: |
| 284 |
description: The image's filesystem type. |
| 285 |
type: str |
| 286 |
sample: ext4 |
| 287 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 288 |
version_added: 9.5.0 |
| 289 |
size: |
| 290 |
description: The image's size in MegaBytes. |
| 291 |
type: int |
| 292 |
sample: 10000 |
| 293 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 294 |
version_added: 9.5.0 |
| 295 |
cloning_ops: |
| 296 |
description: The image's cloning operations per second. |
| 297 |
type: int |
| 298 |
sample: 0 |
| 299 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 300 |
version_added: 9.5.0 |
| 301 |
cloning_id: |
| 302 |
description: The image's cloning ID. |
| 303 |
type: int |
| 304 |
sample: -1 |
| 305 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 306 |
version_added: 9.5.0 |
| 307 |
target_snapshot: |
| 308 |
description: The image's target snapshot. |
| 309 |
type: int |
| 310 |
sample: 1 |
| 311 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 312 |
version_added: 9.5.0 |
| 313 |
datastore_id: |
| 314 |
description: The image's datastore ID. |
| 315 |
type: int |
| 316 |
sample: 100 |
| 317 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 318 |
version_added: 9.5.0 |
| 319 |
datastore: |
| 320 |
description: The image's datastore name. |
| 321 |
type: int |
| 322 |
sample: image_datastore |
| 323 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 324 |
version_added: 9.5.0 |
| 325 |
vms: |
| 326 |
description: The image's list of VM ID's. |
| 327 |
type: list |
| 328 |
elements: int |
| 329 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 330 |
sample: |
| 331 |
- 1 |
| 332 |
- 2 |
| 333 |
- 3 |
| 334 |
version_added: 9.5.0 |
| 335 |
clones: |
| 336 |
description: The image's list of clones ID's. |
| 337 |
type: list |
| 338 |
elements: int |
| 339 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 340 |
sample: |
| 341 |
- 1 |
| 342 |
- 2 |
| 343 |
- 3 |
| 344 |
version_added: 9.5.0 |
| 345 |
app_clones: |
| 346 |
description: The image's list of app_clones ID's. |
| 347 |
type: list |
| 348 |
elements: int |
| 349 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 350 |
sample: |
| 351 |
- 1 |
| 352 |
- 2 |
| 353 |
- 3 |
| 354 |
version_added: 9.5.0 |
| 355 |
snapshots: |
| 356 |
description: The image's list of snapshots. |
| 357 |
type: list |
| 358 |
returned: when O(state=present), O(state=cloned), or O(state=renamed) |
| 359 |
version_added: 9.5.0 |
| 360 |
sample: |
| 361 |
- date: 123123 |
| 362 |
parent: 1 |
| 363 |
size: 10228 |
| 364 |
allow_orphans: 1 |
| 365 |
children: 0 |
| 366 |
active: 1 |
| 367 |
name: SampleName |
| 368 |
""" |
| 369 |
|
| 370 |
|
| 371 |
import time |
| 372 |
|
| 373 |
from ansible_collections.community.general.plugins.module_utils._opennebula import OpenNebulaModule |
| 374 |
|
| 375 |
IMAGE_STATES = [ |
| 376 |
"INIT", |
| 377 |
"READY", |
| 378 |
"USED", |
| 379 |
"DISABLED", |
| 380 |
"LOCKED", |
| 381 |
"ERROR", |
| 382 |
"CLONE", |
| 383 |
"DELETE", |
| 384 |
"USED_PERS", |
| 385 |
"LOCKED_USED", |
| 386 |
"LOCKED_USED_PERS", |
| 387 |
] |
| 388 |
|
| 389 |
|
| 390 |
class ImageModule(OpenNebulaModule): |
| 391 |
def __init__(self): |
| 392 |
argument_spec = dict( |
| 393 |
id=dict(type="int"), |
| 394 |
name=dict(type="str"), |
| 395 |
state=dict(type="str", choices=["present", "absent", "cloned", "renamed"], default="present"), |
| 396 |
enabled=dict(type="bool"), |
| 397 |
new_name=dict(type="str"), |
| 398 |
persistent=dict(type="bool"), |
| 399 |
create=dict(type="bool"), |
| 400 |
template=dict(type="str"), |
| 401 |
datastore_id=dict(type="int"), |
| 402 |
wait_timeout=dict(type="int", default=60), |
| 403 |
) |
| 404 |
required_if = [ |
| 405 |
["state", "renamed", ["id"]], |
| 406 |
["create", True, ["template", "datastore_id", "name"]], |
| 407 |
] |
| 408 |
mutually_exclusive = [ |
| 409 |
["id", "name"], |
| 410 |
] |
| 411 |
|
| 412 |
OpenNebulaModule.__init__( |
| 413 |
self, |
| 414 |
argument_spec, |
| 415 |
supports_check_mode=True, |
| 416 |
mutually_exclusive=mutually_exclusive, |
| 417 |
required_if=required_if, |
| 418 |
) |
| 419 |
|
| 420 |
def run(self, one, module, result): |
| 421 |
params = module.params |
| 422 |
id = params.get("id") |
| 423 |
name = params.get("name") |
| 424 |
desired_state = params.get("state") |
| 425 |
enabled = params.get("enabled") |
| 426 |
new_name = params.get("new_name") |
| 427 |
persistent = params.get("persistent") |
| 428 |
create = params.get("create") |
| 429 |
template = params.get("template") |
| 430 |
datastore_id = params.get("datastore_id") |
| 431 |
wait_timeout = params.get("wait_timeout") |
| 432 |
|
| 433 |
self.result = {} |
| 434 |
|
| 435 |
image = self.get_image_instance(id, name) |
| 436 |
if not image and desired_state != "absent": |
| 437 |
if create: |
| 438 |
self.result = self.create_image(name, template, datastore_id, wait_timeout) |
| 439 |
# Using 'if id:' doesn't work properly when id=0 |
| 440 |
elif id is not None: |
| 441 |
module.fail_json(msg=f"There is no image with id={id}") |
| 442 |
elif name is not None: |
| 443 |
module.fail_json(msg=f"There is no image with name={name}") |
| 444 |
|
| 445 |
if desired_state == "absent": |
| 446 |
self.result = self.delete_image(image, wait_timeout) |
| 447 |
else: |
| 448 |
if persistent is not None: |
| 449 |
self.result = self.change_persistence(image, persistent) |
| 450 |
if enabled is not None: |
| 451 |
self.result = self.enable_image(image, enabled) |
| 452 |
if desired_state == "cloned": |
| 453 |
self.result = self.clone_image(image, new_name, wait_timeout) |
| 454 |
elif desired_state == "renamed": |
| 455 |
self.result = self.rename_image(image, new_name) |
| 456 |
|
| 457 |
self.exit() |
| 458 |
|
| 459 |
def get_image(self, predicate): |
| 460 |
# Filter -2 means fetch all images user can Use |
| 461 |
pool = self.one.imagepool.info(-2, -1, -1, -1) |
| 462 |
|
| 463 |
for image in pool.IMAGE: |
| 464 |
if predicate(image): |
| 465 |
return image |
| 466 |
|
| 467 |
return None |
| 468 |
|
| 469 |
def get_image_by_name(self, image_name): |
| 470 |
return self.get_image(lambda image: image_name == image.NAME) |
| 471 |
|
| 472 |
def get_image_by_id(self, image_id): |
| 473 |
return self.get_image(lambda image: image_id == image.ID) |
| 474 |
|
| 475 |
def get_image_instance(self, requested_id, requested_name): |
| 476 |
# Using 'if requested_id:' doesn't work properly when requested_id=0 |
| 477 |
if requested_id is not None: |
| 478 |
return self.get_image_by_id(requested_id) |
| 479 |
else: |
| 480 |
return self.get_image_by_name(requested_name) |
| 481 |
|
| 482 |
def create_image(self, image_name, template, datastore_id, wait_timeout): |
| 483 |
if not self.module.check_mode: |
| 484 |
image_id = self.one.image.allocate(f'NAME = "{image_name}"\n{template}', datastore_id) |
| 485 |
self.wait_for_ready(image_id, wait_timeout) |
| 486 |
image = self.get_image_by_id(image_id) |
| 487 |
result = self.get_image_info(image) |
| 488 |
|
| 489 |
result["changed"] = True |
| 490 |
return result |
| 491 |
|
| 492 |
def wait_for_ready(self, image_id, wait_timeout=60): |
| 493 |
start_time = time.time() |
| 494 |
|
| 495 |
while (time.time() - start_time) < wait_timeout: |
| 496 |
image = self.one.image.info(image_id) |
| 497 |
state = image.STATE |
| 498 |
|
| 499 |
if state in [IMAGE_STATES.index("ERROR")]: |
| 500 |
self.module.fail_json(msg=f"Got an ERROR state: {image.TEMPLATE['ERROR']}") |
| 501 |
|
| 502 |
if state in [IMAGE_STATES.index("READY")]: |
| 503 |
return True |
| 504 |
|
| 505 |
time.sleep(1) |
| 506 |
self.module.fail_json(msg="Wait timeout has expired!") |
| 507 |
|
| 508 |
def wait_for_delete(self, image_id, wait_timeout=60): |
| 509 |
start_time = time.time() |
| 510 |
|
| 511 |
while (time.time() - start_time) < wait_timeout: |
| 512 |
# It might be already deleted by the time this function is called |
| 513 |
try: |
| 514 |
image = self.one.image.info(image_id) |
| 515 |
except Exception: |
| 516 |
check_image = self.get_image_instance(image_id) |
| 517 |
if not check_image: |
| 518 |
return True |
| 519 |
|
| 520 |
state = image.STATE |
| 521 |
|
| 522 |
if state in [IMAGE_STATES.index("DELETE")]: |
| 523 |
return True |
| 524 |
|
| 525 |
time.sleep(1) |
| 526 |
|
| 527 |
self.module.fail_json(msg="Wait timeout has expired!") |
| 528 |
|
| 529 |
def enable_image(self, image, enable): |
| 530 |
image = self.one.image.info(image.ID) |
| 531 |
changed = False |
| 532 |
|
| 533 |
state = image.STATE |
| 534 |
|
| 535 |
if state not in [IMAGE_STATES.index("READY"), IMAGE_STATES.index("DISABLED"), IMAGE_STATES.index("ERROR")]: |
| 536 |
if enable: |
| 537 |
self.module.fail_json(msg=f"Cannot enable {IMAGE_STATES[state]} image!") |
| 538 |
else: |
| 539 |
self.module.fail_json(msg=f"Cannot disable {IMAGE_STATES[state]} image!") |
| 540 |
|
| 541 |
if (enable and state != IMAGE_STATES.index("READY")) or ( |
| 542 |
not enable and state != IMAGE_STATES.index("DISABLED") |
| 543 |
): |
| 544 |
changed = True |
| 545 |
|
| 546 |
if changed and not self.module.check_mode: |
| 547 |
self.one.image.enable(image.ID, enable) |
| 548 |
|
| 549 |
result = self.get_image_info(image) |
| 550 |
result["changed"] = changed |
| 551 |
|
| 552 |
return result |
| 553 |
|
| 554 |
def change_persistence(self, image, enable): |
| 555 |
image = self.one.image.info(image.ID) |
| 556 |
changed = False |
| 557 |
|
| 558 |
state = image.STATE |
| 559 |
|
| 560 |
if state not in [IMAGE_STATES.index("READY"), IMAGE_STATES.index("DISABLED"), IMAGE_STATES.index("ERROR")]: |
| 561 |
if enable: |
| 562 |
self.module.fail_json(msg=f"Cannot enable persistence for {IMAGE_STATES[state]} image!") |
| 563 |
else: |
| 564 |
self.module.fail_json(msg=f"Cannot disable persistence for {IMAGE_STATES[state]} image!") |
| 565 |
|
| 566 |
if (enable and state != IMAGE_STATES.index("READY")) or ( |
| 567 |
not enable and state != IMAGE_STATES.index("DISABLED") |
| 568 |
): |
| 569 |
changed = True |
| 570 |
|
| 571 |
if changed and not self.module.check_mode: |
| 572 |
self.one.image.persistent(image.ID, enable) |
| 573 |
|
| 574 |
result = self.get_image_info(image) |
| 575 |
result["changed"] = changed |
| 576 |
|
| 577 |
return result |
| 578 |
|
| 579 |
def clone_image(self, image, new_name, wait_timeout): |
| 580 |
if new_name is None: |
| 581 |
new_name = f"Copy of {image.NAME}" |
| 582 |
|
| 583 |
tmp_image = self.get_image_by_name(new_name) |
| 584 |
if tmp_image: |
| 585 |
result = self.get_image_info(image) |
| 586 |
result["changed"] = False |
| 587 |
return result |
| 588 |
|
| 589 |
if IMAGE_STATES.index("DISABLED") == image.STATE: |
| 590 |
self.module.fail_json(msg="Cannot clone DISABLED image") |
| 591 |
|
| 592 |
if not self.module.check_mode: |
| 593 |
new_id = self.one.image.clone(image.ID, new_name) |
| 594 |
self.wait_for_ready(new_id, wait_timeout) |
| 595 |
image = self.one.image.info(new_id) |
| 596 |
|
| 597 |
result = self.get_image_info(image) |
| 598 |
result["changed"] = True |
| 599 |
|
| 600 |
return result |
| 601 |
|
| 602 |
def rename_image(self, image, new_name): |
| 603 |
if new_name is None: |
| 604 |
self.module.fail_json(msg="'new_name' option has to be specified when the state is 'renamed'") |
| 605 |
|
| 606 |
if new_name == image.NAME: |
| 607 |
result = self.get_image_info(image) |
| 608 |
result["changed"] = False |
| 609 |
return result |
| 610 |
|
| 611 |
tmp_image = self.get_image_by_name(new_name) |
| 612 |
if tmp_image: |
| 613 |
self.module.fail_json(msg=f"Name '{new_name}' is already taken by IMAGE with id={tmp_image.ID!s}") |
| 614 |
|
| 615 |
if not self.module.check_mode: |
| 616 |
self.one.image.rename(image.ID, new_name) |
| 617 |
|
| 618 |
result = self.get_image_info(image) |
| 619 |
result["changed"] = True |
| 620 |
return result |
| 621 |
|
| 622 |
def delete_image(self, image, wait_timeout): |
| 623 |
if not image: |
| 624 |
return {"changed": False} |
| 625 |
|
| 626 |
if image.RUNNING_VMS > 0: |
| 627 |
self.module.fail_json(msg=f"Cannot delete image. There are {image.RUNNING_VMS!s} VMs using it.") |
| 628 |
|
| 629 |
if not self.module.check_mode: |
| 630 |
self.one.image.delete(image.ID) |
| 631 |
self.wait_for_delete(image.ID, wait_timeout) |
| 632 |
|
| 633 |
return {"changed": True} |
| 634 |
|
| 635 |
|
| 636 |
def main(): |
| 637 |
ImageModule().run_module() |
| 638 |
|
| 639 |
|
| 640 |
if __name__ == "__main__": |
| 641 |
main() |