Fixes from api 2

This commit is contained in:
2019-11-21 17:44:37 +01:00
parent d49aa17a9a
commit fb54505a40
6 changed files with 29 additions and 27 deletions

29
api.py
View File

@@ -1,5 +1,5 @@
#!/usr/bin/python3
BUILD = 24
BUILD = 26
API_VER = 2
from flask import Flask, request
import random
@@ -59,10 +59,8 @@ def getComputerExists():
if sql.select("SELECT COUNT(*) 'RESULT' FROM COMPUTERS WHERE Name='"+ComputerName+"' AND UUID IS NULL")[0]['RESULT'] != 0: #UUID doesn't in database, insert it
sql.insert("UPDATE COMPUTERS SET UUID='"+UUID+"' WHERE Name='"+ComputerName+"'")
return str({'RESULT': '1'})
return
elif sql.select("SELECT COUNT(*) 'RESULT' FROM COMPUTERS WHERE Name='"+ComputerName+"'")[0]['RESULT'] != 0: #Not UUID match, but computer exists
return str({'TEXT': 'Error, computer and UUID doesn\'t match in database', 'RESULT': 'ERROR','EXITCODE':'2'})
return
if sqlAddComputers == True: #Computer doesn't exist, but you have enabled add all computers
sql.insert("INSERT INTO COMPUTERS (`Name`,`UUID`) VALUES('"+ComputerName+"','"+UUID+"')")
return str({'RESULT': '1'})
@@ -77,23 +75,26 @@ def getComputerExists():
# getGroups: Know all groups or groups of a computer
# /get/groups?
# @param ComputerName/ComputerID -> Computer to see groups (Not neccesary)
# @param GroupID -> For know Name of a GroupID
##
@app.route("/get/groups",methods=['POST'])
def getGroups(): #Get local groups of a Computer
logit(request.base_url)
ComputerID=None
ComputerID,GroupID=None,None
for key, value in request.args.to_dict().items():
if key == "ComputerName":
ComputerID = str(sql.select("SELECT ID_C FROM COMPUTERS WHERE Name='"+value+"'")[0]['ID_C'])
if key == "ComputerID":
ComputerID = value
if key == "GroupID":
GroupID = value
if GroupID is not None:
return str(sql.select("SELECT * FROM GROUPS WHERE ID_G='"+str(GroupID)+"' LIMIT 1")[0]) #NOT RETURNS A TABLE
if ComputerID is None:
data = sql.select("SELECT * FROM GROUPS ORDER BY Name ASC")
return str(data)
return str(sql.select("SELECT * FROM GROUPS ORDER BY Name ASC"))
else:
data = sql.select("SELECT * FROM GROUPS WHERE ID_G IN (SELECT ID_G FROM COMPUTER_GROUP WHERE ID_C = '"+ComputerID+"') ORDER BY Name ASC")
return str(data)
return str(sql.select("SELECT * FROM GROUPS WHERE ID_G IN (SELECT ID_G FROM COMPUTER_GROUP WHERE ID_C = '"+ComputerID+"') ORDER BY Name ASC"))
##
# getComputers: List all computers with data
@@ -223,7 +224,7 @@ def addCookGrp(): #Assign Cook to group
return str({'TEXT': 'This union GROUP-CookName exists','RESULT':'0'})
else:
result = sql.insert("INSERT INTO COOKS_IDG (`ID_G`,`CookName`) VALUES ('"+GroupID+"','"+CookName+"')")
return str({'TEXT': 'OK'})
return str(result) #str({'TEXT': 'OK'}) ?
##
# delCookGrp: Delete cook from a group
@@ -335,7 +336,7 @@ def getStatusCook(): # Get Status of a Cook (If Brief=1 is sent too, brief statu
CookName = value
if CookName is not None:
return str(sql.select("SELECT Name, Revision, Error FROM COMPUTERS,COOKS_STATUS WHERE CookName = '"+CookName+"' AND COMPUTERS.ID_C=COOKS_STATUS.ID_C"))
return str(sql.select("SELECT Name, Revision, Error FROM COMPUTERS,COOKS_STATUS WHERE CookName = '"+CookName+"' AND COMPUTERS.ID_C=COOKS_STATUS.ID_C ORDER BY Name ASC"))
else:
return str({'TEXT': 'Error, no Cook selected','RESULT':'ERROR'})
@@ -627,7 +628,6 @@ def updComputer():
ComputerID = str(sql.select("SELECT ID_C FROM COMPUTERS WHERE Name='"+value+"'")[0]['ID_C'])
except:
return str({'TEXT': 'Error: Computer not exists in database','RESULT':'ERROR','EXITCODE':'1'})
break
if key == "ComputerID":
ComputerID = value
if key == "UUID":
@@ -714,7 +714,6 @@ def loadCook():
CookName= value
else:
return str({'TEXT': 'Error: Cook not exists in folder','RESULT':'ERROR'})
break
if key == "ComputerName":
ComputerID = str(sql.select("SELECT ID_C FROM COMPUTERS WHERE Name='"+value+"'")[0]['ID_C'])
if key == "ComputerID":
@@ -722,7 +721,7 @@ def loadCook():
if key == "UUID":
UUID = value
if CookName is None and response.media is None:
if CookName is None:
return str({'TEXT': 'Error, you need a CookName to load it','RESULT':'ERROR'})
else:
Count = str(sql.select("SELECT COUNT(*) 'Count' FROM COMPUTERS WHERE UUID='"+UUID+"' AND ID_C='"+ComputerID+"'")[0]['Count'])
@@ -743,7 +742,6 @@ def setCookStatus():
CookName= value
else:
return str({'TEXT': 'Error: Cook not exists in folder','RESULT':'ERROR'})
break
if key == "ComputerName":
ComputerID = str(sql.select("SELECT ID_C FROM COMPUTERS WHERE Name='"+value+"'")[0]['ID_C'])
if key == "ComputerID":
@@ -767,7 +765,6 @@ def setCookStatus():
return str(sql.insert("UPDATE COOKS_STATUS SET Revision='"+Revision+"',`Error`='"+Error+"',`ErrorDesc`='"+ErrorDesc+"' WHERE ID_C='"+ComputerID+"' AND CookName='"+CookName+"'"))
else:
return str({'TEXT': 'Error in parameters...','RESULT':'ERROR'})
logit(response.media)
##
@@ -865,4 +862,4 @@ print("Build: "+str(BUILD))
print("API Version: "+str(API_VER))
app.run(debug=False,port=3333,ssl_context='adhoc',host='0.0.0.0')
app.run(debug=False,port=3333,ssl_context='adhoc',host='0.0.0.0',threaded=True)

