| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright 2014 Peter Oliver <[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: pkg5_publisher |
| 12 |
author: "Peter Oliver (@mavit)" |
| 13 |
short_description: Manages Solaris 11 Image Packaging System publishers |
| 14 |
description: |
| 15 |
- IPS packages are the native packages in Solaris 11 and higher. |
| 16 |
- This module configures which publishers a client downloads IPS packages from. |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: none |
| 22 |
diff_mode: |
| 23 |
support: none |
| 24 |
options: |
| 25 |
name: |
| 26 |
description: |
| 27 |
- The publisher's name. |
| 28 |
required: true |
| 29 |
aliases: [publisher] |
| 30 |
type: str |
| 31 |
state: |
| 32 |
description: |
| 33 |
- Whether to ensure that a publisher is present or absent. |
| 34 |
default: present |
| 35 |
choices: [present, absent] |
| 36 |
type: str |
| 37 |
sticky: |
| 38 |
description: |
| 39 |
- Packages installed from a sticky repository can only receive updates from that repository. |
| 40 |
type: bool |
| 41 |
enabled: |
| 42 |
description: |
| 43 |
- Is the repository enabled or disabled? |
| 44 |
type: bool |
| 45 |
origin: |
| 46 |
description: |
| 47 |
- A path or URL to the repository. |
| 48 |
- Multiple values may be provided. |
| 49 |
type: list |
| 50 |
elements: str |
| 51 |
mirror: |
| 52 |
description: |
| 53 |
- A path or URL to the repository mirror. |
| 54 |
- Multiple values may be provided. |
| 55 |
type: list |
| 56 |
elements: str |
| 57 |
""" |
| 58 |
EXAMPLES = r""" |
| 59 |
- name: Fetch packages for the solaris publisher direct from Oracle |
| 60 |
community.general.pkg5_publisher: |
| 61 |
name: solaris |
| 62 |
sticky: true |
| 63 |
origin: https://pkg.oracle.com/solaris/support/ |
| 64 |
|
| 65 |
- name: Configure a publisher for locally-produced packages |
| 66 |
community.general.pkg5_publisher: |
| 67 |
name: site |
| 68 |
origin: 'https://pkg.example.com/site/' |
| 69 |
""" |
| 70 |
|
| 71 |
from ansible.module_utils.basic import AnsibleModule |
| 72 |
|
| 73 |
|
| 74 |
def main(): |
| 75 |
module = AnsibleModule( |
| 76 |
argument_spec=dict( |
| 77 |
name=dict(required=True, aliases=["publisher"]), |
| 78 |
state=dict(default="present", choices=["present", "absent"]), |
| 79 |
sticky=dict(type="bool"), |
| 80 |
enabled=dict(type="bool"), |
| 81 |
# search_after=dict(), |
| 82 |
# search_before=dict(), |
| 83 |
origin=dict(type="list", elements="str"), |
| 84 |
mirror=dict(type="list", elements="str"), |
| 85 |
) |
| 86 |
) |
| 87 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 88 |
|
| 89 |
for option in ["origin", "mirror"]: |
| 90 |
if module.params[option] == [""]: |
| 91 |
module.params[option] = [] |
| 92 |
|
| 93 |
if module.params["state"] == "present": |
| 94 |
modify_publisher(module, module.params) |
| 95 |
else: |
| 96 |
unset_publisher(module, module.params["name"]) |
| 97 |
|
| 98 |
|
| 99 |
def modify_publisher(module, params): |
| 100 |
name = params["name"] |
| 101 |
existing = get_publishers(module) |
| 102 |
|
| 103 |
if name in existing: |
| 104 |
for option in ["origin", "mirror", "sticky", "enabled"]: |
| 105 |
if params[option] is not None: |
| 106 |
if params[option] != existing[name][option]: |
| 107 |
return set_publisher(module, params) |
| 108 |
else: |
| 109 |
return set_publisher(module, params) |
| 110 |
|
| 111 |
module.exit_json() |
| 112 |
|
| 113 |
|
| 114 |
def set_publisher(module, params): |
| 115 |
name = params["name"] |
| 116 |
args = [] |
| 117 |
|
| 118 |
if params["origin"] is not None: |
| 119 |
args.append("--remove-origin=*") |
| 120 |
args.extend([f"--add-origin={u}" for u in params["origin"]]) |
| 121 |
if params["mirror"] is not None: |
| 122 |
args.append("--remove-mirror=*") |
| 123 |
args.extend([f"--add-mirror={u}" for u in params["mirror"]]) |
| 124 |
|
| 125 |
if params["sticky"] is not None and params["sticky"]: |
| 126 |
args.append("--sticky") |
| 127 |
elif params["sticky"] is not None: |
| 128 |
args.append("--non-sticky") |
| 129 |
|
| 130 |
if params["enabled"] is not None and params["enabled"]: |
| 131 |
args.append("--enable") |
| 132 |
elif params["enabled"] is not None: |
| 133 |
args.append("--disable") |
| 134 |
|
| 135 |
rc, out, err = module.run_command(["pkg", "set-publisher"] + args + [name], check_rc=True) |
| 136 |
response = { |
| 137 |
"rc": rc, |
| 138 |
"results": [out], |
| 139 |
"msg": err, |
| 140 |
"changed": True, |
| 141 |
} |
| 142 |
if rc != 0: |
| 143 |
module.fail_json(**response) |
| 144 |
module.exit_json(**response) |
| 145 |
|
| 146 |
|
| 147 |
def unset_publisher(module, publisher): |
| 148 |
if publisher not in get_publishers(module): |
| 149 |
module.exit_json() |
| 150 |
|
| 151 |
rc, out, err = module.run_command(["pkg", "unset-publisher", publisher], check_rc=True) |
| 152 |
response = { |
| 153 |
"rc": rc, |
| 154 |
"results": [out], |
| 155 |
"msg": err, |
| 156 |
"changed": True, |
| 157 |
} |
| 158 |
if rc != 0: |
| 159 |
module.fail_json(**response) |
| 160 |
module.exit_json(**response) |
| 161 |
|
| 162 |
|
| 163 |
def get_publishers(module): |
| 164 |
rc, out, err = module.run_command(["pkg", "publisher", "-Ftsv"], True) |
| 165 |
|
| 166 |
lines = out.splitlines() |
| 167 |
keys = lines.pop(0).lower().split("\t") |
| 168 |
|
| 169 |
publishers = {} |
| 170 |
for line in lines: |
| 171 |
values = dict(zip(keys, map(unstringify, line.split("\t")))) |
| 172 |
name = values["publisher"] |
| 173 |
|
| 174 |
if name not in publishers: |
| 175 |
publishers[name] = {k: values[k] for k in ["sticky", "enabled"]} |
| 176 |
publishers[name]["origin"] = [] |
| 177 |
publishers[name]["mirror"] = [] |
| 178 |
|
| 179 |
if values["type"] is not None: |
| 180 |
publishers[name][values["type"]].append(values["uri"]) |
| 181 |
|
| 182 |
return publishers |
| 183 |
|
| 184 |
|
| 185 |
def unstringify(val): |
| 186 |
if val == "-" or val == "": |
| 187 |
return None |
| 188 |
elif val == "true": |
| 189 |
return True |
| 190 |
elif val == "false": |
| 191 |
return False |
| 192 |
else: |
| 193 |
return val |
| 194 |
|
| 195 |
|
| 196 |
if __name__ == "__main__": |
| 197 |
main() |