Debian Codegraph @

ansible_collections/community/general/plugins/modules/svr4pkg.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2012, Boyd Adamson <boyd () boydadamson.com>
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: svr4pkg
12 short_description: Manage Solaris SVR4 packages
13 description:
14 - Manages SVR4 packages on Solaris 10 and 11.
15 - These were the native packages on Solaris <= 10 and are available as a legacy feature in Solaris 11.
16 - Note that this is a very basic packaging system. It does not enforce dependencies on install or remove.
17 author: "Boyd Adamson (@brontitall)"
18 extends_documentation_fragment:
19 - community.general._attributes
20 attributes:
21 check_mode:
22 support: full
23 diff_mode:
24 support: none
25 options:
26 name:
27 description:
28 - Package name, for example V(SUNWcsr).
29 required: true
30 type: str
31
32 state:
33 description:
34 - Whether to install (V(present)), or remove (V(absent)) a package.
35 - If the package is to be installed, then O(src) is required.
36 - The SVR4 package system does not provide an upgrade operation. You need to uninstall the old, then install the new
37 package.
38 required: true
39 choices: ["present", "absent"]
40 type: str
41
42 src:
43 description:
44 - Specifies the location to install the package from. Required when O(state=present).
45 - "Can be any path acceptable to the C(pkgadd) command's C(-d) option. For example: V(somefile.pkg), V(/dir/with/pkgs),
46 V(http://server/mypkgs.pkg)."
47 - If using a file or directory, they must already be accessible by the host. See the M(ansible.builtin.copy) module
48 for a way to get them there.
49 type: str
50 proxy:
51 description:
52 - HTTP[s] proxy to be used if O(src) is a URL.
53 type: str
54 response_file:
55 description:
56 - Specifies the location of a response file to be used if package expects input on install.
57 type: str
58 zone:
59 description:
60 - Whether to install the package only in the current zone, or install it into all zones.
61 - The installation into all zones works only if you are working with the global zone.
62 default: "all"
63 choices: ["current", "all"]
64 type: str
65 category:
66 description:
67 - Install/Remove category instead of a single package.
68 type: bool
69 default: false
70 """
71
72 EXAMPLES = r"""
73 - name: Install a package from an already copied file
74 community.general.svr4pkg:
75 name: CSWcommon
76 src: /tmp/cswpkgs.pkg
77 state: present
78
79 - name: Install a package directly from an http site
80 community.general.svr4pkg:
81 name: CSWpkgutil
82 src: 'http://get.opencsw.org/now'
83 state: present
84 zone: current
85
86 - name: Install a package with a response file
87 community.general.svr4pkg:
88 name: CSWggrep
89 src: /tmp/third-party.pkg
90 response_file: /tmp/ggrep.response
91 state: present
92
93 - name: Ensure that a package is not installed
94 community.general.svr4pkg:
95 name: SUNWgnome-sound-recorder
96 state: absent
97
98 - name: Ensure that a category is not installed
99 community.general.svr4pkg:
100 name: FIREFOX
101 state: absent
102 category: true
103 """
104
105
106 import os
107 import tempfile
108
109 from ansible.module_utils.basic import AnsibleModule
110
111
112 def package_installed(module, name, category):
113 cmd = [module.get_bin_path("pkginfo", True), "-q"]
114 if category:
115 cmd.append("-c")
116 cmd.append(name)
117 rc, out, err = module.run_command(cmd)
118 return rc == 0
119
120
121 def create_admin_file():
122 (desc, filename) = tempfile.mkstemp(prefix="ansible_svr4pkg", text=True)
123 fullauto = b"""
124 mail=
125 instance=unique
126 partial=nocheck
127 runlevel=quit
128 idepend=nocheck
129 rdepend=nocheck
130 space=quit
131 setuid=nocheck
132 conflict=nocheck
133 action=nocheck
134 networktimeout=60
135 networkretries=3
136 authentication=quit
137 keystore=/var/sadm/security
138 proxy=
139 basedir=default
140 """
141 os.write(desc, fullauto)
142 os.close(desc)
143 return filename
144
145
146 def run_command(module, cmd):
147 progname = cmd[0]
148 cmd[0] = module.get_bin_path(progname, True)
149 return module.run_command(cmd)
150
151
152 def package_install(module, name, src, proxy, response_file, zone, category):
153 adminfile = create_admin_file()
154 cmd = ["pkgadd", "-n"]
155 if zone == "current":
156 cmd += ["-G"]
157 cmd += ["-a", adminfile, "-d", src]
158 if proxy is not None:
159 cmd += ["-x", proxy]
160 if response_file is not None:
161 cmd += ["-r", response_file]
162 if category:
163 cmd += ["-Y"]
164 cmd.append(name)
165 (rc, out, err) = run_command(module, cmd)
166 os.unlink(adminfile)
167 return (rc, out, err)
168
169
170 def package_uninstall(module, name, src, category):
171 adminfile = create_admin_file()
172 if category:
173 cmd = ["pkgrm", "-na", adminfile, "-Y", name]
174 else:
175 cmd = ["pkgrm", "-na", adminfile, name]
176 (rc, out, err) = run_command(module, cmd)
177 os.unlink(adminfile)
178 return (rc, out, err)
179
180
181 def main():
182 module = AnsibleModule(
183 argument_spec=dict(
184 name=dict(required=True),
185 state=dict(required=True, choices=["present", "absent"]),
186 src=dict(),
187 proxy=dict(),
188 response_file=dict(),
189 zone=dict(default="all", choices=["current", "all"]),
190 category=dict(default=False, type="bool"),
191 ),
192 supports_check_mode=True,
193 )
194 state = module.params["state"]
195 name = module.params["name"]
196 src = module.params["src"]
197 proxy = module.params["proxy"]
198 response_file = module.params["response_file"]
199 zone = module.params["zone"]
200 category = module.params["category"]
201 rc = None
202 out = ""
203 err = ""
204 result = {}
205 result["name"] = name
206 result["state"] = state
207
208 if state == "present":
209 if src is None:
210 module.fail_json(name=name, msg="src is required when state=present")
211 if not package_installed(module, name, category):
212 if module.check_mode:
213 module.exit_json(changed=True)
214 (rc, out, err) = package_install(module, name, src, proxy, response_file, zone, category)
215 # Stdout is normally empty but for some packages can be
216 # very long and is not often useful
217 if len(out) > 75:
218 out = f"{out[:75]}..."
219
220 elif state == "absent":
221 if package_installed(module, name, category):
222 if module.check_mode:
223 module.exit_json(changed=True)
224 (rc, out, err) = package_uninstall(module, name, src, category)
225 out = out[:75]
226
227 # Returncodes as per pkgadd(1m)
228 # 0 Successful completion
229 # 1 Fatal error.
230 # 2 Warning.
231 # 3 Interruption.
232 # 4 Administration.
233 # 5 Administration. Interaction is required. Do not use pkgadd -n.
234 # 10 Reboot after installation of all packages.
235 # 20 Reboot after installation of this package.
236 # 99 (observed) pkgadd: ERROR: could not process datastream from </tmp/pkgutil.pkg>
237 if rc in (0, 2, 3, 10, 20):
238 result["changed"] = True
239 # no install nor uninstall, or failed
240 else:
241 result["changed"] = False
242
243 # rc will be none when the package already was installed and no action took place
244 # Only return failed=False when the returncode is known to be good as there may be more
245 # undocumented failure return codes
246 if rc not in (None, 0, 2, 10, 20):
247 result["failed"] = True
248 else:
249 result["failed"] = False
250
251 if out:
252 result["stdout"] = out
253 if err:
254 result["stderr"] = err
255
256 module.exit_json(**result)
257
258
259 if __name__ == "__main__":
260 main()