Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flatpak module #51993

Merged
merged 17 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/ref/modules/all/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ execution modules
extfs
file
firewalld
flatpak
freebsd_sysctl
freebsd_update
freebsdjail
Expand Down
6 changes: 6 additions & 0 deletions doc/ref/modules/all/salt.modules.flatpak.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
====================
salt.modules.flatpak
====================

.. automodule:: salt.modules.flatpak
:members:
1 change: 1 addition & 0 deletions doc/ref/states/all/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ state modules
file
firewall
firewalld
flatpak
gem
git
github
Expand Down
6 changes: 6 additions & 0 deletions doc/ref/states/all/salt.modules.flatpak.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
===================
salt.states.flatpak
===================

.. automodule:: salt.states.flatpak
:members:
153 changes: 153 additions & 0 deletions salt/modules/flatpak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# -*- coding: utf-8 -*-
'''
Manage flatpak packages via Salt

.. versionadded:: Neon

:depends: flatpak for distribution
'''

from __future__ import absolute_import, print_function, unicode_literals
import re

import salt.utils.path

FLATPAK_BINARY_NAME = 'flatpak'

__virtualname__ = 'flatpak'


def __virtual__():
if salt.utils.path.which('flatpak'):
return __virtualname__

return False, 'The flatpak execution module cannot be loaded: the "flatpak" binary is not in the path.'


def install(location, name):
'''
Install the specified flatpak package or runtime from the specified location.

Args:
location (str): The location or remote to install from.
name (str): The name of the package or runtime.

Returns:
dict: The ``result`` and ``output``.

CLI Example:
.. code-block:: bash

salt '*' flatpak.install flathub org.gimp.GIMP
'''
twangboy marked this conversation as resolved.
Show resolved Hide resolved
ret = {'result': None, 'output': ''}

out = __salt__['cmd.run_all'](FLATPAK_BINARY_NAME + ' install ' + location + ' ' + name)

if out['retcode'] and out['stderr']:
ret['stderr'] = out['stderr'].strip()
ret['result'] = False
else:
ret['stdout'] = out['stdout'].strip()
ret['result'] = True

return ret


def is_installed(name):
'''
Determine if a package or runtime is installed.

Args:
name (str): The name of the package or the runtime.

Returns:
bool: True if the specified package or runtime is installed.

CLI Example:
.. code-block:: bash

salt '*' flatpak.is_installed org.gimp.GIMP
'''
twangboy marked this conversation as resolved.
Show resolved Hide resolved
out = __salt__['cmd.run_all'](FLATPAK_BINARY_NAME + ' info ' + name)

if out['retcode'] and out['stderr']:
return False
else:
return True


def uninstall(pkg):
'''
Uninstall the specified package.

Args:
pkg (str): The package name.

Returns:
dict: The ``result`` and ``output``.

CLI Example:
.. code-block:: bash

salt '*' flatpak.uninstall org.gimp.GIMP
'''
ret = {'result': None, 'output': ''}

out = __salt__['cmd.run_all'](FLATPAK_BINARY_NAME + ' uninstall ' + pkg)

if out['retcode'] and out['stderr']:
ret['stderr'] = out['stderr'].strip()
ret['result'] = False
else:
ret['stdout'] = out['stdout'].strip()
ret['result'] = True

return ret


def add_remote(name, location):
'''
:noindex:

CLI Example:
.. code-block:: bash
twangboy marked this conversation as resolved.
Show resolved Hide resolved

salt '*' flatpak.add_remote flathub https://flathub.org/repo/flathub.flatpakrepo
'''
ret = {'result': None, 'output': ''}
out = __salt__['cmd.run_all'](FLATPAK_BINARY_NAME + ' remote-add ' + name + ' ' + location)

if out['retcode'] and out['stderr']:
ret['stderr'] = out['stderr'].strip()
ret['result'] = False
else:
ret['stdout'] = out['stdout'].strip()
ret['result'] = True

return ret


def is_remote_added(remote):
'''
Determines if a remote exists.

Args:
remote (str): The remote's name.

Returns:
bool: True if the remote has already been added.

CLI Example:
.. code-block:: bash

salt '*' flatpak.is_remote_added flathub
'''
out = __salt__['cmd.run_all'](FLATPAK_BINARY_NAME + ' remotes')

