1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/pyinventory_ynh.git synced 2024-09-03 20:16:09 +02:00

Merge pull request #10 from YunoHost-Apps/testing

testing -> master
This commit is contained in:
Jens Diemer 2020-12-10 06:34:54 +01:00 committed by GitHub
commit 09b533f044
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 351 additions and 68 deletions

20
.github/workflows/package_linter.yml vendored Normal file
View file

@ -0,0 +1,20 @@
name: YunoHost apps package linter
on:
schedule:
- cron: '0 8 * * *'
push:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: 'Clone YunoHost apps package linter'
run: |
git clone --depth=1 https://github.com/YunoHost/package_linter ~/package_linter
- name: 'Run linter'
run: |
~/package_linter/package_linter.py .

4
.gitignore vendored
View file

@ -1,4 +1,6 @@
.*
!.github
!.gitignore
__pycache__
secret.txt
secret.txt
/local_test/

View file

@ -37,29 +37,30 @@ TODO: https://github.com/django-auth-ldap/django-auth-ldap
## Links
* Report a bug about this package: https://github.com/jedie/pyinventory_ynh
* Report a bug about this package: https://github.com/YunoHost-Apps/pyinventory_ynh
* Report a bug about PyInventory itself: https://github.com/jedie/PyInventory
* YunoHost website: https://yunohost.org/
---
Developer info
----------------
# Developer info
Please send your pull request to https://github.com/jedie/pyinventory_ynh
## package installation / debugging
Please send your pull request to https://github.com/YunoHost-Apps/pyinventory_ynh
Try 'main' branch, e.g.:
```bash
sudo yunohost app install https://github.com/jedie/pyinventory_ynh/tree/main --debug
sudo yunohost app install https://github.com/YunoHost-Apps/pyinventory_ynh/tree/main --debug
or
sudo yunohost app upgrade pyinventory -u https://github.com/jedie/pyinventory_ynh/tree/main --debug
sudo yunohost app upgrade pyinventory -u https://github.com/YunoHost-Apps/pyinventory_ynh/tree/main --debug
```
Try 'testing' branch, e.g.:
```bash
sudo yunohost app install https://github.com/jedie/pyinventory_ynh/tree/testing --debug
sudo yunohost app install https://github.com/YunoHost-Apps/pyinventory_ynh/tree/testing --debug
or
sudo yunohost app upgrade pyinventory -u https://github.com/jedie/pyinventory_ynh/tree/testing --debug
sudo yunohost app upgrade pyinventory -u https://github.com/YunoHost-Apps/pyinventory_ynh/tree/testing --debug
```
To remove call e.g.:
@ -91,7 +92,7 @@ drwxr-xr-x 6 pyinventory pyinventory 6 Dec 8 08:37 venv
root@yunohost:~# cd /opt/yunohost/pyinventory/
root@yunohost:/opt/yunohost/pyinventory# source venv/bin/activate
(venv) root@yunohost:/opt/yunohost/pyinventory# ./manage.py check
PyInventory v0.8.1rc2 (Django v2.2.17)
PyInventory v0.8.1 (Django v2.2.17)
DJANGO_SETTINGS_MODULE='ynh_pyinventory_settings'
PROJECT_PATH:/opt/yunohost/pyinventory/venv/lib/python3.7/site-packages
BASE_PATH:/opt/yunohost/pyinventory
@ -102,4 +103,32 @@ root@yunohost:~# cat /etc/systemd/system/pyinventory.service
root@yunohost:~# systemctl reload-or-restart pyinventory
root@yunohost:~# journalctl --unit=pyinventory --follow
```
```
## local test
For quicker developing of PyInventory in the context of YunoHost app,
it's possible to run the Django developer server with the settings
and urls made for YunoHost installation.
For this, just run `local_test.py` in a PyInventory virtualenv.
e.g.:
```bash
~$ git clone https://github.com/jedie/PyInventory.git
~$ git clone https://github.com/YunoHost-Apps/pyinventory_ynh.git
~$ cd PyInventory/
~/PyInventory$ make install
~/PyInventory$ poetry shell
(pyinventory-yd_5sxYx-py3.8) ~/PyInventory$ cd ../pyinventory_ynh/
(pyinventory-yd_5sxYx-py3.8) ~/pyinventory_ynh$ ./local_test.py
...
Django version 2.2.17, using settings 'ynh_pyinventory_settings'
Starting development server at http://127.0.0.1:8000/
```
Notes:
* SQlite database will be used
* A super user with username `test` and password `test` is created
* The page is available under `http://127.0.0.1:8000/app_path/`

