2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 13:17:19 +02:00
|
|
|
# Copyright (c) 2017 John Kwiatkoski (@JayKayy) <jkwiat40@gmail.com>
|
|
|
|
# Copyright (c) 2018 Alexander Bethke (@oolongbrothers) <oolongbrothers@gmx.net>
|
|
|
|
# Copyright (c) 2017 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = r'''
|
|
|
|
---
|
|
|
|
module: flatpak_remote
|
|
|
|
short_description: Manage flatpak repository remotes
|
|
|
|
description:
|
|
|
|
- Allows users to add or remove flatpak remotes.
|
|
|
|
- The flatpak remotes concept is comparable to what is called repositories in other packaging
|
|
|
|
formats.
|
|
|
|
- Currently, remote addition is only supported via I(flatpakrepo) file URLs.
|
|
|
|
- Existing remotes will not be updated.
|
2020-06-29 14:59:15 +02:00
|
|
|
- See the M(community.general.flatpak) module for managing flatpaks.
|
2020-03-09 10:11:07 +01:00
|
|
|
author:
|
|
|
|
- John Kwiatkoski (@JayKayy)
|
|
|
|
- Alexander Bethke (@oolongbrothers)
|
|
|
|
requirements:
|
|
|
|
- flatpak
|
|
|
|
options:
|
|
|
|
executable:
|
|
|
|
description:
|
|
|
|
- The path to the C(flatpak) executable to use.
|
|
|
|
- By default, this module looks for the C(flatpak) executable on the path.
|
2020-05-04 16:42:15 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
default: flatpak
|
|
|
|
flatpakrepo_url:
|
|
|
|
description:
|
|
|
|
- The URL to the I(flatpakrepo) file representing the repository remote to add.
|
|
|
|
- When used with I(state=present), the flatpak remote specified under the I(flatpakrepo_url)
|
|
|
|
is added using the specified installation C(method).
|
|
|
|
- When used with I(state=absent), this is not required.
|
|
|
|
- Required when I(state=present).
|
2020-05-04 16:42:15 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
method:
|
|
|
|
description:
|
|
|
|
- The installation method to use.
|
|
|
|
- Defines if the I(flatpak) is supposed to be installed globally for the whole C(system)
|
|
|
|
or only for the current C(user).
|
2020-05-04 16:42:15 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
choices: [ system, user ]
|
|
|
|
default: system
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The desired name for the flatpak remote to be registered under on the managed host.
|
|
|
|
- When used with I(state=present), the remote will be added to the managed host under
|
|
|
|
the specified I(name).
|
|
|
|
- When used with I(state=absent) the remote with that name will be removed.
|
2020-05-04 16:42:15 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicates the desired package state.
|
2020-05-04 16:42:15 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
choices: [ absent, present ]
|
|
|
|
default: present
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Add the Gnome flatpak remote to the system installation
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.flatpak_remote:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: gnome
|
|
|
|
state: present
|
|
|
|
flatpakrepo_url: https://sdk.gnome.org/gnome-apps.flatpakrepo
|
|
|
|
|
|
|
|
- name: Add the flathub flatpak repository remote to the user installation
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.flatpak_remote:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: flathub
|
|
|
|
state: present
|
|
|
|
flatpakrepo_url: https://dl.flathub.org/repo/flathub.flatpakrepo
|
|
|
|
method: user
|
|
|
|
|
|
|
|
- name: Remove the Gnome flatpak remote from the user installation
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.flatpak_remote:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: gnome
|
|
|
|
state: absent
|
|
|
|
method: user
|
|
|
|
|
|
|
|
- name: Remove the flathub remote from the system installation
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.flatpak_remote:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: flathub
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r'''
|
|
|
|
command:
|
|
|
|
description: The exact flatpak command that was executed
|
|
|
|
returned: When a flatpak command has been executed
|
|
|
|
type: str
|
|
|
|
sample: "/usr/bin/flatpak remote-add --system flatpak-test https://dl.flathub.org/repo/flathub.flatpakrepo"
|
|
|
|
msg:
|
|
|
|
description: Module error message
|
|
|
|
returned: failure
|
|
|
|
type: str
|
|
|
|
sample: "Executable '/usr/local/bin/flatpak' was not found on the system."
|
|
|
|
rc:
|
|
|
|
description: Return code from flatpak binary
|
|
|
|
returned: When a flatpak command has been executed
|
|
|
|
type: int
|
|
|
|
sample: 0
|
|
|
|
stderr:
|
|
|
|
description: Error output from flatpak binary
|
|
|
|
returned: When a flatpak command has been executed
|
|
|
|
type: str
|
|
|
|
sample: "error: GPG verification enabled, but no summary found (check that the configured URL in remote config is correct)\n"
|
|
|
|
stdout:
|
|
|
|
description: Output from flatpak binary
|
|
|
|
returned: When a flatpak command has been executed
|
|
|
|
type: str
|
|
|
|
sample: "flathub\tFlathub\thttps://dl.flathub.org/repo/\t1\t\n"
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_bytes, to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def add_remote(module, binary, name, flatpakrepo_url, method):
|
|
|
|
"""Add a new remote."""
|
2022-08-12 12:33:02 +02:00
|
|
|
global result # pylint: disable=global-variable-not-assigned
|
2020-05-10 14:46:10 +02:00
|
|
|
command = [binary, "remote-add", "--{0}".format(method), name, flatpakrepo_url]
|
2020-03-09 10:11:07 +01:00
|
|
|
_flatpak_command(module, module.check_mode, command)
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
|
|
|
|
def remove_remote(module, binary, name, method):
|
|
|
|
"""Remove an existing remote."""
|
2022-08-12 12:33:02 +02:00
|
|
|
global result # pylint: disable=global-variable-not-assigned
|
2020-05-10 14:46:10 +02:00
|
|
|
command = [binary, "remote-delete", "--{0}".format(method), "--force", name]
|
2020-03-09 10:11:07 +01:00
|
|
|
_flatpak_command(module, module.check_mode, command)
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
|
|
|
|
def remote_exists(module, binary, name, method):
|
|
|
|
"""Check if the remote exists."""
|
2020-05-10 14:46:10 +02:00
|
|
|
command = [binary, "remote-list", "-d", "--{0}".format(method)]
|
2020-03-09 10:11:07 +01:00
|
|
|
# The query operation for the remote needs to be run even in check mode
|
|
|
|
output = _flatpak_command(module, False, command)
|
|
|
|
for line in output.splitlines():
|
|
|
|
listed_remote = line.split()
|
|
|
|
if len(listed_remote) == 0:
|
|
|
|
continue
|
|
|
|
if listed_remote[0] == to_native(name):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _flatpak_command(module, noop, command):
|
2022-08-12 12:33:02 +02:00
|
|
|
global result # pylint: disable=global-variable-not-assigned
|
2020-05-10 14:46:10 +02:00
|
|
|
result['command'] = ' '.join(command)
|
2020-03-09 10:11:07 +01:00
|
|
|
if noop:
|
|
|
|
result['rc'] = 0
|
|
|
|
return ""
|
|
|
|
|
2020-05-09 10:03:04 +02:00
|
|
|
result['rc'], result['stdout'], result['stderr'] = module.run_command(
|
2020-05-10 14:46:10 +02:00
|
|
|
command, check_rc=True
|
2020-05-09 10:03:04 +02:00
|
|
|
)
|
|
|
|
return result['stdout']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
flatpakrepo_url=dict(type='str'),
|
|
|
|
method=dict(type='str', default='system',
|
|
|
|
choices=['user', 'system']),
|
|
|
|
state=dict(type='str', default="present",
|
|
|
|
choices=['absent', 'present']),
|
|
|
|
executable=dict(type='str', default="flatpak")
|
|
|
|
),
|
|
|
|
# This module supports check mode
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
name = module.params['name']
|
|
|
|
flatpakrepo_url = module.params['flatpakrepo_url']
|
|
|
|
method = module.params['method']
|
|
|
|
state = module.params['state']
|
|
|
|
executable = module.params['executable']
|
|
|
|
binary = module.get_bin_path(executable, None)
|
|
|
|
|
|
|
|
if flatpakrepo_url is None:
|
|
|
|
flatpakrepo_url = ''
|
|
|
|
|
|
|
|
global result
|
|
|
|
result = dict(
|
|
|
|
changed=False
|
|
|
|
)
|
|
|
|
|
|
|
|
# If the binary was not found, fail the operation
|
|
|
|
if not binary:
|
|
|
|
module.fail_json(msg="Executable '%s' was not found on the system." % executable, **result)
|
|
|
|
|
|
|
|
remote_already_exists = remote_exists(module, binary, to_bytes(name), method)
|
|
|
|
|
|
|
|
if state == 'present' and not remote_already_exists:
|
|
|
|
add_remote(module, binary, name, flatpakrepo_url, method)
|
|
|
|
elif state == 'absent' and remote_already_exists:
|
|
|
|
remove_remote(module, binary, name, method)
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|