Skip to content

Commit

Permalink
Merge pull request #8 from technikamateur/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
technikamateur authored Nov 27, 2019
2 parents f4cc0bc + 42da594 commit e5589b8
Show file tree
Hide file tree
Showing 17 changed files with 429 additions and 87 deletions.
2 changes: 1 addition & 1 deletion dirkules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from apscheduler.jobstores.base import ConflictingIdError
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore

VERSION = "1.0.1"
VERSION = "1.0.2"

baseDir = os.path.abspath(os.path.dirname(__file__))
staticDir = os.path.join(baseDir, 'static')
Expand Down
2 changes: 1 addition & 1 deletion dirkules/manager/driveManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def pool_gen():
raid = "unbekannt"
drives = ""
for part in value:
drives = drives + str(Drive.query.get(part.drive_id)) + ","
drives = drives + str(Drive.query.get(part.drive_id).name) + ","
drives = drives[:-1]
value = value[0]
missing = absent_drive(drives)
Expand Down
148 changes: 138 additions & 10 deletions dirkules/samba/manager.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,160 @@
from dirkules.models import Pool

from dirkules.config import staticDir

from dirkules import db, app_version
from dirkules import db, app_version, app

from dirkules.samba.models import SambaGlobal, SambaShare, SambaOption


def get_pools():
"""
This function returns suitable pools for a share.
:return: List of tuples (id, label) of pools
"""
pools = Pool.query.all()
choices = [(str(pool.id), pool.label + ": " + pool.drives + " mounted on " + pool.mountpoint) for pool in pools]
return choices

from dirkules.samba.models import SambaGlobal

def create_share(name, path, user, dir_mask, create_mask, writeable, btrfs, recycling):
"""
Creates a samba share in db with all needed options for proper generation.
:param name: Share name
:type name: String
:param path: Path to share. might change in later versions.
:type path: String
:param user: User which is allowed to access
:type user: String
:param dir_mask: The UNIX octal mask for directories in share
:type dir_mask: Integer
:param create_mask: The UNIX octal mask for files in share
:type create_mask: Integer
:param writeable: Defines whether share is writeable or not
:type writeable: Boolean
:param btrfs: Defines the btrfs vfs_object
:type btrfs: Boolean
:param recycling: Defines the recycle vfs_object
:type recycling: Boolean
:return: Nothing
:rtype: None
"""
path = Pool.query.get(int(path)).mountpoint + "/{}".format(name)
share = SambaShare(name, path, btrfs=btrfs, recycle=recycling)
user = SambaOption("valid users", user)
if dir_mask is None:
dir_mask = "0700"
if create_mask is None:
create_mask = "0600"
dir_mask = SambaOption("directory mask", dir_mask)
create_mask = SambaOption("create mask", create_mask)
if writeable:
writeable = SambaOption("writeable", "yes")
else:
writeable = SambaOption("writeable", "no")
share.options.extend([user, dir_mask, create_mask, writeable])
db.session.add(share)
db.session.commit()


def set_samba_global(workgroup, name):
"""
Sets the variables for the [global] part in smb.conf
:param workgroup: Workgroup name
:type workgroup: string
:param name: Smbd server name
:type name: string
:return: nothing
:rtype: None
"""
SambaGlobal.query.delete()
workgroup = SambaGlobal("workgroup", workgroup)
name = SambaGlobal("server string", "%h {}".format(name))
name = SambaGlobal("server string", "{}".format(name))
db.session.add(workgroup)
db.session.add(name)
db.session.commit()


