From 20839809b0d1e74b474a64eb98180796a13e3625 Mon Sep 17 00:00:00 2001 From: Wendell Jones Date: Thu, 3 Jul 2025 15:19:49 -0400 Subject: [PATCH] adding new scripts to start and stop clients as well as wrapper scripts to execute them --- personal/system/start_wrapper.sh | 2 + personal/system/stop_wrapper.sh | 2 + .../{vm_shutdown.py => vm_shutdown.py.v1} | 0 personal/system/vm_start.py | 66 +++++++++++++++ personal/system/vm_startup.py | 70 ---------------- personal/system/vm_stop.py | 50 +++++++++++ personal/system/vm_stop.py.v1 | 82 +++++++++++++++++++ 7 files changed, 202 insertions(+), 70 deletions(-) create mode 100644 personal/system/start_wrapper.sh create mode 100644 personal/system/stop_wrapper.sh rename personal/system/{vm_shutdown.py => vm_shutdown.py.v1} (100%) create mode 100644 personal/system/vm_start.py delete mode 100644 personal/system/vm_startup.py create mode 100644 personal/system/vm_stop.py create mode 100644 personal/system/vm_stop.py.v1 diff --git a/personal/system/start_wrapper.sh b/personal/system/start_wrapper.sh new file mode 100644 index 0000000..d71ef6c --- /dev/null +++ b/personal/system/start_wrapper.sh @@ -0,0 +1,2 @@ +#!/bin/bash +/root/cron_scripts/power/vm_start.py "lxc" "['100', '105', '104', '103', '112', '107', '101', '108', '109', '106', '115', '118', '116', '117', '121', '119', '111', '113', '114', '110', '120', '123', '124']" \ No newline at end of file diff --git a/personal/system/stop_wrapper.sh b/personal/system/stop_wrapper.sh new file mode 100644 index 0000000..bb0773c --- /dev/null +++ b/personal/system/stop_wrapper.sh @@ -0,0 +1,2 @@ +#!/bin/bash +/root/cron_scripts/power/vm_stop.py "lxc" "['100', '105', '104', '103', '112', '107', '101', '108', '109', '106', '115', '118', '116', '117', '121', '119', '111', '113', '114', '110', '120', '123', '124']" \ No newline at end of file diff --git a/personal/system/vm_shutdown.py b/personal/system/vm_shutdown.py.v1 similarity index 100% rename from personal/system/vm_shutdown.py rename to personal/system/vm_shutdown.py.v1 diff --git a/personal/system/vm_start.py b/personal/system/vm_start.py new file mode 100644 index 0000000..e2dc564 --- /dev/null +++ b/personal/system/vm_start.py @@ -0,0 +1,66 @@ +#!/usr/bin/python3 +import sys, ast +from subprocess import check_output, run +from time import sleep +from datetime import datetime + + +def get_time(): + ts1 = str(datetime.now()).split('.')[0] + return ts1 + + +def call_startup(client_type, clientid): + if client_type == 'vm': + run(['/usr/sbin/qm', 'start', clientid]) + elif client_type == 'lxc': + run(['/usr/bin/lxc-start', clientid]) + else: + print('invalid client type or client id') + + +def get_status(client_type, clientid, vmstatus, status_wanted): + checker = None + if client_type == 'lxc': + checker = check_output(['lxc-info', clientid]) + elif client_type == 'vm': + checker = check_output(['/usr/sbin/qm', 'status', clientid]) + while vmstatus != status_wanted: + sleep(5) + vmoutput = checker + if (str.encode(status_wanted)) in vmoutput.upper(): + vmstatus = status_wanted + return status_wanted + + +def print_state(vmtype, vmhost, vmstatus): + print(f'{vmtype} {vmhost} is {vmstatus}') + + +def main(): + client_type = None + client_list = None + try: + client_type = sys.argv[1] + client_list = ast.literal_eval(sys.argv[2]) + except Exception as e: + print(f'Unable to process cli arguments. {e}') + else: + print(f'{client_type} {client_list}') + timestamp = get_time() + print(f'Start time: {timestamp}') + for clientid in client_list: + try: + print(f'Starting {clientid}') + call_startup(client_type, clientid) + vresult = get_status(client_type, clientid, 'STOPPED', 'RUNNING') + except Exception as e: + print(f'Unable to start container {clientid}: {e}') + else: + print_state(client_type.upper(), clientid, vresult) + timestamp = get_time() + print(f'End time: {timestamp}') + + +if __name__ == '__main__': + main() diff --git a/personal/system/vm_startup.py b/personal/system/vm_startup.py deleted file mode 100644 index 90a2cb5..0000000 --- a/personal/system/vm_startup.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/python3 -from subprocess import check_output, run -from time import sleep -from datetime import datetime - - -def get_time(): - ts1 = str(datetime.now()).split('.')[0] - return ts1 - - -def call_vm_startup(vmid): - run(['/usr/sbin/qm', 'start', vmid]) - - -def call_lxc_startup(lxc): - run(['/usr/bin/lxc-start', lxc]) - - -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' - - -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' - print(vmstatus) - return 'Done' - - -def main(): - timestamp = get_time() - print(f'Start time: {timestamp}') - vms = ['115', '109', '108', '107', '101'] - for vm in vms: - try: - print(f'Starting {vm}') - call_vm_startup(vm) - vresult = monitor_status(vm) - print(f'{vresult}') - except Exception: - print(f'{vm} currently not running') - lxcs = [ - '121', '116', '114', '113', '111', '106', - '103', '104', '105', '112', '100', '117', - '118', '119', '110' - ] - for lxc in lxcs: - try: - print(f'Starting {lxc}') - call_lxc_startup(lxc) - vresult = lxc_status(lxc) - print(f'{vresult}') - except Exception: - print(f'{lxc} currently not running') - timestamp = get_time() - print(f'End time: {timestamp}') - - -if __name__ == '__main__': - main() diff --git a/personal/system/vm_stop.py b/personal/system/vm_stop.py new file mode 100644 index 0000000..0f51c69 --- /dev/null +++ b/personal/system/vm_stop.py @@ -0,0 +1,50 @@ +#!/usr/bin/python3 +import os, sys, ast, platform +import subprocess +from time import sleep +from datetime import datetime + + +def get_time(): + ts1 = str(datetime.now()).split('.')[0] + return ts1 + + +def client_shutdown(client_type, clientid): + lxc_filepath = "/usr/bin/lxc-stop" + qemu_filepath = "/usr/sbin/qm" + try: + print(f'Stopping {clientid}') + if client_type == 'vm': + process_id = os.spawnv(os.P_NOWAIT, qemu_filepath, ['shutdown', clientid]) + elif client_type == 'lxc': + process_id = os.spawnv(os.P_NOWAIT, lxc_filepath, [client_type, clientid]) + print(process_id) + except Exception as e: + print(f'Unable to stop container {clientid}: {e}') + +def main(): + client_type = None + client_list = None + try: + client_type = sys.argv[1] + client_list = ast.literal_eval(sys.argv[2]) + except Exception as e: + print(f'Unable to process cli arguments. {e}') + else: + print(f'{client_type} {client_list}') + timestamp = get_time() + print(f'Start time: {timestamp}') + for clientid in client_list: + client_shutdown(client_type, clientid) + else: + print(f'Successfully stopped client list.') + + timestamp = get_time() + print(f'End time: {timestamp}') + + +if __name__ == '__main__': + main() +# sys.exit(0) +# sys.exit(1) diff --git a/personal/system/vm_stop.py.v1 b/personal/system/vm_stop.py.v1 new file mode 100644 index 0000000..5df20b0 --- /dev/null +++ b/personal/system/vm_stop.py.v1 @@ -0,0 +1,82 @@ +#!/usr/bin/python3 +import sys, ast, platform +import subprocess +from time import sleep +from datetime import datetime + + +def get_time(): + ts1 = str(datetime.now()).split('.')[0] + return ts1 + + +def call_shutdown(client_type, clientid): + if client_type == 'vm': + subprocess.run(['/usr/sbin/qm', 'shutdown', clientid]) + elif client_type == 'lxc': + subprocess.run(['/usr/bin/lxc-stop', clientid]) + else: + print('Unable to stop {clientid}. Invalid client type or client id') + print(f'client_type: {client_type}') + print(f'client id: {clientid}') + + +def get_status(client_type, clientid, vmstatus, status_wanted): + checker = None + if client_type == 'lxc': + checker = subprocess.check_output(['lxc-info', clientid]) + elif client_type == 'vm': + checker = subprocess.check_output(['/usr/sbin/qm', 'status', clientid]) + while vmstatus != status_wanted: + sleep(5) + vmoutput = checker + if (str.encode(status_wanted)) in vmoutput.upper(): + vmstatus = status_wanted + return status_wanted + + +def print_state(vmtype, vmhost, vmstatus): + print(f'{vmtype} {vmhost} is {vmstatus}') + + +def client_shutdown(client_type, clientid): + failed_attempt = 0 + vresult = None + try: + print(f'Stopping {clientid}') + call_shutdown(client_type, clientid) + vresult = get_status(client_type, clientid, 'RUNNING', 'STOPPED') + except Exception as e: + print(f'Unable to stop container {clientid}: {e}') + failed_attempt = 1 + return ([failed_attempt, client_type, clientid]) + + +def main(): + client_type = None + client_list = None + failed_count = 0 + fail_list = [] + try: + client_type = sys.argv[1] + client_list = ast.literal_eval(sys.argv[2]) + except Exception as e: + print(f'Unable to process cli arguments. {e}') + else: + print(f'{client_type} {client_list}') + timestamp = get_time() + print(f'Start time: {timestamp}') + for clientid in client_list: + counter = client_shutdown(client_type, clientid) + failed_count += counter[0] + fail_list.append(counter[1:]) + print(f'Successfully stopped client list.') + + timestamp = get_time() + print(f'End time: {timestamp}') + + +if __name__ == '__main__': + main() +# sys.exit(0) +# sys.exit(1)