1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/pyinventory_ynh.git synced 2024-09-03 20:16:09 +02:00
This commit is contained in:
JensDiemer 2020-12-08 09:31:10 +01:00
parent ba39fc8bf7
commit aa6c3f7d89
14 changed files with 219 additions and 297 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
.* .*
!.gitignore !.gitignore
__pycache__ __pycache__
secret.txt

View file

@ -48,9 +48,56 @@ Developer info
Please send your pull request to https://github.com/jedie/pyinventory_ynh Please send your pull request to https://github.com/jedie/pyinventory_ynh
Try e.g.: 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/jedie/pyinventory_ynh/tree/main --debug
or 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/jedie/pyinventory_ynh/tree/main --debug
``` ```
Try 'testing' branch, e.g.:
```bash
sudo yunohost app install https://github.com/jedie/pyinventory_ynh/tree/testing --debug
or
sudo yunohost app upgrade pyinventory -u https://github.com/jedie/pyinventory_ynh/tree/testing --debug
```
To remove call e.g.:
```bash
sudo yunohost app remove pyinventory
```
Debug installation, e.g.:
```bash
root@yunohost:~# ls -la /var/www/pyinventory/
total 18
drwxr-xr-x 4 root root 4 Dec 8 08:36 .
drwxr-xr-x 6 root root 6 Dec 8 08:36 ..
drwxr-xr-x 2 root root 2 Dec 8 08:36 media
drwxr-xr-x 7 root root 8 Dec 8 08:40 static
root@yunohost:~# ls -la /opt/yunohost/pyinventory/
total 58
drwxr-xr-x 5 pyinventory pyinventory 11 Dec 8 08:39 .
drwxr-xr-x 3 root root 3 Dec 8 08:36 ..
-rw-r--r-- 1 pyinventory pyinventory 460 Dec 8 08:39 gunicorn.conf.py
-rw-r--r-- 1 pyinventory pyinventory 0 Dec 8 08:39 local_settings.py
-rwxr-xr-x 1 pyinventory pyinventory 274 Dec 8 08:39 manage.py
-rw-r--r-- 1 pyinventory pyinventory 171 Dec 8 08:39 secret.txt
drwxr-xr-x 6 pyinventory pyinventory 6 Dec 8 08:37 venv
-rw-r--r-- 1 pyinventory pyinventory 115 Dec 8 08:39 wsgi.py
-rw-r--r-- 1 pyinventory pyinventory 4737 Dec 8 08:39 ynh_pyinventory_settings.py
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.1rc1 (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
System check identified no issues (0 silenced).
root@yunohost:~# tail -f /var/log/pyinventory/pyinventory.log
root@yunohost:~# cat /etc/systemd/system/pyinventory.service
root@yunohost:~# ynh_systemd_action --service_name="pyinventory" --action="restart"
```

View file

@ -3,7 +3,6 @@
""" """
import multiprocessing import multiprocessing
bind = '127.0.0.1:__PORT__' bind = '127.0.0.1:__PORT__'
# https://docs.gunicorn.org/en/latest/settings.html#workers # https://docs.gunicorn.org/en/latest/settings.html#workers
@ -12,8 +11,9 @@ workers = multiprocessing.cpu_count() * 2 + 1
# https://docs.gunicorn.org/en/latest/settings.html#logging # https://docs.gunicorn.org/en/latest/settings.html#logging
loglevel = 'info' loglevel = 'info'
# https://docs.gunicorn.org/en/latest/settings.html#syslog # https://docs.gunicorn.org/en/latest/settings.html#logging
syslog = True accesslog = '__LOG_FILE__'
errorlog = '__LOG_FILE__'
# https://docs.gunicorn.org/en/latest/settings.html#pidfile # https://docs.gunicorn.org/en/latest/settings.html#pidfile
pidfile = '__FINAL_HOME_PATH__/gunicorn.pid' pidfile = '__FINAL_HOME_PATH__/gunicorn.pid'

View file

