Extract run shell command

rename check_fstab to fstab_exists
This commit is contained in:
Lucas Noki 2020-02-01 03:28:46 +01:00
parent e25c3f16d6
commit 50bea3cba0

View File

@ -1,8 +1,14 @@
import subprocess import subprocess
from typing import List
from AbstractModule import AbstractModule from AbstractModule import AbstractModule
from PyInquirer import prompt from PyInquirer import prompt
def _run_shell_command(cmd: List[str]) -> str:
return subprocess.check_output(cmd).decode("UTF-8").strip()
class SwapModule(AbstractModule): class SwapModule(AbstractModule):
def __init__(self): def __init__(self):
@ -18,7 +24,7 @@ class SwapModule(AbstractModule):
"Get swap size": self._get_swap_size, "Get swap size": self._get_swap_size,
"Show swapiness": self._show_swapiness, "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 "Check fstab for entry": self._fstab_entry_exists
} }
menu = [ menu = [
{ {
@ -36,7 +42,7 @@ class SwapModule(AbstractModule):
self._make_swap_persistent() self._make_swap_persistent()
def _get_swap_location(self): 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: try:
swap_location = output_swaps.split()[5] swap_location = output_swaps.split()[5]
print("Swap is located here:", swap_location) print("Swap is located here:", swap_location)
@ -46,7 +52,7 @@ class SwapModule(AbstractModule):
return None return None
def _get_swap_size(self): def _get_swap_size(self):
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") swap_size = _run_shell_command(['swapon', '--show'])
try: try:
swap_size = swap_size.split()[7] swap_size = swap_size.split()[7]
print("Swap is {}b big!".format(swap_size)) print("Swap is {}b big!".format(swap_size))
@ -54,10 +60,9 @@ class SwapModule(AbstractModule):
print("Swap file doesn´t exist!") print("Swap file doesn´t exist!")
def _create_resize_swap(self): def _create_resize_swap(self):
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") output_swapon = _run_shell_command(['swapon', '--show'])
if output_swapon: if output_swapon is not "":
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") swap_size = output_swapon.split()[7]
swap_size = swap_size.split()[7]
print("") print("")
print("Swap already installed! You can resize it!") print("Swap already installed! You can resize it!")
print("Curr. swap size: {}b".format(swap_size)) print("Curr. swap size: {}b".format(swap_size))
@ -67,7 +72,7 @@ class SwapModule(AbstractModule):
"sudo mkswap /swapfile && " + \ "sudo mkswap /swapfile && " + \
"sudo swapon /swapfile" "sudo swapon /swapfile"
subprocess.call(resize_swapfile, shell=True) 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()) print(output_free.strip())
else: else:
@ -78,7 +83,7 @@ class SwapModule(AbstractModule):
"sudo swapon /swapfile" "sudo swapon /swapfile"
subprocess.call(create_swapfile, shell=True) 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()) print(output_free.strip())
def _make_swap_persistent(self): def _make_swap_persistent(self):
@ -88,7 +93,7 @@ class SwapModule(AbstractModule):
return return
backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" 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) 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!") print("Swap is already persistent!")
else: else:
subprocess.call(backup_fstab, shell=True) subprocess.call(backup_fstab, shell=True)
@ -96,8 +101,7 @@ class SwapModule(AbstractModule):
print("Swap is now persistent!") print("Swap is now persistent!")
def _show_swapiness(self): def _show_swapiness(self):
get_swapiness = "cat /proc/sys/vm/swappiness" print(_run_shell_command(["cat", "/proc/sys/vm/swappiness"]))
subprocess.call(get_swapiness, shell=True)
def _adjust_swapiness_temp(self): def _adjust_swapiness_temp(self):
options = { options = {
@ -125,7 +129,7 @@ class SwapModule(AbstractModule):
return return
disable_swapfile = "sudo swapoff {} && ".format(swap_location) + \ disable_swapfile = "sudo swapoff {} && ".format(swap_location) + \
"sudo rm {}".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: with open("/etc/fstab", "r") as fstab_out:
content = fstab_out.readlines() content = fstab_out.readlines()
with open("/etc/fstab", "w") as fstab_in: with open("/etc/fstab", "w") as fstab_in:
@ -135,13 +139,13 @@ class SwapModule(AbstractModule):
else: else:
print("No entry in /etc/fstab!") print("No entry in /etc/fstab!")
subprocess.call(disable_swapfile, shell=True) subprocess.call(disable_swapfile, shell=True)
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") output_swapon = _run_shell_command(['swapon', '--show'])
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") output_free = _run_shell_command(["free", "-h"])
if not output_swapon: if not output_swapon:
print("Swap deleted!") print("Swap deleted!")
print(output_free.strip()) print(output_free.strip())
def _check_fstab_entry(self): def _fstab_entry_exists(self):
swap_location = self._get_swap_location() swap_location = self._get_swap_location()
fstab_entry = "{} none swap sw 0 0\n".format(swap_location) fstab_entry = "{} none swap sw 0 0\n".format(swap_location)
with open("/etc/fstab", "r") as fstab_file: with open("/etc/fstab", "r") as fstab_file: