Skip to content

Commit

Permalink
Merge pull request #51993 from steveno/add_flatpak
Browse files Browse the repository at this point in the history
Add flatpak module
  • Loading branch information
waynew authored Apr 25, 2019
2 parents 95354e5 + ee6fd6b commit 211589a
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 0 deletions.
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
5 changes: 5 additions & 0 deletions doc/ref/modules/all/salt.modules.flatpak.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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
5 changes: 5 additions & 0 deletions doc/ref/states/all/salt.states.flatpak.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
salt.states.flatpak
===================

.. automodule:: salt.states.flatpak
:members:
165 changes: 165 additions & 0 deletions salt/modules/flatpak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# -*- 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
'''
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
'''
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):
'''
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``.
CLI Example:
.. code-block:: bash
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
177 changes: 177 additions & 0 deletions salt/states/flatpak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# -*- 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:: yaml
install_package:
flatpack.installed:
- location: flathub
- name: gimp
'''
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:: yaml
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:: yaml
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

0 comments on commit 211589a

Please sign in to comment.