lines = out.splitlines()
for item in lines:
i = re.split(r'\t+', item.rstrip('\t'))
if i[0] == remote:
return True
return False
174 changes: 174 additions & 0 deletions salt/states/flatpak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# -*- coding: utf-8 -*-
'''
Management of flatpak packages
==============================
Allows the installation and uninstallation of flatpak packages.

.. versionadded:: Neon
'''
from __future__ import absolute_import, print_function, unicode_literals

import salt.utils.path

__virtualname__ = 'flatpak'


def __virtual__():
if salt.utils.path.which('flatpak'):
return __virtualname__

return False, 'The flatpak state module cannot be loaded: the "flatpak" binary is not in the path.'


def installed(location, name):
'''
Ensure that the named package is installed.

Args:
location (str): The location or remote to install the flatpak from.
name (str): The name of the package or runtime.

Returns:
dict: The ``result`` and ``output``.

Example:
.. code-block:: bash

install_package:
flatpack.installed:
- location: flathub
- name: gimp
'''
twangboy marked this conversation as resolved.
Show resolved Hide resolved
ret = {'name': name,
'changes': {},
'result': None,
'comment': ''}

old = __salt__['flatpak.is_installed'](name)
if not old:
if __opts__['test']:
ret['comment'] = 'Package "{0}" would have been installed'.format(name)
ret['changes']['new'] = name
ret['changes']['old'] = None
ret['result'] = None
return ret

install_ret = __salt__['flatpak.install'](name, location)
if __salt__['flatpak.is_installed'](name):
ret['comment'] = 'Package "{0}" was installed'.format(name)
ret['changes']['new'] = name
ret['changes']['old'] = None
ret['result'] = True
return ret

ret['comment'] = 'Package "{0}" failed to install'.format(name)
ret['comment'] += '\noutput:\n' + install_ret['output']
ret['result'] = False
return ret

ret['comment'] = 'Package "{0}" is already installed'.format(name)
if __opts__['test']:
ret['result'] = None
return ret

ret['result'] = True
return ret


def uninstalled(name):
'''
Ensure that the named package is not installed.

Args:
name (str): The flatpak package.

Returns:
dict: The ``result`` and ``output``.

Example:
.. code-block:: bash

uninstall_package:
flatpack.uninstalled:
- name: gimp
'''
ret = {'name': name,
'changes': {},
'result': None,
'comment': ''}

old = __salt__['flatpak.is_installed'](name)
if not old:
ret['comment'] = 'Package {0} is not installed'.format(name)
ret['result'] = True
return ret
else:
if __opts__['test']:
ret['comment'] = 'Package {0} would have been uninstalled'.format(name)
ret['changes']['old'] = old[0]['version']
ret['changes']['new'] = None
ret['result'] = None
return ret

__salt__['flatpak.uninstall'](name)
if not __salt__['flatpak.is_installed'](name):
ret['comment'] = 'Package {0} uninstalled'.format(name)
ret['changes']['old'] = old[0]['version']
ret['changes']['new'] = None
ret['result'] = True
return ret


def add_remote(name, location):
'''
Adds a new location to install flatpak packages from.

Args:
name (str): The repository's name.
location (str): The location of the repository.

Returns:
dict: The ``result`` and ``output``.

Example:
.. code-block:: bash

add_flathub:
flatpack.add_remote:
- name: flathub
- location: https://flathub.org/repo/flathub.flatpakrepo
'''
ret = {'name': name,
'changes': {},
'result': None,
'comment': ''}

old = __salt__['flatpak.is_remote_added'](name)
if not old:
if __opts__['test']:
ret['comment'] = 'Remote "{0}" would have been added'.format(name)
ret['changes']['new'] = name
ret['changes']['old'] = None
ret['result'] = None
return ret

install_ret = __salt__['flatpak.add_remote'](name)
if __salt__['flatpak.is_remote_added'](name):
ret['comment'] = 'Remote "{0}" was added'.format(name)
ret['changes']['new'] = name
ret['changes']['old'] = None
ret['result'] = True
return ret

ret['comment'] = 'Failed to add remote "{0}"'.format(name)
ret['comment'] += '\noutput:\n' + install_ret['output']
ret['result'] = False
return ret

ret['comment'] = 'Remote "{0}" already exists'.format(name)
if __opts__['test']:
ret['result'] = None
return ret

ret['result'] = True
return ret