@ -1,5 +1,6 @@
location /static/ { location /static/ {
# Django static files
alias __PUBLIC_PATH__/static/; alias __PUBLIC_PATH__/static/;
expires 30d; expires 30d;
} }
@ -12,17 +13,16 @@ location /static/ {
#} #}
location / { location / {
# https://github.com/benoitc/gunicorn/blob/master/examples/nginx.conf
# this is needed if you have file import via upload enabled # this is needed if you have file import via upload enabled
client_max_body_size 100M; client_max_body_size 100M;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Protocol $scheme; proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Cert-Hash $ssl_client_fingerprint;
proxy_set_header X-SSL-Protocol $ssl_protocol;
proxy_set_header X-Scheme $scheme; proxy_set_header X-Scheme $scheme;
proxy_read_timeout 30; proxy_read_timeout 30;
@ -30,8 +30,7 @@ location / {
proxy_connect_timeout 30; proxy_connect_timeout 30;
proxy_redirect off; proxy_redirect off;
set $upstream http://127.0.0.1:__PORT__/; proxy_pass http://127.0.0.1:__PORT__/;
proxy_pass $upstream;
# Include SSOWAT user panel. # Include SSOWAT user panel.
#include conf.d/yunohost_panel.conf.inc; #include conf.d/yunohost_panel.conf.inc;

View file

@ -7,7 +7,7 @@ User=__APP__
Group=__APP__ Group=__APP__
WorkingDirectory=__FINALPATH__/ WorkingDirectory=__FINALPATH__/
ExecStart=__FINALPATH__/virtualenv/bin/gunicorn --config __FINALPATH__/gunicorn.conf.py wsgi ExecStart=__FINALPATH__/venv/bin/gunicorn --config __FINALPATH__/gunicorn.conf.py wsgi
StandardOutput=syslog StandardOutput=syslog
StandardError=syslog StandardError=syslog

View file

@ -1 +0,0 @@
Dihmi_RjizdIJsCMt0y6UbRhtYyzegbBx6BF4MDzC_WIS1iNBkXQcWSlN049hfkJbdkBGPjczk79qONSfF5JfZ-SnXyzB7_7ZVZyTvDY_6jLAw0FIfH-vQzH4ETy8_FYfY7Mj3e2YJqZ_jJiR-WgBO2F2WE__yqiSlQ-8LFhHZI

View file

@ -1,9 +1,8 @@
""" """
WSGI config WSGI config
""" """
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'ynh_pyinventory_settings'
from django.core.wsgi import get_wsgi_application from django.core.wsgi import get_wsgi_application
application = get_wsgi_application() application = get_wsgi_application()

View file

@ -8,7 +8,7 @@
################################################################################ ################################################################################
################################################################################ ################################################################################
from logging.handlers import SysLogHandler
from pathlib import Path as __Path from pathlib import Path as __Path
from inventory_project.settings.base import * # noqa from inventory_project.settings.base import * # noqa
@ -17,8 +17,14 @@ DEBUG = False
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
FINAL_HOME_PATH = '__FINAL_HOME_PATH__' FINAL_HOME_PATH = __Path('__FINAL_HOME_PATH__')
FINAL_WWW_PATH = '__FINAL_WWW_PATH__' assert FINAL_HOME_PATH.is_dir(), f'Directory not exists: {FINAL_HOME_PATH}'
FINAL_WWW_PATH = __Path('__FINAL_WWW_PATH__')
assert FINAL_WWW_PATH.is_dir(), f'Directory not exists: {FINAL_WWW_PATH}'
LOG_FILE = __Path('__LOG_FILE__')
assert LOG_FILE.is_file(), f'File not exists: {LOG_FILE}'
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@ -28,8 +34,6 @@ ADMINS = (
MANAGERS = ADMINS MANAGERS = ADMINS
SECRET_KEY = '__KEY__'
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.postgresql', 'ENGINE': 'django.db.backends.postgresql',
@ -62,7 +66,6 @@ DEFAULT_FROM_EMAIL = '__ADMINMAIL__'
# List of URLs your site is supposed to serve # List of URLs your site is supposed to serve
ALLOWED_HOSTS = ['__DOMAIN__'] ALLOWED_HOSTS = ['__DOMAIN__']
# _____________________________________________________________________________ # _____________________________________________________________________________
# Configuration for caching # Configuration for caching
CACHES = { CACHES = {
@ -86,10 +89,10 @@ CACHES = {
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/' STATIC_URL = '/static/'
STATIC_ROOT = str(__Path(FINAL_WWW_PATH, 'static')) STATIC_ROOT = str(FINAL_WWW_PATH / 'static')
MEDIA_URL = '/media/' MEDIA_URL = '/media/'
MEDIA_ROOT = str(__Path(FINAL_WWW_PATH, 'media')) MEDIA_ROOT = str(FINAL_WWW_PATH / 'media')
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@ -111,18 +114,17 @@ LOGGING = {
}, },
'syslog': { 'syslog': {
'level': 'DEBUG', 'level': 'DEBUG',
'class': 'logging.handlers.SysLogHandler', 'class': 'logging.handlers.WatchedFileHandler',
'formatter': 'verbose', 'formatter': 'verbose',
'address': '/dev/log', 'filename': str(LOG_FILE),
'facility': SysLogHandler.LOG_LOCAL2,
}, },
}, },
'loggers': { 'loggers': {
'': {'handlers': ['syslog', 'mail_admins'], 'level': 'DEBUG', 'propagate': False}, '': {'handlers': ['syslog', 'mail_admins'], 'level': 'INFO', 'propagate': False},
'django': {'handlers': ['syslog', 'mail_admins'], 'level': 'INFO', 'propagate': False}, 'django': {'handlers': ['syslog', 'mail_admins'], 'level': 'INFO', 'propagate': False},
'axes': {'handlers': ['syslog', 'mail_admins'], 'level': 'WARNING', 'propagate': False}, 'axes': {'handlers': ['syslog', 'mail_admins'], 'level': 'WARNING', 'propagate': False},
'django_tools': {'handlers': ['syslog', 'mail_admins'], 'level': 'INFO', 'propagate': False}, 'django_tools': {'handlers': ['syslog', 'mail_admins'], 'level': 'INFO', 'propagate': False},
'inventory': {'handlers': ['syslog', 'mail_admins'], 'level': 'DEBUG', 'propagate': False}, 'inventory': {'handlers': ['syslog', 'mail_admins'], 'level': 'INFO', 'propagate': False},
}, },
} }

View file

@ -5,19 +5,16 @@
"description": { "description": {
"en": "Web based management to catalog things including state and location etc." "en": "Web based management to catalog things including state and location etc."
}, },
"version": "0.8.1rc1~ynh1", "version": "0.8.1rc1~ynh2",
"url": "https://github.com/jedie/PyInventory", "url": "https://github.com/jedie/PyInventory",
"license": "GPL-3.0", "license": "GPL-3.0",
"maintainer": { "maintainer": {
"name": "",
"email": ""
},
"previous_maintainers": [{
"name": "Jens Diemer", "name": "Jens Diemer",
"email": "pyinventory_yng@jensdiemer.de" "email": "pyinventory_yng@jensdiemer.de"
}], },
"previous_maintainers": [],
"requirements": { "requirements": {
"yunohost": ">= 4.0.8" "yunohost": ">= 4.0"
}, },
"multi_instance": true, "multi_instance": true,
"services": [ "services": [
@ -35,14 +32,13 @@
"example": "domain.org" "example": "domain.org"
}, },
{ {
"name": "path", "name": "admin",
"type": "path", "type": "user",
"ask": { "ask": {
"en": "Choose a path for PyInventory", "en": "Choose an admin user for PyInventory",
"fr": "Choisissez un chemin pour PyInventory" "fr": "Choisissez l'administrateur pour PyInventory"
}, },
"example": "/pyinventory", "example": "johndoe"
"default": "/pyinventory"
}, },
{ {
"name": "is_public", "name": "is_public",
@ -56,15 +52,6 @@
"fr": "Tout utilisateur YunoHost et les personnes anonymes pourront accéder à l'application" "fr": "Tout utilisateur YunoHost et les personnes anonymes pourront accéder à l'application"
}, },
"default": false "default": false
},
{
"name": "admin",
"type": "user",
"ask": {
"en": "Choose an admin user for PyInventory",
"fr": "Choisissez l'administrateur pour PyInventory"
},
"example": "johndoe"
} }
] ]
} }

