diff --git a/modules/swap/module.py b/modules/swap/module.py index 3ee717e..b03140d 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -7,16 +7,14 @@ class SwapModule(AbstractModule): def __init__(self): super().__init__() - self.swap_file = None def run(self): - actions = { "Create/Resize temp. swap": self._create_resize_swap, "Create/Resize persistent swap": self._create_resize_persistent_swap, "Delete swap": self._delete_swap, "Make temp. swap persistent": self._make_swap_persistent, - "Get swap location": self._swap_location_check, + "Get swap location": self._get_swap_location, "Get swap size": self._get_swap_size, "Show swapiness": self._show_swapiness, "Adjust temp. swapiness": self._adjust_swapiness_temp @@ -36,14 +34,15 @@ class SwapModule(AbstractModule): self._create_resize_swap() self._make_swap_persistent() - def _swap_location_check(self): + def _get_swap_location(self): output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") try: swap_location = output_swaps.split()[5] print("Swap is located here:", swap_location) - self.swap_file = swap_location + return swap_location except IndexError: print("Swap file doesn´t exist!") + return None def _get_swap_size(self): swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") @@ -82,9 +81,12 @@ class SwapModule(AbstractModule): print(output_free.strip()) def _make_swap_persistent(self): - self._swap_location_check() + swap_location = self._get_swap_location() + if swap_location is None: + print("Swap file doesn't exist!") + return backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" - enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(self.swap_file) + enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(swap_location) subprocess.call(backup_fstab, shell=True) subprocess.call(enable_persistence, shell=True) print("Swap is now persistent!") @@ -114,12 +116,11 @@ class SwapModule(AbstractModule): print("Temporary swapiness is " + str(options[selected_swapiness])) def _delete_swap(self): - self._swap_location_check() - if self.swap_file is None: + swap_location = self._get_swap_location() + if swap_location is None: return - disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \ - "sudo rm {}".format(self.swap_file) - + disable_swapfile = "sudo swapoff {} && ".format(swap_location) + \ + "sudo rm {}".format(swap_location) subprocess.call(disable_swapfile, shell=True) output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")