| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2020, Andrew Klaus <[email protected]> |
| 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 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: sysupgrade |
| 11 |
short_description: Manage OpenBSD system upgrades |
| 12 |
version_added: 1.1.0 |
| 13 |
description: |
| 14 |
- Manage OpenBSD system upgrades using C(sysupgrade). |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: none |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
options: |
| 23 |
snapshot: |
| 24 |
description: |
| 25 |
- Apply the latest snapshot. |
| 26 |
- Otherwise release is applied. |
| 27 |
default: false |
| 28 |
type: bool |
| 29 |
force: |
| 30 |
description: |
| 31 |
- Force upgrade (for snapshots only). |
| 32 |
default: false |
| 33 |
type: bool |
| 34 |
keep_files: |
| 35 |
description: |
| 36 |
- Keep the files under C(/home/_sysupgrade). |
| 37 |
- By default, the files are deleted after the upgrade. |
| 38 |
default: false |
| 39 |
type: bool |
| 40 |
fetch_only: |
| 41 |
description: |
| 42 |
- Fetch and verify files and create C(/bsd.upgrade) but do not reboot. |
| 43 |
- Set to V(false) if you want C(sysupgrade) to reboot. This causes the module to fail. See the examples. |
| 44 |
default: true |
| 45 |
type: bool |
| 46 |
installurl: |
| 47 |
description: |
| 48 |
- OpenBSD mirror top-level URL for fetching an upgrade. |
| 49 |
- By default, the mirror URL is pulled from C(/etc/installurl). |
| 50 |
type: str |
| 51 |
author: |
| 52 |
- Andrew Klaus (@precurse) |
| 53 |
""" |
| 54 |
|
| 55 |
EXAMPLES = r""" |
| 56 |
- name: Upgrade to latest release |
| 57 |
community.general.sysupgrade: |
| 58 |
register: sysupgrade |
| 59 |
|
| 60 |
- name: Upgrade to latest snapshot |
| 61 |
community.general.sysupgrade: |
| 62 |
snapshot: true |
| 63 |
installurl: https://cloudflare.cdn.openbsd.org/pub/OpenBSD |
| 64 |
register: sysupgrade |
| 65 |
|
| 66 |
- name: Reboot to apply upgrade if needed |
| 67 |
ansible.builtin.reboot: |
| 68 |
when: sysupgrade.changed |
| 69 |
|
| 70 |
# Note: Ansible will error when running this way due to how |
| 71 |
# the reboot is forcefully handled by sysupgrade: |
| 72 |
|
| 73 |
- name: Have sysupgrade automatically reboot |
| 74 |
community.general.sysupgrade: |
| 75 |
fetch_only: false |
| 76 |
ignore_errors: true |
| 77 |
""" |
| 78 |
|
| 79 |
|
| 80 |
from ansible.module_utils.basic import AnsibleModule |
| 81 |
|
| 82 |
|
| 83 |
def sysupgrade_run(module): |
| 84 |
sysupgrade_bin = module.get_bin_path("/usr/sbin/sysupgrade", required=True) |
| 85 |
cmd = [sysupgrade_bin] |
| 86 |
changed = False |
| 87 |
|
| 88 |
# Setup command flags |
| 89 |
if module.params["snapshot"]: |
| 90 |
run_flag = ["-s"] |
| 91 |
if module.params["force"]: |
| 92 |
# Force only applies to snapshots |
| 93 |
run_flag.append("-f") |
| 94 |
else: |
| 95 |
# release flag |
| 96 |
run_flag = ["-r"] |
| 97 |
|
| 98 |
if module.params["keep_files"]: |
| 99 |
run_flag.append("-k") |
| 100 |
|
| 101 |
if module.params["fetch_only"]: |
| 102 |
run_flag.append("-n") |
| 103 |
|
| 104 |
# installurl must be the last argument |
| 105 |
if module.params["installurl"]: |
| 106 |
run_flag.append(module.params["installurl"]) |
| 107 |
|
| 108 |
rc, out, err = module.run_command(cmd + run_flag) |
| 109 |
|
| 110 |
if rc != 0: |
| 111 |
module.fail_json(msg=f"Command {cmd} failed rc={rc}, out={out}, err={err}") |
| 112 |
elif out.lower().find("already on latest snapshot") >= 0: |
| 113 |
changed = False |
| 114 |
elif out.lower().find("upgrade on next reboot") >= 0: |
| 115 |
changed = True |
| 116 |
|
| 117 |
return dict( |
| 118 |
changed=changed, |
| 119 |
rc=rc, |
| 120 |
stderr=err, |
| 121 |
stdout=out, |
| 122 |
) |
| 123 |
|
| 124 |
|
| 125 |
def main(): |
| 126 |
module = AnsibleModule( |
| 127 |
argument_spec=dict( |
| 128 |
snapshot=dict(type="bool", default=False), |
| 129 |
fetch_only=dict(type="bool", default=True), |
| 130 |
force=dict(type="bool", default=False), |
| 131 |
keep_files=dict(type="bool", default=False), |
| 132 |
installurl=dict(type="str"), |
| 133 |
), |
| 134 |
supports_check_mode=False, |
| 135 |
) |
| 136 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 137 |
return_dict = sysupgrade_run(module) |
| 138 |
module.exit_json(**return_dict) |
| 139 |
|
| 140 |
|
| 141 |
if __name__ == "__main__": |
| 142 |
main() |