View file

@ -5,20 +5,19 @@
#================================================= #=================================================
domain=$YNH_APP_ARG_DOMAIN domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH path_url="/"
admin=$YNH_APP_ARG_ADMIN admin=$YNH_APP_ARG_ADMIN
is_public=$YNH_APP_ARG_IS_PUBLIC is_public=$YNH_APP_ARG_IS_PUBLIC
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
#================================================= #=================================================
# SET CONSTANTS # SET CONSTANTS
#================================================= #=================================================
public_path=/var/www/$app public_path=/var/www/$app
final_path=/opt/yunohost/$app final_path=/opt/yunohost/$app
log_path=/var/log/$app
log_file="${log_path}/pyinventory.log"
#================================================= #=================================================
# COMMON VARIABLES # COMMON VARIABLES
@ -28,77 +27,10 @@ final_path=/opt/yunohost/$app
pkg_dependencies="build-essential python3-dev python3-pip python3-virtualenv virtualenv git \ pkg_dependencies="build-essential python3-dev python3-pip python3-virtualenv virtualenv git \
postgresql postgresql-contrib" postgresql postgresql-contrib"
python_version="$(python3 -V | cut -d' ' -f2 | cut -d. -f1-2)"
# PyInventory's version for PIP and settings file # PyInventory's version for PIP and settings file
pyinventory_version="0.8.1rc1" pyinventory_version="0.8.1rc1"
#=================================================
# EXPERIMENTAL HELPERS
#=================================================
# Send an email to inform the administrator
#
# usage: ynh_send_readme_to_admin app_message [recipients]
# | arg: app_message - The message to send to the administrator.
# | arg: recipients - The recipients of this email. Use spaces to separate multiples recipients. - default: root
# example: "root admin@domain"
# If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you
# example: "root admin@domain user1 user2"
ynh_send_readme_to_admin() {
local app_message="${1:-...No specific information...}"
local recipients="${2:-root}"
# Retrieve the email of users
find_mails () {
local list_mails="$1"
local mail
local recipients=" "
# Read each mail in argument
for mail in $list_mails
do
# Keep root or a real email address as it is
if [ "$mail" = "root" ] || echo "$mail" | grep --quiet "@"
then
recipients="$recipients $mail"
else
# But replace an user name without a domain after by its email
if mail=$(ynh_user_get_info "$mail" "mail" 2> /dev/null)
then
recipients="$recipients $mail"
fi
fi
done
echo "$recipients"
}
recipients=$(find_mails "$recipients")
local mail_subject="☁️🆈🅽🅷☁️: \`$app\` was just installed!"
local mail_message="This is an automated message from your beloved YunoHost server.
Specific information for the application $app.
$app_message
---
Automatic diagnosis data from YunoHost
$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')"
# Define binary to use for mail command
if [ -e /usr/bin/bsd-mailx ]
then
local mail_bin=/usr/bin/bsd-mailx
else
local mail_bin=/usr/bin/mail.mailutils
fi
# Send the email to the recipients
echo "$mail_message" | $mail_bin -a "Content-Type: text/plain; charset=UTF-8" -s "$mail_subject" "$recipients"
}
#================================================= #=================================================
# Redis HELPERS # Redis HELPERS
#================================================= #=================================================
@ -140,7 +72,3 @@ ynh_redis_remove_db() {
local db=$1 local db=$1
redis-cli -n "$db" flushall redis-cli -n "$db" flushall
} }

