Skip to content

Commit

Permalink
lint & template fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy committed Jan 14, 2024
1 parent 31d8f9a commit aaf43f7
Show file tree
Hide file tree
Showing 24 changed files with 200 additions and 81 deletions.
1 change: 0 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ jobs:
yamllint --version
yamllint .
shell: bash

2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
on:

Check warning on line 3 in .github/workflows/release.yml

View workflow job for this annotation

GitHub Actions / build

3:1 [truthy] truthy value should be one of [false, no, true, yes]
push:
tags:
- '*'
- '*'

jobs:
publish:
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ jobs:
pip install -r requirements.txt
shell: bash

- name: Testing DB migrations for errors
run: |
python3 manage.py makemigrations
python3 manage.py migrate
shell: bash
working-directory: ansible-webui/

- name: Testing DB migrations for warnings
run: |
m1=$(python3 manage.py makemigrations 2>&1)
if echo "$m1" | grep -q 'WARNING'; then exit 1;fi
m2=$(python3 manage.py migrate 2>&1)
if echo "$m2" | grep -q 'WARNING'; then exit 1;fi
shell: bash
working-directory: ansible-webui/

- name: Running Tests
run: python3 -m pytest
shell: bash
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
dist/
venv/
ansible_webui.egg-info/
__pychache__.py
__pychache__.py
**/aw.db
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ignore=CVS
# ignore-list. The regex matches against paths and can be in Posix or Windows
# format. Because '\\' represents the directory delimiter on Windows systems,
# it can't be used as an escape character.
ignore-paths=
ignore-paths=venv/*

# Files or directories matching the regular expression patterns are skipped.
# The regex matches against base names, not paths. The default value ignores
Expand Down
3 changes: 3 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ rules:
allowed-values: ['true', 'false', 'yes', 'no']
line-length:
max: 160

ignore: |
venv/*
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Ansible WebUI

[![Documentation](https://readthedocs.org/projects/ansible-webui/badge/?version=latest)](https://ansible-webui.readthedocs.io/en/latest/?badge=latest)
[![Lint](https://github.com/ansibleguy/ansible-webui/actions/workflows/lint.yml/badge.svg)](https://github.com/ansibleguy/ansible-webui/actions/workflows/lint.yml)
[![Test](https://github.com/ansibleguy/ansible-webui/actions/workflows/test.yml/badge.svg)](https://github.com/ansibleguy/ansible-webui/actions/workflows/test.yml)

This project was inspired by [ansible-semaphore](https://github.com/ansible-semaphore/semaphore).

The goal is to allow users to quickly install a WebUI for using Ansible locally.
Expand Down Expand Up @@ -27,6 +31,12 @@ python3 -m ansible-webui

----

## Usage

[Documentation](http://ansible-webui.readthedocs.io/)

----

## Contribute

Feel free to contribute to this project using [pull-requests](https://github.com/ansibleguy/ansible-webui/pulls), [issues](https://github.com/ansibleguy/ansible-webui/issues) and [discussions](https://github.com/ansibleguy/ansible-webui/discussions)!
Expand Down
45 changes: 3 additions & 42 deletions ansible-webui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,6 @@


if __name__ == '__main__':
import signal
from platform import uname
from threading import Thread
from os import getpid, environ
from os import kill as os_kill
from time import sleep

from gunicorn.arbiter import Arbiter

from aw.config.main import init_globals
init_globals()

from base.webserver import create_webserver
from base.scheduler import Scheduler

if uname().system.lower() != 'linux':
raise SystemError('Currently only linux systems are supported!')

scheduler = Scheduler()
schedulerThread = Thread(target=scheduler.start)
webserver = create_webserver()

# override gunicorn signal handling to allow for graceful shutdown
def signal_exit(signum=None, stack=None):
scheduler.stop(signum)
os_kill(getpid(), signal.SIGQUIT) # trigger 'Arbiter.stop'
sleep(5)
exit(0)

def signal_reload(signum=None, stack=None):
scheduler.reload(signum)

Arbiter.SIGNALS.remove(signal.SIGHUP)
Arbiter.SIGNALS.remove(signal.SIGINT)
Arbiter.SIGNALS.remove(signal.SIGTERM)

signal.signal(signal.SIGHUP, signal_reload)
signal.signal(signal.SIGINT, signal_exit)
signal.signal(signal.SIGTERM, signal_exit)

schedulerThread.start()
webserver.run()
# pylint: disable=E0401
from main import main
main()
1 change: 1 addition & 0 deletions ansible-webui/aw/config/hardcoded.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
ENV_KEY_DEV = 'AW_DEV'
THREAD_JOIN_TIMEOUT = 3
RELOAD_INTERVAL = 10
LOGIN_PATH = '/accounts/login/'
2 changes: 0 additions & 2 deletions ansible-webui/aw/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ class BareModel(models.Model):

class Meta:
abstract = True


38 changes: 35 additions & 3 deletions ansible-webui/aw/model/job.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.conf import settings
from django.contrib.auth.models import Group

from crontab import CronTab

Expand All @@ -13,14 +14,45 @@ class JobError(BareModel):
logfile = models.FilePathField()


class JobPermission(BareModel):
field_list = ['name', 'permission', 'users', 'groups']

name = models.CharField(max_length=100)
permission = models.CharField(
max_length=50,
choices=[('all', 'Full'), ('read', 'Read'), ('write', 'Write'), ('execute', 'Execute')],
)
users = models.ManyToManyField(
settings.AUTH_USER_MODEL,
through='JobPermissionMemberUser',
through_fields=('permission', 'user'),
)
groups = models.ManyToManyField(
Group,
through='JobPermissionMemberGroup',
through_fields=('permission', 'group'),
)


class JobPermissionMemberUser(BareModel):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
permission = models.ForeignKey(JobPermission, on_delete=models.CASCADE)


class JobPermissionMemberGroup(BareModel):
group = models.ForeignKey(Group, on_delete=models.CASCADE)
permission = models.ForeignKey(JobPermission, on_delete=models.CASCADE)


class Job(BareModel):
field_list = ['inventory', 'playbook', 'schedule', 'name', 'job_id']
field_list = ['job_id', 'inventory', 'playbook', 'schedule', 'name', 'permission']

job_id = models.PositiveIntegerField(primary_key=True)
inventory = models.CharField(max_length=150)
playbook = models.CharField(max_length=150)
schedule = models.CharField(max_length=50, validators=[CronTab])
name = models.CharField(max_length=100)
job_id = models.PositiveIntegerField(max_length=50)
permission = models.ForeignKey(JobPermission, on_delete=models.SET_NULL, null=True)


class JobExecution(BareModel):
Expand All @@ -31,7 +63,7 @@ class JobExecution(BareModel):
]

user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, blank=True, null=True,
settings.AUTH_USER_MODEL, on_delete=models.PROTECT, blank=True, null=True,
related_name=f"jobexec_fk_user"
)
job = models.ForeignKey(Job, on_delete=models.CASCADE, related_name=f"jobexec_fk_job")
Expand Down
2 changes: 1 addition & 1 deletion ansible-webui/aw/models.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from aw.model.job import JobError, JobExecution, Job
from aw.model.job import JobError, JobExecution, Job, JobPermission, JobPermissionMemberGroup, JobPermissionMemberUser
13 changes: 7 additions & 6 deletions ansible-webui/aw/route.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth.decorators import login_required, user_passes_test
from django.shortcuts import HttpResponse, redirect

from aw.config.hardcoded import LOGIN_PATH
from aw.permission import authorized_to_access, authorized_to_exec, authorized_to_write


Expand All @@ -10,32 +11,32 @@ def _deny(request) -> (bool, HttpResponse):


@login_required
@user_passes_test(authorized_to_access, login_url='/')
@user_passes_test(authorized_to_access, login_url=LOGIN_PATH)
def ui(request, **kwargs):
bad, deny = _deny(request)
if bad:
return deny

if request.method == 'POST':
return ui_write
return ui_write(request)

if request.method == 'PUT':
return ui_write
return ui_exec(request)

return HttpResponse(status=200, content=b"OK - read")


@login_required
@user_passes_test(authorized_to_write, login_url='/')
@user_passes_test(authorized_to_write, login_url=LOGIN_PATH)
def ui_write(request, **kwargs):
return HttpResponse(status=200, content=b"OK - write")


@login_required
@user_passes_test(authorized_to_exec, login_url='/')
@user_passes_test(authorized_to_exec, login_url=LOGIN_PATH)
def ui_exec(request, **kwargs):
return HttpResponse(status=200, content=b"OK - exec")


def catchall(request, **kwargs):
return redirect('/accounts/login/')
return redirect(LOGIN_PATH)
9 changes: 6 additions & 3 deletions ansible-webui/aw/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
from os import environ

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve()
BASE_DIR = Path(__file__).resolve().parent.parent

from aw.config.main import config
from aw.config.hardcoded import ENV_KEY_DEV, LOGIN_PATH


DEBUG = ENV_KEY_DEV in environ
ALLOWED_HOSTS = ['*']
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

# Application definition
INSTALLED_APPS = [
Expand Down Expand Up @@ -83,9 +86,9 @@

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = [os_path.join(BASE_DIR, 'static/')]
STATICFILES_DIRS = [BASE_DIR / 'aw' / 'static']
LOGIN_REDIRECT_URL = '/ui/'
LOGOUT_REDIRECT_URL = '/accounts/login/'
LOGOUT_REDIRECT_URL = LOGIN_PATH
handler403 = 'aw.utils.handlers.handler403'
handler500 = 'aw.utils.handlers.handler500'

Expand Down
2 changes: 1 addition & 1 deletion ansible-webui/aw/templates/error/js_disabled.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<noscript>
<div class="alert alert-danger">
<strong>You must enable javascript to use this webinterface.</strong>
<strong>You have to enable javascript for the App to work!</strong>
</div>
</noscript>
2 changes: 1 addition & 1 deletion ansible-webui/aw/templates/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{% if request.user_agent.is_mobile %}
<meta name="viewport" content="width=640"/>
{% endif %}
<link rel="icon" type="image/jpg" href="{% static 'img/ansible.svg' %}">
<link rel="icon" type="image/svg" href="{% static 'img/ansible.svg' %}">

{% load bootstrap5 %}
{% bootstrap_javascript %}
Expand Down
11 changes: 1 addition & 10 deletions ansible-webui/aw/templates/registration/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
{% endfor %}
{% endif %}
<div class="alert alert-info">
{% if user.is_authenticated %}
Your account doesn't have access to this page. To proceed,
please login with an account that has access.
{% else %}
{% if not user.is_authenticated %}
Please login to see this page.
{% endif %}
</div>
Expand All @@ -43,11 +40,5 @@
<br>
<button type="submit" value="login" class="btn btn-secondary">Login</button>
</form>
<div class="alert alert-info aw-info-special-mode">
<strong>This is the GrowAutomation Beta-System</strong><br>
You can get access to this preview by supporting our cause:<br>
<a href="https://github.com/sponsors/ansibleguy">Sponsor</a>
</div>
{% endif %}
</div>
{% endblock %}
26 changes: 26 additions & 0 deletions ansible-webui/aw/templatetags/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
from django import template

from aw.config.hardcoded import VERSION


register = template.Library()


@register.simple_tag
def get_version() -> str:
return VERSION


@register.simple_tag
def set_var(val):
return val


@register.filter
def get_full_uri(request):
return request.build_absolute_uri()


# @register.filter
# def format_ts(datetime_obj):
# return datetime.strftime(datetime_obj, config.DATETIME_TS_FORMAT)


# @register.simple_tag
# def random_gif() -> str:
# return f"img/500/{randint(1, 20)}.gif"
4 changes: 4 additions & 0 deletions ansible-webui/aw/utils/util_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


def test_dummy():
pass
Loading

0 comments on commit aaf43f7

Please sign in to comment.