Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2017 John Kwiatkoski (@JayKayy) <[email protected]>
4 # Copyright (c) 2018 Alexander Bethke (@oolongbrothers) <[email protected]>
5 # Copyright (c) 2017 Ansible Project
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 from __future__ import annotations
10
11 DOCUMENTATION = r"""
12 module: flatpak_remote
13 short_description: Manage flatpak repository remotes
14 description:
15 - Allows users to add or remove flatpak remotes.
16 - The flatpak remotes concept is comparable to what is called repositories in other packaging formats.
17 - Currently, remote addition is only supported using C(flatpakrepo) file URLs.
18 - Existing remotes are not updated.
19 - See the M(community.general.flatpak) module for managing flatpaks.
20 author:
21 - John Kwiatkoski (@JayKayy)
22 - Alexander Bethke (@oolongbrothers)
23 requirements:
24 - flatpak
25 extends_documentation_fragment:
26 - community.general._attributes
27 attributes:
28 check_mode:
29 support: full
30 diff_mode:
31 support: none
32 options:
33 executable:
34 description:
35 - The path to the C(flatpak) executable to use.
36 - By default, this module looks for the C(flatpak) executable on the path.
37 type: str
38 default: flatpak
39 flatpakrepo_url:
40 description:
41 - The URL to the C(flatpakrepo) file representing the repository remote to add.
42 - When used with O(state=present), the flatpak remote specified under the O(flatpakrepo_url) is added using the specified
43 installation O(method).
44 - When used with O(state=absent), this is not required.
45 - Required when O(state=present).
46 type: str
47 method:
48 description:
49 - The installation method to use.
50 - Defines if the C(flatpak) is supposed to be installed globally for the whole V(system) or only for the current V(user).
51 type: str
52 choices: [system, user]
53 default: system
54 name:
55 description:
56 - The desired name for the flatpak remote to be registered under on the managed host.
57 - When used with O(state=present), the remote is added to the managed host under the specified O(name).
58 - When used with O(state=absent) the remote with that name is removed.
59 type: str
60 required: true
61 state:
62 description:
63 - Indicates the desired package state.
64 type: str
65 choices: [absent, present]
66 default: present
67 enabled:
68 description:
69 - Indicates whether this remote is enabled.
70 type: bool
71 default: true
72 version_added: 6.4.0
73 """
74
75 EXAMPLES = r"""
76 - name: Add the Gnome flatpak remote to the system installation
77 community.general.flatpak_remote:
78 name: gnome
79 state: present
80 flatpakrepo_url: https://sdk.gnome.org/gnome-apps.flatpakrepo
81
82 - name: Add the flathub flatpak repository remote to the user installation
83 community.general.flatpak_remote:
84 name: flathub
85 state: present
86 flatpakrepo_url: https://dl.flathub.org/repo/flathub.flatpakrepo
87 method: user
88
89 - name: Remove the Gnome flatpak remote from the user installation
90 community.general.flatpak_remote:
91 name: gnome
92 state: absent
93 method: user
94
95 - name: Remove the flathub remote from the system installation
96 community.general.flatpak_remote:
97 name: flathub
98 state: absent
99
100 - name: Disable the flathub remote in the system installation
101 community.general.flatpak_remote:
102 name: flathub
103 state: present
104 enabled: false
105 """
106
107 RETURN = r"""
108 command:
109 description: The exact flatpak command that was executed.
110 returned: When a flatpak command has been executed
111 type: str
112 sample: "/usr/bin/flatpak remote-add --system flatpak-test https://dl.flathub.org/repo/flathub.flatpakrepo"
113 """
114
115 from ansible.module_utils.basic import AnsibleModule
116
117
118 def add_remote(module, binary, name, flatpakrepo_url, method):
119 """Add a new remote."""
120 global result # pylint: disable=global-variable-not-assigned
121 command = [binary, "remote-add", f"--{method}", name, flatpakrepo_url]
122 _flatpak_command(module, module.check_mode, command)
123 result["changed"] = True
124
125
126 def remove_remote(module, binary, name, method):
127 """Remove an existing remote."""
128 global result # pylint: disable=global-variable-not-assigned
129 command = [binary, "remote-delete", f"--{method}", "--force", name]
130 _flatpak_command(module, module.check_mode, command)
131 result["changed"] = True
132
133
134 def remote_exists(module, binary, name, method):
135 """Check if the remote exists."""
136 command = [binary, "remote-list", "--show-disabled", f"--{method}"]
137 # The query operation for the remote needs to be run even in check mode
138 output = _flatpak_command(module, False, command)
139 for line in output.splitlines():
140 listed_remote = line.split()
141 if len(listed_remote) == 0:
142 continue
143 if listed_remote[0] == name:
144 return True
145 return False
146
147
148 def enable_remote(module, binary, name, method):
149 """Enable a remote."""
150 global result # pylint: disable=global-variable-not-assigned
151 command = [binary, "remote-modify", "--enable", f"--{method}", name]
152 _flatpak_command(module, module.check_mode, command)
153 result["changed"] = True
154
155
156 def disable_remote(module, binary, name, method):
157 """Disable a remote."""
158 global result # pylint: disable=global-variable-not-assigned
159 command = [binary, "remote-modify", "--disable", f"--{method}", name]
160 _flatpak_command(module, module.check_mode, command)
161 result["changed"] = True
162
163
164 def remote_enabled(module, binary, name, method):
165 """Check if the remote is enabled."""
166 command = [binary, "remote-list", "--show-disabled", f"--{method}"]
167 # The query operation for the remote needs to be run even in check mode
168 output = _flatpak_command(module, False, command)
169 for line in output.splitlines():
170 listed_remote = line.split()
171 if len(listed_remote) == 0:
172 continue
173 if listed_remote[0] == name:
174 return len(listed_remote) == 1 or "disabled" not in listed_remote[1].split(",")
175 return False
176
177
178 def _flatpak_command(module, noop, command):
179 global result # pylint: disable=global-variable-not-assigned
180 result["command"] = " ".join(command)
181 if noop:
182 result["rc"] = 0
183 return ""
184
185 result["rc"], result["stdout"], result["stderr"] = module.run_command(command, check_rc=True)
186 return result["stdout"]
187
188
189 def main():
190 module = AnsibleModule(
191 argument_spec=dict(
192 name=dict(type="str", required=True),
193 flatpakrepo_url=dict(type="str"),
194 method=dict(type="str", default="system", choices=["user", "system"]),
195 state=dict(type="str", default="present", choices=["absent", "present"]),
196 enabled=dict(type="bool", default=True),
197 executable=dict(type="str", default="flatpak"),
198 ),
199 # This module supports check mode
200 supports_check_mode=True,
201 )
202 module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"}
203
204 name = module.params["name"]
205 flatpakrepo_url = module.params["flatpakrepo_url"]
206 method = module.params["method"]
207 state = module.params["state"]
208 enabled = module.params["enabled"]
209 executable = module.params["executable"]
210 binary = module.get_bin_path(executable, None)
211
212 if flatpakrepo_url is None:
213 flatpakrepo_url = ""
214
215 global result
216 result = dict(changed=False)
217
218 # If the binary was not found, fail the operation
219 if not binary:
220 module.fail_json(msg=f"Executable '{executable}' was not found on the system.", **result)
221
222 remote_already_exists = remote_exists(module, binary, name, method)
223
224 if state == "present" and not remote_already_exists:
225 add_remote(module, binary, name, flatpakrepo_url, method)
226 elif state == "absent" and remote_already_exists:
227 remove_remote(module, binary, name, method)
228
229 if state == "present":
230 remote_already_enabled = remote_enabled(module, binary, name, method)
231
232 if enabled and not remote_already_enabled:
233 enable_remote(module, binary, name, method)
234 if not enabled and remote_already_enabled:
235 disable_remote(module, binary, name, method)
236
237 module.exit_json(**result)
238
239
240 if __name__ == "__main__":
241 main()