add main Loop
parent
1ad2fc8c9a
commit
94292ab0f7
|
|
@ -0,0 +1,15 @@
|
||||||
|
import socket
|
||||||
|
|
||||||
|
def get_local_ip():
|
||||||
|
try:
|
||||||
|
# Ein temporärer Socket erstellen, um die lokale IP-Adresse zu ermitteln
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
# Verbindung zu einem öffentlichen DNS-Server herstellen
|
||||||
|
s.connect(("8.8.8.8", 80))
|
||||||
|
# Die lokale IP-Adresse aus dem Socket abrufen
|
||||||
|
local_ip = s.getsockname()[0]
|
||||||
|
s.close()
|
||||||
|
return local_ip
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Fehler beim Ermitteln der lokalen IP-Adresse: {e}")
|
||||||
|
return None
|
||||||
57
main.py
57
main.py
|
|
@ -1,14 +1,18 @@
|
||||||
|
import time
|
||||||
import update_check
|
import update_check
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
import requests
|
import requests
|
||||||
|
import functions.ipaddress
|
||||||
|
|
||||||
api_server = "http://api.stines.de:8001/"
|
api_server = "http://api.stines.de:8001/"
|
||||||
api_key = ""
|
api_key = "^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8"
|
||||||
headers = {"Content-Type":"application/json",
|
headers = {"Content-Type":"application/json",
|
||||||
"access_token":f"{api_key}"}
|
"access_token":f"{api_key}"}
|
||||||
|
|
||||||
|
service_ID = []
|
||||||
|
|
||||||
def restart_service():
|
def restart_service():
|
||||||
check_os = platform.system()
|
check_os = platform.system()
|
||||||
if check_os == ("Linux"):
|
if check_os == ("Linux"):
|
||||||
|
|
@ -17,23 +21,54 @@ def restart_service():
|
||||||
else:
|
else:
|
||||||
subprocess.run('sc', 'start', 'Agents')
|
subprocess.run('sc', 'start', 'Agents')
|
||||||
|
|
||||||
def get_response(endpoint):
|
def get_response(endpoint,data):
|
||||||
respone = requests.get(f"{api_server}{endpoint}",headers=headers)
|
response = requests.get(f"{api_server}{endpoint}",headers=headers, json=data)
|
||||||
return respone.text
|
print(response.text)
|
||||||
|
return response.text
|
||||||
|
|
||||||
def post_response(endpoint, data):
|
def post_response(endpoint, data):
|
||||||
respone = requests.post(f"{api_server}{endpoint}",headers=headers, json=data)
|
response = requests.post(f"{api_server}{endpoint}",headers=headers, json=data)
|
||||||
return respone.text
|
print(response.text)
|
||||||
|
return response.text
|
||||||
|
|
||||||
|
def check_update():
|
||||||
if update_check.check_version() == True:
|
if update_check.check_version() == True:
|
||||||
restart_service()
|
restart_service()
|
||||||
post_response("update_checked",update_check.current_version)
|
post_response("update_checked",{"ipaddress":f'{functions.ipaddress.get_local_ip()}',"version": update_check.current_version})
|
||||||
|
|
||||||
|
def ping():
|
||||||
|
post_response("ping",{"ipaddress":f'{functions.ipaddress.get_local_ip()}'})
|
||||||
|
|
||||||
## run ping (10sec)
|
def controller_systeminfo():
|
||||||
## run controller(1m)
|
|
||||||
## Check RAM
|
## Check RAM
|
||||||
## Check CPU
|
## Check CPU
|
||||||
## Check HDD
|
## Check HDD
|
||||||
## run service (60m)
|
print("controller.systeminfo()")
|
||||||
|
|
||||||
|
def services_run():
|
||||||
|
service_ID = get_response("get/service",{"ipaddress":f'{functions.ipaddress.get_local_ip()}'})
|
||||||
|
print(service_ID)
|
||||||
|
|
||||||
|
|
||||||
|
def main_loop():
|
||||||
|
tasks = [
|
||||||
|
{"interval": 30, "last_run": time.time(), "functions": [ping,services_run]},
|
||||||
|
{"interval": 60, "last_run": time.time(), "functions": [controller_systeminfo]},
|
||||||
|
{"interval": 3600, "last_run": time.time(), "functions": [services_run]}
|
||||||
|
]
|
||||||
|
|
||||||
|
while True:
|
||||||
|
current_time = time.time()
|
||||||
|
|
||||||
|
for task in tasks:
|
||||||
|
if current_time - task["last_run"] >= task["interval"]:
|
||||||
|
for function in task["functions"]:
|
||||||
|
function()
|
||||||
|
task["last_run"] = current_time
|
||||||
|
|
||||||
|
# Eine kurze Pause, um die CPU nicht zu überlasten
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main_loop()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def ping():
|
||||||
|
print("Ping alle 30 Sekunden")
|
||||||
|
|
||||||
|
|
||||||
|
def ping1():
|
||||||
|
print("Ping1 alle 30 Sekunden")
|
||||||
|
|
||||||
|
|
||||||
|
def ping2():
|
||||||
|
print("Ping2 alle 60 Sekunden")
|
||||||
|
|
||||||
|
|
||||||
|
def ping3():
|
||||||
|
print("Ping3 alle 3600 Sekunden")
|
||||||
|
|
||||||
|
|
||||||
|
def main_loop():
|
||||||
|
tasks = [
|
||||||
|
{"interval": 3, "last_run": time.time(), "functions": [ping, ping1]},
|
||||||
|
{"interval": 6, "last_run": time.time(), "functions": [ping2]},
|
||||||
|
{"interval": 36, "last_run": time.time(), "functions": [ping3]}
|
||||||
|
]
|
||||||
|
|
||||||
|
while True:
|
||||||
|
current_time = time.time()
|
||||||
|
|
||||||
|
for task in tasks:
|
||||||
|
if current_time - task["last_run"] >= task["interval"]:
|
||||||
|
for function in task["functions"]:
|
||||||
|
function()
|
||||||
|
task["last_run"] = current_time
|
||||||
|
|
||||||
|
# Eine kurze Pause, um die CPU nicht zu überlasten
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main_loop()
|
||||||
|
|
@ -4,8 +4,9 @@ import requests
|
||||||
import git
|
import git
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
current_version = []
|
def current_version():
|
||||||
new_version = []
|
current_version = open("version", "r").read()
|
||||||
|
return current_version
|
||||||
|
|
||||||
def get_latest_release(owner, repo):
|
def get_latest_release(owner, repo):
|
||||||
url = f"http://gitlab.stines.de/api/v1/repos/sebastian.serfling/Agents/releases/latest"
|
url = f"http://gitlab.stines.de/api/v1/repos/sebastian.serfling/Agents/releases/latest"
|
||||||
|
|
@ -14,11 +15,9 @@ def get_latest_release(owner, repo):
|
||||||
release_info = response.json()
|
release_info = response.json()
|
||||||
return release_info['name']
|
return release_info['name']
|
||||||
|
|
||||||
|
def check_version():
|
||||||
new_version = get_latest_release("", "")
|
new_version = get_latest_release("", "")
|
||||||
current_version = open("version", "r").read()
|
if current_version() != new_version:
|
||||||
print(current_version)
|
|
||||||
|
|
||||||
if current_version != new_version:
|
|
||||||
try:
|
try:
|
||||||
git.Repo.clone_from("http://172.17.1.251/sebastian.serfling/Agents.git",f'../{get_latest_release("","")}')
|
git.Repo.clone_from("http://172.17.1.251/sebastian.serfling/Agents.git",f'../{get_latest_release("","")}')
|
||||||
except:
|
except:
|
||||||
|
|
@ -29,8 +28,9 @@ if current_version != new_version:
|
||||||
except:
|
except:
|
||||||
print("folder not Found")
|
print("folder not Found")
|
||||||
shutil.move(f"../{new_version}", "../latest")
|
shutil.move(f"../{new_version}", "../latest")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue