add shutdown nas and startup, reqs.txt and deploy.sh
This commit is contained in:
parent
672690974d
commit
02a485591d
10
cicd_deploy.sh
Executable file
10
cicd_deploy.sh
Executable 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
|
47
power.py
47
power.py
@ -4,6 +4,7 @@ import logging
|
|||||||
import smtplib
|
import smtplib
|
||||||
import asyncio
|
import asyncio
|
||||||
import discord
|
import discord
|
||||||
|
import paramiko
|
||||||
from ping3 import ping
|
from ping3 import ping
|
||||||
from nut2 import PyNUTClient
|
from nut2 import PyNUTClient
|
||||||
from proxmoxer import ProxmoxAPI
|
from proxmoxer import ProxmoxAPI
|
||||||
@ -23,7 +24,8 @@ user_id = config.USER_ID
|
|||||||
pve_host = config.PVE_HOST
|
pve_host = config.PVE_HOST
|
||||||
pve_user = config.PVE_USER
|
pve_user = config.PVE_USER
|
||||||
pve_pass = config.PVE_PASS
|
pve_pass = config.PVE_PASS
|
||||||
pve_nodes = ['pve', 'c530']
|
servers = ['pve', 'c530']
|
||||||
|
nas_ip = config.HOST1_IP
|
||||||
|
|
||||||
wol_targets = {
|
wol_targets = {
|
||||||
'host1': {'mac': config.HOST1_MAC, 'ip': config.HOST1_IP},
|
'host1': {'mac': config.HOST1_MAC, 'ip': config.HOST1_IP},
|
||||||
@ -89,16 +91,7 @@ def send_discord(message):
|
|||||||
await client.start(bot_token)
|
await client.start(bot_token)
|
||||||
asyncio.run(message_send())
|
asyncio.run(message_send())
|
||||||
|
|
||||||
# Server Power Management
|
# UPS Monitor
|
||||||
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 monitor_ups():
|
def monitor_ups():
|
||||||
client = PyNUTClient()
|
client = PyNUTClient()
|
||||||
try:
|
try:
|
||||||
@ -111,8 +104,29 @@ def monitor_ups():
|
|||||||
logger.error(f"Error communicating with NUT: {e}")
|
logger.error(f"Error communicating with NUT: {e}")
|
||||||
return None
|
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):
|
def wake_up(battery):
|
||||||
if battery > 90:
|
|
||||||
for target, addr in wol_targets.items():
|
for target, addr in wol_targets.items():
|
||||||
mac = addr['mac']
|
mac = addr['mac']
|
||||||
ip = addr['ip']
|
ip = addr['ip']
|
||||||
@ -134,7 +148,7 @@ def pwr_online(battery):
|
|||||||
logger.info(message)
|
logger.info(message)
|
||||||
send_email(f"{ups_id}: Power On Line", message)
|
send_email(f"{ups_id}: Power On Line", message)
|
||||||
send_discord(message)
|
send_discord(message)
|
||||||
if ups_id == "Server":
|
if ups_id == "Server" and battery > 90:
|
||||||
wake_up(battery)
|
wake_up(battery)
|
||||||
|
|
||||||
def pwr_offline(battery):
|
def pwr_offline(battery):
|
||||||
@ -154,6 +168,11 @@ def batt_crit(battery):
|
|||||||
logger.warning(message)
|
logger.warning(message)
|
||||||
send_email(f"{ups_id} battery critical!!", message)
|
send_email(f"{ups_id} battery critical!!", message)
|
||||||
send_discord(message)
|
send_discord(message)
|
||||||
|
if ups_id == "Server":
|
||||||
|
for node in servers:
|
||||||
|
shutdown_pve(node)
|
||||||
|
shutdown_nas()
|
||||||
|
time.sleep(300)
|
||||||
|
|
||||||
# Main Loop
|
# Main Loop
|
||||||
def main():
|
def main():
|
||||||
@ -176,7 +195,7 @@ def main():
|
|||||||
if battery < 25:
|
if battery < 25:
|
||||||
batt_crit(battery)
|
batt_crit(battery)
|
||||||
if ups_id == "Server":
|
if ups_id == "Server":
|
||||||
for node in pve_nodes:
|
for node in servers:
|
||||||
shutdown_pve(node)
|
shutdown_pve(node)
|
||||||
time.sleep(300)
|
time.sleep(300)
|
||||||
else:
|
else:
|
||||||
|
7
requirements.txt
Normal file
7
requirements.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
nut2
|
||||||
|
ping3
|
||||||
|
config
|
||||||
|
discord
|
||||||
|
paramiko
|
||||||
|
proxmoxer
|
||||||
|
wakeonlan
|
Loading…
Reference in New Issue
Block a user