mirror of
https://gitlab.com/JKANetwork/powerfulcomputermanager.git
synced 2026-02-19 19:51:31 +01:00
826 lines
33 KiB
Python
826 lines
33 KiB
Python
#!/usr/bin/python3
|
|
BUILD = 20
|
|
API_VER = 1
|
|
##It can be run directly with "waitress-serve --port=3333 api:api"
|
|
import falcon
|
|
import random
|
|
import os
|
|
import time
|
|
import sql # SQL work file
|
|
import json,yaml
|
|
from datetime import datetime
|
|
|
|
import sysopt #File of options
|
|
|
|
def logit(text):
|
|
text = str(text)
|
|
now = datetime.now()
|
|
print ("Log: " + text)
|
|
with open('api.log', 'a') as file:
|
|
file.write("Log ("+now.strftime("%x %X") + "): ")
|
|
file.write(text)
|
|
file.write('\n')
|
|
|
|
def retPassword():
|
|
try:
|
|
return sql.select("SELECT Value FROM OPTIONS WHERE Option='Password'")[0]['Value']
|
|
except:
|
|
return ""
|
|
|
|
##
|
|
# getComputerExists: Know if computer exists and matches UUID
|
|
# /get/computerexists?
|
|
# @param ComputerName -> Computer to deploy it
|
|
# @param UUID: Check UUID validation of this computer
|
|
# Error exit code:
|
|
# 1: No params supplied
|
|
# 2: Computer and UUID not match database
|
|
# 3: Computer doesn't exists in database
|
|
##
|
|
class getComputerExists(object):
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
ComputerName,UUID = None, None
|
|
for key, value in request.params.items():
|
|
if key == "ComputerName":
|
|
ComputerName = value
|
|
if key == "UUID":
|
|
UUID = value
|
|
|
|
if ComputerName is None:
|
|
response.media = {'TEXT':'No params','RESULT': 'ERROR','EXITCODE':'1'}
|
|
else:
|
|
result = sql.select("SELECT COUNT(*) 'RESULT' FROM COMPUTERS WHERE Name='"+ComputerName+"' AND UUID='"+UUID+"'")
|
|
|
|
if result[0]['RESULT'] == 0: # 0 or 1
|
|
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+"'")
|
|
response.media = {'RESULT': '1'}
|
|
return
|
|
elif sql.select("SELECT COUNT(*) 'RESULT' FROM COMPUTERS WHERE Name='"+ComputerName+"'")[0]['RESULT'] != 0: #Not UUID match, but computer exists
|
|
response.media = {'TEXT': 'Error, computer and UUID doesn\'t match in database', 'RESULT': 'ERROR','EXITCODE':'2'}
|
|
return
|
|
if sysopt.addComputers == True: #Computer doesn't exist, but you have enabled add all computers
|
|
sql.insert("INSERT INTO COMPUTERS (`Name`,`UUID`) VALUES('"+ComputerName+"','"+UUID+"')")
|
|
response.media = {'RESULT': '1'}
|
|
else: #Computer doesn't exist and you don't want to be added
|
|
response.media = {'TEXT':'Error: Computer not exists in database', 'RESULT': 'ERROR','EXITCODE':'3'}
|
|
|
|
else: #Exists
|
|
response.media = {'RESULT': '1'}
|
|
|
|
|
|
##
|
|
# getGroups: Know all groups or groups of a computer
|
|
# /get/groups?
|
|
# @param ComputerName/ComputerID -> Computer to see groups (Not neccesary)
|
|
##
|
|
class getGroups(object): #Get local groups of a Computer
|
|
def on_get(self,request, response):
|
|
logit(request)
|
|
ComputerID=None
|
|
for key, value in request.params.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 ComputerID is None:
|
|
data = sql.select("SELECT * FROM GROUPS ORDER BY Name ASC")
|
|
response.media = data
|
|
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")
|
|
response.media = data
|
|
|
|
class getComputers(object):
|
|
def on_get(self,request, response):
|
|
logit(request)
|
|
ComputerID=None
|
|
for key, value in request.params.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 ComputerID is None:
|
|
data = sql.select("SELECT * FROM COMPUTERS ORDER BY Name ASC")
|
|
response.media = data
|
|
else:
|
|
data = sql.select("SELECT * FROM COMPUTERS WHERE ID_C = '"+ComputerID+"') ORDER BY Name ASC")
|
|
response.media = data
|
|
|
|
|
|
##
|
|
# getComputersGrp: Know all computers that have this group
|
|
# /get/computersgrp?
|
|
# @param GroupName/GroupID -> Group to see computers
|
|
##
|
|
class getComputersGrp(object): #List of computers in a group
|
|
def on_get(self,request, response):
|
|
logit(request)
|
|
GroupID=None
|
|
for key, value in request.params.items():
|
|
if key == "GroupName":
|
|
GroupID = str(sql.select("SELECT ID_G FROM GROUPS WHERE Name='"+value+"'")[0]['ID_G'])
|
|
if key == "GroupID":
|
|
GroupID = value
|
|
data = sql.select("SELECT * FROM COMPUTERS WHERE ID_C IN (SELECT ID_C FROM COMPUTER_GROUP WHERE ID_G='"+GroupID+"') ORDER BY Name")
|
|
response.media = data
|
|
|
|
##
|
|
# getCookAll: Get the list of cooks added at least to one group
|
|
# /get/cookall?
|
|
# NoParams
|
|
##
|
|
class getCookAll(object):
|
|
def on_get(self,request, response):
|
|
logit(request)
|
|
data = sql.select("SELECT DISTINCT CookName FROM COOKS_IDG ORDER BY CookName ASC") #All cooks
|
|
response.media = data
|
|
|
|
|
|
##
|
|
# getCookPend: Get the list of cooks that are pending to deploy to a computer
|
|
# /get/cookpend?
|
|
# @param ComputerName/ComputerID -> Computer to deploy it
|
|
# @param GroupName/GroupID: Groups
|
|
# @param SeeAll: Send all cooks from this computer
|
|
# @param UUID: Check UUID validation of this computer (Not implemented yet)
|
|
##
|
|
class getCookPend(object): # Get the list of cooks for a computer to implement (Var all=1 for see all of, implemented or not)
|
|
def on_get(self,request, response):
|
|
logit(request)
|
|
ComputerID,GroupID, SeeAll =None, None, None
|
|
for key, value in request.params.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 == "GroupName":
|
|
GroupID = str(sql.select("SELECT ID_G FROM GROUPS WHERE Name='"+value+"'")[0]['ID_G'])
|
|
if key == "GroupID":
|
|
GroupID = value
|
|
if key == "SeeAll":
|
|
SeeAll = str(value)
|
|
|
|
if ComputerID is None and GroupID is None: # Error of null parameters
|
|
response.media = {'TEXT': 'I need a Group or Computer to search','RESULT':'ERROR'}
|
|
elif ComputerID is not None and SeeAll is None:
|
|
data = sql.select("SELECT DISTINCT CookName FROM COOKS_IDG WHERE ID_G IN (SELECT ID_G FROM COMPUTER_GROUP WHERE ID_C = '"+ComputerID+"')") #All cooks for this computer
|
|
fordelete = []
|
|
for i in range(len(data)):
|
|
for key, value in data[i].items(): #Iterate in a dict (json)
|
|
if key == 'CookName':
|
|
CookName = value #Name
|
|
with open('cooks/'+CookName+'.yaml', 'r') as myfile:
|
|
filecook = myfile.read()
|
|
coun = sql.select("SELECT COUNT(*) 'RESULT' FROM COOKS_STATUS WHERE ID_C='"+ComputerID+"' AND CookName='"+CookName+"' AND Revision='"+str(yaml.safe_load(filecook)['revision'])+"' AND `Error`='0'")
|
|
if coun[0]['RESULT'] == 1:
|
|
fordelete.append(i) #Its good, do not say to client
|
|
for x in reversed(fordelete): #Deleting cooks that are implemented in client. Reverse order (Because is an array and index..)
|
|
data.pop(x)
|
|
response.media = data
|
|
else: # SeeAll. Send all cooks
|
|
response.media = sql.select("SELECT DISTINCT CookName FROM COOKS_IDG WHERE ID_G IN (SELECT ID_G FROM COMPUTER_GROUP WHERE ID_C = '"+ComputerID+"') ORDER BY CookName ASC") #All cooks for this computer
|
|
|
|
##
|
|
# addCookGrp: Assign Cook to group
|
|
# /add/cookgrp?
|
|
# @param GroupName/GroupID -> Group to show cooks
|
|
# @param CookName -> Cook to assign
|
|
# @param Password -> Password to validate
|
|
##
|
|
class addCookGrp(object): #Assign Cook to group
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
GroupID, CookName, Password = None, None, None # Initialize
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
exists = os.path.isfile('cooks/'+value+'.yaml')
|
|
CookName = value
|
|
if key == "GroupName":
|
|
GroupID = str(sql.select("SELECT ID_G FROM GROUPS WHERE Name='"+value+"'")[0]['ID_G'])
|
|
if key == "GroupID":
|
|
GroupID = value
|
|
if key == "Password" and value == retPassword():
|
|
Password=value
|
|
if GroupID is None or exists is False:
|
|
response.media = {'TEXT': 'GroupID is not defined or Cook not exists','RESULT':'ERROR'}
|
|
elif Password is None: # Validate password
|
|
response.media = {'TEXT': 'Invalid password','RESULT':'ERROR'}
|
|
elif int(sql.select("SELECT COUNT(*) 'COUNT' FROM COOKS_IDG WHERE `ID_G`='"+GroupID+"' AND `CookName`='"+CookName+"'")[0]['COUNT']) > 0:
|
|
response.media = {'TEXT': 'This union GROUP-CookName exists','RESULT':'0'}
|
|
else:
|
|
result = sql.insert("INSERT INTO COOKS_IDG (`ID_G`,`CookName`) VALUES ('"+GroupID+"','"+CookName+"')")
|
|
response.media = {'TEXT': 'OK'}
|
|
|
|
##
|
|
# delCookGrp: Delete cook from a group
|
|
# /del/cookgrp?
|
|
# @param GroupName/GroupID -> Group to show cooks
|
|
# @param CookName -> Cook to assign
|
|
##
|
|
class delCookGrp(object): #Delete cook from a group
|
|
def on_get(self,request,response):
|
|
logit(request)
|
|
GroupID, CookName = None, None # Initialize
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
exists = os.path.isfile('cooks/'+value+'.yaml')
|
|
if exists:
|
|
CookName = value
|
|
if key == "GroupName":
|
|
GroupID = str(sql.select("SELECT ID_G FROM GROUPS WHERE Name='"+value+"'")[0]['ID_G'])
|
|
if key == "GroupID":
|
|
GroupID = value
|
|
|
|
if GroupID is not None and CookName is not None:
|
|
result = sql.insert("DELETE FROM COOKS_IDG WHERE ID_G='"+GroupID+"' AND CookName='"+CookName+"'")
|
|
response.media = result
|
|
else:
|
|
response.media = {'TEXT': 'Error, no Group, or CookName does\'t exists','RESULT':'ERROR'}
|
|
|
|
##
|
|
# delEmptyPcsGroup: Delete all computers from from a group
|
|
# /del/emptypcsgroup?
|
|
# @param GroupName/GroupID -> Group to delete computers
|
|
##
|
|
class delEmptyPcsGroup(object): #Delete all computers from a group
|
|
def on_get(self,request,response):
|
|
logit(request)
|
|
GroupID = None # Initialize
|
|
for key, value in request.params.items():
|
|
if key == "GroupName":
|
|
GroupID = str(sql.select("SELECT ID_G FROM GROUPS WHERE Name='"+value+"'")[0]['ID_G'])
|
|
if key == "GroupID":
|
|
GroupID = value
|
|
|
|
if GroupID is not None:
|
|
result = sql.insert("DELETE FROM COMPUTER_GROUP WHERE ID_G='"+GroupID+"'")
|
|
response.media = result
|
|
else:
|
|
response.media = {'TEXT': 'Error, this group doesn\'t exists','RESULT':'ERROR'}
|
|
|
|
|
|
##
|
|
# getCookGrp: Get cooks from a Group
|
|
# /get/cookgrp?
|
|
# @param GroupName/GroupID -> Group to show cooks
|
|
##
|
|
class getCookGrp(object):
|
|
def on_get(self,request,response):
|
|
logit(request)
|
|
GroupID = None # Initialize
|
|
for key, value in request.params.items():
|
|
if key == "GroupName":
|
|
GroupID = str(sql.select("SELECT ID_G FROM GROUPS WHERE Name='"+value+"'")[0]['ID_G'])
|
|
if key == "GroupID":
|
|
GroupID = value
|
|
|
|
if GroupID is not None:
|
|
result = sql.select("SELECT * FROM COOKS_IDG WHERE ID_G='"+GroupID+"' ORDER BY CookName ASC")
|
|
response.media = result
|
|
else:
|
|
response.media = {'TEXT': 'Error, no Group selected','RESULT':'ERROR'}
|
|
|
|
##
|
|
# getGrpCook: Get groups from a Cook
|
|
# /get/grpcook?
|
|
# @param CookName -> Cook to show groups
|
|
##
|
|
class getGrpCook(object): # Get Groups of a Cook
|
|
def on_get(self,request,response):
|
|
logit(request)
|
|
CookName = None # Initialize
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
exists = os.path.isfile('cooks/'+value+'.yaml')
|
|
if exists:
|
|
CookName = value
|
|
|
|
if CookName is not None:
|
|
response.media = sql.select("SELECT Name FROM GROUPS WHERE ID_G IN (SELECT ID_G FROM COOKS_IDG WHERE CookName='"+CookName+"') ORDER BY Name ASC")
|
|
else:
|
|
response.media = {'TEXT': 'Error, no Cook selected','RESULT':'ERROR'}
|
|
|
|
##
|
|
# getStatusCook: Get groups from a Cook
|
|
# /get/statuscook?
|
|
# @param CookName -> Cook to show detailed status
|
|
##
|
|
class getStatusCook(object): # Get Status of a Cook (If Brief=1 is sent too, brief status (Completed:X, updated:X..))
|
|
def on_get(self,request,response):
|
|
logit(request)
|
|
CookName = None # Initialize
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
exists = os.path.isfile('cooks/'+value+'.yaml')
|
|
if exists:
|
|
CookName = value
|
|
|
|
if CookName is not None:
|
|
response.media = sql.select("SELECT Name, Revision, Error FROM COMPUTERS,COOKS_STATUS WHERE CookName = '"+CookName+"' AND COMPUTERS.ID_C=COOKS_STATUS.ID_C")
|
|
else:
|
|
response.media = {'TEXT': 'Error, no Cook selected','RESULT':'ERROR'}
|
|
|
|
##
|
|
# getLastRevisionCook: Get last revision from a Cook
|
|
# /get/lastrevisioncook?
|
|
# @param CookName -> Cook to show detailed status
|
|
##
|
|
class getLastRevisionCook(object): # Get Number Revision (Revision=X)
|
|
def on_get(self,request,response):
|
|
logit(request)
|
|
CookName = None # Initialize
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
exists = os.path.isfile('cooks/'+value+'.yaml')
|
|
if exists:
|
|
CookName = value
|
|
|
|
if CookName is not None:
|
|
with open('cooks/'+CookName+'.yaml', 'r') as myfile:
|
|
filecook = myfile.read()
|
|
response.media = {'Revision': str(yaml.safe_load(filecook)['revision'])}
|
|
else:
|
|
response.media = {'TEXT': 'Error, no Cook selected','RESULT':'ERROR'}
|
|
|
|
##
|
|
# addComputer: Add a computer in database
|
|
# /add/computer?
|
|
# @param ComputerName -> ComputerName to add
|
|
##
|
|
class addComputer(object):
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
ComputerName = None
|
|
for key,value in request.params.items():
|
|
if key == "ComputerName":
|
|
ComputerName = value
|
|
|
|
if ComputerName is None:
|
|
response.media = {'TEXT': 'Error, you need a ComputerName to add','RESULT':'ERROR'}
|
|
else:
|
|
result = sql.insert("INSERT INTO COMPUTERS (Name) VALUES ('"+ComputerName+"')")
|
|
response.media = result
|
|
|
|
##
|
|
# addGroup: Add a new group
|
|
# /add/group?
|
|
# @param GroupName -> Name of group to add
|
|
##
|
|
class addGroup(object):
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
GroupName= None
|
|
for key,value in request.params.items():
|
|
if key == "GroupName":
|
|
GroupName = value
|
|
|
|
|
|
if GroupName is None:
|
|
response.media = {'TEXT': 'Error, you need a GroupName to add','RESULT':'ERROR'}
|
|
else:
|
|
result = sql.insert("INSERT INTO GROUPS (Name) VALUES ('"+GroupName+"')")
|
|
response.media = result
|
|
|
|
##
|
|
# delGroup: Delete a group and all tables that has it.
|
|
# /del/group?
|
|
# @param GroupName/GroupID -> Name of group to delete
|
|
##
|
|
class delGroup(object): #Delete group
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
GroupID= None
|
|
for key,value in request.params.items():
|
|
if key == "GroupID":
|
|
GroupID = value
|
|
if key == "GroupName":
|
|
GroupID = str(sql.select("SELECT ID_G FROM GROUPS WHERE Name='"+value+"'")[0]['ID_G'])
|
|
|
|
if GroupID is None:
|
|
response.media = {'TEXT': 'Error, you need a GroupName to delete','RESULT':'ERROR'}
|
|
else:
|
|
sql.insert("DELETE FROM COMPUTER_GROUP WHERE ID_G='"+GroupID+"'")
|
|
sql.insert("DELETE FROM COOKS_IDG WHERE ID_G='"+GroupID+"'")
|
|
result = sql.insert("DELETE FROM GROUPS WHERE ID_G='"+GroupID+"'")
|
|
response.media = result
|
|
|
|
|
|
##
|
|
# delCook: Delete a cook and all tables that has it. It doesn't delete files
|
|
# /del/cook?
|
|
# @param CookName -> Name of cook to disappear
|
|
##
|
|
class delCook(object):
|
|
def on_get(self,request,response):
|
|
logit(request)
|
|
CookName = None, None # Initialize
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
exists = os.path.isfile('cooks/'+value+'.yaml')
|
|
if exists:
|
|
CookName = value
|
|
|
|
if CookName is not None:
|
|
sql.insert("DELETE FROM COOKS_ONETIME WHERE CookName='"+CookName+"'")
|
|
sql.insert("DELETE FROM COOKS_STATUS WHERE CookName='"+CookName+"'")
|
|
result = sql.insert("DELETE FROM COOKS_IDG WHERE CookName='"+CookName+"'")
|
|
response.media = result
|
|
else:
|
|
response.media = {'TEXT': 'Error, CookName don\'t exists','RESULT':'ERROR'}
|
|
|
|
##
|
|
# delGroupsCook: Delete groups of a cook
|
|
# /del/groupscook?
|
|
# @param CookName -> Name of cook to make his groups disappear
|
|
##
|
|
class delGroupsCook(object):
|
|
def on_get(self,request,response):
|
|
logit(request)
|
|
CookName = None, None # Initialize
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
exists = os.path.isfile('cooks/'+value+'.yaml')
|
|
if exists:
|
|
CookName = value
|
|
|
|
if CookName is not None:
|
|
result = sql.insert("DELETE FROM COOKS_IDG WHERE CookName='"+CookName+"'")
|
|
response.media = result
|
|
else:
|
|
response.media = {'TEXT': 'Error, CookName don\'t exists','RESULT':'ERROR'}
|
|
|
|
##
|
|
# delCleanCook: Delete groups of a cook
|
|
# /del/cleancook?
|
|
# @param CookName -> Clean status of a cook (Restart it)
|
|
##
|
|
class delCleanCook(object):
|
|
def on_get(self,request,response):
|
|
logit(request)
|
|
CookName = None, None # Initialize
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
exists = os.path.isfile('cooks/'+value+'.yaml')
|
|
if exists:
|
|
CookName = value
|
|
|
|
if CookName is not None:
|
|
result = sql.insert("DELETE FROM COOKS_STATUS WHERE CookName='"+CookName+"'")
|
|
response.media = result
|
|
else:
|
|
response.media = {'TEXT': 'Error, CookName don\'t exists','RESULT':'ERROR'}
|
|
|
|
|
|
class updGroup(object): #Delete group
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
GroupID, GroupNewName= None, None
|
|
for key,value in request.params.items():
|
|
if key == "GroupID":
|
|
GroupID = value
|
|
if key == "GroupName":
|
|
GroupID = str(sql.select("SELECT ID_G FROM GROUPS WHERE Name='"+value+"'")[0]['ID_G'])
|
|
if key == "GroupNewName":
|
|
Count = str(sql.select("SELECT COUNT(*) 'Count' FROM GROUPS WHERE Name='"+value+"'")[0]['Count'])
|
|
if Count == "0":
|
|
GroupNewName = value
|
|
else:
|
|
response.media = {'TEXT': 'Error, New group name exists','RESULT':'ERROR'}
|
|
|
|
if GroupID is None or GroupNewName is None:
|
|
response.media = {'TEXT': 'Error, you need a GroupName and new name to update name','RESULT':'ERROR'}
|
|
else:
|
|
result = sql.insert("UPDATE GROUPS SET Name='"+GroupNewName+"' WHERE ID_G='"+GroupID+"'")
|
|
response.media = result
|
|
|
|
class addGrpComputer(object): #Add computer to a group (Local)
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
ComputerID, GroupID= None, None
|
|
for key, value in request.params.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 == "GroupName":
|
|
GroupID = str(sql.select("SELECT ID_G FROM GROUPS WHERE Name='"+value+"'")[0]['ID_G'])
|
|
if key == "GroupID":
|
|
GroupID = value
|
|
|
|
if ComputerID is None or GroupID is None:
|
|
response.media = {'TEXT': 'Error, you need a Name and Group to add','RESULT':'ERROR'}
|
|
else:
|
|
result = sql.insert("INSERT INTO COMPUTER_GROUP (ID_C,ID_G) VALUES ('"+ComputerID+"','"+GroupID+"')")
|
|
response.media = result
|
|
|
|
class delGrpComputer(object): #Del computer from a group
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
ComputerID, GroupID= None, None
|
|
for key, value in request.params.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 == "GroupName":
|
|
GroupID = str(sql.select("SELECT ID_G FROM GROUPS WHERE Name='"+value+"'")[0]['ID_G'])
|
|
if key == "GroupID":
|
|
GroupID = value
|
|
|
|
if ComputerID is None or GroupID is None:
|
|
response.media = {'TEXT': 'Error, you need a Name and Group to add','RESULT':'ERROR'}
|
|
else:
|
|
result = sql.insert("DELETE FROM COMPUTER_GROUP WHERE ID_C='"+ComputerID+"' AND ID_G='"+GroupID+"'")
|
|
response.media = result
|
|
|
|
|
|
##
|
|
# delComputer: Delete computer and data of it
|
|
# /del/computer?
|
|
# @param ComputerName/ComputerID -> Computer ID or Name
|
|
# @param Password -> Password validated command
|
|
##
|
|
class delComputer(object): #Delete computer
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
ComputerID= None
|
|
for key, value in request.params.items():
|
|
if key == "ComputerName":
|
|
ComputerID = str(sql.select("SELECT ID_C FROM COMPUTERS WHERE Name='"+value+"'")[0]['ID_C'])
|
|
if key == "ComputerID":
|
|
ComputerID = str(sql.select("SELECT ID_C FROM COMPUTERS WHERE ID_C='"+value+"'")[0]['ID_C'])
|
|
if key == "Password" and value == retPassword():
|
|
Password=value
|
|
if Password is None:
|
|
response.media = {'TEXT': 'Invalid password','RESULT':'ERROR'}
|
|
elif ComputerID is None:
|
|
response.media = {'TEXT': 'Error, you need a Name/ComputerID to update data','RESULT':'ERROR'}
|
|
elif ComputerID=='':
|
|
response.media = {'TEXT': 'Error, this Name/ID is not valid','RESULT':'ERROR'}
|
|
else:
|
|
sql.insert("DELETE FROM COMPUTER_GROUP WHERE ID_C='"+ComputerID+"'")
|
|
sql.insert("DELETE FROM COOKS_STATUS WHERE ID_C='"+ComputerID+"'")
|
|
resp = sql.insert("DELETE FROM COMPUTERS WHERE ID_C='"+ComputerID+"'")
|
|
logit(resp)
|
|
|
|
|
|
class updComputer(object):
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
ComputerID, UUID, Password= None, None,None
|
|
for key, value in request.params.items():
|
|
if key == "ComputerName":
|
|
try:
|
|
ComputerID = str(sql.select("SELECT ID_C FROM COMPUTERS WHERE Name='"+value+"'")[0]['ID_C'])
|
|
except:
|
|
response.media = {'TEXT': 'Error: Computer not exists in database','RESULT':'ERROR','EXITCODE':'1'}
|
|
break
|
|
if key == "ComputerID":
|
|
ComputerID = value
|
|
if key == "UUID":
|
|
UUID = value
|
|
if key == "Password" and value == retPassword():
|
|
Password=value
|
|
if Password is None:
|
|
response.media = {'TEXT': 'Invalid password','RESULT':'ERROR'}
|
|
if ComputerID is None and response.media is None:
|
|
response.media = {'TEXT': 'Error, you need a ComputerName/ComputerID to update data','RESULT':'ERROR'}
|
|
elif response.media is None and ComputerID is not None and UUID is not None:
|
|
Count = str(sql.select("SELECT COUNT(*) 'Count' FROM COMPUTERS WHERE UUID='"+UUID+"' AND ID_C='"+ComputerID+"'")[0]['Count'])
|
|
if Count == "0":
|
|
response.media = {'TEXT': 'Error, computer and UUID doesn\'t match in database','RESULT':'ERROR','EXITCODE':'2'}
|
|
else:
|
|
for key, value in request.params.items():
|
|
if key == "SOVersion":
|
|
response.media = sql.insert("UPDATE COMPUTERS SET SOVersion ='"+value+"' WHERE ID_C='"+ComputerID+"'")
|
|
if key == "SOBit":
|
|
response.media = sql.insert("UPDATE COMPUTERS SET SOBit ='"+value+"' WHERE ID_C='"+ComputerID+"'")
|
|
if key == "SOCaption":
|
|
response.media = sql.insert("UPDATE COMPUTERS SET SOCaption ='"+value+"' WHERE ID_C='"+ComputerID+"'")
|
|
if key == "LastConnection":
|
|
response.media = sql.insert("UPDATE COMPUTERS SET LastConnection ='"+value+"' WHERE ID_C='"+ComputerID+"'")
|
|
if key == "LastUser":
|
|
response.media = sql.insert("UPDATE COMPUTERS SET LastUser='"+value+"' WHERE ID_C='"+ComputerID+"'")
|
|
if key == "RAM":
|
|
response.media = sql.insert("UPDATE COMPUTERS SET RAM='"+value+"' WHERE ID_C='"+ComputerID+"'")
|
|
if key == "CPUName":
|
|
response.media = sql.insert("UPDATE COMPUTERS SET CPUName='"+value+"' WHERE ID_C='"+ComputerID+"'")
|
|
|
|
##
|
|
# updCookName: Rename cook name
|
|
# /upd/cookname?
|
|
# @param CookName -> Original Cook Name
|
|
# @param CookNewName -> New Cook Name
|
|
# @param Password -> Password validated command
|
|
##
|
|
class updCookName(object):
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
CookName, CookNewName,Password= None, None,None #Initialize
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
if os.path.isfile('cooks/'+value+'.yaml'):
|
|
CookName= value
|
|
else:
|
|
response.media = {'TEXT': 'Error: Cook not exists in folder','RESULT':'ERROR'}
|
|
if key == "CookNewName":
|
|
if os.path.isfile('cooks/'+value+'.yaml'):
|
|
response.media = {'TEXT': 'Error: There is a cook with the new name in folder!','RESULT':'ERROR'}
|
|
else:
|
|
CookNewName= value
|
|
if key == "Password" and value == retPassword():
|
|
Password=value
|
|
if Password is None:
|
|
response.media = {'TEXT': 'Invalid password','RESULT':'ERROR'}
|
|
if response.media is not None:
|
|
pass
|
|
elif CookName is None or CookNewName is None:
|
|
response.media = {'TEXT': 'Error, you need the old and new Cook Name to update data','RESULT':'ERROR'}
|
|
else: #Do it.
|
|
old_file = os.path.join("cooks", CookName+'.yaml')
|
|
new_file = os.path.join("cooks", CookNewName+'.yaml')
|
|
os.rename(old_file, new_file)
|
|
sql.insert("UPDATE COOKS_IDG SET CookName ='"+CookNewName+"' WHERE CookName='"+CookName+"'")
|
|
response.media = sql.insert("UPDATE COOKS_STATUS SET CookName ='"+CookNewName+"' WHERE CookName='"+CookName+"'")
|
|
|
|
|
|
class loadCook(object):
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
CookName, ComputerID, UUID= None, None, None #Initialize
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
if os.path.isfile('cooks/'+value+'.yaml'):
|
|
CookName= value
|
|
else:
|
|
response.media = {'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":
|
|
ComputerID = value
|
|
if key == "UUID":
|
|
UUID = value
|
|
|
|
if CookName is None and response.media is None:
|
|
response.media = {'TEXT': 'Error, you need a CookName to load it','RESULT':'ERROR'}
|
|
elif response.media is None:
|
|
Count = str(sql.select("SELECT COUNT(*) 'Count' FROM COMPUTERS WHERE UUID='"+UUID+"' AND ID_C='"+ComputerID+"'")[0]['Count'])
|
|
if Count == "0":
|
|
response.media = {'TEXT': 'Error, computer doesn\'t exists in database','RESULT':'ERROR'}
|
|
else:
|
|
with open('cooks/'+CookName+'.yaml', 'r') as myfile:
|
|
data = myfile.read()
|
|
response.media = yaml.safe_load(data)
|
|
|
|
class setCookStatus(object):
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
CookName, ComputerID, Revision, Error,ErrorDesc= None, None, None, None, ""
|
|
for key, value in request.params.items():
|
|
if key == "CookName":
|
|
if os.path.isfile('cooks/'+value+'.yaml'): # You have to know that cook exists.
|
|
CookName= value
|
|
else:
|
|
response.media = {'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":
|
|
ComputerID = value
|
|
if key == "Revision":
|
|
Revision = value
|
|
if key == "Error":
|
|
Error = value
|
|
if key == "ErrorDesc":
|
|
ErrorDesc = value
|
|
|
|
if CookName is None and response.media is None:
|
|
response.media = {'TEXT': 'Error, you need a CookName to load it','RESULT':'ERROR'}
|
|
elif response.media is None and CookName is not None and ComputerID is not None and Revision is not None and Error is not None:
|
|
statt = sql.select("SELECT COUNT(*) 'RESULT' FROM COOKS_STATUS WHERE CookName='"+CookName+"' AND ID_C='"+ComputerID+"'")[0]['RESULT']
|
|
if statt == 0:
|
|
#INSERT, NEW
|
|
response.media = sql.insert("INSERT INTO COOKS_STATUS (CookName,ID_C,Revision,`Error`,`ErrorDesc`) VALUES ('"+CookName+"', '"+ComputerID+"', '"+Revision+"','"+Error+"','"+ErrorDesc+"')")
|
|
else:
|
|
#UPDATE, NOT NEW
|
|
response.media = sql.insert("UPDATE COOKS_STATUS SET Revision='"+Revision+"',`Error`='"+Error+"',`ErrorDesc`='"+ErrorDesc+"' WHERE ID_C='"+ComputerID+"' AND CookName='"+CookName+"'")
|
|
else:
|
|
response.media = {'TEXT': 'Error in parameters...','RESULT':'ERROR'}
|
|
logit(response.media)
|
|
|
|
|
|
##
|
|
# checkPassword: Check password
|
|
# /check/password?
|
|
# @param Password -> SHA-256 of password
|
|
# Error exit code:
|
|
# 0: No error
|
|
# 1: No password supplied
|
|
# 2: Password incorrect
|
|
# 3: Password not set in app yet
|
|
##
|
|
class checkPassword(object): #Check password (ERROR if password not valid or no password and 1 if valid)
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
Password= None
|
|
for key, value in request.params.items():
|
|
if key == "Password":
|
|
Password = value
|
|
|
|
if Password is None:
|
|
response.media = {'TEXT': 'Error, you need to supply a password','RESULT':'ERROR','EXITCODE':'1'}
|
|
else:
|
|
pass
|
|
res = sql.select("SELECT COUNT(*) 'RESULT' FROM OPTIONS WHERE Option='Password' AND Value='"+Password+"'")[0]['RESULT']
|
|
if res == 1:
|
|
response.media = {'RESULT':'1','TEXT':'OK','EXITCODE':'0'}
|
|
else:
|
|
res = sql.select("SELECT COUNT(*) 'RESULT' FROM OPTIONS WHERE Option='Password'")[0]['RESULT']
|
|
if res == 0: #Password doesn't exists in database
|
|
response.media = {'RESULT':'ERROR','TEXT':'No password set. Please set it','EXITCODE':'3'}
|
|
else:
|
|
response.media = {'RESULT':'ERROR','TEXT':'Password incorrect','EXITCODE':'2'}
|
|
|
|
##
|
|
# updPassword: Update password
|
|
# /upd/password?
|
|
# @param OldPassword -> SHA-256 of password (Old)
|
|
# @param NewPassword -> SHA-256 of password (New)
|
|
##
|
|
class updPassword(object): #Update password (ERROR if password not valid or no password and 1 if changed)
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
OldPassword,NewPassword= None, None
|
|
for key, value in request.params.items():
|
|
if key == "NewPassword":
|
|
NewPassword = value
|
|
if key == "OldPassword":
|
|
OldPassword = value
|
|
|
|
res = sql.select("SELECT COUNT(*) 'RESULT' FROM OPTIONS WHERE Option='Password'")[0]['RESULT']
|
|
if res == 0 and NewPassword is not None: #Password doesn't exists in database
|
|
response.media = sql.insert("INSERT INTO OPTIONS VALUES('Password','"+NewPassword+"')")
|
|
elif res == 0 and NewPassword is None: #No password supplied
|
|
response.media = {'RESULT':'ERROR','TEXT':'Error, you need to supply a password'}
|
|
elif res == 1 and OldPassword is not None: # Password exists in database (And you supplied OldPassword)
|
|
res = sql.select("SELECT COUNT(*) 'RESULT' FROM OPTIONS WHERE Option='Password' AND Value='"+OldPassword+"'")[0]['RESULT']
|
|
if res == 0: # Old password incorrect
|
|
response.media = {'RESULT':'ERROR','TEXT':'Old password incorrect'}
|
|
elif res == 1 and NewPassword is not None: # All is supplied
|
|
response.media = sql.insert("UPDATE OPTIONS SET Value='"+NewPassword+"' WHERE Option='Password'")
|
|
else:
|
|
response.media = {'RESULT':'ERROR', 'TEXT':'No password supplied or bad args'}
|
|
else:
|
|
response.media = {'RESULT':'ERROR', 'TEXT':'No password supplied or bad args'}
|
|
|
|
|
|
|
|
api = falcon.API()
|
|
api.add_route('/add/computer', addComputer()) #Add computer
|
|
api.add_route('/add/group', addGroup()) #Add group to the list of local groups
|
|
api.add_route('/add/cookgrp', addCookGrp()) #Assign cook to group
|
|
api.add_route('/add/grpcomputer', addGrpComputer()) #Add computer to a group
|
|
api.add_route('/del/computer', delComputer()) #Delete computer
|
|
api.add_route('/del/cookgrp', delCookGrp()) # Deassign cook from a group
|
|
api.add_route('/del/emptypcsgroup', delEmptyPcsGroup()) # Delete all computers from a group
|
|
api.add_route('/del/group', delGroup()) #Delete group
|
|
api.add_route('/del/grpcomputer', delGrpComputer()) #Delete computer from a group
|
|
api.add_route('/del/cook', delCook()) #Delete cook and all status about it
|
|
api.add_route('/del/groupscook', delGroupsCook()) #Delete all groups of a cook
|
|
api.add_route('/del/cleancook', delCleanCook()) #Delete status of a cook for all computers (Restart cook). No delete groups
|
|
|
|
api.add_route('/get/computerexists', getComputerExists()) #Returns 0 or 1 (name status)
|
|
api.add_route('/get/computers', getComputers()) #Get list of computer
|
|
api.add_route('/get/cookall', getCookAll()) #Get all cooks implemented in one group at least
|
|
api.add_route('/get/cookgrp', getCookGrp()) # See cooks that have a determinated group
|
|
api.add_route('/get/cookpend', getCookPend()) #Get cooks pending to implement to a computer
|
|
api.add_route('/get/groups', getGroups()) #Get groups of a computer (Or list if not args)
|
|
api.add_route('/get/computersgrp', getComputersGrp()) #Get computers in a group
|
|
api.add_route('/get/grpcook', getGrpCook()) # See groups of a determinated cook
|
|
api.add_route('/get/lastrevisioncook', getLastRevisionCook()) # Returns number of last revision of a cook
|
|
api.add_route('/get/statuscook', getStatusCook()) # See status of a cook
|
|
api.add_route('/load/cook', loadCook()) # Load a cook (Transfer in json way)
|
|
api.add_route('/set/cookstatus', setCookStatus()) # Update status of a cook in a computer (OLD)
|
|
api.add_route('/upd/cookstatus', setCookStatus()) # Update status of a cook in a computer
|
|
api.add_route('/upd/computer', updComputer()) #Update data of computer
|
|
api.add_route('/upd/cookname', updCookName()) #Update file name of cook and SQL references to it.
|
|
api.add_route('/upd/group', updGroup()) #Update group name
|
|
|
|
|
|
api.add_route('/check/password', checkPassword()) #Check admin password
|
|
api.add_route('/upd/password', updPassword()) # Update password (Or create it not exists)
|
|
#api.add_route('/get/password', getPassword()) # Get password (Will be '' if password not exists)
|
|
|
|
class getApiVer(object):
|
|
def on_get(self, request, response):
|
|
logit(request)
|
|
response.media = {'API':API_VER}
|
|
api.add_route('/get/apiver', getApiVer()) # Get API version
|
|
|
|
print("Build: "+str(BUILD))
|
|
print("API Version: "+str(API_VER))
|