mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Send schdules via API
This commit is contained in:
parent
addd878942
commit
c02472fb16
@ -450,7 +450,6 @@ class TasksManager:
|
||||
# created task a child of itself.
|
||||
if str(job_data.get("parent")) == str(sch_id):
|
||||
job_data["parent"] = None
|
||||
|
||||
HelpersManagement.update_scheduled_task(sch_id, job_data)
|
||||
|
||||
if not (
|
||||
|
@ -1652,336 +1652,6 @@ class PanelHandler(BaseHandler):
|
||||
|
||||
self.redirect("/panel/config_json")
|
||||
|
||||
if page == "new_schedule":
|
||||
server_id = self.check_server_id()
|
||||
if not server_id:
|
||||
return
|
||||
|
||||
if (
|
||||
not permissions["Schedule"]
|
||||
in self.controller.server_perms.get_user_id_permissions_list(
|
||||
exec_user["user_id"], server_id
|
||||
)
|
||||
and not superuser
|
||||
):
|
||||
self.redirect(
|
||||
"/panel/error?error=Unauthorized access: User not authorized"
|
||||
)
|
||||
return
|
||||
|
||||
difficulty = bleach.clean(self.get_argument("difficulty", None))
|
||||
server_obj = self.controller.servers.get_server_obj(server_id)
|
||||
enabled = bleach.clean(self.get_argument("enabled", "0"))
|
||||
name = bleach.clean(self.get_argument("name", ""))
|
||||
if difficulty == "basic":
|
||||
action = bleach.clean(self.get_argument("action", None))
|
||||
interval = bleach.clean(self.get_argument("interval", None))
|
||||
interval_type = bleach.clean(self.get_argument("interval_type", None))
|
||||
# only check for time if it's number of days
|
||||
if interval_type == "days":
|
||||
sch_time = bleach.clean(self.get_argument("time", None))
|
||||
if int(interval) > 30:
|
||||
self.redirect(
|
||||
"/panel/error?error=Invalid argument."
|
||||
" Days must be 30 or fewer."
|
||||
)
|
||||
return
|
||||
if action == "command":
|
||||
command = self.get_argument("command", None)
|
||||
elif action == "start":
|
||||
command = "start_server"
|
||||
elif action == "stop":
|
||||
command = "stop_server"
|
||||
elif action == "restart":
|
||||
command = "restart_server"
|
||||
elif action == "backup":
|
||||
command = "backup_server"
|
||||
|
||||
elif difficulty == "reaction":
|
||||
interval_type = "reaction"
|
||||
action = bleach.clean(self.get_argument("action", None))
|
||||
delay = bleach.clean(self.get_argument("delay", None))
|
||||
parent = bleach.clean(self.get_argument("parent", None))
|
||||
if action == "command":
|
||||
command = self.get_argument("command", None)
|
||||
elif action == "start":
|
||||
command = "start_server"
|
||||
elif action == "stop":
|
||||
command = "stop_server"
|
||||
elif action == "restart":
|
||||
command = "restart_server"
|
||||
elif action == "backup":
|
||||
command = "backup_server"
|
||||
|
||||
else:
|
||||
interval_type = ""
|
||||
cron_string = bleach.clean(self.get_argument("cron", ""))
|
||||
if not croniter.is_valid(cron_string):
|
||||
self.redirect(
|
||||
"/panel/error?error=INVALID FORMAT: Invalid Cron Format."
|
||||
)
|
||||
return
|
||||
action = bleach.clean(self.get_argument("action", None))
|
||||
if action == "command":
|
||||
command = self.get_argument("command", None)
|
||||
elif action == "start":
|
||||
command = "start_server"
|
||||
elif action == "stop":
|
||||
command = "stop_server"
|
||||
elif action == "restart":
|
||||
command = "restart_server"
|
||||
elif action == "backup":
|
||||
command = "backup_server"
|
||||
if bleach.clean(self.get_argument("enabled", "0")) == "1":
|
||||
enabled = True
|
||||
else:
|
||||
enabled = False
|
||||
if bleach.clean(self.get_argument("one_time", "0")) == "1":
|
||||
one_time = True
|
||||
else:
|
||||
one_time = False
|
||||
|
||||
if interval_type == "days":
|
||||
job_data = {
|
||||
"name": name,
|
||||
"server_id": server_id,
|
||||
"action": action,
|
||||
"interval_type": interval_type,
|
||||
"interval": interval,
|
||||
"command": command,
|
||||
"start_time": sch_time,
|
||||
"enabled": enabled,
|
||||
"one_time": one_time,
|
||||
"cron_string": "",
|
||||
"parent": None,
|
||||
"delay": 0,
|
||||
}
|
||||
elif difficulty == "reaction":
|
||||
job_data = {
|
||||
"name": name,
|
||||
"server_id": server_id,
|
||||
"action": action,
|
||||
"interval_type": interval_type,
|
||||
"interval": "",
|
||||
# We'll base every interval off of a midnight start time.
|
||||
"start_time": "",
|
||||
"command": command,
|
||||
"cron_string": "",
|
||||
"enabled": enabled,
|
||||
"one_time": one_time,
|
||||
"parent": parent,
|
||||
"delay": delay,
|
||||
}
|
||||
elif difficulty == "advanced":
|
||||
job_data = {
|
||||
"name": name,
|
||||
"server_id": server_id,
|
||||
"action": action,
|
||||
"interval_type": "",
|
||||
"interval": "",
|
||||
# We'll base every interval off of a midnight start time.
|
||||
"start_time": "",
|
||||
"command": command,
|
||||
"cron_string": cron_string,
|
||||
"enabled": enabled,
|
||||
"one_time": one_time,
|
||||
"parent": None,
|
||||
"delay": 0,
|
||||
}
|
||||
else:
|
||||
job_data = {
|
||||
"name": name,
|
||||
"server_id": server_id,
|
||||
"action": action,
|
||||
"interval_type": interval_type,
|
||||
"interval": interval,
|
||||
"command": command,
|
||||
"enabled": enabled,
|
||||
# We'll base every interval off of a midnight start time.
|
||||
"start_time": "00:00",
|
||||
"one_time": one_time,
|
||||
"cron_string": "",
|
||||
"parent": None,
|
||||
"delay": 0,
|
||||
}
|
||||
|
||||
self.tasks_manager.schedule_job(job_data)
|
||||
|
||||
self.controller.management.add_to_audit_log(
|
||||
exec_user["user_id"],
|
||||
f"Edited server {server_id}: added scheduled job",
|
||||
server_id,
|
||||
self.get_remote_ip(),
|
||||
)
|
||||
self.tasks_manager.reload_schedule_from_db()
|
||||
self.redirect(f"/panel/server_detail?id={server_id}&subpage=schedules")
|
||||
|
||||
if page == "edit_schedule":
|
||||
server_id = self.check_server_id()
|
||||
if not server_id:
|
||||
return
|
||||
|
||||
if (
|
||||
not permissions["Schedule"]
|
||||
in self.controller.server_perms.get_user_id_permissions_list(
|
||||
exec_user["user_id"], server_id
|
||||
)
|
||||
and not superuser
|
||||
):
|
||||
self.redirect(
|
||||
"/panel/error?error=Unauthorized access: User not authorized"
|
||||
)
|
||||
return
|
||||
|
||||
sch_id = self.get_argument("sch_id", None)
|
||||
if sch_id is None:
|
||||
self.redirect("/panel/error?error=Invalid Schedule ID")
|
||||
|
||||
difficulty = bleach.clean(self.get_argument("difficulty", None))
|
||||
server_obj = self.controller.servers.get_server_obj(server_id)
|
||||
enabled = bleach.clean(self.get_argument("enabled", "0"))
|
||||
name = bleach.clean(self.get_argument("name", ""))
|
||||
if difficulty == "basic":
|
||||
action = bleach.clean(self.get_argument("action", None))
|
||||
interval = bleach.clean(self.get_argument("interval", None))
|
||||
interval_type = bleach.clean(self.get_argument("interval_type", None))
|
||||
# only check for time if it's number of days
|
||||
if interval_type == "days":
|
||||
sch_time = bleach.clean(self.get_argument("time", None))
|
||||
if int(interval) > 30:
|
||||
self.redirect(
|
||||
"/panel/error?error=Invalid argument."
|
||||
" Days must be 30 or fewer."
|
||||
)
|
||||
return
|
||||
if action == "command":
|
||||
command = self.get_argument("command", None)
|
||||
elif action == "start":
|
||||
command = "start_server"
|
||||
elif action == "stop":
|
||||
command = "stop_server"
|
||||
elif action == "restart":
|
||||
command = "restart_server"
|
||||
elif action == "backup":
|
||||
command = "backup_server"
|
||||
elif difficulty == "reaction":
|
||||
interval_type = "reaction"
|
||||
action = bleach.clean(self.get_argument("action", None))
|
||||
delay = bleach.clean(self.get_argument("delay", None))
|
||||
parent = bleach.clean(self.get_argument("parent", None))
|
||||
if action == "command":
|
||||
command = self.get_argument("command", None)
|
||||
elif action == "start":
|
||||
command = "start_server"
|
||||
elif action == "stop":
|
||||
command = "stop_server"
|
||||
elif action == "restart":
|
||||
command = "restart_server"
|
||||
elif action == "backup":
|
||||
command = "backup_server"
|
||||
parent = bleach.clean(self.get_argument("parent", None))
|
||||
else:
|
||||
interval_type = ""
|
||||
cron_string = bleach.clean(self.get_argument("cron", ""))
|
||||
if not croniter.is_valid(cron_string):
|
||||
self.redirect(
|
||||
"/panel/error?error=INVALID FORMAT: Invalid Cron Format."
|
||||
)
|
||||
return
|
||||
action = bleach.clean(self.get_argument("action", None))
|
||||
if action == "command":
|
||||
command = self.get_argument("command", None)
|
||||
elif action == "start":
|
||||
command = "start_server"
|
||||
elif action == "stop":
|
||||
command = "stop_server"
|
||||
elif action == "restart":
|
||||
command = "restart_server"
|
||||
elif action == "backup":
|
||||
command = "backup_server"
|
||||
if bleach.clean(self.get_argument("enabled", "0")) == "1":
|
||||
enabled = True
|
||||
else:
|
||||
enabled = False
|
||||
if bleach.clean(self.get_argument("one_time", "0")) == "1":
|
||||
one_time = True
|
||||
else:
|
||||
one_time = False
|
||||
|
||||
if interval_type == "days":
|
||||
job_data = {
|
||||
"name": name,
|
||||
"server_id": server_id,
|
||||
"action": action,
|
||||
"interval_type": interval_type,
|
||||
"interval": interval,
|
||||
"command": command,
|
||||
"start_time": sch_time,
|
||||
"enabled": enabled,
|
||||
"one_time": one_time,
|
||||
"cron_string": "",
|
||||
"parent": None,
|
||||
"delay": 0,
|
||||
}
|
||||
elif difficulty == "advanced":
|
||||
job_data = {
|
||||
"name": name,
|
||||
"server_id": server_id,
|
||||
"action": action,
|
||||
"interval_type": "",
|
||||
"interval": "",
|
||||
# We'll base every interval off of a midnight start time.
|
||||
"start_time": "",
|
||||
"command": command,
|
||||
"cron_string": cron_string,
|
||||
"delay": "",
|
||||
"parent": "",
|
||||
"enabled": enabled,
|
||||
"one_time": one_time,
|
||||
}
|
||||
elif difficulty == "reaction":
|
||||
job_data = {
|
||||
"name": name,
|
||||
"server_id": server_id,
|
||||
"action": action,
|
||||
"interval_type": interval_type,
|
||||
"interval": "",
|
||||
# We'll base every interval off of a midnight start time.
|
||||
"start_time": "",
|
||||
"command": command,
|
||||
"cron_string": "",
|
||||
"enabled": enabled,
|
||||
"one_time": one_time,
|
||||
"parent": parent,
|
||||
"delay": delay,
|
||||
}
|
||||
else:
|
||||
job_data = {
|
||||
"name": name,
|
||||
"server_id": server_id,
|
||||
"action": action,
|
||||
"interval_type": interval_type,
|
||||
"interval": interval,
|
||||
"command": command,
|
||||
"enabled": enabled,
|
||||
# We'll base every interval off of a midnight start time.
|
||||
"start_time": "00:00",
|
||||
"delay": "",
|
||||
"parent": "",
|
||||
"one_time": one_time,
|
||||
"cron_string": "",
|
||||
}
|
||||
self.tasks_manager.update_job(sch_id, job_data)
|
||||
|
||||
self.controller.management.add_to_audit_log(
|
||||
exec_user["user_id"],
|
||||
f"Edited server {server_id}: updated schedule",
|
||||
server_id,
|
||||
self.get_remote_ip(),
|
||||
)
|
||||
self.tasks_manager.reload_schedule_from_db()
|
||||
self.redirect(f"/panel/server_detail?id={server_id}&subpage=schedules")
|
||||
|
||||
elif page == "edit_user":
|
||||
if bleach.clean(self.get_argument("username", None)).lower() == "system":
|
||||
self.redirect(
|
||||
|
@ -35,6 +35,7 @@ task_patch_schema = {
|
||||
"",
|
||||
],
|
||||
},
|
||||
"name": {"type": "string"},
|
||||
"start_time": {"type": "string", "pattern": r"\d{1,2}:\d{1,2}"},
|
||||
"command": {"type": ["string", "null"]},
|
||||
"one_time": {"type": "boolean", "default": False},
|
||||
@ -133,6 +134,7 @@ class ApiServersServerTasksTaskIndexHandler(BaseApiHandler):
|
||||
if str(data.get("parent")) == str(task_id) and data.get("parent") is not None:
|
||||
data["parent"] = None
|
||||
|
||||
data["server_id"] = server_id
|
||||
self.tasks_manager.update_job(task_id, data)
|
||||
|
||||
self.controller.management.add_to_audit_log(
|
||||
|
@ -37,15 +37,12 @@
|
||||
<div class="row">
|
||||
<div class="col-md-8 col-sm-8">
|
||||
{% if data['new_schedule'] == True %}
|
||||
<form class="forms-sample" method="post"
|
||||
<form class="forms-sample" method="post" id="new_schedule_form"
|
||||
action="/panel/new_schedule?id={{ data['server_stats']['server_id']['server_id'] }}">
|
||||
{% else %}
|
||||
<form class="forms-sample" method="post"
|
||||
<form class="forms-sample" method="post" id="schedule_form"
|
||||
action="/panel/edit_schedule?id={{ data['server_stats']['server_id']['server_id'] }}&sch_id={{ data['schedule']['schedule_id'] }}">
|
||||
{% end %}
|
||||
{% raw xsrf_form_html() %}
|
||||
<input type="hidden" name="id" value="{{ data['server_stats']['server_id']['server_id'] }}">
|
||||
<input type="hidden" name="subpage" value="config">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="name">{{ translate('serverSchedules', 'name' , data['lang']) }}</label>
|
||||
@ -89,7 +86,7 @@
|
||||
class="text-muted ml-1"> - {{ translate('serverScheduleConfig', 'interval-explain' ,
|
||||
data['lang']) }}</small> </label>
|
||||
<input type="number" class="form-control" name="interval" id="interval"
|
||||
value="{{ data['schedule']['interval'] }}" placeholder="Interval" required>
|
||||
value="{{ data['schedule']['interval'] }}" placeholder="Interval" required min="1">
|
||||
<br>
|
||||
<br>
|
||||
<select id="interval_type" onchange="ifDays(this);" name="interval_type"
|
||||
@ -108,7 +105,7 @@
|
||||
<label for="time">{{ translate('serverScheduleConfig', 'time' , data['lang']) }} <small
|
||||
class="text-muted ml-1"> - {{ translate('serverScheduleConfig', 'time-explain' ,
|
||||
data['lang']) }}</small> </label>
|
||||
<input type="time" class="form-control" name="time" id="time"
|
||||
<input type="time" class="form-control" name="start_time" id="time"
|
||||
value="{{ data['schedule']['time'] }}" placeholder="Time" required>
|
||||
</div>
|
||||
</div>
|
||||
@ -127,7 +124,7 @@
|
||||
<label for="cron">{{ translate('serverScheduleConfig', 'cron' , data['lang']) }} <small
|
||||
class="text-muted ml-1"> - {{ translate('serverScheduleConfig', 'cron-explain' , data['lang'])
|
||||
}}</small> </label>
|
||||
<input type="input" class="form-control" name="cron" id="cron"
|
||||
<input type="input" class="form-control" name="cron_string" id="cron"
|
||||
value="{{ data['schedule']['cron_string'] }}" placeholder="* * * * *">
|
||||
</div>
|
||||
</div>
|
||||
@ -234,8 +231,99 @@
|
||||
return r ? r[1] : undefined;
|
||||
}
|
||||
|
||||
function replacer(key, value) {
|
||||
if (key != "start_time" && key != "cron_string" && key != "interval_type") {
|
||||
if (typeof value == "boolean") {
|
||||
return value
|
||||
} else {
|
||||
return (isNaN(value) ? value : +value);
|
||||
}
|
||||
} else {
|
||||
if (value === "" && key == "start_time"){
|
||||
return "00:00";
|
||||
}else{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const serverId = new URLSearchParams(document.location.search).get('id');
|
||||
const schId = new URLSearchParams(document.location.search).get('sch_id');
|
||||
$(document).ready(function () {
|
||||
console.log("ready!");
|
||||
$("#new_schedule_form").on("submit", async function (e) {
|
||||
e.preventDefault();
|
||||
var token = getCookie("_xsrf")
|
||||
let schForm = document.getElementById("new_schedule_form");
|
||||
|
||||
let formData = new FormData(schForm);
|
||||
formData.delete("difficulty");
|
||||
//Create an object from the form data entries
|
||||
let formDataObject = Object.fromEntries(formData.entries());
|
||||
//We need to make sure these are sent regardless of whether or not they're checked
|
||||
formDataObject.enabled = $("#enabled").prop('checked');
|
||||
formDataObject.one_time = $("#one_time").prop('checked');
|
||||
if ($("#difficulty").val() == "reaction"){
|
||||
formDataObject.interval_type = "reaction";
|
||||
}
|
||||
if (formDataObject.cron_string != ""){
|
||||
formDataObject.interval_type = '';
|
||||
}
|
||||
console.log(formDataObject);
|
||||
// Format the plain form data as JSON
|
||||
let formDataJsonString = JSON.stringify(formDataObject, replacer);
|
||||
|
||||
console.log(formDataJsonString);
|
||||
|
||||
let res = await fetch(`/api/v2/servers/${serverId}/tasks/`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-XSRFToken': token
|
||||
},
|
||||
body: formDataJsonString,
|
||||
});
|
||||
let responseData = await res;
|
||||
if (responseData.statusText === "OK") {
|
||||
window.location.href = `/panel/server_detail?id=${serverId}&subpage=schedules`;
|
||||
}
|
||||
});
|
||||
|
||||
$("#schedule_form").on("submit", async function (e) {
|
||||
e.preventDefault();
|
||||
var token = getCookie("_xsrf")
|
||||
let schForm = document.getElementById("schedule_form");
|
||||
|
||||
let formData = new FormData(schForm);
|
||||
formData.delete("difficulty");
|
||||
//Create an object from the form data entries
|
||||
let formDataObject = Object.fromEntries(formData.entries());
|
||||
//We need to make sure these are sent regardless of whether or not they're checked
|
||||
formDataObject.enabled = $("#enabled").prop('checked');
|
||||
formDataObject.one_time = $("#one_time").prop('checked');
|
||||
if ($("#difficulty").val() == "reaction"){
|
||||
formDataObject.interval_type = "reaction";
|
||||
}
|
||||
if (formDataObject.cron_string != ""){
|
||||
formDataObject.interval_type = '';
|
||||
}
|
||||
console.log(formDataObject);
|
||||
// Format the plain form data as JSON
|
||||
let formDataJsonString = JSON.stringify(formDataObject, replacer);
|
||||
|
||||
console.log(formDataJsonString);
|
||||
|
||||
let res = await fetch(`/api/v2/servers/${serverId}/tasks/${schId}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'X-XSRFToken': token
|
||||
},
|
||||
body: formDataJsonString,
|
||||
});
|
||||
let responseData = await res;
|
||||
if (responseData.statusText === "OK") {
|
||||
window.location.href = `/panel/server_detail?id=${serverId}&subpage=schedules`;
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -265,6 +353,7 @@
|
||||
document.getElementById("parent").required = true;
|
||||
document.getElementById("interval").required = false;
|
||||
document.getElementById("time").required = false;
|
||||
$("#cron").val("");
|
||||
}
|
||||
else {
|
||||
document.getElementById("ifAdvanced").style.display = "none";
|
||||
@ -274,6 +363,7 @@
|
||||
document.getElementById("parent").required = false;
|
||||
document.getElementById("interval").required = true;
|
||||
document.getElementById("time").required = true;
|
||||
$("#cron").val("");
|
||||
}
|
||||
}
|
||||
function ifDays() {
|
||||
@ -286,20 +376,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function del_task(sch_id, id) {
|
||||
var token = getCookie("_xsrf")
|
||||
|
||||
let res = await fetch(`/api/v2/servers/${id}/tasks/${sch_id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'token': token,
|
||||
},
|
||||
});
|
||||
let responseData = await res;
|
||||
if (responseData.statusText === "OK") {
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
function startup() {
|
||||
try {
|
||||
document.getElementById("{{ data['schedule']['interval_type'] }}").setAttribute('selected', true);
|
||||
|
@ -90,7 +90,7 @@
|
||||
<p>{{schedule.command}}</p>
|
||||
</td>
|
||||
<td id="{{schedule.interval}}" class="action">
|
||||
{% if schedule.interval != '' %}
|
||||
{% if schedule.interval_type != '' and schedule.interval_type != 'reaction' %}
|
||||
<p>{{ translate('serverSchedules', 'every', data['lang']) }}</p>
|
||||
<p>{{schedule.interval}} {{schedule.interval_type}}</p>
|
||||
{% elif schedule.interval_type == 'reaction' %}
|
||||
|
Loading…
Reference in New Issue
Block a user