View file

@ -1,7 +1,5 @@
#!/bin/bash #!/bin/bash
#=================================================
# GENERIC START
#================================================= #=================================================
# IMPORT GENERIC HELPERS # IMPORT GENERIC HELPERS
#================================================= #=================================================
@ -28,11 +26,14 @@ test ! -e "$public_path" || ynh_die --message="This path already contains a fold
test ! -e "$final_path" || ynh_die --message="This path already contains a folder" test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
# Register (book) web path # Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url ynh_webpath_register --app="$app" --domain="$domain" --path_url="$path_url"
mkdir -p "$public_path/media" "$public_path/static" mkdir -p "$public_path/media" "$public_path/static"
mkdir -p "$final_path" mkdir -p "$final_path"
sudo mkdir -p "$log_path"
sudo touch "${log_file}"
#================================================= #=================================================
# STORE SETTINGS FROM MANIFEST # STORE SETTINGS FROM MANIFEST
#================================================= #=================================================
@ -44,6 +45,17 @@ ynh_app_setting_set --app="$app" --key=admin --value="$admin"
ynh_app_setting_set --app="$app" --key=is_public --value="$is_public" ynh_app_setting_set --app="$app" --key=is_public --value="$is_public"
ynh_app_setting_set --app="$app" --key=public_path --value="$public_path" ynh_app_setting_set --app="$app" --key=public_path --value="$public_path"
ynh_app_setting_set --app="$app" --key=final_path --value="$final_path" ynh_app_setting_set --app="$app" --key=final_path --value="$final_path"
ynh_app_setting_set --app="$app" --key=log_path --value="$log_file"
# Find a free port
port=$(ynh_find_port --port=8000)
# Set port as application setting
# https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/setting
ynh_app_setting_set --app="$app" --key=port --value="$port"
db_pwd=$(ynh_app_setting_get --app="$app" --key=psqlpwd)
admin_mail=$(ynh_user_get_info --username="$admin" --key=mail)
redis_db=$(ynh_redis_get_free_db)
#================================================= #=================================================
# STANDARD MODIFICATIONS # STANDARD MODIFICATIONS
@ -75,7 +87,7 @@ ynh_script_progression --message="Configuring nginx web server..."
# Create a dedicated nginx config # Create a dedicated nginx config
# https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/nginx # https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/nginx
ynh_add_nginx_config "public_path" ynh_add_nginx_config "public_path" "port"
#================================================= #=================================================
# CREATE DEDICATED USER # CREATE DEDICATED USER
@ -85,15 +97,13 @@ ynh_script_progression --message="Configuring system user..."
# A home directory for venv and settings etc. # A home directory for venv and settings etc.
ynh_system_user_create --username="$app" --home_dir="$final_path" --use_shell ynh_system_user_create --username="$app" --home_dir="$final_path" --use_shell
#=================================================
# SPECIFIC SETUP
#================================================= #=================================================
# PIP INSTALLATION # PIP INSTALLATION
#================================================= #=================================================
ynh_script_progression --message="Install PyInventory using PIP..." --weight=80 ynh_script_progression --message="Install PyInventory using PIP..." --weight=80
virtualenv --python=python3 "${final_path}/venv" virtualenv --python=python3 "${final_path}/venv"
chown -R "$app": "$final_path" sudo chown -R "$app" "$final_path"
#run source in a 'sub shell' #run source in a 'sub shell'
( (
@ -106,19 +116,14 @@ chown -R "$app": "$final_path"
) )
#================================================= #=================================================
# SPECIFIC SETUP # copy config files
# ================================================= # ================================================
ynh_script_progression --message="Create pyinventory configuration file..." ynh_script_progression --message="Create pyinventory configuration file..."
# Find a free port
port=$(ynh_find_port --port=8000)
# Set port as application setting
# https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/setting
ynh_app_setting_set --app="$app" --key=port --value="$port"
gunicorn_conf="$final_path/gunicorn.conf.py" gunicorn_conf="$final_path/gunicorn.conf.py"
cp "../conf/gunicorn.conf.py" "$gunicorn_conf" cp "../conf/gunicorn.conf.py" "$gunicorn_conf"
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$gunicorn_conf" ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$gunicorn_conf"
ynh_replace_string --match_string="__LOG_FILE__" --replace_string="$log_file" --target_file="$gunicorn_conf"
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$gunicorn_conf" ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$gunicorn_conf"
cp ../conf/manage.py "$final_path/manage.py" cp ../conf/manage.py "$final_path/manage.py"
@ -129,26 +134,23 @@ cp ../conf/wsgi.py "$final_path/wsgi.py"
settings="$final_path/ynh_pyinventory_settings.py" settings="$final_path/ynh_pyinventory_settings.py"
cp "../conf/ynh_pyinventory_settings.py" "$settings" cp "../conf/ynh_pyinventory_settings.py" "$settings"
touch "$final_path/local_settings.py"
db_pwd=$(ynh_app_setting_get --app="$app" --key=psqlpwd)
admin_mail=$(ynh_user_get_info --username="$admin" --key=mail)
key=$(ynh_string_random --length=50)
redis_db=$(ynh_redis_get_free_db)
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="$settings" ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="$settings"
ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$settings" ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$settings"
ynh_replace_string --match_string="__ADMIN__" --replace_string="$admin" --target_file="$settings" ynh_replace_string --match_string="__ADMIN__" --replace_string="$admin" --target_file="$settings"
ynh_replace_string --match_string="__ADMINMAIL__" --replace_string="$admin_mail" --target_file="$settings" ynh_replace_string --match_string="__ADMINMAIL__" --replace_string="$admin_mail" --target_file="$settings"
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$settings" ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$settings"
ynh_replace_string --match_string="__KEY__" --replace_string="$key" --target_file="$settings"
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$settings" ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$settings"
ynh_replace_string --match_string="__FINAL_WWW_PATH__" --replace_string="$public_path" --target_file="$settings" ynh_replace_string --match_string="__FINAL_WWW_PATH__" --replace_string="$public_path" --target_file="$settings"
ynh_replace_string --match_string="__LOG_FILE__" --replace_string="$log_file" --target_file="$settings"
ynh_replace_string --match_string="__REDIS_DB__" --replace_string="$redis_db" --target_file="$settings" ynh_replace_string --match_string="__REDIS_DB__" --replace_string="$redis_db" --target_file="$settings"
ynh_replace_string --match_string="__PYTHON_VERSION__" --replace_string="$python_version" --target_file="$settings"
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$settings"
ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db" ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db"
touch "$final_path/local_settings.py"
#================================================= #=================================================
# MIGRATE / COLLECTSTATIC / CREATEADMIN # MIGRATE / COLLECTSTATIC / CREATEADMIN
#================================================= #=================================================
@ -173,19 +175,19 @@ ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight
) )
#================================================= #=================================================
# STORE THE CHECKSUM OF THE CONFIG FILE # SETUP LOGROTATE
#================================================= #=================================================
ynh_script_progression --message="Configuring log rotation..."
# Calculate and store the config file checksum into the app settings # Use logrotate to manage app-specific logfile(s)
ynh_store_file_checksum --file="$settings" ynh_use_logrotate "$log_file"
#================================================= #=================================================
# SETUP SYSTEMD # INTEGRATE SERVICE IN YUNOHOST
#================================================= #=================================================
ynh_script_progression --message="Configuring a systemd service..." ynh_script_progression --message="Integrating service in YunoHost..."
# https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/systemd yunohost service add "$app" --log "${log_file}"
ynh_add_systemd_config --service="$app" --template="pyinventory.service"
#================================================= #=================================================
# GENERIC FINALIZATION # GENERIC FINALIZATION
@ -194,22 +196,17 @@ ynh_add_systemd_config --service="$app" --template="pyinventory.service"
#================================================= #=================================================
# Set permissions to app files # Set permissions to app files
chown -R "$app": "$final_path" sudo chown -R "$app" "$log_path"
sudo chown -R "$app" "$public_path"
sudo chown -R "$app" "$final_path"
#================================================= #=================================================
# SETUP LOGROTATE # SETUP SYSTEMD
#================================================= #=================================================
ynh_script_progression --message="Configuring log rotation..." ynh_script_progression --message="Configuring a systemd service..."
# Use logrotate to manage application logfile(s) # https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/systemd
ynh_use_logrotate --non-append ynh_add_systemd_config --service="$app" --template="pyinventory.service"
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add "$app" --log "/var/log/$app/pyinventory.log"
#================================================= #=================================================
# SETUP SSOWAT # SETUP SSOWAT
@ -224,7 +221,7 @@ then
fi fi
#================================================= #=================================================
# Start pyinventory # Start pyinventory via systemd
#================================================= #=================================================
ynh_script_progression --message="Starting PyInventory's services..." --weight=5 ynh_script_progression --message="Starting PyInventory's services..." --weight=5
@ -237,15 +234,6 @@ ynh_script_progression --message="Reloading nginx web server..."
ynh_systemd_action --service_name="nginx" --action="reload" ynh_systemd_action --service_name="nginx" --action="reload"
#=================================================
# SEND A README FOR THE ADMIN
#=================================================
message="
PyInventory settings file : $settings
If you facing an issue or want to improve this app, please open a new issue in this project: https://github.com/jedie/pyinventory_ynh
"
ynh_send_readme_to_admin "$message" "$admin"
ynh_script_progression --message="Installation of $app completed" --last ynh_script_progression --message="Installation of $app completed" --last

