First working prototype.

This commit is contained in:
Andrea Dell'Amico 2022-04-13 19:29:34 +02:00
parent e3eb1e66f6
commit 1bd9a56468
Signed by: andrea.dellamico
GPG Key ID: 147ABE6CEB9E20FF
5 changed files with 117 additions and 2 deletions

1
.gitignore vendored
View File

@ -152,3 +152,4 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
*.ini

View File

@ -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
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
```

14
createdir.service Normal file
View File

@ -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

69
dircreate/__init__.py Normal file
View File

@ -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

9
requirements.txt Normal file
View File

@ -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