Add scripts for managing VM and LXC container power states and fan control
- Implemented set_fanspeed.py for dynamic fan speed control based on temperature. - Created ilo_poweroff.py and ilo_poweron.py for iLO power management. - Added ipmi_poweroff.py and ipmi_poweron.py for IPMI power control. - Developed vm_start.py, vm_stop.py, vm_restart.py, and vm_shutdown.py for VM and LXC management. - Introduced wrapper scripts (start_wrapper.sh, stop_wrapper.sh) for simplified execution. - Removed deprecated shell scripts for power control.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env python3
|
||||
import subprocess
|
||||
import re
|
||||
import sys
|
||||
import datetime
|
||||
import argparse
|
||||
|
||||
# IPMI settings
|
||||
IPMIUSER = "<username>"
|
||||
IPMIPW = "<password>"
|
||||
IPMIEK = "0000000000000000000000000000000000000000"
|
||||
|
||||
# Fan / temperature settings
|
||||
FANSPEED = 20 # percent
|
||||
MAXTEMP = 37 # Celsius
|
||||
|
||||
def run_ipmi(args):
|
||||
base = ["ipmitool", "-I", "lanplus", "-H", IPMIHOST, "-U", IPMIUSER, "-P", IPMIPW, "-y", IPMIEK]
|
||||
cmd = base + args
|
||||
p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
if p.returncode != 0:
|
||||
raise RuntimeError(f"ipmitool failed: {' '.join(cmd)}\n{p.stderr.strip()}")
|
||||
return p.stdout
|
||||
|
||||
def log_systemd(tag, msg):
|
||||
try:
|
||||
subprocess.run(["systemd-cat", "-t", tag], input=msg + "\n", text=True, check=True)
|
||||
except Exception:
|
||||
# fallback to stdout
|
||||
ts_print(msg)
|
||||
|
||||
def ts_print(msg):
|
||||
"""ts_print message prefixed with a timestamp (YYYY-MM-DD HH:MM:SS)."""
|
||||
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
print(f"{now} {msg}")
|
||||
|
||||
def get_ambient_temp():
|
||||
out = run_ipmi(["sdr", "type", "temperature"])
|
||||
# find lines like "Exhuat Temp | 29 degrees C"
|
||||
lines = [l for l in out.splitlines() if "Exhaust" in l and "degrees" in l]
|
||||
if not lines:
|
||||
raise RuntimeError("Exhaust temperature line not found in ipmitool output")
|
||||
# Extract last number found in the last matching line (like the original)
|
||||
field = lines[-1].split("|")[4]
|
||||
#ts_print("Debug: field =", field)
|
||||
m = re.search(r"(\d{1,3})", field)
|
||||
if not m:
|
||||
raise RuntimeError("Could not parse temperature from line: " + lines[-1])
|
||||
return int(m.group(1))
|
||||
|
||||
def main():
|
||||
# parse CLI args (IPMI host can be overridden with -H/--host)
|
||||
parser = argparse.ArgumentParser(description="Set fan speed via IPMI")
|
||||
parser.add_argument("-H", "--host", default="127.0.0.1", help="IPMI host/IP (default: %(default)s)")
|
||||
args = parser.parse_args()
|
||||
|
||||
global IPMIHOST
|
||||
# set global IPMIHOST so run_ipmi() uses the provided host
|
||||
IPMIHOST = args.host
|
||||
|
||||
ts_print(f"{IPMIHOST}: Fan speed check for host {IPMIHOST}")
|
||||
try:
|
||||
temp = get_ambient_temp()
|
||||
ts_print(f"{IPMIHOST}: Exhaust temperature: {temp} C")
|
||||
except Exception as e:
|
||||
ts_print(f"{IPMIHOST}: Error reading temperature: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
speed_hex = format(FANSPEED, "x")
|
||||
|
||||
if temp > MAXTEMP:
|
||||
msg = f"{IPMIHOST}:Warning: Temperature is too high! Activating dynamic fan control! ({temp} C)"
|
||||
log_systemd("R710-IPMI-TEMP", msg)
|
||||
ts_print(msg)
|
||||
run_ipmi(["raw", "0x30", "0x30", "0x01", "0x01"])
|
||||
else:
|
||||
okmsg = f"{IPMIHOST}: Temperature is OK ({temp} C)"
|
||||
log_systemd("R710-IPMI-TEMP", okmsg)
|
||||
ts_print(okmsg)
|
||||
ts_print(f"{IPMIHOST}: Activating manual fan speeds! (requested %d%%)" % FANSPEED)
|
||||
run_ipmi(["raw", "0x30", "0x30", "0x01", "0x00"])
|
||||
run_ipmi(["raw", "0x30", "0x30", "0x02", "0xff", "0x" + speed_hex])
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import subprocess
|
||||
import getpass
|
||||
import sys
|
||||
import datetime
|
||||
|
||||
def ts_print(msg):
|
||||
"""ts_print message prefixed with a timestamp (YYYY-MM-DD HH:MM:SS)."""
|
||||
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
print(f"{now} {msg}")
|
||||
|
||||
def run(cmd):
|
||||
try:
|
||||
subprocess.run(cmd, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Command failed: {' '.join(cmd)}", file=sys.stderr)
|
||||
sys.exit(e.returncode)
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description="Run iLO REST commands (login, serverinfo, reboot)")
|
||||
p.add_argument("-H", "--host", required=True, help="iLO host/IP")
|
||||
p.add_argument("-a", "--action", default="ForceOff", choices=["ForceOff", "GracefulRestart", "Reset"],
|
||||
help="reboot action to run (default: ForceOff)")
|
||||
p.add_argument("--no-serverinfo", action="store_true", help="skip running serverinfo")
|
||||
args = p.parse_args()
|
||||
|
||||
# login
|
||||
ts_print(f"Sending {args.action} to {args.host}")
|
||||
run(['ilorest', 'login', args.host, '-u', '<username>', '-p', '<password>'])
|
||||
# optional serverinfo
|
||||
if not args.no_serverinfo:
|
||||
run(['ilorest', 'serverinfo'])
|
||||
# power off / reboot action
|
||||
run(['ilorest', 'reboot', args.action])
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import subprocess
|
||||
import getpass
|
||||
import sys
|
||||
import datetime
|
||||
|
||||
def ts_print(msg):
|
||||
"""ts_print message prefixed with a timestamp (YYYY-MM-DD HH:MM:SS)."""
|
||||
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
print(f"{now} {msg}")
|
||||
|
||||
def run(cmd):
|
||||
try:
|
||||
subprocess.run(cmd, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Command failed: {' '.join(cmd)}", file=sys.stderr)
|
||||
sys.exit(e.returncode)
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description="Login to iLO, show serverinfo and power on")
|
||||
p.add_argument("-H", "--host", required=True, help="iLO host/IP")
|
||||
p.add_argument("-a", "--action", default="On", choices=["On","Off","GracefulRestart","ForceOff","Reset"],
|
||||
help="power/reboot action to run (default: On)")
|
||||
p.add_argument("--no-serverinfo", action="store_true", help="skip running serverinfo")
|
||||
args = p.parse_args()
|
||||
|
||||
ts_print(f"Sending {args.action} to {args.host}")
|
||||
run(['ilorest', 'login', args.host, '-u', '<username>', '-p', '<password>'])
|
||||
if not args.no_serverinfo:
|
||||
run(['ilorest', 'serverinfo'])
|
||||
run(['ilorest', 'reboot', args.action])
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
IPMIUSER = "<username>"
|
||||
IPMIPW = "<password>"
|
||||
IPMIEK = "0000000000000000000000000000000000000000"
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description="Send IPMI power off")
|
||||
p.add_argument("-H", "--host", required=True, help="IPMI host address")
|
||||
args = p.parse_args()
|
||||
|
||||
cmd = [
|
||||
"ipmitool",
|
||||
"-I", "lanplus",
|
||||
"-H", args.host,
|
||||
"-U", IPMIUSER,
|
||||
"-P", IPMIPW,
|
||||
"-y", IPMIEK,
|
||||
"power", "off",
|
||||
]
|
||||
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
if proc.returncode != 0:
|
||||
print("ipmitool failed:", proc.stderr.strip(), file=sys.stderr)
|
||||
sys.exit(proc.returncode)
|
||||
print(proc.stdout.strip())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import subprocess
|
||||
import sys
|
||||
import datetime
|
||||
|
||||
def ts_print(msg):
|
||||
"""ts_print message prefixed with a timestamp (YYYY-MM-DD HH:MM:SS)."""
|
||||
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
print(f"{now} {msg}")
|
||||
|
||||
IPMIUSER = "<username>"
|
||||
IPMIPW = "<password>"
|
||||
IPMIEK = "0000000000000000000000000000000000000000"
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description="Send IPMI power off")
|
||||
p.add_argument("-H", "--host", required=True, help="IPMI host address")
|
||||
args = p.parse_args()
|
||||
|
||||
cmd = [
|
||||
"ipmitool",
|
||||
"-I", "lanplus",
|
||||
"-H", args.host,
|
||||
"-U", IPMIUSER,
|
||||
"-P", IPMIPW,
|
||||
"-y", IPMIEK,
|
||||
"power", "on",
|
||||
]
|
||||
print(f"Sending power on to {args.host}")
|
||||
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
if proc.returncode != 0:
|
||||
print("ipmitool failed:", proc.stderr.strip(), file=sys.stderr)
|
||||
sys.exit(proc.returncode)
|
||||
print(proc.stdout.strip())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -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']"
|
||||
@@ -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']"
|
||||
@@ -0,0 +1,47 @@
|
||||
#!/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
|
||||
|
||||
|
||||
def get_time():
|
||||
ts1 = str(datetime.now()).split('.')[0]
|
||||
return ts1
|
||||
|
||||
|
||||
def call_powerup(vmid):
|
||||
run(['/usr/sbin/qm','start',vmid])
|
||||
|
||||
|
||||
def monitor_status(vmid):
|
||||
vmstatus = 'stopped'
|
||||
while vmstatus == 'stopped':
|
||||
sleep(5)
|
||||
vmstatus = check_output(['/usr/sbin/qm','status',vmid])
|
||||
vmstatus = vmstatus.decode().split(': ')[1].rstrip()
|
||||
return 'Done'
|
||||
|
||||
|
||||
def main():
|
||||
timestamp = get_time()
|
||||
print(f'Start time: {timestamp}')
|
||||
vms = ['105']
|
||||
for vm in vms:
|
||||
try:
|
||||
print(f'starting up {vm}')
|
||||
call_powerup(vm)
|
||||
vresult = monitor_status(vm)
|
||||
print(f'{vresult}')
|
||||
except Exception as e:
|
||||
print(f'{vm} currently not running')
|
||||
timestamp = get_time()
|
||||
print(f'End time: {timestamp}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,91 @@
|
||||
#!/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_shutdown(client_type, clientid):
|
||||
if client_type == 'vm':
|
||||
run(['/usr/sbin/qm', 'shutdown', clientid])
|
||||
elif client_type == 'lxc':
|
||||
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 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}')
|
||||
|
||||
|
||||
# This script is used to restart VMs or LXC containers on a Proxmox host.
|
||||
# It takes two command line arguments: the type of client (vm or lxc) and a list of client IDs.
|
||||
# example: ./cron_scripts/power/vm_restart.py "lxc" "['110', '113', '114', '120']"
|
||||
|
||||
|
||||
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'Shutting down {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}')
|
||||
else:
|
||||
print_state(client_type.upper(), clientid, vresult)
|
||||
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()
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
#!/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_shutdown(vmid):
|
||||
run(['/usr/sbin/qm', 'shutdown', vmid])
|
||||
|
||||
|
||||
def call_lxc_shutdown(lxc):
|
||||
run(['/usr/bin/lxc-stop', lxc])
|
||||
|
||||
|
||||
def get_status(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'
|
||||
|
||||
|
||||
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 shutdown_host():
|
||||
run(['/usr/sbin/init', '0'])
|
||||
|
||||
|
||||
def main():
|
||||
timestamp = get_time()
|
||||
print(f'Start time: {timestamp}')
|
||||
vms = ['115', '109', '108', '107', '101']
|
||||
for vm in vms:
|
||||
try:
|
||||
print(f'Shutting down {vm}')
|
||||
call_vm_shutdown(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'Shutting down {lxc}')
|
||||
call_lxc_shutdown(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()
|
||||
@@ -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()
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user