33
check_process Normal file
View file

@ -0,0 +1,33 @@
# See here for more information
# https://github.com/YunoHost/package_check#syntax-check_process-file
# Move this file from check_process.default to check_process when you have filled it.
;; Test complet
; Manifest
domain="domain.tld" (DOMAIN)
path="/path" (PATH)
admin="john" (USER)
is_public=1 (PUBLIC|public=1|private=0)
password="pass"
port="666" (PORT)
; Checks
pkg_linter=1
setup_sub_dir=1
setup_root=1
setup_nourl=0
setup_private=1
setup_public=1
upgrade=1
backup_restore=1
multi_instance=1
port_already_use=0
change_url=1
;;; Options
Email=
Notification=none
;;; Upgrade options
; commit=CommitHash
name=Name and date of the commit.
manifest_arg=domain=DOMAIN&path=PATH&admin=USER&language=fr&is_public=1&password=pass&port=666&

43
conf/create_superuser.py Normal file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env python3
import argparse
import os
import sys
def main():
os.environ['DJANGO_SETTINGS_MODULE'] = 'ynh_pyinventory_settings'
parser = argparse.ArgumentParser(
description='Create or update Django super user.'
)
parser.add_argument('--username')
parser.add_argument('--email')
parser.add_argument('--password')
args = parser.parse_args()
username = args.username
email = args.email or ''
password = args.password
import django
django.setup()
from django.contrib.auth import get_user_model
User = get_user_model()
super_user = User.objects.filter(username=username).first()
if super_user:
print('Update existing super user and set his password.', file=sys.stderr)
super_user.set_password(password)
super_user.email=email
super_user.save()
else:
print('Create new super user', file=sys.stderr)
User.objects.create_superuser(
username=username,
email=email,
password=password
)
if __name__ == '__main__':
main()

View file

