| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2019 Felix Fontein <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: boot |
| 14 |
short_description: Set boot configuration |
| 15 |
version_added: 1.2.0 |
| 16 |
author: |
| 17 |
- Felix Fontein (@felixfontein) |
| 18 |
description: |
| 19 |
- Set the boot configuration for a dedicated server. |
| 20 |
seealso: |
| 21 |
- module: community.hrobot.ssh_key |
| 22 |
description: Add, remove or update SSH key. |
| 23 |
- module: community.hrobot.ssh_key_info |
| 24 |
description: Query information on SSH keys. |
| 25 |
extends_documentation_fragment: |
| 26 |
- community.hrobot.robot |
| 27 |
- community.hrobot.attributes |
| 28 |
- community.hrobot.attributes.actiongroup_robot |
| 29 |
|
| 30 |
attributes: |
| 31 |
action_group: |
| 32 |
version_added: 1.6.0 |
| 33 |
check_mode: |
| 34 |
support: full |
| 35 |
diff_mode: |
| 36 |
support: none |
| 37 |
idempotent: |
| 38 |
support: full |
| 39 |
|
| 40 |
options: |
| 41 |
server_number: |
| 42 |
description: |
| 43 |
- The server number of the server whose boot configuration to adjust. |
| 44 |
type: int |
| 45 |
required: true |
| 46 |
regular_boot: |
| 47 |
description: |
| 48 |
- If this option is provided, all special boot configurations are removed and the installed operating system will be |
| 49 |
booted up next (assuming it is bootable). |
| 50 |
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc), O(install_windows), O(install_plesk), |
| 51 |
and O(install_cpanel) must be provided. |
| 52 |
type: bool |
| 53 |
choices: |
| 54 |
- true |
| 55 |
rescue: |
| 56 |
description: |
| 57 |
- If this option is provided, the rescue system will be activated for the next boot. |
| 58 |
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc), O(install_windows), O(install_plesk), |
| 59 |
and O(install_cpanel) must be provided. |
| 60 |
type: dict |
| 61 |
suboptions: |
| 62 |
os: |
| 63 |
description: |
| 64 |
- The operating system to use for the rescue system. Possible choices can change over time. |
| 65 |
- Currently, V(linux), V(linuxold), V(freebsd), V(freebsdold), V(freebsdax), V(freebsdbetaax), V(vkvm), and V(vkvmold) |
| 66 |
seem to be available. |
| 67 |
type: str |
| 68 |
required: true |
| 69 |
arch: |
| 70 |
description: |
| 71 |
- The architecture to use for the rescue system. |
| 72 |
- Not all architectures are available for all operating systems. |
| 73 |
- Defaults to V(64). |
| 74 |
- This option is deprecated and will be removed in community.hrobot 3.0.0. |
| 75 |
type: int |
| 76 |
choices: |
| 77 |
- 32 |
| 78 |
- 64 |
| 79 |
authorized_keys: |
| 80 |
description: |
| 81 |
- One or more SSH key fingerprints to equip the rescue system with. You can also specify the public key itself, |
| 82 |
the module will compute its fingerprint and pass it on to the Robot API. |
| 83 |
- Only fingerprints for SSH keys deposited in the Robot API can be used. |
| 84 |
- You can use the M(community.hrobot.ssh_key_info) module to query the SSH keys you can use, and the M(community.hrobot.ssh_key) |
| 85 |
module to add or update SSH keys. |
| 86 |
type: list |
| 87 |
elements: str |
| 88 |
install_linux: |
| 89 |
description: |
| 90 |
- If this option is provided, a Linux system install will be activated for the next boot. |
| 91 |
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc), O(install_windows), O(install_plesk), |
| 92 |
and O(install_cpanel) must be provided. |
| 93 |
type: dict |
| 94 |
suboptions: |
| 95 |
dist: |
| 96 |
description: |
| 97 |
- The distribution to install. |
| 98 |
type: str |
| 99 |
required: true |
| 100 |
arch: |
| 101 |
description: |
| 102 |
- The architecture to use for the install. |
| 103 |
- Not all architectures are available for all distributions. |
| 104 |
- Defaults to V(64). |
| 105 |
- This option is deprecated and will be removed in community.hrobot 3.0.0. |
| 106 |
type: int |
| 107 |
choices: |
| 108 |
- 32 |
| 109 |
- 64 |
| 110 |
lang: |
| 111 |
description: |
| 112 |
- The language to use for the operating system. |
| 113 |
type: str |
| 114 |
required: true |
| 115 |
authorized_keys: |
| 116 |
description: |
| 117 |
- One or more SSH key fingerprints to equip the rescue system with. You can also specify the public key itself, |
| 118 |
the module will compute its fingerprint and pass it on to the Robot API. |
| 119 |
- Only fingerprints for SSH keys deposited in the Robot API can be used. |
| 120 |
- You can use the M(community.hrobot.ssh_key_info) module to query the SSH keys you can use, and the M(community.hrobot.ssh_key) |
| 121 |
module to add or update SSH keys. |
| 122 |
type: list |
| 123 |
elements: str |
| 124 |
install_vnc: |
| 125 |
description: |
| 126 |
- If this option is provided, a VNC installation will be activated for the next boot. |
| 127 |
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc), O(install_windows), O(install_plesk), |
| 128 |
and O(install_cpanel) must be provided. |
| 129 |
type: dict |
| 130 |
suboptions: |
| 131 |
dist: |
| 132 |
description: |
| 133 |
- The distribution to install. |
| 134 |
type: str |
| 135 |
required: true |
| 136 |
arch: |
| 137 |
description: |
| 138 |
- The architecture to use for the install. |
| 139 |
- Not all architectures are available for all distributions. |
| 140 |
- Defaults to V(64). |
| 141 |
- This option is deprecated and will be removed in community.hrobot 3.0.0. |
| 142 |
type: int |
| 143 |
choices: |
| 144 |
- 32 |
| 145 |
- 64 |
| 146 |
lang: |
| 147 |
description: |
| 148 |
- The language to use for the operating system. |
| 149 |
type: str |
| 150 |
required: true |
| 151 |
install_windows: |
| 152 |
description: |
| 153 |
- If this option is provided, a Windows installation will be activated for the next boot. |
| 154 |
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc), O(install_windows), O(install_plesk), |
| 155 |
and O(install_cpanel) must be provided. |
| 156 |
type: dict |
| 157 |
suboptions: |
| 158 |
lang: |
| 159 |
description: |
| 160 |
- The language to use for Windows. |
| 161 |
type: str |
| 162 |
required: true |
| 163 |
install_plesk: |
| 164 |
description: |
| 165 |
- If this option is provided, a Plesk installation will be activated for the next boot. |
| 166 |
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc), O(install_windows), O(install_plesk), |
| 167 |
and O(install_cpanel) must be provided. |
| 168 |
type: dict |
| 169 |
suboptions: |
| 170 |
dist: |
| 171 |
description: |
| 172 |
- The distribution to install. |
| 173 |
type: str |
| 174 |
required: true |
| 175 |
arch: |
| 176 |
description: |
| 177 |
- The architecture to use for the install. |
| 178 |
- Not all architectures are available for all distributions. |
| 179 |
- Defaults to V(64). |
| 180 |
- This option is deprecated and will be removed in community.hrobot 3.0.0. |
| 181 |
type: int |
| 182 |
choices: |
| 183 |
- 32 |
| 184 |
- 64 |
| 185 |
lang: |
| 186 |
description: |
| 187 |
- The language to use for the operating system. |
| 188 |
type: str |
| 189 |
required: true |
| 190 |
hostname: |
| 191 |
description: |
| 192 |
- The hostname. |
| 193 |
type: str |
| 194 |
required: true |
| 195 |
install_cpanel: |
| 196 |
description: |
| 197 |
- If this option is provided, a cPanel installation will be activated for the next boot. |
| 198 |
- Precisely one of O(regular_boot), O(rescue), O(install_linux), O(install_vnc), O(install_windows), O(install_plesk), |
| 199 |
and O(install_cpanel) must be provided. |
| 200 |
type: dict |
| 201 |
suboptions: |
| 202 |
dist: |
| 203 |
description: |
| 204 |
- The distribution to install. |
| 205 |
type: str |
| 206 |
required: true |
| 207 |
arch: |
| 208 |
description: |
| 209 |
- The architecture to use for the install. |
| 210 |
- Not all architectures are available for all distributions. |
| 211 |
- Defaults to V(64). |
| 212 |
- This option is deprecated and will be removed in community.hrobot 3.0.0. |
| 213 |
type: int |
| 214 |
choices: |
| 215 |
- 32 |
| 216 |
- 64 |
| 217 |
lang: |
| 218 |
description: |
| 219 |
- The language to use for the operating system. |
| 220 |
type: str |
| 221 |
required: true |
| 222 |
hostname: |
| 223 |
description: |
| 224 |
- The hostname. |
| 225 |
type: str |
| 226 |
required: true |
| 227 |
""" |
| 228 |
|
| 229 |
EXAMPLES = r""" |
| 230 |
--- |
| 231 |
- name: Disable all special boot configurations |
| 232 |
community.hrobot.boot: |
| 233 |
hetzner_user: foo |
| 234 |
hetzner_password: bar |
| 235 |
regular_boot: true |
| 236 |
|
| 237 |
- name: Enable a rescue system (64bit Linux) for the next boot |
| 238 |
community.hrobot.boot: |
| 239 |
hetzner_user: foo |
| 240 |
hetzner_password: bar |
| 241 |
rescue: |
| 242 |
os: linux |
| 243 |
|
| 244 |
- name: Enable a Linux install for the next boot |
| 245 |
community.hrobot.boot: |
| 246 |
hetzner_user: foo |
| 247 |
hetzner_password: bar |
| 248 |
install_linux: |
| 249 |
dist: CentOS 5.5 minimal |
| 250 |
lang: en |
| 251 |
authorized_keys: |
| 252 |
- 56:29:99:a4:5d:ed:ac:95:c1:f5:88:82:90:5d:dd:10 |
| 253 |
- 15:28:b0:03:95:f0:77:b3:10:56:15:6b:77:22:a5:bb |
| 254 |
""" |
| 255 |
|
| 256 |
RETURN = r""" |
| 257 |
configuration_type: |
| 258 |
description: |
| 259 |
- Describes the active boot configuration. |
| 260 |
returned: success |
| 261 |
type: str |
| 262 |
choices: |
| 263 |
- regular_boot |
| 264 |
- rescue |
| 265 |
- install_linux |
| 266 |
- install_vnc |
| 267 |
- install_windows |
| 268 |
- install_plesk |
| 269 |
- install_cpanel |
| 270 |
password: |
| 271 |
description: |
| 272 |
- The root password for the active boot configuration, if available. |
| 273 |
- For non-rescue boot configurations, it is avised to change the root password as soon as possible. |
| 274 |
returned: success and if RV(configuration_type) is not V(regular_boot) |
| 275 |
type: str |
| 276 |
""" |
| 277 |
|
| 278 |
from ansible.module_utils.basic import AnsibleModule |
| 279 |
|
| 280 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 281 |
BASE_URL, |
| 282 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 283 |
fetch_url_json, |
| 284 |
) |
| 285 |
|
| 286 |
from ansible_collections.community.hrobot.plugins.module_utils.ssh import ( |
| 287 |
FingerprintError, |
| 288 |
extract_fingerprint, |
| 289 |
) |
| 290 |
|
| 291 |
try: |
| 292 |
from urllib.parse import urlencode |
| 293 |
except ImportError: |
| 294 |
# Python 2.x fallback: |
| 295 |
from urllib import urlencode |
| 296 |
|
| 297 |
|
| 298 |
BOOT_CONFIGURATION_DATA = [ |
| 299 |
('rescue', 'rescue', { |
| 300 |
'os': ('os', 'os'), |
| 301 |
'arch': ('arch', 'arch'), |
| 302 |
'authorized_keys': ('authorized_key', 'authorized_key[]'), |
| 303 |
}), |
| 304 |
('install_linux', 'linux', { |
| 305 |
'dist': ('dist', 'dist'), |
| 306 |
'arch': ('arch', 'arch'), |
| 307 |
'lang': ('lang', 'lang'), |
| 308 |
'authorized_keys': ('authorized_key', 'authorized_key[]'), |
| 309 |
}), |
| 310 |
('install_vnc', 'vnc', { |
| 311 |
'dist': ('dist', 'dist'), |
| 312 |
'arch': ('arch', 'arch'), |
| 313 |
'lang': ('lang', 'lang'), |
| 314 |
}), |
| 315 |
('install_windows', 'windows', { |
| 316 |
'lang': ('lang', 'lang'), |
| 317 |
}), |
| 318 |
('install_plesk', 'plesk', { |
| 319 |
'dist': ('dist', 'dist'), |
| 320 |
'arch': ('arch', 'arch'), |
| 321 |
'lang': ('lang', 'lang'), |
| 322 |
'hostname': ('hostname', 'hostname'), |
| 323 |
}), |
| 324 |
('install_cpanel', 'cpanel', { |
| 325 |
'dist': ('dist', 'dist'), |
| 326 |
'arch': ('arch', 'arch'), |
| 327 |
'lang': ('lang', 'lang'), |
| 328 |
'hostname': ('hostname', 'hostname'), |
| 329 |
}), |
| 330 |
] |
| 331 |
|
| 332 |
|
| 333 |
def main(): |
| 334 |
argument_spec = dict( |
| 335 |
server_number=dict(type='int', required=True), |
| 336 |
regular_boot=dict(type='bool', choices=[True]), |
| 337 |
rescue=dict(type='dict', options=dict( |
| 338 |
os=dict(type='str', required=True), |
| 339 |
arch=dict(type='int', choices=[32, 64], removed_in_version='3.0.0', removed_from_collection='community.hrobot'), |
| 340 |
authorized_keys=dict(type='list', elements='str', no_log=False), |
| 341 |
)), |
| 342 |
install_linux=dict(type='dict', options=dict( |
| 343 |
dist=dict(type='str', required=True), |
| 344 |
arch=dict(type='int', choices=[32, 64], removed_in_version='3.0.0', removed_from_collection='community.hrobot'), |
| 345 |
lang=dict(type='str', required=True), |
| 346 |
authorized_keys=dict(type='list', elements='str', no_log=False), |
| 347 |
)), |
| 348 |
install_vnc=dict(type='dict', options=dict( |
| 349 |
dist=dict(type='str', required=True), |
| 350 |
arch=dict(type='int', choices=[32, 64], removed_in_version='3.0.0', removed_from_collection='community.hrobot'), |
| 351 |
lang=dict(type='str', required=True), |
| 352 |
)), |
| 353 |
install_windows=dict(type='dict', options=dict( |
| 354 |
lang=dict(type='str', required=True), |
| 355 |
)), |
| 356 |
install_plesk=dict(type='dict', options=dict( |
| 357 |
dist=dict(type='str', required=True), |
| 358 |
arch=dict(type='int', choices=[32, 64], removed_in_version='3.0.0', removed_from_collection='community.hrobot'), |
| 359 |
lang=dict(type='str', required=True), |
| 360 |
hostname=dict(type='str', required=True), |
| 361 |
)), |
| 362 |
install_cpanel=dict(type='dict', options=dict( |
| 363 |
dist=dict(type='str', required=True), |
| 364 |
arch=dict(type='int', choices=[32, 64], removed_in_version='3.0.0', removed_from_collection='community.hrobot'), |
| 365 |
lang=dict(type='str', required=True), |
| 366 |
hostname=dict(type='str', required=True), |
| 367 |
)), |
| 368 |
) |
| 369 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 370 |
module = AnsibleModule( |
| 371 |
argument_spec=argument_spec, |
| 372 |
supports_check_mode=True, |
| 373 |
mutually_exclusive=[('regular_boot', 'rescue', 'install_linux', 'install_vnc', 'install_windows', 'install_plesk', 'install_cpanel')], |
| 374 |
required_one_of=[('regular_boot', 'rescue', 'install_linux', 'install_vnc', 'install_windows', 'install_plesk', 'install_cpanel')], |
| 375 |
) |
| 376 |
|
| 377 |
server_number = module.params['server_number'] |
| 378 |
changed = False |
| 379 |
|
| 380 |
# Retrieve current boot config |
| 381 |
url = "{0}/boot/{1}".format(BASE_URL, server_number) |
| 382 |
result, error = fetch_url_json(module, url, accept_errors=['SERVER_NOT_FOUND', 'BOOT_NOT_AVAILABLE']) |
| 383 |
if error is not None: |
| 384 |
if error == 'SERVER_NOT_FOUND': |
| 385 |
module.fail_json(msg='This server does not exist, or you do not have access rights for it') |
| 386 |
if error == 'BOOT_NOT_AVAILABLE': |
| 387 |
module.fail_json(msg='There is no boot configuration available for this server') |
| 388 |
raise AssertionError('Unexpected error {0}'.format(error)) # pragma: no cover |
| 389 |
|
| 390 |
# Deactivate current boot configurations that are not requested |
| 391 |
for option_name, other_name, dummy in BOOT_CONFIGURATION_DATA: |
| 392 |
if (result['boot'].get(other_name) or {}).get('active') and not module.params[option_name]: |
| 393 |
changed = True |
| 394 |
if not module.check_mode: |
| 395 |
url = "{0}/boot/{1}/{2}".format(BASE_URL, server_number, other_name) |
| 396 |
fetch_url_json(module, url, method='DELETE', allow_empty_result=True) |
| 397 |
|
| 398 |
# Enable/compare boot configuration |
| 399 |
return_values = { |
| 400 |
'configuration_type': 'regular_boot', |
| 401 |
'password': None, |
| 402 |
} |
| 403 |
for option_name, other_name, options in BOOT_CONFIGURATION_DATA: |
| 404 |
if module.params[option_name]: |
| 405 |
return_values['configuration_type'] = option_name |
| 406 |
existing = result['boot'].get(other_name) or {} |
| 407 |
return_values['password'] = existing.get('password') |
| 408 |
data = {} |
| 409 |
for option_key, (result_key, data_key) in options.items(): |
| 410 |
option = module.params[option_name][option_key] |
| 411 |
if option is None or option == []: |
| 412 |
continue |
| 413 |
data[data_key] = option |
| 414 |
# Normalize options |
| 415 |
option_key = 'authorized_keys' |
| 416 |
if module.params[option_name].get(option_key): |
| 417 |
should = module.params[option_name][option_key] |
| 418 |
for index, key in enumerate(should): |
| 419 |
if ' ' in key: |
| 420 |
try: |
| 421 |
should[index] = extract_fingerprint(key) |
| 422 |
except FingerprintError as exc: |
| 423 |
module.fail_json( |
| 424 |
msg="Error while extracting fingerprint of {option_name}.{option_key}[{idx}]'s value {key!r}: {exc}".format( |
| 425 |
option_name=option_name, |
| 426 |
option_key=option_key, |
| 427 |
idx=index + 1, |
| 428 |
key=key, |
| 429 |
exc=exc, |
| 430 |
), |
| 431 |
) |
| 432 |
module.params[option_name][option_key] = should |
| 433 |
# Idempotence check |
| 434 |
if existing.get('active'): |
| 435 |
needs_change = False |
| 436 |
for option_key, (result_key, data_key) in options.items(): |
| 437 |
should = module.params[option_name][option_key] |
| 438 |
if should is None: |
| 439 |
continue |
| 440 |
# unfold the return object for the idempotence check to work correctly |
| 441 |
has = existing.get(result_key) |
| 442 |
if has and option_key == 'authorized_keys': |
| 443 |
has = [x['key']['fingerprint'] for x in has] |
| 444 |
if isinstance(has, list): |
| 445 |
has = sorted(has) |
| 446 |
if not isinstance(should, list): |
| 447 |
should = [should] # pragma: no cover |
| 448 |
should = sorted(should) |
| 449 |
if should != has: |
| 450 |
needs_change = True |
| 451 |
else: |
| 452 |
needs_change = True |
| 453 |
|
| 454 |
if needs_change: |
| 455 |
changed = True |
| 456 |
if not module.check_mode: |
| 457 |
url = "{0}/boot/{1}/{2}".format(BASE_URL, server_number, other_name) |
| 458 |
if existing.get('active'): |
| 459 |
# Deactivate existing boot configuration |
| 460 |
fetch_url_json(module, url, method='DELETE', allow_empty_result=True) |
| 461 |
# Enable new boot configuration |
| 462 |
headers = {"Content-type": "application/x-www-form-urlencoded"} |
| 463 |
result, dummy = fetch_url_json( |
| 464 |
module, |
| 465 |
url, |
| 466 |
data=urlencode(data, True), |
| 467 |
headers=headers, |
| 468 |
method='POST', |
| 469 |
) |
| 470 |
return_values['password'] = (result.get(other_name) or {}).get('password') |
| 471 |
else: |
| 472 |
return_values['password'] = None |
| 473 |
|
| 474 |
module.exit_json(changed=changed, **return_values) |
| 475 |
|
| 476 |
|
| 477 |
if __name__ == '__main__': # pragma: no cover |
| 478 |
main() # pragma: no cover |