Skip to content

Commit

Permalink
updated config is immediately executed and reported back without delay,
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
nekromoff committed Aug 12, 2024
1 parent 79a8e8a commit 2c77035
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions report.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,49 @@
hostname=output.stdout.strip()
content["hostname"]=hostname

headers = {"User-Agent": "RPi Monitor Dashboard/"+__version__, "X-Hostname": hostname, "Content-Type": "application/json"}

# check for config update and proceed with update
response=requests.head(config["receiver"], headers=headers)
if "X-Update" in response.headers and response.headers["X-Update"]=="1":
# backup current config in case the new one is messed up
subprocess.run('cp config.toml config.toml.bak', shell=True)
response=requests.get(config["receiver"]+'?update=1', headers=headers)
current_config=open("config.toml", "rt").read()
current_config_lines=current_config.split("\n")
new_config=''
for line_no, line in enumerate(current_config_lines):
new_config=new_config+line+"\n"
# search for our special line
pos=line.strip().find("#-#*+*#-#")
# if found, assemble new config from existing receiver and config update received
if pos!=-1:
new_config=new_config+response.text;
new_config=new_config.strip()
f = open("config.toml", "w")
f.write(new_config)
f.close()
# validate new config
try:
with open("config.toml", "rb") as f:
test_config = tomllib.load(f)
# once validated, confirm it back to receiver
# content["_config"]=open("config.toml", "rt").read()
#json_data = json.dumps(content)
#response = requests.post(config["receiver"], data=json_data, headers=headers)
except tomllib.TOMLDecodeError:
# new config parse fail, invalid TOML config, fallback to backup config
subprocess.run('cp config.toml.bak config.toml', shell=True)
# backup cleanup
subprocess.run('rm config.toml.bak', shell=True)
break;

with open("config.toml", "rb") as f:
config = tomllib.load(f)

# include config contents in the payload
content["_config"]=open("config.toml", "rt").read()

for name, command in config["commands"].items():
try:
# simple command (TOML string)
Expand Down Expand Up @@ -60,7 +103,11 @@
# ignore, skip
pass

headers = {"User-Agent": "RPi Monitor Dashboard/"+__version__, "X-Hostname": hostname}
# post payload
json_data = json.dumps(content)
response = requests.post(config["receiver"], data=json_data, headers=headers)

headers.pop("Content-Type")

# if screenshot command exists, upload image
try:
Expand All @@ -71,46 +118,3 @@
# ignore, skip
pass

# include config contents in the payload
content["_config"]=open("config.toml", "rt").read()

# post payload
headers["Content-Type"]="application/json"
json_data = json.dumps(content)
response = requests.post(config["receiver"], data=json_data, headers=headers)

# check for config update and proceed with update
response=requests.head(config["receiver"], headers=headers)
if "X-Update" in response.headers and response.headers["X-Update"]=="1":
# backup current config in case the new one is messed up
subprocess.run('cp config.toml config.toml.bak', shell=True)
response=requests.get(config["receiver"]+'?update=1', headers=headers)
current_config=open("config.toml", "rt").read()
current_config_lines=current_config.split("\n")
new_config=''
for line_no, line in enumerate(current_config_lines):
new_config=new_config+line+"\n"
# search for our special line
pos=line.strip().find("#-#*+*#-#")
# if found, assemble new config from existing receiver and config update received
if pos!=-1:
new_config=new_config+response.text;
new_config=new_config.strip()
f = open("config.toml", "w")
f.write(new_config)
f.close()
# validate new config
try:
with open("config.toml", "rb") as f:
test_config = tomllib.load(f)
# once validated, confirm it back to receiver
content["_config"]=open("config.toml", "rt").read()
headers["Content-Type"]="application/json"
json_data = json.dumps(content)
response = requests.post(config["receiver"], data=json_data, headers=headers)
except tomllib.TOMLDecodeError:
# new config parse fail, invalid TOML config, fallback to backup config
subprocess.run('cp config.toml.bak config.toml', shell=True)
# backup cleanup
subprocess.run('rm config.toml.bak', shell=True)
break;

0 comments on commit 2c77035

Please sign in to comment.