View file

@ -51,8 +51,8 @@ ynh_psql_remove_db --db_user=$db_user --db_name=$db_name
##================================================= ##=================================================
## REMOVE REDIS DB ## REMOVE REDIS DB
##================================================= ##=================================================
#
#ynh_redis_remove_db ynh_redis_remove_db
#================================================= #=================================================
# REMOVE DEPENDENCIES # REMOVE DEPENDENCIES

View file

@ -68,8 +68,8 @@ ynh_system_user_create --username=$app --home_dir="$final_path" --use_shell
#================================================= #=================================================
# Restore permissions on app files # Restore permissions on app files
chown -R "$app": "$public_path" chown -R "$app" "$public_path"
chown -R "$app": "$final_path" chown -R "$app" "$final_path"
#================================================= #=================================================
# SPECIFIC RESTORATION # SPECIFIC RESTORATION

View file

@ -1,10 +1,9 @@
#!/bin/bash #!/bin/bash
#=================================================
# GENERIC START
#================================================= #=================================================
# IMPORT GENERIC HELPERS # IMPORT GENERIC HELPERS
#================================================= #=================================================
source _common.sh source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
@ -13,18 +12,17 @@ source /usr/share/yunohost/helpers
#================================================= #=================================================
ynh_script_progression --message="Loading installation settings..." ynh_script_progression --message="Loading installation settings..."
domain=$(ynh_app_setting_get --app="$app" --key=domain)
path_url=$(ynh_app_setting_get --app="$app" --key=path) path_url=$(ynh_app_setting_get --app="$app" --key=path)
admin=$(ynh_app_setting_get --app="$app" --key=admin)
is_public=$(ynh_app_setting_get --app="$app" --key=is_public) is_public=$(ynh_app_setting_get --app="$app" --key=is_public)
public_path=$(ynh_app_setting_get --app="$app" --key=public_path) public_path=$(ynh_app_setting_get --app="$app" --key=public_path)
final_path=$(ynh_app_setting_get --app="$app" --key=final_path) final_path=$(ynh_app_setting_get --app="$app" --key=final_path)
db_name=$(ynh_app_setting_get --app="$app" --key=db_name) log_path=$(ynh_app_setting_get --app="$app" --key=log_path)
db_user=$db_name
domain=$(ynh_app_setting_get --app="$app" --key=domain)
db_pwd=$(ynh_app_setting_get --app="$app" --key=psqlpwd) db_pwd=$(ynh_app_setting_get --app="$app" --key=psqlpwd)
admin=$(ynh_app_setting_get --app="$app" --key=admin)
admin_mail=$(ynh_user_get_info "$admin" mail) admin_mail=$(ynh_user_get_info "$admin" mail)
key=$(ynh_string_random 50) redis_db=$(ynh_app_setting_get --app="$app" --key=redis_db)
#redis_db=$(ynh_app_setting_get --app="$app" --key=redis_db)
#================================================= #=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
@ -40,25 +38,6 @@ ynh_clean_setup () {
# Exit if an error occurs during the execution of the script # Exit if an error occurs during the execution of the script
ynh_abort_if_errors ynh_abort_if_errors
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."
# Fix is_public as a boolean value
if [ "$is_public" = "Yes" ]; then
ynh_app_setting_set --app=$app --key=is_public --value=1
is_public=1
elif [ "$is_public" = "No" ]; then
ynh_app_setting_set --app=$app --key=is_public --value=0
is_public=0
fi
if [[ -d "$final_path/bin/" ]]
then
ynh_secure_remove --file="$final_path/bin/"
fi
#================================================= #=================================================
# STANDARD UPGRADE STEPS # STANDARD UPGRADE STEPS
#================================================= #=================================================
@ -74,7 +53,8 @@ ynh_systemd_action --service_name="$app" --action="stop"
ynh_script_progression --message="Upgrading nginx web server configuration..." ynh_script_progression --message="Upgrading nginx web server configuration..."
# Create a dedicated nginx config # Create a dedicated nginx config
ynh_add_nginx_config # https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/nginx
ynh_add_nginx_config "public_path" "port"
#================================================= #=================================================
# SPECIFIC UPGRADE # SPECIFIC UPGRADE
@ -104,94 +84,84 @@ ynh_add_systemd_config --service="$app" --template="pyinventory.service"
# UPGRADE PYINVENTORY # UPGRADE PYINVENTORY
#================================================= #=================================================
upgrade() { ynh_script_progression --message="Install pyinventory using PIP..." --weight=15
new_version=$1
settings_template=$2
#=================================================
# PIP INSTALLATION
#=================================================
ynh_script_progression --message="Install pyinventory using PIP..." --weight=15
virtualenv --python=python3 "${final_path}/venv" virtualenv --python=python3 "${final_path}/venv"
chown -R "$app": "$final_path/venv" sudo chown -R "$app" "$final_path"
#run source in a 'sub shell' #run source in a 'sub shell'
( (
set +o nounset set +o nounset
source "${final_path}/venv/bin/activate" source "${final_path}/venv/bin/activate"
set -o nounset set -o nounset
sudo -u $app $final_path/venv/bin/pip install --upgrade pip 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 setuptools wheel psycopg2-binary
sudo -u $app $final_path/venv/bin/pip install --upgrade pyinventory=="$new_version" sudo -u $app $final_path/venv/bin/pip install --upgrade pyinventory=="$pyinventory_version"
) )
#================================================= #=================================================
# MODIFY A CONFIG FILE # copy config files
#================================================= # ================================================
ynh_script_progression --message="Create pyinventory configuration file..." ynh_script_progression --message="Create pyinventory configuration file..."
ynh_backup_if_checksum_is_different --file="$final_path/wsgi.py" gunicorn_conf="$final_path/gunicorn.conf.py"
cp ../conf/wsgi.py "$final_path/wsgi.py" ynh_backup_if_checksum_is_different --file="$gunicorn_conf"
cp "../conf/gunicorn.conf.py" "$gunicorn_conf"
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$gunicorn_conf"
ynh_replace_string --match_string="__LOG_FILE__" --replace_string="$log_file" --target_file="$gunicorn_conf"
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$gunicorn_conf"
ynh_store_file_checksum --file="$gunicorn_conf"
ynh_backup_if_checksum_is_different --file="$final_path/manage.py" ynh_backup_if_checksum_is_different --file="$final_path/manage.py"
cp ../conf/wsgi.py "$final_path/manage.py" cp ../conf/manage.py "$final_path/manage.py"
chmod +x "$final_path/manage.py" chmod +x "$final_path/manage.py"
# save old settings file ynh_backup_if_checksum_is_different --file="$final_path/wsgi.py"
settings="$final_path/ynh_pyinventory_settings.py" cp ../conf/wsgi.py "$final_path/wsgi.py"
ynh_backup_if_checksum_is_different --file="$settings" # save old settings file
cp "$settings_template" "$settings" settings="$final_path/ynh_pyinventory_settings.py"
ynh_backup_if_checksum_is_different --file="$settings"
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="$settings" cp "../conf/ynh_pyinventory_settings.py" "$settings"
ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$settings"
ynh_replace_string --match_string="__ADMIN__" --replace_string="$admin" --target_file="$settings"
ynh_replace_string --match_string="__ADMINMAIL__" --replace_string="$admin_mail" --target_file="$settings"
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$settings"
ynh_replace_string --match_string="__KEY__" --replace_string="$key" --target_file="$settings"
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$settings"
ynh_replace_string --match_string="__FINAL_WWW_PATH__" --replace_string="$public_path" --target_file="$settings"
ynh_replace_string --match_string="__PYTHON_VERSION__" --replace_string="$python_version" --target_file="$settings"
# remove last "/" of $path_url ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="$settings"
ynh_replace_string --match_string="__PATHURL__" --replace_string="${path_url%/}" --target_file="$settings" ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$settings"
ynh_replace_string --match_string="__ADMIN__" --replace_string="$admin" --target_file="$settings"
ynh_replace_string --match_string="__ADMINMAIL__" --replace_string="$admin_mail" --target_file="$settings"
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$settings"
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$settings"
ynh_replace_string --match_string="__FINAL_WWW_PATH__" --replace_string="$public_path" --target_file="$settings"
ynh_replace_string --match_string="__LOG_FILE__" --replace_string="$log_file" --target_file="$settings"
ynh_replace_string --match_string="__REDIS_DB__" --replace_string="$redis_db" --target_file="$settings"
# Recalculate and store the config file checksum into the app settings # Recalculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$settings" ynh_store_file_checksum --file="$settings"
touch "$final_path/local_settings.py" touch "$final_path/local_settings.py"
#================================================= #=================================================
# MIGRATE PYINVENTORY # MIGRATE PYINVENTORY
#================================================= #=================================================
ynh_script_progression --message="Run migration scripts..." --weight=10 ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight=10
( (
set +o nounset set +o nounset
source "${final_path}/venv/bin/activate" source "${final_path}/venv/bin/activate"
set -o nounset set -o nounset
cd "${final_path}" cd "${final_path}"
./manage.py migrate --noinput # Just for debugging:
./manage.py collectstatic --noinput ./manage.py diffsettings
./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
# Check the configuration # Check the configuration
# This may fail in some cases with errors, etc., but the app works and the user can fix issues later. # This may fail in some cases with errors, etc., but the app works and the user can fix issues later.
if [ "$new_version" == "$pyinventory_version" ]; then
./manage.py check --deploy || true ./manage.py check --deploy || true
fi )
)
}
file_version="${final_path}/venv/lib/python$python_version/site-packages/pyinventory/__init__.py"
if [ -e $file_version ]
then
current_version=$(cat $file_version | grep "^VERSION = " | grep -o "[0-9].[0-9]" | head -n1 | cut -d"." -f1)
else
current_version=3
fi
upgrade $pyinventory_version "../conf/ynh_pyinventory_settings.py"
#================================================= #=================================================
# SETUP LOGROTATE # SETUP LOGROTATE
@ -206,7 +176,7 @@ ynh_use_logrotate --non-append
#================================================= #=================================================
ynh_script_progression --message="Integrating service in YunoHost..." ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add "$app" --log "/var/log/$app/pyinventory.log" yunohost service add "$app" --log "${log_file}"
#================================================= #=================================================
# GENERIC FINALIZATION # GENERIC FINALIZATION
@ -214,15 +184,10 @@ yunohost service add "$app" --log "/var/log/$app/pyinventory.log"
# SECURE FILES AND DIRECTORIES # SECURE FILES AND DIRECTORIES
#================================================= #=================================================
# Set right permissions for curl installation # Set permissions to app files
chown -R "$app": "$final_path" sudo chown -R "$app" "$log_path"
sudo chown -R "$app" "$public_path"
#================================================= sudo chown -R "$app" "$final_path"
# START SYSTEMD SERVICES
#=================================================
ynh_script_progression --message="Starting systemd services..." --weight=5
ynh_systemd_action --service_name="$app" --action="start"
#================================================= #=================================================
# SETUP SSOWAT # SETUP SSOWAT
@ -236,6 +201,13 @@ then
ynh_app_setting_set --app="$app" --key=unprotected_uris --value="/" ynh_app_setting_set --app="$app" --key=unprotected_uris --value="/"
fi fi
#=================================================
# Start pyinventory via systemd
#=================================================
ynh_script_progression --message="Starting PyInventory's services..." --weight=5
ynh_systemd_action --service_name="$app" --action="start"
#================================================= #=================================================
# RELOAD NGINX # RELOAD NGINX
#================================================= #=================================================