def generate_smb():
"""
Function is used to generate a new smb.conf file
:return: nothing
:rtype: None
"""
if SambaGlobal.query.first() is None:
app.logger.warning("Samba not configured. Using default fallback.")
workgroup = 'WORKGROUP'
server_string = '%h dirkules'
else:
workgroup = SambaGlobal.query.get(1)
server_string = SambaGlobal.query.get(2)
workgroup = SambaGlobal.query.get(1).value
server_string = "%h " + str(SambaGlobal.query.get(2).value)
f = open("smb.conf.txt", "w")
samba_global = open(staticDir + "/conf/samba_global.conf", "r")
f.write("# This file was generated by dirkules v{}".format(app_version))
f.write()
f.write("# Global Config")
f.write("server string = {}".format(server_string))
f.write("workgroup = {}".format(workgroup))
samba_share = open(staticDir + "/conf/samba_share.conf", "r")
samba_recycle = open(staticDir + "/conf/samba_recycle.conf", "r")
f.write("# This file was generated by dirkules v{}\n\n".format(app_version))
f.write("server string = {}\n".format(server_string))
f.write("workgroup = {}\n".format(workgroup))
f.write(samba_global.read())
f.write("\n\n")
for share in SambaShare.query.all():
if share.enabled:
f.write("[{}]\n".format(share.name))
f.write("path = {}\n".format(share.path))
if share.recycle or share.btrfs:
vfs_obj = ""
if share.recycle:
vfs_obj = vfs_obj + "recycle "
if share.btrfs:
vfs_obj = vfs_obj + "btrfs"
f.write("vfs objects = {}\n".format(vfs_obj))
f.write(samba_share.read())
# reset file pointer
samba_share.seek(0)
f.write("\n")
for entry in share.options:
f.write("{} = {}\n".format(entry.option, entry.value))
if share.recycle:
f.write(samba_recycle.read())
samba_recycle.seek(0)
f.write("\n\n")
f.close()
samba_global.close()
samba_share.close()
samba_recycle.close()
app.logger.info("Generated new samba config.")


def disable_share(share):
"""
Disables a given share object.
:param share: share object
:return: nothing
"""
try:
share.enabled = False
db.session.commit()
except:
db.session.rollback()


def enable_share(share):
"""
Enables a given share object.
:param share: share object
:return: nothing
"""
try:
share.enabled = True
db.session.commit()
except:
db.session.rollback()


