add shutdown nas and startup, reqs.txt and deploy.sh

This commit is contained in:
crate 2024-10-12 02:31:52 -05:00
parent 672690974d
commit 02a485591d
3 changed files with 50 additions and 14 deletions

10
cicd_deploy.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/bash
git pull origin main
source /.venv/bin/activate
pip install -r requirements.txt
sudo systemctl stop ups-power.service
sudo systemctl start ups-power.service

View File

@ -4,6 +4,7 @@ import logging
import smtplib
import asyncio
import discord
import paramiko
from ping3 import ping
from nut2 import PyNUTClient
from proxmoxer import ProxmoxAPI
@ -23,7 +24,8 @@ user_id = config.USER_ID
pve_host = config.PVE_HOST
pve_user = config.PVE_USER
pve_pass = config.PVE_PASS
pve_nodes = ['pve', 'c530']
servers = ['pve', 'c530']
nas_ip = config.HOST1_IP
wol_targets = {
'host1': {'mac': config.HOST1_MAC, 'ip': config.HOST1_IP},
@ -89,16 +91,7 @@ def send_discord(message):
await client.start(bot_token)
asyncio.run(message_send())
# Server Power Management
proxmox = ProxmoxAPI(pve_host, user=pve_user, password=pve_pass, verify_ssl=False)
def shutdown_pve(node):
try:
proxmox.nodes(node).status.post(command='shutdown')
logger.warning(f"Node {node} is shutting down.")
except Exception as e:
logger.error(e)
# UPS Monitor
def monitor_ups():
client = PyNUTClient()
try:
@ -111,8 +104,29 @@ def monitor_ups():
logger.error(f"Error communicating with NUT: {e}")
return None
# Server Power Management
proxmox = ProxmoxAPI(pve_host, user=pve_user, password=pve_pass, verify_ssl=False)
def shutdown_pve(node):
try:
proxmox.nodes(node).status.post(command='shutdown')
logger.warning(f"Node {node} is shutting down.")
except Exception as e:
logger.error(e)
def shutdown_nas():
try:
logger.warning("Nas is shutting down.")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(nas_ip)
stdin, stdout, stderr = client.exec_command('sudo shutdown')
for line in stdout:
print(line.strip())
except Exception as e:
logger.error(e)
def wake_up(battery):
if battery > 90:
for target, addr in wol_targets.items():
mac = addr['mac']
ip = addr['ip']
@ -134,7 +148,7 @@ def pwr_online(battery):
logger.info(message)
send_email(f"{ups_id}: Power On Line", message)
send_discord(message)
if ups_id == "Server":
if ups_id == "Server" and battery > 90:
wake_up(battery)
def pwr_offline(battery):
@ -154,6 +168,11 @@ def batt_crit(battery):
logger.warning(message)
send_email(f"{ups_id} battery critical!!", message)
send_discord(message)
if ups_id == "Server":
for node in servers:
shutdown_pve(node)
shutdown_nas()
time.sleep(300)
# Main Loop
def main():
@ -176,7 +195,7 @@ def main():
if battery < 25:
batt_crit(battery)
if ups_id == "Server":
for node in pve_nodes:
for node in servers:
shutdown_pve(node)
time.sleep(300)
else:

7
requirements.txt Normal file
View File

@ -0,0 +1,7 @@
nut2
ping3
config
discord
paramiko
proxmoxer
wakeonlan