| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2017, Alberto Murillo <[email protected]> |
| 4 |
# |
| 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 annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: swupd |
| 12 |
short_description: Manages updates and bundles in ClearLinux systems |
| 13 |
deprecated: |
| 14 |
removed_in: 15.0.0 |
| 15 |
why: >- |
| 16 |
ClearLinux was made EOL in July 2025. If you think the module is still useful for another distribution, |
| 17 |
please L(create an issue in the community.general repository, https://github.com/ansible-collections/community.general/issues/). |
| 18 |
alternative: There is none. |
| 19 |
description: |
| 20 |
- Manages updates and bundles with the swupd bundle manager, which is used by the Clear Linux Project for Intel Architecture. |
| 21 |
author: Alberto Murillo (@albertomurillo) |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.general._attributes |
| 24 |
attributes: |
| 25 |
check_mode: |
| 26 |
support: full |
| 27 |
diff_mode: |
| 28 |
support: none |
| 29 |
options: |
| 30 |
contenturl: |
| 31 |
description: |
| 32 |
- URL pointing to the contents of available bundles. If not specified, the contents are retrieved from clearlinux.org. |
| 33 |
type: str |
| 34 |
format: |
| 35 |
description: |
| 36 |
- The format suffix for version file downloads. For example V(1), V(2), V(3), and so on, or the special value V(staging). |
| 37 |
If not specified, the default format is used. |
| 38 |
type: str |
| 39 |
manifest: |
| 40 |
description: |
| 41 |
- The manifest contains information about the bundles at certain version of the OS. Specify a Manifest version to verify |
| 42 |
against that version or leave unspecified to verify against the current version. |
| 43 |
aliases: [release, version] |
| 44 |
type: int |
| 45 |
name: |
| 46 |
description: |
| 47 |
- Name of the (I)bundle to install or remove. |
| 48 |
aliases: [bundle] |
| 49 |
type: str |
| 50 |
state: |
| 51 |
description: |
| 52 |
- Indicates the desired (I)bundle state. V(present) ensures the bundle is installed while V(absent) ensures the (I)bundle |
| 53 |
is not installed. |
| 54 |
default: present |
| 55 |
choices: [present, absent] |
| 56 |
type: str |
| 57 |
update: |
| 58 |
description: |
| 59 |
- Updates the OS to the latest version. |
| 60 |
type: bool |
| 61 |
default: false |
| 62 |
url: |
| 63 |
description: |
| 64 |
- Overrides both O(contenturl) and O(versionurl). |
| 65 |
type: str |
| 66 |
verify: |
| 67 |
description: |
| 68 |
- Verify content for OS version. |
| 69 |
type: bool |
| 70 |
default: false |
| 71 |
versionurl: |
| 72 |
description: |
| 73 |
- URL for version string download. |
| 74 |
type: str |
| 75 |
""" |
| 76 |
|
| 77 |
EXAMPLES = r""" |
| 78 |
- name: Update the OS to the latest version |
| 79 |
community.general.swupd: |
| 80 |
update: true |
| 81 |
|
| 82 |
- name: Installs the "foo" bundle |
| 83 |
community.general.swupd: |
| 84 |
name: foo |
| 85 |
state: present |
| 86 |
|
| 87 |
- name: Removes the "foo" bundle |
| 88 |
community.general.swupd: |
| 89 |
name: foo |
| 90 |
state: absent |
| 91 |
|
| 92 |
- name: Check integrity of filesystem |
| 93 |
community.general.swupd: |
| 94 |
verify: true |
| 95 |
|
| 96 |
- name: Downgrade OS to release 12920 |
| 97 |
community.general.swupd: |
| 98 |
verify: true |
| 99 |
manifest: 12920 |
| 100 |
""" |
| 101 |
|
| 102 |
|
| 103 |
import os |
| 104 |
|
| 105 |
from ansible.module_utils.basic import AnsibleModule |
| 106 |
|
| 107 |
|
| 108 |
class Swupd: |
| 109 |
FILES_NOT_MATCH = "files did not match" |
| 110 |
FILES_REPLACED = "missing files were replaced" |
| 111 |
FILES_FIXED = "files were fixed" |
| 112 |
FILES_DELETED = "files were deleted" |
| 113 |
|
| 114 |
def __init__(self, module): |
| 115 |
# Fail if swupd is not found |
| 116 |
self.module = module |
| 117 |
self.swupd_cmd = module.get_bin_path("swupd", False) |
| 118 |
if not self.swupd_cmd: |
| 119 |
module.fail_json(msg="Could not find swupd.") |
| 120 |
|
| 121 |
# Initialize parameters |
| 122 |
for key in module.params.keys(): |
| 123 |
setattr(self, key, module.params[key]) |
| 124 |
|
| 125 |
# Initialize return values |
| 126 |
self.changed = False |
| 127 |
self.failed = False |
| 128 |
self.msg = None |
| 129 |
self.rc = None |
| 130 |
self.stderr = "" |
| 131 |
self.stdout = "" |
| 132 |
|
| 133 |
def _run_cmd(self, cmd): |
| 134 |
self.rc, self.stdout, self.stderr = self.module.run_command(cmd, check_rc=False) |
| 135 |
|
| 136 |
def _get_cmd(self, command): |
| 137 |
cmd = [self.swupd_cmd] + command |
| 138 |
|
| 139 |
if self.format: |
| 140 |
cmd.append(f"--format={self.format}") |
| 141 |
if self.manifest: |
| 142 |
cmd.append(f"--manifest={self.manifest}") |
| 143 |
if self.url: |
| 144 |
cmd.append(f"--url={self.url}") |
| 145 |
else: |
| 146 |
if self.contenturl and command != "check-update": |
| 147 |
cmd.append(f"--contenturl={self.contenturl}") |
| 148 |
if self.versionurl: |
| 149 |
cmd.append(f"--versionurl={self.versionurl}") |
| 150 |
|
| 151 |
return cmd |
| 152 |
|
| 153 |
def _is_bundle_installed(self, bundle): |
| 154 |
try: |
| 155 |
os.stat(f"/usr/share/clear/bundles/{bundle}") |
| 156 |
except OSError: |
| 157 |
return False |
| 158 |
|
| 159 |
return True |
| 160 |
|
| 161 |
def _needs_update(self): |
| 162 |
cmd = self._get_cmd(["check-update"]) |
| 163 |
self._run_cmd(cmd) |
| 164 |
|
| 165 |
if self.rc == 0: |
| 166 |
return True |
| 167 |
|
| 168 |
if self.rc == 1: |
| 169 |
return False |
| 170 |
|
| 171 |
self.failed = True |
| 172 |
self.msg = "Failed to check for updates" |
| 173 |
|
| 174 |
def _needs_verify(self): |
| 175 |
cmd = self._get_cmd(["verify"]) |
| 176 |
self._run_cmd(cmd) |
| 177 |
|
| 178 |
if self.rc != 0: |
| 179 |
self.failed = True |
| 180 |
self.msg = "Failed to check for filesystem inconsistencies." |
| 181 |
|
| 182 |
return self.FILES_NOT_MATCH in self.stdout |
| 183 |
|
| 184 |
def install_bundle(self, bundle): |
| 185 |
"""Installs a bundle with `swupd bundle-add bundle`""" |
| 186 |
if self.module.check_mode: |
| 187 |
self.module.exit_json(changed=not self._is_bundle_installed(bundle)) |
| 188 |
|
| 189 |
if self._is_bundle_installed(bundle): |
| 190 |
self.msg = f"Bundle {bundle} is already installed" |
| 191 |
return |
| 192 |
|
| 193 |
cmd = self._get_cmd(["bundle-add", bundle]) |
| 194 |
self._run_cmd(cmd) |
| 195 |
|
| 196 |
if self.rc == 0: |
| 197 |
self.changed = True |
| 198 |
self.msg = f"Bundle {bundle} installed" |
| 199 |
return |
| 200 |
|
| 201 |
self.failed = True |
| 202 |
self.msg = f"Failed to install bundle {bundle}" |
| 203 |
|
| 204 |
def remove_bundle(self, bundle): |
| 205 |
"""Removes a bundle with `swupd bundle-remove bundle`""" |
| 206 |
if self.module.check_mode: |
| 207 |
self.module.exit_json(changed=self._is_bundle_installed(bundle)) |
| 208 |
|
| 209 |
if not self._is_bundle_installed(bundle): |
| 210 |
self.msg = "Bundle %s not installed" |
| 211 |
return |
| 212 |
|
| 213 |
cmd = self._get_cmd(["bundle-remove", bundle]) |
| 214 |
self._run_cmd(cmd) |
| 215 |
|
| 216 |
if self.rc == 0: |
| 217 |
self.changed = True |
| 218 |
self.msg = f"Bundle {bundle} removed" |
| 219 |
return |
| 220 |
|
| 221 |
self.failed = True |
| 222 |
self.msg = f"Failed to remove bundle {bundle}" |
| 223 |
|
| 224 |
def update_os(self): |
| 225 |
"""Updates the os with `swupd update`""" |
| 226 |
if self.module.check_mode: |
| 227 |
self.module.exit_json(changed=self._needs_update()) |
| 228 |
|
| 229 |
if not self._needs_update(): |
| 230 |
self.msg = "There are no updates available" |
| 231 |
return |
| 232 |
|
| 233 |
cmd = self._get_cmd(["update"]) |
| 234 |
self._run_cmd(cmd) |
| 235 |
|
| 236 |
if self.rc == 0: |
| 237 |
self.changed = True |
| 238 |
self.msg = "Update successful" |
| 239 |
return |
| 240 |
|
| 241 |
self.failed = True |
| 242 |
self.msg = "Failed to check for updates" |
| 243 |
|
| 244 |
def verify_os(self): |
| 245 |
"""Verifies filesystem against specified or current version""" |
| 246 |
if self.module.check_mode: |
| 247 |
self.module.exit_json(changed=self._needs_verify()) |
| 248 |
|
| 249 |
if not self._needs_verify(): |
| 250 |
self.msg = "No files where changed" |
| 251 |
return |
| 252 |
|
| 253 |
cmd = self._get_cmd(["verify", "--fix"]) |
| 254 |
self._run_cmd(cmd) |
| 255 |
|
| 256 |
if self.rc == 0 and ( |
| 257 |
self.FILES_REPLACED in self.stdout or self.FILES_FIXED in self.stdout or self.FILES_DELETED in self.stdout |
| 258 |
): |
| 259 |
self.changed = True |
| 260 |
self.msg = "Fix successful" |
| 261 |
return |
| 262 |
|
| 263 |
self.failed = True |
| 264 |
self.msg = "Failed to verify the OS" |
| 265 |
|
| 266 |
|
| 267 |
def main(): |
| 268 |
"""The main function.""" |
| 269 |
module = AnsibleModule( |
| 270 |
argument_spec=dict( |
| 271 |
contenturl=dict(type="str"), |
| 272 |
format=dict(type="str"), |
| 273 |
manifest=dict(aliases=["release", "version"], type="int"), |
| 274 |
name=dict(aliases=["bundle"], type="str"), |
| 275 |
state=dict(default="present", choices=["present", "absent"], type="str"), |
| 276 |
update=dict(default=False, type="bool"), |
| 277 |
url=dict(type="str"), |
| 278 |
verify=dict(default=False, type="bool"), |
| 279 |
versionurl=dict(type="str"), |
| 280 |
), |
| 281 |
required_one_of=[["name", "update", "verify"]], |
| 282 |
mutually_exclusive=[["name", "update", "verify"]], |
| 283 |
supports_check_mode=True, |
| 284 |
) |
| 285 |
|
| 286 |
swupd = Swupd(module) |
| 287 |
|
| 288 |
name = module.params["name"] |
| 289 |
state = module.params["state"] |
| 290 |
update = module.params["update"] |
| 291 |
verify = module.params["verify"] |
| 292 |
|
| 293 |
if update: |
| 294 |
swupd.update_os() |
| 295 |
elif verify: |
| 296 |
swupd.verify_os() |
| 297 |
elif state == "present": |
| 298 |
swupd.install_bundle(name) |
| 299 |
elif state == "absent": |
| 300 |
swupd.remove_bundle(name) |
| 301 |
else: |
| 302 |
swupd.failed = True |
| 303 |
|
| 304 |
if swupd.failed: |
| 305 |
module.fail_json(msg=swupd.msg, stdout=swupd.stdout, stderr=swupd.stderr) |
| 306 |
else: |
| 307 |
module.exit_json(changed=swupd.changed, msg=swupd.msg, stdout=swupd.stdout, stderr=swupd.stderr) |
| 308 |
|
| 309 |
|
| 310 |
if __name__ == "__main__": |
| 311 |
main() |