def remove_share(share, remove_data=False):
# TODO: Handling of remove_data. Not implemented because removing of share folder is a future thing.
try:
db.session.delete(share)
db.session.commit()
except:
db.session.rollback()
24 changes: 16 additions & 8 deletions dirkules/samba/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ def __init__(self, element, value):
class SambaShare(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
writeable = db.Column(db.Boolean)
recycling = db.Column(db.Boolean)
path = db.Column(db.String, nullable=False)
recycle = db.Column(db.Boolean)
btrfs = db.Column(db.Boolean)
options = db.relationship('SambaOptions', order_by="SambaOptions.id", backref="samba_share", lazy="select",
enabled = db.Column(db.Boolean)
options = db.relationship('SambaOption', order_by="SambaOption.id", backref="samba_share", lazy="select",
cascade="all, delete-orphan")

def __init__(self, name, writeable=False, recycling=False, btrfs=False):
def __init__(self, name, path, recycle=False, btrfs=False, enabled=True):
self.name = name
self.writeable = writeable
self.recycling = recycling
self.path = path
self.recycle = recycle
self.btrfs = btrfs
self.enabled = enabled


class SambaOptions(db.Model):
class SambaOption(db.Model):
id = db.Column(db.Integer, primary_key=True)
sambashare_id = db.Column(db.Integer, db.ForeignKey('samba_share.id'))
option = db.Column(db.String, nullable=False)
value = db.Column(db.String, nullable=False)
sambashare_id = db.Column(db.Integer, db.ForeignKey('samba_share.id'), nullable=False)

def __init__(self, option, value):
self.option = option
self.value = value


14 changes: 9 additions & 5 deletions dirkules/samba/templates/samba/add.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{% extends "base.html" %}
{% block title %}Dashboard{% endblock %}
{% block title %}Freigabe hinzufügen{% endblock %}
{% block head %}
{{ super() }}
{% endblock %}
{% block body %}
<div class="ui container">
<t2>Neue Samba Share einrichten</t2>
{% from "_formhelpers.html" import render_field %}
<form method=post class="ui form error">
{% if form.errors %}
<form method=post class="ui form error">
{% else %}
<form method=post class="ui form">
{% endif %}
{{ form.hidden_tag() }}
<h4 class="ui dividing header">Allgemeine Informationen</h4>
{{ render_field(form.name) }}
<div class="fields">
Expand All @@ -17,12 +21,12 @@ <h4 class="ui dividing header">Allgemeine Informationen</h4>
</div>
<h4 class="ui dividing header">Erweiterte Informationen</h4>
{{ render_field(form.path) }}
{{ render_field(form.user) }}
<div class="fields">
{{ render_field(form.user) }}
{{ render_field(form.create_mask) }}
{{ render_field(form.dir_mask) }}
</div>
<button class="ui button" type="submit"><i class="plus icon"></i> Freigabe hinzufügen</button>
{{ render_field(form.submit) }}
</form>
</div>
<script>
Expand Down
44 changes: 36 additions & 8 deletions dirkules/samba/templates/samba/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,56 @@
<i class="plus icon"></i> Freigabe hinzufügen
</div>
</a>
<a href="{{ url_for('.generate') }}">
<a href="{{ url_for('.generate') }}">
<div class="ui primary labeled icon button">
<i class="magic icon"></i> Generieren
</div>
</a>
<table class="ui celled fixed table">
<table class="ui celled padded table">
<thead>
<tr>
<th>Name</th>
<th>Schreibzugriff</th>
<th>Papierkorb</th>
<th>BtrFS Optimierungen</th>
<th>Pfad</th>
<th class="collapsing">Papierkorb</th>
<th class="collapsing">BtrFS Modul</th>
<th>Aktionen</th>
</tr>
</thead>

<tbody>
{% for share in shares %}
<tr>
<td>{{ share.name }}</td>
<td>{{ share.writeable }}</td>
<td>{{ share.recycling }}</td>
<td>{{ share.btrfs }}</td>
<td>{{ share.path }}</td>
{% if share.recycle %}
<td class="collapsing"><i class="check icon"></i> Aktiviert</td>
{% else %}
<td class="collapsing"><i class="close icon"></i> Deaktiviert</td>
{% endif %}
{% if share.btrfs %}
<td class="collapsing"><i class="check icon"></i> Aktiviert</td>
{% else %}
<td class="collapsing"><i class="close icon"></i> Deaktiviert</td>
{% endif %}
<td>{% if share.enabled %}
<a href="{{ url_for('.index', disable=share.id) }}">
<div class="ui orange icon basic button">
<i class="unlink icon"></i>
</div>
</a>
{% else %}
<a href="{{ url_for('.index', enable=share.id) }}">
<div class="ui green icon basic button">
<i class="linkify icon"></i>
</div>
</a>
{% endif %}
<a href="{{ url_for('.remove', share=share.id) }}">
<div class="ui red icon basic button">
<i class="trash icon"></i>
</div>
</a>
</td>
</tr>

{% endfor %}
Expand Down
41 changes: 41 additions & 0 deletions dirkules/samba/templates/samba/remove.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% extends "base.html" %}
{% block title %}Freigabe entfernen{% endblock %}
{% block body %}
<div class="topspacer"></div>
<div class="ui container">
<h2 class="ui center aligned icon header">
<i class="circular trash icon"></i>
<div class="content">
Freigabe entfernen
<div class="sub header">Sie möchten die Samba Freigabe mit dem Namen "{{ name }}" entfernen. Bitte
konkretisieren und bestätigen.
</div>
</div>
</h2>
{% from "_formhelpers.html" import render_field %}
{% if form.errors %}
<form method=post class="ui form error">
{% else %}
<form method=post class="ui form">
{% endif %}
{{ form.hidden_tag() }}
{{ render_field(form.remove_data) }}
{% if show_modal %}
<div class="ui segment">
{{ render_field(form.okay) }}
</div>
<div class="ui error message">
<i class="close icon"></i>
<div class="header">
Verletzung der Sicherheitsprotokolle
</div>
Die Sicherheitsprotokolle verhindern die automatische Ausführung.<br>
Für das Ausführen der gewünschten Aktion, ist eine Überbrückung der Sicherheitsprotokolle nötig.
Bitte bestätigen.
</div>
{% endif %}
{{ render_field(form.submit) }}
</form>
</div>
{% endblock %}

Loading

0 comments on commit e5589b8

Please sign in to comment.