View File

@@ -1,5 +1,9 @@
# Changelog
## 2019/11/21
- Fixing bugs of last change and cleaning some code
- Preparing web gui (It's hide yet)
## 2019/11/20
- New API (2). All requests now works in POST and SSL (Self-Signed)
- Now using Flask and not falcon with waitress-serve (Lesser dependencies and ssl support)

View File

@@ -1,22 +1,25 @@
# File hierarchy
## Root folder
- api.py -> Server file. It manages all requests from clients and connects to database
- loadserver.bat -> A helper to run api.py on Windows
- api.py -> Server file. It manages all requests from clients and connects to database. Runs at port 3333 by default
- loadserver.bat -> A helper to run api.py and admin.py on Windows
- loadserver.sh -> A helper to run api.py and admin.py on Linux
- sql.py -> Helper for api.py sql sentences
- sysopt.py -> Config of api and sql
## Client folder
## Client folder. Programs of client
- client.ps1 -> This file has to run every X time (I suggest every hour) on every client and as NT\SYSTEM or local admin. You can do this with a Task Schedule as SYSTEM in a GPO (Active Directory)
- control.pyw -> This is the gui of the program. With that you manages the client-server program. It has to be in same folder as configpcm.ini file
- configpcm.ini -> Configuration for client and control.
## Admin folder. This is the folder for webapp that runs with server if using loadserver from root
-admin.py -> Web app (By default, runs at port 3434)
## BD folder
- database.db -> Database file in sqlite3
## cooks folder
It has the cooks in yaml format.
Warning: Do not change name of cooks created and assigned directly.
Warning: Do not change name of cooks created and assigned directly. Use control for that
## Doc folder
Documentation...

View File

@@ -27,12 +27,10 @@ For server you can use Linux, Mac or Windows, and in theory any environment that
- Flask
- pyopenssl
- Yaml
- Python3 deps can be installed it using pip install command (pip install pyopenssl flask pyyaml)
- For controlling the program, you have to start control.pyw using Python3. It doesn't need to be started in server, but it has to be in that folder (Or with configpcm.ini file).
It uses easygui of python3 (pip install easygui)
- For controlling the program, you have to start control.pyw using Python3. It doesn't need to be started in server, but it has to be in that folder (Or with configpcm.ini file).
- It uses easygui of python3 (pip install easygui)
## Clients
- Windows:

View File

@@ -1,3 +1,3 @@
REM Starting Server. Will only work if python and dependencies are installed and in PATH
cd /D "%~dp0"
python3 api.py
@start /b python3 api.py

View File

@@ -1,3 +1,3 @@
#!/bin/bash
cd "$(dirname "$(realpath "$0")")";
python3 api.py
python3 api.py &