| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright 2018 www.privaz.io Valletech AB |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
# Make coding more python3-ish |
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: one_host |
| 12 |
|
| 13 |
short_description: Manages OpenNebula Hosts |
| 14 |
|
| 15 |
|
| 16 |
requirements: |
| 17 |
- pyone |
| 18 |
|
| 19 |
description: |
| 20 |
- Manages OpenNebula Hosts. |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: none |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
|
| 27 |
options: |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Hostname of the machine to manage. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
state: |
| 34 |
description: |
| 35 |
- Takes the host to the desired lifecycle state. |
| 36 |
- If V(absent) the host is deleted from the cluster. |
| 37 |
- If V(present) the host is created in the cluster (includes V(enabled), V(disabled) and V(offline) states). |
| 38 |
- If V(enabled) the host is fully operational. |
| 39 |
- V(disabled), for example to perform maintenance operations. |
| 40 |
- V(offline), host is totally offline. |
| 41 |
choices: |
| 42 |
- absent |
| 43 |
- present |
| 44 |
- enabled |
| 45 |
- disabled |
| 46 |
- offline |
| 47 |
default: present |
| 48 |
type: str |
| 49 |
im_mad_name: |
| 50 |
description: |
| 51 |
- The name of the information manager, this values are taken from the oned.conf with the tag name IM_MAD (name). |
| 52 |
default: kvm |
| 53 |
type: str |
| 54 |
vmm_mad_name: |
| 55 |
description: |
| 56 |
- The name of the virtual machine manager mad name, this values are taken from the oned.conf with the tag name VM_MAD |
| 57 |
(name). |
| 58 |
default: kvm |
| 59 |
type: str |
| 60 |
cluster_id: |
| 61 |
description: |
| 62 |
- The cluster ID. |
| 63 |
default: 0 |
| 64 |
type: int |
| 65 |
cluster_name: |
| 66 |
description: |
| 67 |
- The cluster specified by name. |
| 68 |
type: str |
| 69 |
labels: |
| 70 |
description: |
| 71 |
- The labels for this host. |
| 72 |
type: list |
| 73 |
elements: str |
| 74 |
template: |
| 75 |
description: |
| 76 |
- The template or attribute changes to merge into the host template. |
| 77 |
aliases: |
| 78 |
- attributes |
| 79 |
type: dict |
| 80 |
|
| 81 |
extends_documentation_fragment: |
| 82 |
- community.general._opennebula |
| 83 |
- community.general._attributes |
| 84 |
|
| 85 |
author: |
| 86 |
- Rafael del Valle (@rvalle) |
| 87 |
""" |
| 88 |
|
| 89 |
EXAMPLES = r""" |
| 90 |
- name: Create a new host in OpenNebula |
| 91 |
community.general.one_host: |
| 92 |
name: host1 |
| 93 |
cluster_id: 1 |
| 94 |
api_url: http://127.0.0.1:2633/RPC2 |
| 95 |
|
| 96 |
- name: Create a host and adjust its template |
| 97 |
community.general.one_host: |
| 98 |
name: host2 |
| 99 |
cluster_name: default |
| 100 |
template: |
| 101 |
LABELS: |
| 102 |
- gold |
| 103 |
- ssd |
| 104 |
RESERVED_CPU: -100 |
| 105 |
""" |
| 106 |
|
| 107 |
# TODO: pending setting guidelines on returned values |
| 108 |
RETURN = r""" |
| 109 |
""" |
| 110 |
|
| 111 |
# TODO: Documentation on valid state transitions is required to properly implement all valid cases |
| 112 |
# TODO: To be coherent with CLI this module should also provide "flush" functionality |
| 113 |
|
| 114 |
from ansible_collections.community.general.plugins.module_utils._opennebula import OpenNebulaModule |
| 115 |
|
| 116 |
try: |
| 117 |
from pyone import HOST_STATES, HOST_STATUS |
| 118 |
except ImportError: |
| 119 |
pass # handled at module utils |
| 120 |
|
| 121 |
|
| 122 |
# Pseudo definitions... |
| 123 |
|
| 124 |
HOST_ABSENT = -99 # the host is absent (special case defined by this module) |
| 125 |
|
| 126 |
|
| 127 |
class HostModule(OpenNebulaModule): |
| 128 |
def __init__(self): |
| 129 |
argument_spec = dict( |
| 130 |
name=dict(type="str", required=True), |
| 131 |
state=dict(choices=["present", "absent", "enabled", "disabled", "offline"], default="present"), |
| 132 |
im_mad_name=dict(type="str", default="kvm"), |
| 133 |
vmm_mad_name=dict(type="str", default="kvm"), |
| 134 |
cluster_id=dict(type="int", default=0), |
| 135 |
cluster_name=dict(type="str"), |
| 136 |
labels=dict(type="list", elements="str"), |
| 137 |
template=dict(type="dict", aliases=["attributes"]), |
| 138 |
) |
| 139 |
|
| 140 |
mutually_exclusive = [["cluster_id", "cluster_name"]] |
| 141 |
|
| 142 |
OpenNebulaModule.__init__(self, argument_spec, mutually_exclusive=mutually_exclusive) |
| 143 |
|
| 144 |
def allocate_host(self): |
| 145 |
""" |
| 146 |
Creates a host entry in OpenNebula |
| 147 |
self.one.host.allocate returns ID of a host |
| 148 |
Returns: True on success, fails otherwise. |
| 149 |
|
| 150 |
""" |
| 151 |
try: |
| 152 |
self.one.host.allocate( |
| 153 |
self.get_parameter("name"), |
| 154 |
self.get_parameter("vmm_mad_name"), |
| 155 |
self.get_parameter("im_mad_name"), |
| 156 |
self.get_parameter("cluster_id"), |
| 157 |
) |
| 158 |
self.result["changed"] = True |
| 159 |
except Exception as e: |
| 160 |
self.fail(msg=f"Could not allocate host, ERROR: {e}") |
| 161 |
|
| 162 |
return True |
| 163 |
|
| 164 |
def wait_for_host_state(self, host, target_states): |
| 165 |
""" |
| 166 |
Utility method that waits for a host state. |
| 167 |
Args: |
| 168 |
host: |
| 169 |
target_states: |
| 170 |
|
| 171 |
""" |
| 172 |
return self.wait_for_state( |
| 173 |
"host", |
| 174 |
lambda: self.one.host.info(host.ID).STATE, |
| 175 |
lambda s: HOST_STATES(s).name, |
| 176 |
target_states, |
| 177 |
invalid_states=[HOST_STATES.ERROR, HOST_STATES.MONITORING_ERROR], |
| 178 |
) |
| 179 |
|
| 180 |
def run(self, one, module, result): |
| 181 |
# Get the list of hosts |
| 182 |
host_name = self.get_parameter("name") |
| 183 |
host = self.get_host_by_name(host_name) |
| 184 |
|
| 185 |
# manage host state |
| 186 |
desired_state = self.get_parameter("state") |
| 187 |
if bool(host): |
| 188 |
current_state = host.STATE |
| 189 |
current_state_name = HOST_STATES(host.STATE).name |
| 190 |
else: |
| 191 |
current_state = HOST_ABSENT |
| 192 |
current_state_name = "ABSENT" |
| 193 |
|
| 194 |
# apply properties |
| 195 |
if desired_state == "present": |
| 196 |
if current_state == HOST_ABSENT: |
| 197 |
self.allocate_host() |
| 198 |
host = self.get_host_by_name(host_name) |
| 199 |
self.wait_for_host_state(host, [HOST_STATES.MONITORED]) |
| 200 |
elif current_state in [HOST_STATES.ERROR, HOST_STATES.MONITORING_ERROR]: |
| 201 |
self.fail(msg=f"invalid host state {current_state_name}") |
| 202 |
|
| 203 |
elif desired_state == "enabled": |
| 204 |
if current_state == HOST_ABSENT: |
| 205 |
self.allocate_host() |
| 206 |
host = self.get_host_by_name(host_name) |
| 207 |
self.wait_for_host_state(host, [HOST_STATES.MONITORED]) |
| 208 |
elif current_state in [HOST_STATES.DISABLED, HOST_STATES.OFFLINE]: |
| 209 |
if one.host.status(host.ID, HOST_STATUS.ENABLED): |
| 210 |
self.wait_for_host_state(host, [HOST_STATES.MONITORED]) |
| 211 |
result["changed"] = True |
| 212 |
else: |
| 213 |
self.fail(msg="could not enable host") |
| 214 |
elif current_state in [HOST_STATES.MONITORED]: |
| 215 |
pass |
| 216 |
else: |
| 217 |
self.fail(msg=f"unknown host state {current_state_name}, cowardly refusing to change state to enable") |
| 218 |
|
| 219 |
elif desired_state == "disabled": |
| 220 |
if current_state == HOST_ABSENT: |
| 221 |
self.fail(msg="absent host cannot be put in disabled state") |
| 222 |
elif current_state in [HOST_STATES.MONITORED, HOST_STATES.OFFLINE]: |
| 223 |
# returns host ID integer |
| 224 |
try: |
| 225 |
one.host.status(host.ID, HOST_STATUS.DISABLED) |
| 226 |
result["changed"] = True |
| 227 |
except Exception as e: |
| 228 |
self.fail(msg=f"Could not disable host, ERROR: {e}") |
| 229 |
self.wait_for_host_state(host, [HOST_STATES.DISABLED]) |
| 230 |
elif current_state in [HOST_STATES.DISABLED]: |
| 231 |
pass |
| 232 |
else: |
| 233 |
self.fail(msg=f"unknown host state {current_state_name}, cowardly refusing to change state to disable") |
| 234 |
|
| 235 |
elif desired_state == "offline": |
| 236 |
if current_state == HOST_ABSENT: |
| 237 |
self.fail(msg="absent host cannot be placed in offline state") |
| 238 |
elif current_state in [HOST_STATES.MONITORED, HOST_STATES.DISABLED]: |
| 239 |
# returns host ID integer |
| 240 |
try: |
| 241 |
one.host.status(host.ID, HOST_STATUS.OFFLINE) |
| 242 |
result["changed"] = True |
| 243 |
except Exception as e: |
| 244 |
self.fail(msg=f"Could not set host offline, ERROR: {e}") |
| 245 |
self.wait_for_host_state(host, [HOST_STATES.OFFLINE]) |
| 246 |
elif current_state in [HOST_STATES.OFFLINE]: |
| 247 |
pass |
| 248 |
else: |
| 249 |
self.fail(msg=f"unknown host state {current_state_name}, cowardly refusing to change state to offline") |
| 250 |
|
| 251 |
elif desired_state == "absent": |
| 252 |
if current_state != HOST_ABSENT: |
| 253 |
# returns host ID integer |
| 254 |
try: |
| 255 |
one.host.delete(host.ID) |
| 256 |
result["changed"] = True |
| 257 |
except Exception as e: |
| 258 |
self.fail(msg=f"Could not delete host from cluster, ERROR: {e}") |
| 259 |
|
| 260 |
# if we reach this point we can assume that the host was taken to the desired state |
| 261 |
|
| 262 |
if desired_state != "absent": |
| 263 |
# manipulate or modify the template |
| 264 |
desired_template_changes = self.get_parameter("template") |
| 265 |
|
| 266 |
if desired_template_changes is None: |
| 267 |
desired_template_changes = dict() |
| 268 |
|
| 269 |
# complete the template with specific ansible parameters |
| 270 |
if self.is_parameter("labels"): |
| 271 |
desired_template_changes["LABELS"] = self.get_parameter("labels") |
| 272 |
|
| 273 |
if self.requires_template_update(host.TEMPLATE, desired_template_changes): |
| 274 |
# setup the root element so that pyone will generate XML instead of attribute vector |
| 275 |
desired_template_changes = {"TEMPLATE": desired_template_changes} |
| 276 |
# merge the template, returns host ID integer |
| 277 |
try: |
| 278 |
one.host.update(host.ID, desired_template_changes, 1) |
| 279 |
result["changed"] = True |
| 280 |
except Exception as e: |
| 281 |
self.fail(msg=f"Failed to update the host template, ERROR: {e}") |
| 282 |
|
| 283 |
# the cluster |
| 284 |
if self.get_parameter("cluster_id") != host.CLUSTER_ID: |
| 285 |
# returns cluster id in int |
| 286 |
try: |
| 287 |
one.cluster.addhost(self.get_parameter("cluster_id"), host.ID) |
| 288 |
result["changed"] = True |
| 289 |
except Exception as e: |
| 290 |
self.fail(msg=f"Failed to update the host cluster, ERROR: {e}") |
| 291 |
|
| 292 |
# return |
| 293 |
self.exit() |
| 294 |
|
| 295 |
|
| 296 |
def main(): |
| 297 |
HostModule().run_module() |
| 298 |
|
| 299 |
|
| 300 |
if __name__ == "__main__": |
| 301 |
main() |