@ -1,5 +1,5 @@
location /static/ {
location __PATH__/static/ {
# Django static files
alias __PUBLIC_PATH__/static/;
expires 30d;

View file

@ -23,7 +23,7 @@ assert FINAL_HOME_PATH.is_dir(), f'Directory not exists: {FINAL_HOME_PATH}'
FINAL_WWW_PATH = __Path('__FINAL_WWW_PATH__') # /var/www/$app
assert FINAL_WWW_PATH.is_dir(), f'Directory not exists: {FINAL_WWW_PATH}'
LOG_FILE = __Path('__LOG_FILE__') # /var/log/$app//pyinventory.log
LOG_FILE = __Path('__LOG_FILE__') # /var/log/$app/pyinventory.log
assert LOG_FILE.is_file(), f'File not exists: {LOG_FILE}'
PATH_URL = '__PATH_URL__' # $YNH_APP_ARG_PATH

View file

@ -5,6 +5,10 @@ from django.urls import path
# settings.PATH_URL is the $YNH_APP_ARG_PATH
if settings.PATH_URL:
urlpatterns = [
# XXX: Hack - the MEDIA_URL contains the "PATH_URL" already:
path(settings.MEDIA_URL.lstrip('/'), include('django_tools.serve_media_app.urls')),
# Prefix all urls with "PATH_URL":
path(f'{settings.PATH_URL}/', include('inventory_project.urls'))
]
else:

150
local_test.py Executable file
View file

@ -0,0 +1,150 @@
#!/usr/bin/env python3
"""
Start PyInventory in YunoHost setup locally.
Note:
You can only run this script, if you are in a activated PyInventory venv!
see README for details ;)
"""
import os
import shlex
import subprocess
import sys
from pathlib import Path
os.environ['DJANGO_SETTINGS_MODULE'] = 'ynh_pyinventory_settings'
try:
import inventory_project # noqa
except ImportError as err:
raise ImportError(
'Couldn\'t import PyInventory. Did you '
'forget to activate a virtual environment?'
) from err
BASE_PATH = Path(__file__).parent.absolute()
TEST_PATH = BASE_PATH / 'local_test'
CONF_PATH = BASE_PATH / 'conf'
FINAL_HOME_PATH = TEST_PATH / 'opt_yunohost'
FINAL_WWW_PATH = TEST_PATH / 'var_www'
LOG_FILE = TEST_PATH / 'var_log_pyinventory.log'
MANAGE_PY_FILE = CONF_PATH / 'manage.py'
CREATE_SUPERUSER_FILE = CONF_PATH / 'create_superuser.py'
SETTINGS_FILE = CONF_PATH / 'ynh_pyinventory_settings.py'
URLS_FILE = CONF_PATH / 'ynh_urls.py'
REPLACES = {
'__FINAL_HOME_PATH__': str(FINAL_HOME_PATH),
'__FINAL_WWW_PATH__': str(FINAL_WWW_PATH),
'__LOG_FILE__': str(TEST_PATH / 'var_log_pyinventory.log'),
'__PATH_URL__': 'app_path',
'__DOMAIN__': '127.0.0.1',
'django.db.backends.postgresql': 'django.db.backends.sqlite3',
"'NAME': '__APP__',": f"'NAME': '{TEST_PATH / 'test_db.sqlite'}',",
'django_redis.cache.RedisCache': 'django.core.cache.backends.dummy.DummyCache',
'DEBUG = False': 'DEBUG = True',
# Just use the default logging setup from PyInventory project:
'LOGGING = {': 'HACKED_DEACTIVATED_LOGGING = {',
}
def verbose_check_call(command, verbose=True, **kwargs):
""" 'verbose' version of subprocess.check_call() """
if verbose:
print('_' * 100)
msg = f'Call: {command!r}'
verbose_kwargs = ', '.join(f'{k}={v!r}' for k, v in sorted(kwargs.items()))
if verbose_kwargs:
msg += f' (kwargs: {verbose_kwargs})'
print(f'{msg}\n', flush=True)
env = os.environ.copy()
env['PYTHONUNBUFFERED'] = '1'
popenargs = shlex.split(command)
subprocess.check_call(
popenargs,
universal_newlines=True,
env=env,
**kwargs
)
def call_manage_py(args):
verbose_check_call(
command=f'{sys.executable} manage.py {args}',
cwd=FINAL_HOME_PATH,
)
def copy_patch(src_file, replaces=None):
dst_file = FINAL_HOME_PATH / src_file.name
print(f'{src_file.relative_to(BASE_PATH)} -> {dst_file.relative_to(BASE_PATH)}')
with src_file.open('r') as f:
content = f.read()
if replaces:
for old, new in replaces.items():
content = content.replace(old, new)
with dst_file.open('w') as f:
f.write(content)
def main():
print('-' * 100)
assert BASE_PATH.is_dir()
assert CONF_PATH.is_dir()
assert SETTINGS_FILE.is_file()
assert URLS_FILE.is_file()
for p in (TEST_PATH, FINAL_HOME_PATH, FINAL_WWW_PATH):
if p.is_dir():
print(f'Already exists: "{p.relative_to(BASE_PATH)}", ok.')
else:
print(f'Create: "{p.relative_to(BASE_PATH)}"')
p.mkdir(parents=True, exist_ok=True)
LOG_FILE.touch(exist_ok=True)
# conf/manage.py -> local_test/manage.py
copy_patch(src_file=MANAGE_PY_FILE)
# conf/create_superuser.py -> local_test/opt_yunohost/create_superuser.py
copy_patch(src_file=CREATE_SUPERUSER_FILE)
# conf/ynh_pyinventory_settings.py -> local_test/ynh_pyinventory_settings.py
copy_patch(src_file=SETTINGS_FILE, replaces=REPLACES)
# conf/ynh_urls.py -> local_test/ynh_urls.py
copy_patch(src_file=URLS_FILE, replaces=REPLACES)
# call "local_test/manage.py" via subprocess:
call_manage_py('check --deploy')
call_manage_py('migrate --no-input')
call_manage_py('collectstatic --no-input')
verbose_check_call(
command=f'{sys.executable} create_superuser.py --username="test" --password="test"',
cwd=FINAL_HOME_PATH,
)
try:
call_manage_py('runserver --nostatic')
except KeyboardInterrupt:
print('\nBye ;)')
if __name__ == '__main__':
main()

View file

@ -5,7 +5,7 @@
"description": {
"en": "Web based management to catalog things including state and location etc."
},
"version": "0.8.1rc3~ynh1",
"version": "0.8.1~ynh3",
"url": "https://github.com/jedie/PyInventory",
"license": "GPL-3.0",
"maintainer": {

View file

@ -28,7 +28,7 @@ pkg_dependencies="build-essential python3-dev python3-pip python3-virtualenv vir
postgresql postgresql-contrib"
# PyInventory's version for PIP and settings file
pyinventory_version="0.8.1rc2"
pyinventory_version="0.8.1"
#=================================================
@ -71,4 +71,19 @@ ynh_redis_get_free_db() {
ynh_redis_remove_db() {
local db=$1
redis-cli -n "$db" flushall
}
#=================================================
# Execute a command as another user
# usage: ynh_exec_as USER COMMAND [ARG ...]
ynh_exec_as() {
local USER=$1
shift 1
if [[ $USER = $(whoami) ]]; then
eval "$@"
else
sudo -u "$USER" "$@"
fi
}

View file

@ -22,13 +22,6 @@ final_path=$(ynh_app_setting_get --app="$app" --key=final_path)
domain=$(ynh_app_setting_get --app="$app" --key=domain)
db_name=$(ynh_app_setting_get --app="$app" --key=db_name)
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_print_info --message="Stopping systemd services..."
ynh_systemd_action --service_name="$app" --action="stop"
#=================================================
# DECLARE DATA AND CONF FILES TO BACKUP
#=================================================
@ -66,12 +59,6 @@ ynh_backup --src_path="/etc/logrotate.d/$app"
ynh_backup --src_path="/etc/systemd/system/$app.service"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_systemd_action --service_name="$app" --action="start"
#=================================================
# END OF SCRIPT
#=================================================

View file

@ -27,11 +27,12 @@ ynh_script_progression --message="Loading installation settings..."
public_path=$(ynh_app_setting_get --app="$app" --key=public_path)
final_path=$(ynh_app_setting_get --app="$app" --key=final_path)
is_public=$(ynh_app_setting_get --app="$app" --key=is_public)
port=$(ynh_app_setting_get --app="$app" --key=port)
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before changing its url (may take a while)..." --weight=40
ynh_script_progression --message="Backing up the app before changing its URL (may take a while)..." --weight=40
# Backup the current version of the app
ynh_backup_before_upgrade

View file

@ -31,8 +31,8 @@ ynh_webpath_register --app="$app" --domain="$domain" --path_url="$path_url"
mkdir -p "$public_path/media" "$public_path/static"
mkdir -p "$final_path"
sudo mkdir -p "$log_path"
sudo touch "${log_file}"
mkdir -p "$log_path"
touch "${log_file}"
#=================================================
# STORE SETTINGS FROM MANIFEST
@ -103,16 +103,16 @@ ynh_system_user_create --username="$app" --home_dir="$final_path" --use_shell
ynh_script_progression --message="Install PyInventory using PIP..." --weight=80
virtualenv --python=python3 "${final_path}/venv"
sudo chown -R "$app" "$final_path"
chown -R "$app" "$final_path"
#run source in a 'sub shell'
(
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
sudo -u $app $final_path/venv/bin/pip install --upgrade pip
sudo -u $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary
sudo -u $app $final_path/venv/bin/pip install --upgrade pyinventory=="$pyinventory_version"
ynh_exec_as $app $final_path/venv/bin/pip install --upgrade pip
ynh_exec_as $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary
ynh_exec_as $app $final_path/venv/bin/pip install --upgrade pyinventory=="$pyinventory_version"
)
#=================================================
@ -120,6 +120,9 @@ sudo chown -R "$app" "$final_path"
# ================================================
ynh_script_progression --message="Create pyinventory configuration file..."
cp ../conf/create_superuser.py "$final_path/create_superuser.py"
chmod +x "$final_path/create_superuser.py"
gunicorn_conf="$final_path/gunicorn.conf.py"
cp "../conf/gunicorn.conf.py" "$gunicorn_conf"
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$gunicorn_conf"
@ -171,7 +174,7 @@ ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight
./manage.py migrate --no-input
./manage.py collectstatic --no-input
echo "from django.contrib.auth import get_user_model; get_user_model().objects.create_superuser('$admin', '$admin_mail', 'pyinventory')" | ./manage.py shell
./create_superuser.py --username="$admin" --email="$admin_mail" --password="pyinventory"
# Check the configuration
# This may fail in some cases with errors, etc., but the app works and the user can fix issues later.
@ -191,7 +194,7 @@ ynh_use_logrotate "$log_file"
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add "$app" --log "${log_file}"
yunohost service add $app --description="Web based management to catalog things" --log="${log_file}"
#=================================================
# GENERIC FINALIZATION
@ -200,9 +203,9 @@ yunohost service add "$app" --log "${log_file}"
#=================================================
# Set permissions to app files
sudo chown -R "$app" "$log_path"
sudo chown -R "$app" "$public_path"
sudo chown -R "$app" "$final_path"
chown -R "$app" "$log_path"
chown -R "$app" "$public_path"
chown -R "$app" "$final_path"
#=================================================
# SETUP SYSTEMD
@ -217,11 +220,12 @@ ynh_add_systemd_config --service="$app" --template="pyinventory.service"
#=================================================
ynh_script_progression --message="Configuring SSOwat..."
# Make app public if necessary
if [ "$is_public" -eq 1 ]
# Make app public if necessary or protect it
if [ $is_public -eq 1 ]
then
# unprotected_uris allows SSO credentials to be passed anyway.
ynh_app_setting_set --app="$app" --key=unprotected_uris --value="/"
# Everyone can access the app.
# The "main" permission is automatically created before the install script.
ynh_permission_update --permission "main" --add "visitors"
fi
#=================================================
@ -238,6 +242,8 @@ ynh_script_progression --message="Reloading nginx web server..."
ynh_systemd_action --service_name="nginx" --action="reload"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last

View file

@ -102,7 +102,7 @@ systemctl enable $app.service --quiet
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add "$app" --log "/var/log/$app/pyinventory.log"
yunohost service add $app --description="Web based management to catalog things" --log="${log_file}"
#=================================================
# RESTORE THE LOGROTATE CONFIGURATION

View file

@ -20,6 +20,7 @@ public_path=$(ynh_app_setting_get --app="$app" --key=public_path)
final_path=$(ynh_app_setting_get --app="$app" --key=final_path)
log_path=$(ynh_app_setting_get --app="$app" --key=log_path)
port=$(ynh_app_setting_get --app="$app" --key=port)
db_pwd=$(ynh_app_setting_get --app="$app" --key=psqlpwd)
admin_mail=$(ynh_user_get_info "$admin" mail)
redis_db=$(ynh_app_setting_get --app="$app" --key=redis_db)
@ -87,16 +88,16 @@ ynh_add_systemd_config --service="$app" --template="pyinventory.service"
ynh_script_progression --message="Install pyinventory using PIP..." --weight=15
virtualenv --python=python3 "${final_path}/venv"
sudo chown -R "$app" "$final_path"
chown -R "$app" "$final_path"
#run source in a 'sub shell'
(
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
sudo -u $app $final_path/venv/bin/pip install --upgrade pip
sudo -u $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary
sudo -u $app $final_path/venv/bin/pip install --upgrade pyinventory=="$pyinventory_version"
ynh_exec_as $app $final_path/venv/bin/pip install --upgrade pip
ynh_exec_as $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary
ynh_exec_as $app $final_path/venv/bin/pip install --upgrade pyinventory=="$pyinventory_version"
)
#=================================================
@ -104,6 +105,10 @@ sudo chown -R "$app" "$final_path"
# ================================================
ynh_script_progression --message="Create pyinventory configuration file..."
ynh_backup_if_checksum_is_different --file="$final_path/create_superuser.py"
cp ../conf/create_superuser.py "$final_path/create_superuser.py"
chmod +x "$final_path/create_superuser.py"
gunicorn_conf="$final_path/gunicorn.conf.py"
ynh_backup_if_checksum_is_different --file="$gunicorn_conf"
cp "../conf/gunicorn.conf.py" "$gunicorn_conf"
@ -159,7 +164,7 @@ ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight
./manage.py migrate --no-input
./manage.py collectstatic --no-input
echo "from django.contrib.auth import get_user_model; get_user_model().objects.create_superuser('$admin', '$admin_mail', 'pyinventory')" | ./manage.py shell
./create_superuser.py --username="$admin" --email="$admin_mail" --password="pyinventory"
# Check the configuration
# This may fail in some cases with errors, etc., but the app works and the user can fix issues later.
@ -179,7 +184,7 @@ ynh_use_logrotate --non-append
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add "$app" --log "${log_file}"
yunohost service add $app --description="Web based management to catalog things" --log="${log_file}"
#=================================================
# GENERIC FINALIZATION
@ -188,21 +193,9 @@ yunohost service add "$app" --log "${log_file}"
#=================================================
# Set permissions to app files
sudo chown -R "$app" "$log_path"
sudo chown -R "$app" "$public_path"
sudo chown -R "$app" "$final_path"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Upgrading SSOwat configuration..."
# Make app public if necessary
if [ "$is_public" -eq 1 ]
then
# unprotected_uris allows SSO credentials to be passed anyway.
ynh_app_setting_set --app="$app" --key=unprotected_uris --value="/"
fi
chown -R "$app" "$log_path"
chown -R "$app" "$public_path"
chown -R "$app" "$final_path"
#=================================================
# Start pyinventory via systemd
@ -222,4 +215,4 @@ ynh_systemd_action --service_name=nginx --action=reload
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last
ynh_script_progression --message="Upgrade of $app completed" --last