-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathpkgset.py
More file actions
96 lines (73 loc) · 2.95 KB
/
pkgset.py
File metadata and controls
96 lines (73 loc) · 2.95 KB
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
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 SUSE LLC
#
# SPDX-License-Identifier: Apache-2.0
"""
Watch RPM or DPkg database via cookies and fire
an event to the SUSE Multi-Linux Manager if that has been changed.
Author: Bo Maryniuk <bo@suse.de>
"""
from __future__ import absolute_import
import os
import salt.cache
import salt.config
__virtualname__ = "pkgset"
SALT_CONFIG_DIR = os.environ.get("SALT_CONFIG_DIR", "/etc/salt")
__opts__ = salt.config.minion_config(os.path.join(SALT_CONFIG_DIR, "minion"))
CACHE = salt.cache.Cache(__opts__)
PKGSET_COOKIES = (
os.path.join(__opts__["cachedir"], "rpmdb.cookie"),
os.path.join(__opts__["cachedir"], "dpkg.cookie"),
)
# pylint: disable-next=invalid-name
def __virtual__():
return __virtualname__
# pylint: disable-next=unused-argument
def validate(config):
"""
The absence of this function could cause noisy logging,
when logging level set to DEBUG or TRACE.
So we need to have it with no any validation inside.
"""
return True, "There is nothing to validate"
# pylint: disable-next=unused-argument
def beacon(config):
"""
Watch the cookie file from package manager plugin.
If its content changes, fire an event to the Master.
Example Config
.. code-block:: yaml
beacons:
pkgset:
interval: 5
"""
ret = []
for cookie_path in PKGSET_COOKIES:
if not os.path.exists(cookie_path):
continue
# pylint: disable-next=unspecified-encoding
with open(cookie_path) as ck_file:
ck_data = ck_file.read().strip()
# pylint: disable-next=undefined-variable
if __virtualname__ not in __context__:
# After a minion restart, when this is running for first time, there is nothing in context yet
# So, if there is any data in the cache, we put it in the context, if not we put the new data.
# and update the data in the cache.
cache_data = CACHE.fetch("beacon/pkgset", "cookie").get("data", None)
if cache_data:
# pylint: disable-next=undefined-variable
__context__[__virtualname__] = cache_data
else:
# pylint: disable-next=undefined-variable
__context__[__virtualname__] = ck_data
CACHE.store("beacon/pkgset", "cookie", {"data": ck_data})
# pylint: disable-next=undefined-variable
if __context__[__virtualname__] != ck_data:
# Now it's time to fire beacon event only if the new data is not yet
# inside the context (meaning not proceesed), and then stop iterating
ret.append({"tag": "changed"})
CACHE.store("beacon/pkgset", "cookie", {"data": ck_data})
# pylint: disable-next=undefined-variable
__context__[__virtualname__] = ck_data
break
return ret