first commit

main
root 2023-12-19 11:32:13 +01:00
commit 8ea55ac5e2
11 changed files with 314 additions and 0 deletions

0
README.md 100644
View File

82
app.py 100644
View File

@ -0,0 +1,82 @@
import subprocess
import os
from flask import Flask, render_template, request, redirect, url_for
from flask_socketio import SocketIO
app = Flask(__name__)
socketio = SocketIO(app)
FILE_PATH = '/data/interface/fichier.txt'
class path:
ansible_scripts = '/data/Ansible-Playbook/'
default_hosts = '/data/Ansible-Playbook/hosts'
binary = '/usr/bin/ansible-playbook '
class flask_config:
port=8000
host='0.0.0.0'
thread=True
debug=False
@app.route('/')
def index():
playbooks=[]
for file in os.listdir(path.ansible_scripts):
if file.endswith(".yml"):
playbooks.append(file)
return render_template('index.html',list=playbooks)
@app.route('/edit', methods=['POST'])
def edit():
if request.form['file_edit']:
FILE_PATH = path.ansible_scripts+request.form['file_edit']
with open(FILE_PATH, 'r') as file:
file_content = file.read()
return render_template('edit_file.html', file_content=file_content, filepath=FILE_PATH)
@app.route('/edit_file', methods=['POST'])
def edit_file():
new_content = request.form.get('file_content')
FILE_PATH = request.form.get('file_path')
print("edit "+FILE_PATH)
with open(FILE_PATH, 'w') as file:
file.write(new_content)
return redirect(url_for('index'))
@app.route('/run')
def run():
return render_template('run.html')
@socketio.on('submit_playbook')
def handle_submit_playbook(data):
playbook = data['playbook']
hosts = data['hosts']
argument = data['argument']
limitation = data['limitation']
if playbook:
command = path.binary+path.ansible_scripts+playbook
if limitation:
command = command +" -l "+limitation
if argument:
command = command +' -e "'+argument+'"'
if hosts:
command = command +" -i "+inventory
else:
command = command +" -i "+path.default_hosts
print(command)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1)
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
socketio.emit('ansible_output', {'data': output.strip()})
process.wait()
socketio.emit('ansible_output', {'data': '*** La tâche Ansible est terminée ***'})
if __name__ == '__main__':
socketio.run(app, host=flask_config.host, port=flask_config.port, debug=flask_config.debug)

8
static/style.css 100644
View File

@ -0,0 +1,8 @@
#numbered {
background: url(http://i.imgur.com/2cOaJ.png);
background-attachment: local;
background-repeat: no-repeat;
padding-left: 35px;
padding-top: 10px;
border-color:#ccc;
}

View File

@ -0,0 +1,12 @@
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.2/socket.io.js"></script>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

View File

@ -0,0 +1,36 @@
{% extends 'base.html' %}
{% block content %}
<div class="card">
<div class="card-header">
Select Playbook and arguments
</div>
<div class="card-body">
<form id="ansibleForm">
<label for="os">Playbook</label>
<select id="playbook" class="form-control" name="playbook" placeholder="Profile" required>
{%for file in list %}
<option value={{ file }}>{{ file }}</option>
{%endfor%}
</select><br>
<label for="nom">Arguments</label>
<input type="text" name="arguments" class="form-control" placeholder="-e "><br>
<label for="nom">Hosts</label>
<input type="text" name="arguments" class="form-control" placeholder="/path/to/inventory"><br>
<button type="submit" class="btn btn-primary"onclick="submitForm()">Executer</button>
</form>
</div>
</div>
<script>
var socket = io.connect('http://' + document.domain + ':' + location.port);
function submitForm() {
var playbook = document.forms["ansibleForm"]["playbook"].value;
var hosts = document.forms["ansibleForm"]["hosts"].value;
var arguments = document.forms["ansibleForm"]["arguments"].value;
socket.emit('submit_playbook', { playbook: playbook, hosts: hosts });
window.location.href = "/run";
}
</script>
{% endblock %}

View File

@ -0,0 +1,23 @@
<!-- templates/run.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ansible Output</title>
</head>
<body>
<div id="output"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.1/socket.io.js"></script>
<script>
var socket = io.connect('http://' + document.domain + ':' + location.port);
socket.on('ansible_output', function(data) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML += data.data + '<br>';
});
</script>
</body>
</html>

