Skip to content

Commit

Permalink
clear cache after upgrade or downgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
cmcmarrow committed Aug 22, 2023
1 parent 12f9260 commit 3dd4f64
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions salt/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ def _pre_flight(self):
errors = []
critical_errors = []

import salt.utils.cache

salt.utils.cache.verify_cache_version(self.opts["cachedir"])

try:
os.chdir("/")
except OSError as err:
Expand Down
2 changes: 2 additions & 0 deletions salt/minion.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import salt.syspaths
import salt.transport
import salt.utils.args
import salt.utils.cache
import salt.utils.context
import salt.utils.crypt
import salt.utils.data
Expand Down Expand Up @@ -1248,6 +1249,7 @@ def __init__(
self.jid_queue = [] if jid_queue is None else jid_queue
self.periodic_callbacks = {}

salt.utils.cache.verify_cache_version(self.opts["cachedir"])
if io_loop is None:
self.io_loop = salt.ext.tornado.ioloop.IOLoop.current()
else:
Expand Down
31 changes: 31 additions & 0 deletions salt/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import os
import re
import shutil
import time

import salt.config
Expand All @@ -15,6 +16,8 @@
import salt.utils.dictupdate
import salt.utils.files
import salt.utils.msgpack
import salt.utils.path
import salt.version
from salt.utils.zeromq import zmq

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -345,3 +348,31 @@ def context_cache_wrap(*args, **kwargs):
return func(*args, **kwargs)

return context_cache_wrap


def verify_cache_version(cache):
"""
Check that cache version matches salt version.
If cache version deos not matches salt version wipe the cache.
:return: True if cache version matched. False if cache version did not match.
"""
with salt.utils.files.fopen(
salt.utils.path.join(cache, "cache_version"), "a+"
) as file:
file.seek(0)
data = "\n".join(file.readlines())
if data != salt.version.__version__:
log.warning(f"Cache version mismatch clearing: {repr(cache)}")
file.truncate(0)
file.write(salt.version.__version__)
for item in os.listdir(cache):
if item != "cache_version":
log.critical(item)
item_path = salt.utils.path.join(cache, item)
if os.path.isfile(item_path):
os.remove(item_path)
else:
shutil.rmtree(item_path)
return False
return True

0 comments on commit 3dd4f64

Please sign in to comment.