remove dynamic hosts

This commit is contained in:
Mateusz Gruszczyński 2025-03-10 16:04:04 +01:00
parent 125599e86a
commit 7706a521b4
3 changed files with 2 additions and 113 deletions

49
app.py
View File

@ -152,19 +152,7 @@ class LocalDefaultEntry(db.Model):
entry_content += f" {dynamic_content}"
return entry_content
class UserDynamicVariables(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
variable_name = db.Column(db.String(255), nullable=False)
variable_value = db.Column(db.String(255), nullable=False)
user = db.relationship('User', backref='dynamic_variables')
# Funkcje pomocnicze
def get_user_dynamic_variables(user_id):
user_variables = UserDynamicVariables.query.filter_by(user_id=user_id).all()
return {var.variable_name: var.variable_value for var in user_variables}
def ensure_local_defaults(content, user_id):
default_entries = LocalDefaultEntry.query.filter_by(user_id=user_id).all()
required_lines = []
@ -1808,43 +1796,6 @@ def delete_local_default(entry_id):
flash('Wpis został usunięty.', 'info')
return redirect(url_for('local_defaults'))
@app.route('/dynamic-variables', methods=['GET', 'POST'])
def dynamic_variables():
if 'user_id' not in session:
return redirect(url_for('login'))
user_id = session['user_id']
if request.method == 'POST':
variable_name = request.form.get('variable_name', '').strip()
variable_value = request.form.get('variable_value', '').strip()
if variable_name and variable_value:
new_variable = UserDynamicVariables(user_id=user_id, variable_name=variable_name, variable_value=variable_value)
db.session.add(new_variable)
db.session.commit()
flash('Dodano nową zmienną dynamiczną.', 'success')
else:
flash('Nazwa i wartość zmiennej są wymagane.', 'danger')
return redirect(url_for('dynamic_variables'))
variables = UserDynamicVariables.query.filter_by(user_id=user_id).all()
return render_template('dynamic_variables.html', variables=variables)
@app.route('/dynamic-variables/delete/<int:variable_id>', methods=['POST'])
def delete_dynamic_variable(variable_id):
if 'user_id' not in session:
return redirect(url_for('login'))
variable = UserDynamicVariables.query.get(variable_id)
if not variable or variable.user_id != session['user_id']:
flash('Nie znaleziono zmiennej lub brak uprawnień.', 'danger')
return redirect(url_for('dynamic_variables'))
db.session.delete(variable)
db.session.commit()
flash('Zmienna została usunięta.', 'info')
return redirect(url_for('dynamic_variables'))
scheduler = BackgroundScheduler(timezone=get_localzone())
scheduler.add_job(func=scheduled_deployments, trigger="interval", minutes=1, next_run_time=datetime.now())
scheduler.add_job(func=automated_backups, trigger="interval", minutes=1, next_run_time=datetime.now())

View File

@ -1,57 +0,0 @@
{% extends "base.html" %}
{% block title %}Zarządzanie zmiennymi dynamicznymi{% endblock %}
{% block content %}
<div class="card">
<div class="card-header">
<h3>Definiowanie zmiennych dynamicznych</h3>
<p class="text-muted">Te zmienne mogą być używane w konfiguracji <code>/etc/hosts</code>, np. <code>${app_name}</code>, <code>${hostname}</code>.</p>
</div>
<div class="card-body">
<form method="POST">
<div class="row">
<div class="col-md-5">
<label class="form-label">Nazwa zmiennej</label>
<input type="text" class="form-control" name="variable_name" placeholder="np. app_name" required>
</div>
<div class="col-md-5">
<label class="form-label">Wartość zmiennej</label>
<input type="text" class="form-control" name="variable_value" placeholder="np. MojaAplikacja" required>
</div>
<div class="col-md-2 d-flex align-items-end">
<button type="submit" class="btn btn-primary w-100">Dodaj</button>
</div>
</div>
</form>
<table class="table table-striped mt-4">
<thead>
<tr>
<th>ID</th>
<th>Nazwa zmiennej</th>
<th>Wartość</th>
<th>Akcje</th>
</tr>
</thead>
<tbody>
{% for var in variables %}
<tr>
<td>{{ var.id }}</td>
<td><code>${{ var.variable_name }}</code></td>
<td>{{ var.variable_value }}</td>
<td>
<form method="POST" action="{{ url_for('delete_dynamic_variable', variable_id=var.id) }}" onsubmit="return confirm('Usunąć zmienną?');">
<button class="btn btn-danger btn-sm">Usuń</button>
</form>
</td>
</tr>
{% else %}
<tr>
<td colspan="4">Brak zdefiniowanych zmiennych.</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}

View File

@ -9,19 +9,14 @@
<div class="card-body">
<form method="POST">
<div class="row">
<div class="col-md-4">
<div class="col-md-6">
<label class="form-label">Adres IP</label>
<input type="text" class="form-control" name="ip_address" placeholder="np. 127.0.0.1" required>
</div>
<div class="col-md-4">
<div class="col-md-6">
<label class="form-label">Hostname</label>
<input type="text" class="form-control" name="hostname" placeholder="np. localhost" required>
</div>
<div class="col-md-4">
<label class="form-label">Dynamiczna zmienna (opcjonalnie)</label>
<input type="text" class="form-control" name="dynamic_variable" placeholder="np. ${app_name}">
<small class="form-text">Dostępne zmienne: ${resolved_hostname}, ${hostname}, ${app_name}, ${function}, ${user}</small>
</div>
</div>
<button type="submit" class="btn btn-primary mt-3">Dodaj wpis</button>
</form>