View File

@ -0,0 +1,37 @@
{% extends 'base.html' %}
{% block content %}
<div class="card">
<div class="card-header">
Running playbook
</div>
<div class="card-body">
Command to run : <code>ansible-playbook {{ playbook }} {{ argument }}</code>
</div>
</div>
<div class="card">
<div class="card-header">
Output
</div>
<div class="card-body">
Test
</div>
</div>
<pre id="output"></pre>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.2/socket.io.js"></script>
<script>
var socket = io.connect('http://' + document.domain + ':' + location.port);
socket.on('output', function(data) {
document.getElementById('output').textContent += data.data;
});
function executeCommand() {
socket.emit('execute_command', "ansible-playbook {{ playbook }} {{ argument }}");
}
</script>
{% endblock %}

View File

@ -0,0 +1,44 @@
<html>
<head>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.min.js"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/mode/perl/perl.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.min.css"></link>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/theme/abbott.min.css"></link>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.2/socket.io.js"></script>
<link href="{{url_for('static', filename = 'style.css')}}" rel="stylesheet">
</head>
<body>
<div class="container">
{% block content %}{% endblock %}
</div>
<script>
var socket = io.connect('http://' + document.domain + ':' + location.port);
function submitForm() {
var playbook = document.forms["ansibleForm"]["playbook"].value;
var hosts = document.forms["ansibleForm"]["hosts"].value;
var argument = document.forms["ansibleForm"]["argument"].value;
var limitation = document.forms["ansibleForm"]["limitation"].value;
socket.emit('submit_playbook', { playbook: playbook, hosts: hosts, argument: argument, limitation: limitation });
window.location.href = "/run";
}
socket.on('ansible_output', function(data) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML += data.data + '<br>';
});
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
lineNumbers: true,
mode: 'text/x-perl',
theme: 'abbott',
});
</script>
</body>
</html>

View File

@ -0,0 +1,11 @@
{% extends 'base.html' %}
{% block content %}
<a href="/">< Back</a>
<br>
<form method="post" action="{{ url_for('edit_file') }}">
<textarea id="code" name="file_content" class="form-control numbered">{{ file_content }}</textarea>
<input type="hidden" id="file_path" name="file_path" value="{{ filepath }}">
<button type="submit" value="Save Changes" class="btn btn-info" >Save</button>
</form>
{% endblock %}

View File

@ -0,0 +1,48 @@
{% extends 'base.html' %}
{% block content %}
<div class="card">
<div class="card-header">
Select Playbook and arguments
</div>
<div class="card-body">
<form id="ansibleForm">
<label for="playbook">Playbook <span style="color:red"></span></label>
<select id="playbook" class="form-control" name="playbook" placeholder="Profile" required>
{%for file in list %}
<option value={{ file }}>{{ file }}</option>
{%endfor%}
</select>
<label for="hosts">Hosts:</label>
<input type="text" name="hosts" class="form-control" placeholder="/path/to/inventory">
<label for="argument">Arguments</label>
<input type="text" name="argument" class="form-control" placeholder="-e "><br>
<label for="limitation">Limitation</label>
<input type="text" name="limitation" class="form-control" placeholder="-l "><br>
<br>
<button type="button" class="btn btn-info" onclick="submitForm()">Run</button>
</form>
</div>
</div>
<br>
<div class="card">
<div class="card-header">
Select Playbook to edit:
</div>
<div class="card-body">
<form id="fileedit" action="/edit" method="post">
<label for="playbook">Playbook:</label>
<select id="playbook" class="form-control" name="file_edit" placeholder="Profile" required>
{%for file in list %}
<option value={{ file }}>{{ file }}</option>
{%endfor%}
</select>
<br>
<button type="submit" class="btn btn-info">Edit</button>
</form>
</div>
</div>
{% endblock %}

13
templates/run.html 100644
View File

@ -0,0 +1,13 @@
{% extends 'base.html' %}
{% block content %}
<a href="/">< Back</a>
<div class="card">
<div class="card-header">
Command output
</div>
<div class="card-body">
<div id="output" class="numbered"></div>
</div>
</div>
{% endblock %}