#!/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)