generated from status-im/infra-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vault-plugin: create caching mechanism
referenced issue: status-im/infra-template#13 Signed-off-by: markoburcul <[email protected]>
- Loading branch information
1 parent
02eb698
commit 92583d5
Showing
3 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import os | ||
|
||
from ansible.plugins.callback import CallbackBase | ||
from ansible.utils.display import Display | ||
|
||
display = Display() | ||
|
||
class CallbackModule(CallbackBase): | ||
CALLBACK_VERSION = 2.0 | ||
CALLBACK_TYPE = 'aggregate' | ||
CALLBACK_NAME = 'cache_cleanup' | ||
|
||
def __init__(self): | ||
super(CallbackModule, self).__init__() | ||
self.cache_dir = "./ansible/files/cache/vault" | ||
self.cache_file = f"{self.cache_dir}/{os.getpid()}.cache" | ||
self._create_cache_dir() | ||
|
||
def _create_cache_dir(self): | ||
"""Creates the cache dir if it doesn't exist.""" | ||
try: | ||
if not os.path.exists(self.cache_dir): | ||
os.makedirs(self.cache_dir) | ||
display.v(f"Created cache directory: {self.cache_dir}") | ||
except Exception as e: | ||
display.error(f"Failed to create cache dir: {e}") | ||
|
||
def v2_playbook_on_stats(self, stats): | ||
"""Called when the playbook ends, deletes all files in the cache directory.""" | ||
try: | ||
if os.path.exists(self.cache_dir): | ||
for filename in os.listdir(self.cache_dir): | ||
file_path = os.path.join(self.cache_dir, filename) | ||
try: | ||
os.remove(file_path) | ||
display.v(f"Deleted cache file: {file_path}") | ||
except Exception as e: | ||
display.error(f"Failed to delete {file_path}: {e}") | ||
except Exception as e: | ||
display.error(f"Failed to clean up cache directory: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters