179 lines
5.6 KiB
Django/Jinja
179 lines
5.6 KiB
Django/Jinja
import subprocess
|
|
import requests
|
|
from lxml import html
|
|
from flask import Flask, request, jsonify
|
|
import json
|
|
import os
|
|
from datetime import datetime, timedelta
|
|
|
|
app = Flask(__name__)
|
|
|
|
def start_service():
|
|
command = "systemctl start {{ service_control_name }}"
|
|
try:
|
|
# Run the command using subprocess.run()
|
|
process = subprocess.Popen(command, shell=True)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
return {"Error": e.strip('\n')}
|
|
|
|
command = "systemctl status {{ service_control_name }} | grep Active | cut -d ':' -f 2- | cut -b 2-"
|
|
try:
|
|
# Run the command using subprocess.run()
|
|
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
|
|
return {"Message": result.stdout.strip('\n')}
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
return {"Error": e.strip('\n')}
|
|
|
|
def stop_service():
|
|
command = "systemctl stop {{ service_control_name }}"
|
|
try:
|
|
# Run the command using subprocess.run()
|
|
process = subprocess.Popen(command, shell=True)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
return {"Error": e.strip('\n')}
|
|
|
|
command = "systemctl status {{ service_control_name }} | grep Active | cut -d ':' -f 2- | cut -b 2-"
|
|
try:
|
|
# Run the command using subprocess.run()
|
|
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
|
|
return {"Message": result.stdout.strip('\n')}
|
|
except subprocess.CalledProcessError as e:
|
|
return {"Error": e.strip('\n')}
|
|
|
|
def service_status():
|
|
command = "systemctl status {{ service_control_name }} | grep Active | cut -d ':' -f 2- | cut -b 2-"
|
|
try:
|
|
# Run the command using subprocess.run()
|
|
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
|
|
command = "systemctl status {{ service_control_name }} | grep Active | cut -d ':' -f 2 | cut -d ' ' -f 2"
|
|
status = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
|
|
if status.stdout.strip('\n') == "active":
|
|
elapsed_time = check_time_elapsed(result.stdout.strip('\n'))
|
|
times_up(elapsed_time)
|
|
else:
|
|
elapsed_time = -1
|
|
return {"Elapsed_Time": elapsed_time, "Service Name": "{{ service_control_name }}", "Message": result.stdout.strip('\n'), "Status": status.stdout.strip('\n')}
|
|
except subprocess.CalledProcessError as e:
|
|
return {"Error": e.strip('\n')}
|
|
|
|
def save_duration(new_duration):
|
|
# this will be an integer from 0-3 for
|
|
# 30min, 1hr, 2hr, and 6rh respectively
|
|
seconds = 1800
|
|
if new_duration == "0":
|
|
seconds = 1800
|
|
if new_duration == "1":
|
|
seconds = 3600
|
|
if new_duration == "2":
|
|
seconds = 7200
|
|
if new_duration == "3":
|
|
seconds = 21600
|
|
|
|
info = {
|
|
"duration": new_duration,
|
|
"seconds": seconds
|
|
}
|
|
with open("duration.json", "w") as file:
|
|
json.dump(info, file, indent=4)
|
|
return {"status": "duration.json stored"}
|
|
|
|
def get_duration():
|
|
try:
|
|
with open('duration.json', 'r') as file:
|
|
data = json.load(file)
|
|
return (data)
|
|
except Exception as e:
|
|
return {"error": str(e)}, 500
|
|
|
|
def check_time_elapsed(time_str):
|
|
# Extract the time part from the string
|
|
datetime_extract = time_str.split("since ")[1].split(";")[0]
|
|
|
|
try:
|
|
time_format = "%a %Y-%m-%d %H:%M:%S %Z"
|
|
given_time = datetime.strptime(datetime_extract, time_format)
|
|
except ValueError as e:
|
|
raise ValueError(f"Error parsing the time string: {datetime_extract}" + str(e))
|
|
|
|
# Get the current time
|
|
now = datetime.now()
|
|
|
|
# Calculate the difference in seconds
|
|
delta = now - given_time
|
|
total_seconds = int(delta.total_seconds())
|
|
|
|
return total_seconds
|
|
|
|
def times_up(time_elapsed):
|
|
duration_json = get_duration()
|
|
stored_duration = duration_json["duration"]
|
|
if stored_duration == 0:
|
|
if time_elapsed > 1800:
|
|
stop_service()
|
|
elif stored_duration == 1:
|
|
if time_elapsed > 3600:
|
|
stop_service()
|
|
elif stored_duration == 2:
|
|
if time_elapsed > 7200:
|
|
stop_service()
|
|
elif stored_duration == 3:
|
|
if time_elapsed > 21600:
|
|
stop_service()
|
|
|
|
|
|
|
|
@app.route('/start', methods=['GET'])
|
|
def start():
|
|
try:
|
|
return jsonify(start_service())
|
|
except ValueError as e:
|
|
print(e)
|
|
return jsonify({'error': e}), 400
|
|
|
|
@app.route('/stop', methods=['GET'])
|
|
def stop():
|
|
try:
|
|
return jsonify(stop_service())
|
|
except ValueError as e:
|
|
print(e)
|
|
return jsonify({'error': e}), 400
|
|
|
|
@app.route('/status', methods=['GET'])
|
|
def status():
|
|
try:
|
|
return jsonify(service_status())
|
|
except ValueError as e:
|
|
print(e)
|
|
return jsonify({'error': e}), 400
|
|
|
|
@app.route('/check_duration', methods=['GET'])
|
|
def check_duration():
|
|
try:
|
|
return jsonify(get_duration())
|
|
except ValueError as e:
|
|
print(e)
|
|
return jsonify({'error': e}), 400
|
|
|
|
@app.route('/store_duration', methods=['GET'])
|
|
def store_duration():
|
|
try:
|
|
new_duration = request.args.get('new_duration')
|
|
except ValueError as e:
|
|
return jsonify({'error': e}), 400
|
|
if new_duration:
|
|
try:
|
|
return jsonify(save_duration(new_duration))
|
|
except ValueError as e:
|
|
print(e)
|
|
return jsonify({'error': e}), 400
|
|
|
|
@app.route('/test', methods=['GET'])
|
|
def test():
|
|
return jsonify({'message': 'Hello there'})
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, host='0.0.0.0', port=5000)
|
|
|