Re-implement SwapModule

This commit is contained in:
Lucas Noki 2020-01-18 04:15:08 +01:00
parent 23771e22be
commit fdd1c48bac
2 changed files with 66 additions and 46 deletions

5
modules/swap/__init__.py Normal file
View File

@ -0,0 +1,5 @@
from .module import SwapModule
def get_module():
return SwapModule()

View File

@ -1,61 +1,76 @@
import subprocess import subprocess
from PyInquirer import prompt
from AbstractModule import AbstractModule from AbstractModule import AbstractModule
from PyInquirer import prompt
class VimModule(AbstractModule): class SwapModule(AbstractModule):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.swap_file = None
def run(self):
# Check default swap location actions = ["Create/Resize", "Delete"]
def convert_string(string): menu = [
li = list(string.split()) {
return li 'type': 'list',
'message': 'Select action',
'name': 'action',
'choices': list(map(lambda x: {"name": x}, actions))
}
]
selected_action = prompt(menu)['action']
if selected_action is "Create/Resize":
self.create_resize_swap()
elif selected_action is "Delete":
self.delete_swap()
# Check default swap location
def swap_location_check(self) -> None:
output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8")
li = list(output_swaps.split())
final_list = li
try:
print("Swap is located here:", final_list[5])
self.swap_file = final_list[5]
except IndexError:
print("Swap file doesn´t exist!\n")
# End of location check
output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") def create_resize_swap(self):
final_list = convert_string(output_swaps) output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
print(final_list[5]) if not output_swapon:
# End of location check size = input("How big should the swap be(numbers in Gigabyte)? ")
create_swapfile = """sudo fallocate -l {}G /swapfile &&
sudo chmod 600 /swapfile &&
sudo mkswap /swapfile &&
sudo swapon /swapfile""".format(size)
subprocess.call(create_swapfile, shell=True)
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") print(output_free.strip())
option = input("Enter option. 1.Create/Resize 2.Delete: ") else:
print("")
print("Swap already installed! You can resize it!")
resize = input("How big should your swap become(numbers in Gigabyte)? ")
resize_swapfile = """sudo swapoff /swapfile &&
sudo fallocate -l {}G /swapfile &&
sudo mkswap /swapfile &&
sudo swapon /swapfile""".format(resize)
subprocess.call(resize_swapfile, shell=True)
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
print(output_free.strip())
if option == "1": def delete_swap(self):
if not output_swapon: self.swap_location_check()
size = input("How big should the swap be(numbers in Gigabyte)? ") disable_swapfile = """sudo swapoff {0} &&
create_swapfile = """sudo fallocate -l {}G /swapfile && sudo rm {0}""".format(self.swap_file)
sudo chmod 600 /swapfile &&
sudo mkswap /swapfile &&
sudo swapon /swapfile""".format(size)
subprocess.call(create_swapfile, shell=True)
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_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
print(output_free.strip()) if not output_swapon:
print("Swap deleted!")
else: print(output_free.strip())
print("")
print("Swap already installed! You can resize it!")
resize = input("How big should your swap become(numbers in Gigabyte)? ")
resize_swapfile = """sudo swapoff /swapfile &&
sudo fallocate -l {}G /swapfile &&
sudo mkswap /swapfile &&
sudo swapon /swapfile""".format(resize)
subprocess.call(resize_swapfile, shell=True)
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
print(output_free.strip())
elif option == "2":
disable_swapfile = """sudo swapoff /swapfile &&
sudo rm /swapfile"""
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")
if not output_swapon:
print("Swap deleted!")
print(output_free.strip())
else:
print("Wrong input!")