Files
SnarfCode/scripts/deploy_nut_config.py

73 lines
2.5 KiB
Python

import paramiko
def main():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('14.4.4.44', username='root', key_filename=r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet', timeout=10)
# 1. Deploy /usr/local/bin/nut-notify.sh
with open(r'scripts\nut_notify.sh', 'r', encoding='utf-8') as f:
nut_notify_content = f.read()
sftp = ssh.open_sftp()
with sftp.file('/usr/local/bin/nut-notify.sh', 'w') as f:
f.write(nut_notify_content)
ssh.exec_command('chmod 755 /usr/local/bin/nut-notify.sh')
print("Deployed /usr/local/bin/nut-notify.sh")
# 2. Deploy /usr/local/bin/homelab_power_shutdown.sh
with open(r'scripts\homelab_power_shutdown.sh', 'r', encoding='utf-8') as f:
power_shutdown_content = f.read()
with sftp.file('/usr/local/bin/homelab_power_shutdown.sh', 'w') as f:
f.write(power_shutdown_content)
ssh.exec_command('chmod 755 /usr/local/bin/homelab_power_shutdown.sh')
print("Deployed /usr/local/bin/homelab_power_shutdown.sh")
sftp.close()
# 3. Update /etc/nut/upsmon.conf
upsmon_conf = """
MONITOR server-closet@localhost 1 admin e3e34r4rT%T%^Y^Y primary
MINSUPPLIES 1
SHUTDOWNCMD "/usr/local/bin/homelab_power_shutdown.sh"
NOTIFYCMD /usr/local/bin/nut-notify.sh
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 15
DEADTIME 15
POWERDOWNFLAG /etc/killpower
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 180
NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT SYSLOG+WALL+EXEC
NOTIFYFLAG FSD SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD SYSLOG+WALL+EXEC
NOTIFYFLAG SHUTDOWN SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT SYSLOG+WALL
NOTIFYFLAG NOCOMM SYSLOG+WALL
"""
# Backup existing upsmon.conf
ssh.exec_command('cp /etc/nut/upsmon.conf /etc/nut/upsmon.conf.bak')
stdin, stdout, stderr = ssh.exec_command('cat > /etc/nut/upsmon.conf')
stdin.write(upsmon_conf.strip() + "\n")
stdin.channel.shutdown_write()
print("Updated /etc/nut/upsmon.conf")
# Restart nut-client / nut-monitor
stdin, stdout, stderr = ssh.exec_command('systemctl restart nut-client || systemctl restart nut-monitor')
print("Restart service:", stdout.read().decode(), stderr.read().decode())
# Check status
stdin, stdout, stderr = ssh.exec_command('systemctl is-active nut-client || systemctl is-active nut-monitor')
print("Service is-active:", stdout.read().decode().strip())
ssh.close()
if __name__ == '__main__':
main()