From b9bf1454ad729d3ab7df53a39d08e710124fb6ab Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Tue, 28 Jan 2020 09:14:09 +0100 Subject: [PATCH 1/4] Add function to check for fstab entry, Prevent double entries --- modules/swap/module.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index b03140d..ffedbab 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -17,7 +17,8 @@ class SwapModule(AbstractModule): "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 + "Adjust temp. swapiness": self._adjust_swapiness_temp, + "Check fstab for entry": self._check_fstab_entry } menu = [ { @@ -82,14 +83,18 @@ class SwapModule(AbstractModule): def _make_swap_persistent(self): swap_location = self._get_swap_location() + persistence_entry = self._check_fstab_entry() 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(swap_location) - subprocess.call(backup_fstab, shell=True) - subprocess.call(enable_persistence, shell=True) - print("Swap is now persistent!") + if persistence_entry is True: + print("Swap is already persistent!") + else: + subprocess.call(backup_fstab, shell=True) + subprocess.call(enable_persistence, shell=True) + print("Swap is now persistent!") def _show_swapiness(self): get_swapiness = "cat /proc/sys/vm/swappiness" @@ -127,3 +132,15 @@ class SwapModule(AbstractModule): if not output_swapon: print("Swap deleted!") print(output_free.strip()) + + def _check_fstab_entry(self): + swap_location = self._get_swap_location() + fstab_entry = "{} none swap sw 0 0\n".format(swap_location) + with open("/etc/fstab", "r") as fstab_file: + line = fstab_file.readlines()[-1] + if line != fstab_entry: + print("No entry in /etc/fstab") + return False + else: + print("fstab entry:", line.strip()) + return True From faac5d22bef83ca670473b8b703b5823b1518b3b Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Wed, 29 Jan 2020 08:52:50 +0100 Subject: [PATCH 2/4] If swap gets deleted entry from /etc/fstab gets also deleted --- modules/swap/module.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/swap/module.py b/modules/swap/module.py index ffedbab..7fb7827 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -126,6 +126,15 @@ class SwapModule(AbstractModule): return disable_swapfile = "sudo swapoff {} && ".format(swap_location) + \ "sudo rm {}".format(swap_location) + if self._check_fstab_entry() is True: + with open("/etc/fstab", "r") as fstab_out: + content = fstab_out.readlines() + with open("/etc/fstab", "w") as fstab_in: + content = content[:-1] + for line in content: + fstab_in.write(line) + else: + print("No entry in /etc/fstab!") 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") From e25c3f16d6d16c86542809a51ee6eba92e83aefb Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 1 Feb 2020 03:06:37 +0100 Subject: [PATCH 3/4] Simplify if statements --- modules/swap/module.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index 7fb7827..f6a6838 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -83,13 +83,12 @@ class SwapModule(AbstractModule): def _make_swap_persistent(self): swap_location = self._get_swap_location() - persistence_entry = self._check_fstab_entry() 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(swap_location) - if persistence_entry is True: + if self._check_fstab_entry(): print("Swap is already persistent!") else: subprocess.call(backup_fstab, shell=True) @@ -126,7 +125,7 @@ class SwapModule(AbstractModule): return disable_swapfile = "sudo swapoff {} && ".format(swap_location) + \ "sudo rm {}".format(swap_location) - if self._check_fstab_entry() is True: + if self._check_fstab_entry(): with open("/etc/fstab", "r") as fstab_out: content = fstab_out.readlines() with open("/etc/fstab", "w") as fstab_in: From 50bea3cba00fa22770e729333be8b142f1d7612f Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 1 Feb 2020 03:28:46 +0100 Subject: [PATCH 4/4] Extract run shell command rename check_fstab to fstab_exists --- modules/swap/module.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index f6a6838..cd342bc 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -1,8 +1,14 @@ import subprocess +from typing import List + from AbstractModule import AbstractModule from PyInquirer import prompt +def _run_shell_command(cmd: List[str]) -> str: + return subprocess.check_output(cmd).decode("UTF-8").strip() + + class SwapModule(AbstractModule): def __init__(self): @@ -18,7 +24,7 @@ class SwapModule(AbstractModule): "Get swap size": self._get_swap_size, "Show swapiness": self._show_swapiness, "Adjust temp. swapiness": self._adjust_swapiness_temp, - "Check fstab for entry": self._check_fstab_entry + "Check fstab for entry": self._fstab_entry_exists } menu = [ { @@ -36,7 +42,7 @@ class SwapModule(AbstractModule): self._make_swap_persistent() def _get_swap_location(self): - output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") + output_swaps = _run_shell_command(["cat", "/proc/swaps"]) try: swap_location = output_swaps.split()[5] print("Swap is located here:", swap_location) @@ -46,7 +52,7 @@ class SwapModule(AbstractModule): return None def _get_swap_size(self): - swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") + swap_size = _run_shell_command(['swapon', '--show']) try: swap_size = swap_size.split()[7] print("Swap is {}b big!".format(swap_size)) @@ -54,10 +60,9 @@ class SwapModule(AbstractModule): print("Swap file doesn´t exist!") def _create_resize_swap(self): - output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") - if output_swapon: - swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") - swap_size = swap_size.split()[7] + output_swapon = _run_shell_command(['swapon', '--show']) + if output_swapon is not "": + swap_size = output_swapon.split()[7] print("") print("Swap already installed! You can resize it!") print("Curr. swap size: {}b".format(swap_size)) @@ -67,7 +72,7 @@ class SwapModule(AbstractModule): "sudo mkswap /swapfile && " + \ "sudo swapon /swapfile" subprocess.call(resize_swapfile, shell=True) - output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") + output_free = _run_shell_command(["free", "-h"]) print(output_free.strip()) else: @@ -78,7 +83,7 @@ class SwapModule(AbstractModule): "sudo swapon /swapfile" subprocess.call(create_swapfile, shell=True) - output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") + output_free = _run_shell_command(["free", "-h"]) print(output_free.strip()) def _make_swap_persistent(self): @@ -88,7 +93,7 @@ class SwapModule(AbstractModule): return backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(swap_location) - if self._check_fstab_entry(): + if self._fstab_entry_exists(): print("Swap is already persistent!") else: subprocess.call(backup_fstab, shell=True) @@ -96,8 +101,7 @@ class SwapModule(AbstractModule): print("Swap is now persistent!") def _show_swapiness(self): - get_swapiness = "cat /proc/sys/vm/swappiness" - subprocess.call(get_swapiness, shell=True) + print(_run_shell_command(["cat", "/proc/sys/vm/swappiness"])) def _adjust_swapiness_temp(self): options = { @@ -125,7 +129,7 @@ class SwapModule(AbstractModule): return disable_swapfile = "sudo swapoff {} && ".format(swap_location) + \ "sudo rm {}".format(swap_location) - if self._check_fstab_entry(): + if self._fstab_entry_exists(): with open("/etc/fstab", "r") as fstab_out: content = fstab_out.readlines() with open("/etc/fstab", "w") as fstab_in: @@ -135,13 +139,13 @@ class SwapModule(AbstractModule): else: print("No entry in /etc/fstab!") 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") + output_swapon = _run_shell_command(['swapon', '--show']) + output_free = _run_shell_command(["free", "-h"]) if not output_swapon: print("Swap deleted!") print(output_free.strip()) - def _check_fstab_entry(self): + def _fstab_entry_exists(self): swap_location = self._get_swap_location() fstab_entry = "{} none swap sw 0 0\n".format(swap_location) with open("/etc/fstab", "r") as fstab_file: