/
pkgng.py
1658 lines (1184 loc) · 40 KB
/
pkgng.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
'''
Support for ``pkgng``, the new package manager for FreeBSD
.. warning::
This module has been completely rewritten. Up to and including version
0.17.x, it was available as the ``pkgng`` module, (``pkgng.install``,
``pkgng.delete``, etc.), but moving forward this module will no longer be
available as ``pkgng``, as it will behave like a normal Salt ``pkg``
provider. The documentation below should not be considered to apply to this
module in versions <= 0.17.x. If your minion is running a 0.17.x release or
older, then the documentation for this module can be viewed using the
:mod:`sys.doc <salt.modules.sys.doc>` function:
.. code-block:: bash
salt bsdminion sys.doc pkgng
This module provides an interface to ``pkg(8)``. It acts as the default
package provider for FreeBSD 10 and newer. For FreeBSD hosts which have
been upgraded to use pkgng, you will need to override the ``pkg`` provider
by setting the :conf_minion:`providers` parameter in your Minion config
file, in order to use this module to manage packages, like so:
.. code-block:: yaml
providers:
pkg: pkgng
'''
from __future__ import absolute_import
# Import python libs
import copy
import logging
import os
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, MinionError
import salt.ext.six as six
log = logging.getLogger(__name__)
# Define the module's virtual name
__virtualname__ = 'pkg'
def __virtual__():
'''
Load as 'pkg' on FreeBSD 10 and greater.
Load as 'pkg' on DragonFly BSD.
Load as 'pkg' on FreeBSD 9 when config option
``providers:pkg`` is set to 'pkgng'.
'''
if __grains__['kernel'] == 'DragonFly':
return __virtualname__
if __grains__['os'] == 'FreeBSD' and float(__grains__['osrelease']) >= 10:
return __virtualname__
if __grains__['os'] == 'FreeBSD' and \
float(__grains__['osmajorrelease']) == 9:
providers = {}
if 'providers' in __opts__:
providers = __opts__['providers']
log.debug('__opts__.providers: {0}'.format(providers))
if providers and 'pkg' in providers and providers['pkg'] == 'pkgng':
log.debug('Configuration option \'providers:pkg\' is set to '
'\'pkgng\', using \'pkgng\' in favor of \'freebsdpkg\'.')
return __virtualname__
return False
def _pkg(jail=None, chroot=None):
'''
Returns the prefix for a pkg command, using -j if a jail is specified, or
-c if chroot is specified.
'''
ret = 'pkg'
if jail:
ret += ' -j {0!r}'.format(jail)
elif chroot:
ret += ' -c {0!r}'.format(chroot)
return ret
def _get_pkgng_version(jail=None, chroot=None):
'''
return the version of 'pkg'
'''
return __salt__['cmd.run']([_pkg(jail, chroot), '--version']).strip()
def _get_version(name, results):
'''
``pkg search`` will return all packages for which the pattern is a match.
Narrow this down and return the package version, or None if no exact match.
'''
for line in results.splitlines():
if not line:
continue
try:
pkgname, pkgver = line.rsplit('-', 1)
except ValueError:
continue
if pkgname == name:
return pkgver
return None
def _contextkey(jail=None, chroot=None, prefix='pkg.list_pkgs'):
'''
As this module is designed to manipulate packages in jails and chroots, use
the passed jail/chroot to ensure that a key in the __context__ dict that is
unique to that jail/chroot is used.
'''
if jail:
return str(prefix) + '.jail_{0}'.format(jail)
elif chroot:
return str(prefix) + '.chroot_{0}'.format(chroot)
return prefix
def parse_config(file_name='/usr/local/etc/pkg.conf'):
'''
Return dict of uncommented global variables.
CLI Example:
.. code-block:: bash
salt '*' pkg.parse_config
``NOTE:`` not working properly right now
'''
ret = {}
if not os.path.isfile(file_name):
return 'Unable to find {0} on file system'.format(file_name)
with salt.utils.fopen(file_name) as ifile:
for line in ifile:
if line.startswith('#') or line.startswith('\n'):
pass
else:
key, value = line.split('\t')
ret[key] = value
ret['config_file'] = file_name
return ret
def version(*names, **kwargs):
'''
Returns a string representing the package version or an empty string if not
installed. If more than one package name is specified, a dict of
name/version pairs is returned.
.. note::
This function can accessed using ``pkg.info`` in addition to
``pkg.version``, to more closely match the CLI usage of ``pkg(8)``.
jail
Get package version information for the specified jail
chroot
Get package version information for the specified chroot (ignored if
``jail`` is specified)
with_origin : False
Return a nested dictionary containing both the origin name and version
for each specified package.
.. versionadded:: 2014.1.0
CLI Example:
.. code-block:: bash
salt '*' pkg.version <package name>
salt '*' pkg.version <package name> jail=<jail name or id>
salt '*' pkg.version <package1> <package2> <package3> ...
'''
with_origin = kwargs.pop('with_origin', False)
ret = __salt__['pkg_resource.version'](*names, **kwargs)
if not salt.utils.is_true(with_origin):
return ret
# Put the return value back into a dict since we're adding a subdict
if len(names) == 1:
ret = {names[0]: ret}
origins = __context__.get('pkg.origin', {})
return dict([
(x, {'origin': origins.get(x, ''), 'version': y})
for x, y in six.iteritems(ret)
])
# Support pkg.info get version info, since this is the CLI usage
info = salt.utils.alias_function(version, 'info')
def refresh_db(jail=None, chroot=None, force=False):
'''
Refresh PACKAGESITE contents
.. note::
This function can accessed using ``pkg.update`` in addition to
``pkg.refresh_db``, to more closely match the CLI usage of ``pkg(8)``.
CLI Example:
.. code-block:: bash
salt '*' pkg.refresh_db
jail
Refresh the pkg database within the specified jail
chroot
Refresh the pkg database within the specified chroot (ignored if
``jail`` is specified)
force
Force a full download of the repository catalog without regard to the
respective ages of the local and remote copies of the catalog.
CLI Example:
.. code-block:: bash
salt '*' pkg.refresh_db force=True
'''
opts = ''
if force:
opts += ' -f'
return __salt__['cmd.retcode'](
'{0} update{1}'.format(_pkg(jail, chroot), opts),
python_shell=False) == 0
# Support pkg.update to refresh the db, since this is the CLI usage
update = salt.utils.alias_function(refresh_db, 'update')
def latest_version(*names, **kwargs):
'''
Return the latest version of the named package available for upgrade or
installation. If more than one package name is specified, a dict of
name/version pairs is returned.
If the latest version of a given package is already installed, an empty
string will be returned for that package.
CLI Example:
.. code-block:: bash
salt '*' pkg.latest_version <package name>
salt '*' pkg.latest_version <package name> jail=<jail name or id>
salt '*' pkg.latest_version <package name> chroot=/path/to/chroot
'''
if len(names) == 0:
return ''
ret = {}
# Initialize the dict with empty strings
for name in names:
ret[name] = ''
jail = kwargs.get('jail')
chroot = kwargs.get('chroot')
pkgs = list_pkgs(versions_as_list=True, jail=jail, chroot=chroot)
if salt.utils.compare_versions(_get_pkgng_version(jail, chroot), '>=', '1.6.0'):
quiet = True
else:
quiet = False
for name in names:
cmd = [_pkg(jail, chroot), 'search']
if quiet:
cmd.append('-q')
cmd.append(name)
pkgver = _get_version(
name,
__salt__['cmd.run'](cmd, python_shell=False, output_loglevel='trace')
)
if pkgver is not None:
installed = pkgs.get(name, [])
if not installed:
ret[name] = pkgver
else:
if not any(
(salt.utils.compare_versions(ver1=x,
oper='>=',
ver2=pkgver)
for x in installed)
):
ret[name] = pkgver
# Return a string if only one package name passed
if len(names) == 1:
return ret[names[0]]
return ret
# available_version is being deprecated
available_version = salt.utils.alias_function(latest_version, 'available_version')
def list_pkgs(versions_as_list=False,
jail=None,
chroot=None,
with_origin=False,
**kwargs):
'''
List the packages currently installed as a dict::
{'<package_name>': '<version>'}
jail
List the packages in the specified jail
chroot
List the packages in the specified chroot (ignored if ``jail`` is
specified)
with_origin : False
Return a nested dictionary containing both the origin name and version
for each installed package.
.. versionadded:: 2014.1.0
CLI Example:
.. code-block:: bash
salt '*' pkg.list_pkgs
salt '*' pkg.list_pkgs jail=<jail name or id>
salt '*' pkg.list_pkgs chroot=/path/to/chroot
'''
# not yet implemented or not applicable
if any([salt.utils.is_true(kwargs.get(x))
for x in ('removed', 'purge_desired')]):
return {}
versions_as_list = salt.utils.is_true(versions_as_list)
contextkey_pkg = _contextkey(jail, chroot)
contextkey_origins = _contextkey(jail, chroot, prefix='pkg.origin')
if contextkey_pkg in __context__:
ret = copy.deepcopy(__context__[contextkey_pkg])
if not versions_as_list:
__salt__['pkg_resource.stringify'](ret)
if salt.utils.is_true(with_origin):
origins = __context__.get(contextkey_origins, {})
return dict([
(x, {'origin': origins.get(x, ''), 'version': y})
for x, y in six.iteritems(ret)
])
return ret
ret = {}
origins = {}
cmd = '{0} info -ao'.format(_pkg(jail, chroot))
out = __salt__['cmd.run_stdout'](
cmd,
python_shell=False,
output_loglevel='trace')
for line in out.splitlines():
if not line:
continue
try:
pkg, origin = line.split()
pkgname, pkgver = pkg.rsplit('-', 1)
except ValueError:
continue
__salt__['pkg_resource.add_pkg'](ret, pkgname, pkgver)
origins[pkgname] = origin
__salt__['pkg_resource.sort_pkglist'](ret)
__context__[contextkey_pkg] = copy.deepcopy(ret)
__context__[contextkey_origins] = origins
if not versions_as_list:
__salt__['pkg_resource.stringify'](ret)
if salt.utils.is_true(with_origin):
return dict([
(x, {'origin': origins.get(x, ''), 'version': y})
for x, y in six.iteritems(ret)
])
return ret
def update_package_site(new_url):
'''
Updates remote package repo URL, PACKAGESITE var to be exact.
Must use ``http://``, ``ftp://``, or ``https://`` protocol
CLI Example:
.. code-block:: bash
salt '*' pkg.update_package_site http://127.0.0.1/
'''
config_file = parse_config()['config_file']
__salt__['file.sed'](
config_file, 'PACKAGESITE.*', 'PACKAGESITE\t : {0}'.format(new_url)
)
# add change return later
return True
def stats(local=False, remote=False, jail=None, chroot=None):
'''
Return pkgng stats.
CLI Example:
.. code-block:: bash
salt '*' pkg.stats
local
Display stats only for the local package database.
CLI Example:
.. code-block:: bash
salt '*' pkg.stats local=True
remote
Display stats only for the remote package database(s).
CLI Example:
.. code-block:: bash
salt '*' pkg.stats remote=True
jail
Retrieve stats from the specified jail.
CLI Example:
.. code-block:: bash
salt '*' pkg.stats jail=<jail name or id>
salt '*' pkg.stats jail=<jail name or id> local=True
salt '*' pkg.stats jail=<jail name or id> remote=True
chroot
Retrieve stats from the specified chroot (ignored if ``jail`` is
specified).
CLI Example:
.. code-block:: bash
salt '*' pkg.stats chroot=/path/to/chroot
salt '*' pkg.stats chroot=/path/to/chroot local=True
salt '*' pkg.stats chroot=/path/to/chroot remote=True
'''
opts = ''
if local:
opts += 'l'
if remote:
opts += 'r'
if opts:
opts = '-' + opts
res = __salt__['cmd.run'](
'{0} stats {1}'.format(_pkg(jail, chroot), opts),
python_shell=False,
output_loglevel='trace'
)
res = [x.strip("\t") for x in res.split("\n")]
return res
def backup(file_name, jail=None, chroot=None):
'''
Export installed packages into yaml+mtree file
CLI Example:
.. code-block:: bash
salt '*' pkg.backup /tmp/pkg
jail
Backup packages from the specified jail. Note that this will run the
command within the jail, and so the path to the backup file will be
relative to the root of the jail
CLI Example:
.. code-block:: bash
salt '*' pkg.backup /tmp/pkg jail=<jail name or id>
chroot
Backup packages from the specified chroot (ignored if ``jail`` is
specified). Note that this will run the command within the chroot, and
so the path to the backup file will be relative to the root of the
chroot.
CLI Example:
.. code-block:: bash
salt '*' pkg.backup /tmp/pkg chroot=/path/to/chroot
'''
res = __salt__['cmd.run'](
'{0} backup -d {1!r}'.format(_pkg(jail, chroot), file_name),
python_shell=False,
output_loglevel='trace'
)
return res.split('...')[1]
def restore(file_name, jail=None, chroot=None):
'''
Reads archive created by pkg backup -d and recreates the database.
CLI Example:
.. code-block:: bash
salt '*' pkg.restore /tmp/pkg
jail
Restore database to the specified jail. Note that this will run the
command within the jail, and so the path to the file from which the pkg
database will be restored is relative to the root of the jail.
CLI Example:
.. code-block:: bash
salt '*' pkg.restore /tmp/pkg jail=<jail name or id>
chroot
Restore database to the specified chroot (ignored if ``jail`` is
specified). Note that this will run the command within the chroot, and
so the path to the file from which the pkg database will be restored is
relative to the root of the chroot.
CLI Example:
.. code-block:: bash
salt '*' pkg.restore /tmp/pkg chroot=/path/to/chroot
'''
return __salt__['cmd.run'](
'{0} backup -r {1!r}'.format(_pkg(jail, chroot), file_name),
python_shell=False,
output_loglevel='trace'
)
def audit(jail=None, chroot=None):
'''
Audits installed packages against known vulnerabilities
CLI Example:
.. code-block:: bash
salt '*' pkg.audit
jail
Audit packages within the specified jail
CLI Example:
.. code-block:: bash
salt '*' pkg.audit jail=<jail name or id>
chroot
Audit packages within the specified chroot (ignored if ``jail`` is
specified)
CLI Example:
.. code-block:: bash
salt '*' pkg.audit chroot=/path/to/chroot
'''
return __salt__['cmd.run'](
'{0} audit -F'.format(_pkg(jail, chroot)),
python_shell=False,
output_loglevel='trace'
)
def install(name=None,
fromrepo=None,
pkgs=None,
sources=None,
jail=None,
chroot=None,
orphan=False,
force=False,
glob=False,
local=False,
dryrun=False,
quiet=False,
reinstall_requires=False,
regex=False,
pcre=False,
**kwargs):
'''
Install package(s) from a repository
name
The name of the package to install
CLI Example:
.. code-block:: bash
salt '*' pkg.install <package name>
jail
Install the package into the specified jail
chroot
Install the package into the specified chroot (ignored if ``jail`` is
specified)
orphan
Mark the installed package as orphan. Will be automatically removed
if no other packages depend on them. For more information please
refer to ``pkg-autoremove(8)``.
CLI Example:
.. code-block:: bash
salt '*' pkg.install <package name> orphan=True
force
Force the reinstallation of the package if already installed.
CLI Example:
.. code-block:: bash
salt '*' pkg.install <package name> force=True
glob
Treat the package names as shell glob patterns.
CLI Example:
.. code-block:: bash
salt '*' pkg.install <package name> glob=True
local
Do not update the repository catalogs with ``pkg-update(8)``. A
value of ``True`` here is equivalent to using the ``-U`` flag with
``pkg install``.
CLI Example:
.. code-block:: bash
salt '*' pkg.install <package name> local=True
dryrun
Dru-run mode. The list of changes to packages is always printed,
but no changes are actually made.
CLI Example:
.. code-block:: bash
salt '*' pkg.install <package name> dryrun=True
quiet
Force quiet output, except when dryrun is used, where pkg install
will always show packages to be installed, upgraded or deleted.
CLI Example:
.. code-block:: bash
salt '*' pkg.install <package name> quiet=True
reinstall_requires
When used with force, reinstalls any packages that require the
given package.
CLI Example:
.. code-block:: bash
salt '*' pkg.install <package name> reinstall_requires=True force=True
.. versionchanged:: 2014.7.0
``require`` kwarg renamed to ``reinstall_requires``
fromrepo
In multi-repo mode, override the pkg.conf ordering and only attempt
to download packages from the named repository.
CLI Example:
.. code-block:: bash
salt '*' pkg.install <package name> fromrepo=repo
regex
Treat the package names as a regular expression
CLI Example:
.. code-block:: bash
salt '*' pkg.install <regular expression> regex=True
pcre
Treat the package names as extended regular expressions.
CLI Example:
.. code-block:: bash
salt '*' pkg.install <extended regular expression> pcre=True
'''
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
if pkg_params is None or len(pkg_params) == 0:
return {}
opts = 'y'
repo_opts = ''
if salt.utils.is_true(orphan):
opts += 'A'
if salt.utils.is_true(force):
opts += 'f'
if salt.utils.is_true(glob):
opts += 'g'
if salt.utils.is_true(local):
opts += 'U'
if salt.utils.is_true(dryrun):
opts += 'n'
if salt.utils.is_true(quiet):
opts += 'q'
if salt.utils.is_true(reinstall_requires):
opts += 'R'
if fromrepo:
repo_opts += 'r {0}'.format(fromrepo)
if salt.utils.is_true(regex):
opts += 'x'
if salt.utils.is_true(pcre):
opts += 'X'
if opts:
opts = '-' + opts
if repo_opts:
repo_opts = '-' + repo_opts
old = list_pkgs(jail=jail, chroot=chroot)
if pkg_type == 'file':
pkg_cmd = 'add'
# pkg add has smaller set of options (i.e. no -y or -n), filter below
opts = ''.join([opt for opt in opts if opt in 'AfIMq'])
targets = pkg_params
elif pkg_type == 'repository':
pkg_cmd = 'install'
if pkgs is None and kwargs.get('version') and len(pkg_params) == 1:
# Only use the 'version' param if 'name' was not specified as a
# comma-separated list
pkg_params = {name: kwargs.get('version')}
targets = []
for param, version_num in six.iteritems(pkg_params):
if version_num is None:
targets.append(param)
else:
targets.append('{0}-{1}'.format(param, version_num))
cmd = '{0} {1} {2} {3} {4}'.format(
_pkg(jail, chroot), pkg_cmd, repo_opts, opts, ' '.join(targets)
)
if pkg_cmd == 'add' and salt.utils.is_true(dryrun):
# pkg add doesn't have a dryrun mode, so echo out what will be run
return cmd
__salt__['cmd.run'](cmd, python_shell=False, output_loglevel='trace')
__context__.pop(_contextkey(jail, chroot), None)
__context__.pop(_contextkey(jail, chroot, prefix='pkg.origin'), None)
new = list_pkgs(jail=jail, chroot=chroot)
return salt.utils.compare_dicts(old, new)
def remove(name=None,
pkgs=None,
jail=None,
chroot=None,
all_installed=False,
force=False,
glob=False,
dryrun=False,
recurse=False,
regex=False,
pcre=False,
**kwargs):
'''
Remove a package from the database and system
.. note::
This function can accessed using ``pkg.delete`` in addition to
``pkg.remove``, to more closely match the CLI usage of ``pkg(8)``.
name
The package to remove
CLI Example:
.. code-block:: bash
salt '*' pkg.remove <package name>
jail
Delete the package from the specified jail
chroot
Delete the package from the specified chroot (ignored if ``jail`` is
specified)
all_installed
Deletes all installed packages from the system and empties the
database. USE WITH CAUTION!
CLI Example:
.. code-block:: bash
salt '*' pkg.remove all all_installed=True force=True
force
Forces packages to be removed despite leaving unresolved
dependencies.
CLI Example:
.. code-block:: bash
salt '*' pkg.remove <package name> force=True
glob
Treat the package names as shell glob patterns.
CLI Example:
.. code-block:: bash
salt '*' pkg.remove <package name> glob=True
dryrun
Dry run mode. The list of packages to delete is always printed, but
no packages are actually deleted.
CLI Example:
.. code-block:: bash
salt '*' pkg.remove <package name> dryrun=True
recurse
Delete all packages that require the listed package as well.
CLI Example:
.. code-block:: bash
salt '*' pkg.remove <package name> recurse=True
regex
Treat the package names as regular expressions.
CLI Example:
.. code-block:: bash
salt '*' pkg.remove <regular expression> regex=True
pcre
Treat the package names as extended regular expressions.
CLI Example:
.. code-block:: bash
salt '*' pkg.remove <extended regular expression> pcre=True
'''
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
targets = []
old = list_pkgs(jail=jail, chroot=chroot, with_origin=True)
for pkg in pkg_params.items():
# FreeBSD pkg supports `openjdk` and `java/openjdk7` package names
if pkg[0].find("/") > 0:
origin = pkg[0]
pkg = [k for k, v in old.iteritems() if v['origin'] == origin][0]
if pkg[0] in old:
targets.append(pkg[0])
if not targets:
return {}
opts = ''
if salt.utils.is_true(all_installed):
opts += 'a'
if salt.utils.is_true(force):
opts += 'f'
if salt.utils.is_true(glob):
opts += 'g'
if salt.utils.is_true(dryrun):
opts += 'n'
if not salt.utils.is_true(dryrun):
opts += 'y'
if salt.utils.is_true(recurse):
opts += 'R'
if salt.utils.is_true(regex):
opts += 'x'
if salt.utils.is_true(pcre):
opts += 'X'
if opts:
opts = '-' + opts
cmd = '{0} delete {1} {2}'.format(
_pkg(jail, chroot), opts, ' '.join(targets)
)
__salt__['cmd.run'](cmd, python_shell=False, output_loglevel='trace')
__context__.pop(_contextkey(jail, chroot), None)
__context__.pop(_contextkey(jail, chroot, prefix='pkg.origin'), None)
new = list_pkgs(jail=jail, chroot=chroot, with_origin=True)
return salt.utils.compare_dicts(old, new)
# Support pkg.delete to remove packages, since this is the CLI usage
delete = salt.utils.alias_function(remove, 'delete')
# No equivalent to purge packages, use remove instead
purge = salt.utils.alias_function(remove, 'purge')
def upgrade(*names, **kwargs):
'''
Upgrade named or all packages (run a ``pkg upgrade``). If <package name> is
omitted, the operation is executed on all packages.
CLI Example:
.. code-block:: bash
salt '*' pkg.upgrade <package name>
jail
Audit packages within the specified jail
CLI Example:
.. code-block:: bash
salt '*' pkg.upgrade <package name> jail=<jail name or id>
chroot
Audit packages within the specified chroot (ignored if ``jail`` is
specified)
CLI Example:
.. code-block:: bash
salt '*' pkg.upgrade <package name> chroot=/path/to/chroot
Any of the below options can also be used with ``jail`` or ``chroot``.
force
Force reinstalling/upgrading the whole set of packages.