Skip to content

Commit

Permalink
revert main
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjones-plip committed Aug 31, 2024
1 parent 946c1c9 commit 38b78bf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
6 changes: 6 additions & 0 deletions conf.example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
ssh_password = 'timekpr-next-remote'
ssh_timekpra_bin = '/usr/bin/timekpra'
ssh_key = './id_timekpr'

gotify = {
'enabled' : False,
'token' : 'token-here',
'url' : 'http://url-here.com'
}
40 changes: 32 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import conf, re
import conf, re, humanize
from fabric import Connection
from paramiko.ssh_exception import AuthenticationException
from paramiko.ssh_exception import NoValidConnectionsError
from gotify import Gotify


def get_config():
return conf.trackme


def send_alert(user, action, seconds, computer, ssh):
if conf.gotify['enabled']:
gotify = Gotify(
base_url = conf.gotify['url'],
app_token= conf.gotify['token'],
)
try:
usage = get_usage(user, computer, ssh)
added = humanize.naturaldelta(seconds)
remain = humanize.precisedelta(usage['time_left'])
result = gotify.create_message(
f"{user} {action} {added}, and has {remain} remaining :)",
title=f"{user} {action} time",
priority=2,
)
except Exception as e:
print(f"Failed to call Gotify. Config is: {conf.gotify}. Error is: {e}")
return False
print(f"Gotify alert sent to {conf.gotify['url']}")


def get_usage(user, computer, ssh):
# to do - maybe check if user is in timekpr first? (/usr/bin/timekpra --userlist)
global timekpra_userinfo_output
Expand Down Expand Up @@ -68,22 +90,24 @@ def get_connection(computer):
finally:
return connection

def adjust_time(up_down_string, seconds, ssh, user):
def adjust_time(up_down_string, seconds, ssh, user, computer):
command = conf.ssh_timekpra_bin + ' --settimeleft ' + user + ' ' + up_down_string + ' ' + str(seconds)
ssh.run(command)
if up_down_string == '-':
print(f"added {str(seconds)} for user {user}")
action = "added"
else:
print(f"removed {str(seconds)} for user {user}")
action = "removed"
print(f"{action} {seconds} for user '{user}'")
send_alert(user, action, seconds, computer, ssh)
# todo - return false if this fails
return True


def increase_time(seconds, ssh, user):
return adjust_time('+', seconds, ssh, user)
def increase_time(seconds, ssh, user, computer):
return adjust_time('+', seconds, ssh, user, computer)


def decrease_time(seconds, ssh, user):
return adjust_time('-', seconds, ssh, user)
def decrease_time(seconds, ssh, user, computer):
return adjust_time('-', seconds, ssh, user, computer)


4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fabric
paramiko
flask
flask
gotify
humanize
4 changes: 2 additions & 2 deletions timekpr-next-web.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def increase_time(computer, user, seconds):
if validate_request(computer, user)['result'] == "fail":
return validate_request(computer, user), 500
ssh = main.get_connection(computer)
if main.increase_time(seconds, ssh, user):
if main.increase_time(seconds, ssh, user, computer):
usage = main.get_usage(user, computer, ssh)
return {'result': "success", "time_left": usage['time_left'], "time_spent": usage['time_spent']}, 200
else:
Expand All @@ -52,7 +52,7 @@ def decrease_time(computer, user, seconds):
if validate_request(computer, user)['result'] == "fail":
return validate_request(computer, user), 500
ssh = main.get_connection(computer)
if main.decrease_time(seconds, ssh, user):
if main.decrease_time(seconds, ssh, user, computer):
usage = main.get_usage(user, computer, ssh)
return {'result': "success", "time_left": usage['time_left'], "time_spent": usage['time_spent']}, 200
else:
Expand Down

0 comments on commit 38b78bf

Please sign in to comment.