5d519f2380
Fix error case when someone tries to delete swap even if it doesnt exist in the first place, Fix typo
133 lines
5.1 KiB
Python
133 lines
5.1 KiB
Python
import subprocess
|
||
from AbstractModule import AbstractModule
|
||
from PyInquirer import prompt
|
||
|
||
|
||
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 size": self._get_swap_size,
|
||
"Show swapiness": self._show_swapiness,
|
||
"Adjust temp. swapiness": self._adjust_swapiness_temp
|
||
}
|
||
menu = [
|
||
{
|
||
'type': 'list',
|
||
'message': 'Select action',
|
||
'name': 'action',
|
||
'choices': list(map(lambda x: {"name": x}, actions.keys()))
|
||
}
|
||
]
|
||
selected_action = prompt(menu)['action']
|
||
actions[selected_action]()
|
||
|
||
def _create_resize_persistent_swap(self):
|
||
self._create_resize_swap()
|
||
self._make_swap_persistent()
|
||
|
||
def _swap_location_check(self):
|
||
output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8")
|
||
final_list = list(output_swaps.split())
|
||
try:
|
||
print("Swap is located here:", final_list[5])
|
||
self.swap_file = final_list[5]
|
||
except IndexError:
|
||
print("Swap file doesn´t exist!")
|
||
return "NoSwap"
|
||
|
||
def _get_swap_size(self):
|
||
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
||
final_list = list(swap_size.split())
|
||
try:
|
||
print("Swap is {}b big!".format(final_list[7]))
|
||
except IndexError:
|
||
print("Swap file doesn´t exist!")
|
||
|
||
def _create_resize_swap(self):
|
||
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
||
if not output_swapon:
|
||
size = input("How big should the swap be (numbers in Gigabyte)? ")
|
||
create_swapfile = "sudo fallocate -l {}G /swapfile && ".format(size) + \
|
||
"sudo chmod 600 /swapfile && " + \
|
||
"sudo mkswap /swapfile && " + \
|
||
"sudo swapon /swapfile"
|
||
subprocess.call(create_swapfile, shell=True)
|
||
|
||
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
|
||
print(output_free.strip())
|
||
|
||
else:
|
||
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
||
final_list = list(swap_size.split())
|
||
print("")
|
||
print("Swap already installed! You can resize it!")
|
||
print("Curr. swap size: {}b".format(final_list[7]))
|
||
resize = input("How big should your swap become(numbers in Gigabyte)? ")
|
||
resize_swapfile = "sudo swapoff /swapfile && " + \
|
||
"sudo fallocate -l {}G /swapfile && ".format(resize) + \
|
||
"sudo mkswap /swapfile && " + \
|
||
"sudo swapon /swapfile"
|
||
subprocess.call(resize_swapfile, shell=True)
|
||
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
|
||
print(output_free.strip())
|
||
|
||
def _make_swap_persistent(self):
|
||
self._swap_location_check()
|
||
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)
|
||
|
||
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"
|
||
|
||
subprocess.call(get_swapiness, shell=True)
|
||
|
||
def _adjust_swapiness_temp(self):
|
||
actions = {
|
||
"Light": 25,
|
||
"Default": 60,
|
||
"Aggressive": 100
|
||
}
|
||
menu = [
|
||
{
|
||
'type': 'list',
|
||
'message': 'Select action',
|
||
'name': 'action',
|
||
'choices': list(map(lambda x: {"name": x}, actions.keys()))
|
||
}
|
||
]
|
||
selected_swapiness = prompt(menu)['action']
|
||
|
||
adjust = "sudo sysctl vm.swappiness=" + str(actions[selected_swapiness])
|
||
subprocess.call(adjust, shell=True)
|
||
print("Temporary swapiness is " + str(actions[selected_swapiness]))
|
||
|
||
def _delete_swap(self):
|
||
location = self._swap_location_check()
|
||
if location == "NoSwap":
|
||
return None
|
||
else:
|
||
disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \
|
||
"sudo rm {}".format(self.swap_file)
|
||
|
||
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())
|