Skip to content

Commit

Permalink
Cleanup endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
r-dvl committed May 17, 2024
1 parent ae78d99 commit 2765c78
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 580 deletions.
36 changes: 36 additions & 0 deletions api/v1/endpoints/crontab.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,41 @@ def get_crontab(crontab: str = Query(..., description="Crontab name")):
return {"crontab": crontab_data}
except FileNotFoundError:
return {"error": f"{crontab} crontab not found."}
except Exception as e:
return {"error": str(e)}

@router.get("/get-task")
def get_crontab(
crontab: str = Query(..., description="Crontab name"),
task: str = Query(..., description="script to fetch")
):
try:
if not crontab:
raise HTTPException(status_code=400, detail="File name not specified")

with open(f"{path}/{crontab}", 'r') as f:
crontab_output = f.read()

crontab_lines = crontab_output.strip().split("\n")
task_data = []
description = ""
for line in crontab_lines:
if line.startswith("#"):
description = line[2:]
else:
match = re.match(r"(\S+ \S+ \S+ \S+ \S+) /home/ansible/ansible-playbooks/scripts/(.+)\.sh", line)
if match:
cron_expression, task_name = match.groups()
if task in task_name:
task_data.append({
"description": description,
"cron_expression": cron_expression,
"task_name": task_name
})
description = ""

return {"task": task_data}
except FileNotFoundError:
return {"error": f"{crontab} crontab not found."}
except Exception as e:
return {"error": str(e)}
48 changes: 41 additions & 7 deletions api/v1/endpoints/playbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,69 @@
import yaml
import os
import glob
import re

router = APIRouter()

# Playbooks path
base_path = "/ansible-playbooks/playbooks/"
crontab_path = '/crontabs'

@router.get("/get-playbooks")
def get_playbooks():
"""
This endpoint reads all .yaml files in the specified path, ignoring directories.
Each playbook is added to the 'playbooks_info' dictionary with its name as the key.
This endpoint reads all .yaml files in the specified path and checks if it is scheduled in crontab.
Each playbook is added to the 'playbooks' list.
"""
# Check if path exists
if not os.path.exists(base_path):
return {"error": "The specified path does not exist."}

# Find every playbook
# Fetch every playbook
yaml_files = glob.glob(os.path.join(base_path, '*.yaml'))

playbooks_info = {}
playbooks = {}

# Read every playbook
for yaml_file in yaml_files:
with open(yaml_file, 'r') as stream:
try:
data = yaml.safe_load(stream)
playbook_name = os.path.basename(yaml_file).replace('.yaml', '')
playbooks_info[playbook_name] = data
playbook_file_name = os.path.splitext(os.path.basename(yaml_file))[0]
with open(crontab_path, 'r') as f:
crontab_output = f.read()

crontab_lines = crontab_output.strip().split("\n")
description = ""
scheduled = False

for line in crontab_lines:
if line.startswith("#"):
description = line[2:]
else:
match = re.match(r"(\S+ \S+ \S+ \S+ \S+) /home/ansible/ansible-playbooks/scripts/(.+)\.sh", line)
if match:
cron_expression, task_name = match.groups()
if playbook_file_name in task_name:
# Add the 'hosts' and 'schedule' fields to the dictionary
playbooks[task_name] = {
'hosts': data[0]['hosts'],
"description": description,
"cron_expression": cron_expression,
"task_name": task_name
}
description = ""
scheduled = True

if not scheduled:
playbooks[playbook_file_name] = {
'hosts': data[0]['hosts'],
"description": "-",
"cron_expression": "-",
"task_name": playbook_file_name
}

except yaml.YAMLError as exc:
return {"error": f"Error reading {yaml_file}."}

return playbooks_info
return playbooks
3 changes: 1 addition & 2 deletions ui/config/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//const apiUrl = 'https://ansible-manager-api.rdvl-server.site';
const apiUrl = 'http://localhost:8080';
const apiUrl = 'https://ansible-manager-api.rdvl-server.site';

export default apiUrl;
10 changes: 0 additions & 10 deletions ui/src/layouts/dashboard/config-navigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ const navConfig = [
path: '/hosts',
icon: icon('hosts'),
},
{
title: 'user',
path: '/user',
icon: icon('user'),
},
{
title: 'login',
path: '/login',
icon: icon('login'),
},
];

export default navConfig;
17 changes: 0 additions & 17 deletions ui/src/pages/user.jsx

This file was deleted.

2 changes: 0 additions & 2 deletions ui/src/routes/sections.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import DashboardLayout from 'src/layouts/dashboard';
export const HomePage = lazy(() => import('src/pages/home'));
export const HostsPage = lazy(() => import('src/pages/hosts'));
export const PlaybooksPage = lazy(() => import('src/pages/playbooks'));
export const UserPage = lazy(() => import('src/pages/user'));
export const LoginPage = lazy(() => import('src/pages/login'));
export const Page404 = lazy(() => import('src/pages/page-not-found'));

Expand All @@ -32,7 +31,6 @@ export default function Router() {
{ element: <HomePage />, index: true },
{ path: 'hosts', element: <HostsPage /> },
{ path: 'playbooks', element: <PlaybooksPage /> },
{ path: 'user', element: <UserPage /> },
],
},
{
Expand Down
29 changes: 0 additions & 29 deletions ui/src/sections/user/table-empty-rows.jsx

This file was deleted.

36 changes: 0 additions & 36 deletions ui/src/sections/user/table-no-data.jsx

This file was deleted.

73 changes: 0 additions & 73 deletions ui/src/sections/user/user-table-head.jsx

This file was deleted.

Loading

0 comments on commit 2765c78

Please sign in to comment.