From 1bd9a5646867e8c8fd95b7d789cfd6e617c229fc Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Wed, 13 Apr 2022 19:29:34 +0200 Subject: [PATCH] First working prototype. --- .gitignore | 1 + README.md | 26 ++++++++++++++-- createdir.service | 14 +++++++++ dircreate/__init__.py | 69 +++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 9 ++++++ 5 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 createdir.service create mode 100644 dircreate/__init__.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 55be276..ea68efc 100644 --- a/.gitignore +++ b/.gitignore @@ -152,3 +152,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +*.ini diff --git a/README.md b/README.md index 530873c..ce2a3f0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,26 @@ # nfs-dirmaker -Creates a directory into a NFS server exported file system. -We use it to create the users' home directory before trying the mount via autofs \ No newline at end of file +Creates a directory into a NFS server exported file system. +We use it to create the users' home directory before trying the mount via autofs. + +## Requirements + +* Python 3.8+ +* Flask 2.1 + +### Build the environment + +``` shell +python3 -m venv venv +. venv/bin/activate +pip install Flask +``` + +### Run the service + +``` shell +source ./venv/bin/activate +export FLASK_APP=dircreate +export FLASK_ENV=development +flask run +``` diff --git a/createdir.service b/createdir.service new file mode 100644 index 0000000..cc73507 --- /dev/null +++ b/createdir.service @@ -0,0 +1,14 @@ +[Unit] +Description=Small service that creates a directory +After=syslog.target network.target + +[Service] +Environment=FLASK_CONFIG=production +Environment=FLASK_APP=dircreate +User=dircreate +WorkingDirectory=/opt/dircreate +ExecStart=/opt/createdir/venv/bin/gunicorn -b localhost:5000 dircreate:app +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/dircreate/__init__.py b/dircreate/__init__.py new file mode 100644 index 0000000..ce0021f --- /dev/null +++ b/dircreate/__init__.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +import configparser +import os +from os.path import exists +from flask import Flask +from flask import request + +debug = False + +if debug: + config_path_dir = '/tmp' +else: + config_path_dir = './instance' +config_filename = f"{config_path_dir}/config.ini" + +if exists(config_filename): + config = configparser.ConfigParser() + config.read(config_filename) + if debug: + print("Sections found in the config file: ", config.sections()) + if 'create-homedir' in config.sections(): + for key in config['create-homedir']: + if debug: + print('Key: ', key) + if config['create-homedir']['SECRET_KEY']: + SECRET_KEY = config['create-homedir']['SECRET_KEY'] + else: + raise SystemExit('Error: config file, no SECRET_KEY found.') + if config['create-homedir']['DIR_PREFIX']: + DIR_PREFIX = config['create-homedir']['DIR_PREFIX'] + else: + raise SystemExit('Error: config file, no DIR_PREFIX found.') + else: + raise SystemExit('Error: no create-homedir section found.') +else: + raise SystemExit('Error: no config file found.') + +def make_homedir(dir_path): + if not os.path.isdir(dir_path): + permissions = 0o700 + try: + os.mkdir(dir_path, permissions) + except OSError: + return("Creation of the directory %s failed" % dir_path), 500 + else: + return("Directory %s created successfully" % dir_path), 200 + else: + return("Directory %s already exists" % dir_path), 200 + +def create_app(test_config=None): + # create and configure the app + create_home = Flask(__name__, instance_relative_config=True) + # ensure the instance folder exists + try: + os.path.isdir(DIR_PREFIX) + except OSError: + pass + + # a simple page that says hello + @create_home.route('/', methods=['GET', 'POST']) + def makedir(): + if request.method == 'POST': + username = request.form['user'] + return make_homedir(f"{DIR_PREFIX}/{username}") + else: + return "Nosey, aren't you?" + + return create_home diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a3236d0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +click==8.1.2 +Flask==2.1.1 +gunicorn==20.1.0 +importlib-metadata==4.11.3 +itsdangerous==2.1.2 +Jinja2==3.1.1 +MarkupSafe==2.1.1 +Werkzeug==2.1.1 +zipp==3.8.0