diff --git a/personal/vm_shutdown/vm_shutdown.py b/personal/vm_shutdown/vm_shutdown.py index 76f8f5e..ff84dd4 100644 --- a/personal/vm_shutdown/vm_shutdown.py +++ b/personal/vm_shutdown/vm_shutdown.py @@ -1,11 +1,6 @@ #!/usr/bin/python3 -import os -import sys -import subprocess from subprocess import check_output, run -import time from time import sleep -import datetime from datetime import datetime @@ -15,38 +10,42 @@ def get_time(): def call_vm_shutdown(vmid): - run(['/usr/sbin/qm','shutdown',vmid]) + run(['/usr/sbin/qm', 'shutdown', vmid]) + def call_lxc_shutdown(lxc): - run(['/usr/bin/lxc-stop',lxc]) + run(['/usr/bin/lxc-stop', lxc]) + def get_status(vmid): - vmstatus = check_output(['/usr/sbin/qm','shutdown',vmid]) + vmstatus = check_output(['/usr/sbin/qm', 'shutdown', vmid]) vmstatus = vmstatus.decode().split(': ')[1].rstrip() return vmstatus def monitor_status(vmid): - vmstatus = 'running' - while vmstatus == 'running': - sleep(5) - vmstatus = check_output(['/usr/sbin/qm','status',vmid]) - vmstatus = vmstatus.decode().split(': ')[1].rstrip() - return 'Done' + vmstatus = 'running' + while vmstatus == 'running': + sleep(5) + vmstatus = check_output(['/usr/sbin/qm', 'status', vmid]) + vmstatus = vmstatus.decode().split(': ')[1].rstrip() + return 'Done' + def lxc_status(lxc): - vmstatus = 'running' - while vmstatus.lower() == 'running': - sleep(5) - vmoutput = check_output(['lxc-info', lxc]) - if b'RUNNING' not in vmoutput: - vmstatus = 'stopped' - #vmstatus = vmstatus.decode().split(': ')[1].rstrip() - print(vmstatus) - return 'Done' + vmstatus = 'running' + while vmstatus.lower() == 'running': + sleep(5) + vmoutput = check_output(['lxc-info', lxc]) + if b'RUNNING' not in vmoutput: + vmstatus = 'stopped' + print(vmstatus) + return 'Done' + def shutdown_host(): - run(['/usr/sbin/init','0']) + run(['/usr/sbin/init', '0']) + def main(): timestamp = get_time() @@ -58,20 +57,24 @@ def main(): call_vm_shutdown(vm) vresult = monitor_status(vm) print(f'{vresult}') - except Exception as e: + except Exception: print(f'{vm} currently not running') - lxcs = ['110', '114', '113', '111', '103', '104', '105', '112'] + lxcs = [ + '121', '116', '114', '113', '111', '106', + '103', '104', '105', '112', '100', '117', + '118', '119', '110' + ] for lxc in lxcs: try: print(f'Shutting down {lxc}') call_lxc_shutdown(lxc) vresult = lxc_status(lxc) print(f'{vresult}') - except Exception as e: + except Exception: print(f'{lxc} currently not running') timestamp = get_time() print(f'End time: {timestamp}') - #shutdown_host() + if __name__ == '__main__': main()