diff --git a/.gitignore b/.gitignore index d226724..1def7b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .* !.gitignore -__pycache__ \ No newline at end of file +__pycache__ +secret.txt \ No newline at end of file diff --git a/README.md b/README.md index 24b650d..6719244 100644 --- a/README.md +++ b/README.md @@ -48,9 +48,56 @@ Developer info 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 or 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" +``` \ No newline at end of file diff --git a/conf/gunicorn.conf.py b/conf/gunicorn.conf.py index 88cb667..fde7958 100644 --- a/conf/gunicorn.conf.py +++ b/conf/gunicorn.conf.py @@ -3,7 +3,6 @@ """ import multiprocessing - bind = '127.0.0.1:__PORT__' # 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 loglevel = 'info' -# https://docs.gunicorn.org/en/latest/settings.html#syslog -syslog = True +# https://docs.gunicorn.org/en/latest/settings.html#logging +accesslog = '__LOG_FILE__' +errorlog = '__LOG_FILE__' # https://docs.gunicorn.org/en/latest/settings.html#pidfile pidfile = '__FINAL_HOME_PATH__/gunicorn.pid' diff --git a/conf/nginx.conf b/conf/nginx.conf index a308033..9c3cf6f 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,5 +1,6 @@ location /static/ { + # Django static files alias __PUBLIC_PATH__/static/; expires 30d; } @@ -12,26 +13,24 @@ location /static/ { #} location / { + # https://github.com/benoitc/gunicorn/blob/master/examples/nginx.conf + # this is needed if you have file import via upload enabled client_max_body_size 100M; proxy_http_version 1.1; proxy_set_header Host $host; - proxy_set_header X-Forwarded-For $remote_addr; - proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Protocol $scheme; 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_read_timeout 30; proxy_send_timeout 30; proxy_connect_timeout 30; - proxy_redirect off; + proxy_redirect off; - set $upstream http://127.0.0.1:__PORT__/; - proxy_pass $upstream; + proxy_pass http://127.0.0.1:__PORT__/; # Include SSOWAT user panel. #include conf.d/yunohost_panel.conf.inc; diff --git a/conf/pyinventory.service b/conf/pyinventory.service index 858b289..67f504b 100644 --- a/conf/pyinventory.service +++ b/conf/pyinventory.service @@ -7,7 +7,7 @@ User=__APP__ Group=__APP__ 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 StandardError=syslog diff --git a/conf/secret.txt b/conf/secret.txt deleted file mode 100644 index f47048a..0000000 --- a/conf/secret.txt +++ /dev/null @@ -1 +0,0 @@ -Dihmi_RjizdIJsCMt0y6UbRhtYyzegbBx6BF4MDzC_WIS1iNBkXQcWSlN049hfkJbdkBGPjczk79qONSfF5JfZ-SnXyzB7_7ZVZyTvDY_6jLAw0FIfH-vQzH4ETy8_FYfY7Mj3e2YJqZ_jJiR-WgBO2F2WE__yqiSlQ-8LFhHZI \ No newline at end of file diff --git a/conf/wsgi.py b/conf/wsgi.py index d9598cf..34badf7 100644 --- a/conf/wsgi.py +++ b/conf/wsgi.py @@ -1,9 +1,8 @@ """ WSGI config """ - +import os +os.environ['DJANGO_SETTINGS_MODULE'] = 'ynh_pyinventory_settings' from django.core.wsgi import get_wsgi_application - - -application = get_wsgi_application() +application = get_wsgi_application() \ No newline at end of file diff --git a/conf/ynh_pyinventory_settings.py b/conf/ynh_pyinventory_settings.py index e7f2f09..dfab0db 100644 --- a/conf/ynh_pyinventory_settings.py +++ b/conf/ynh_pyinventory_settings.py @@ -8,7 +8,7 @@ ################################################################################ ################################################################################ -from logging.handlers import SysLogHandler + from pathlib import Path as __Path from inventory_project.settings.base import * # noqa @@ -17,8 +17,14 @@ DEBUG = False # ----------------------------------------------------------------------------- -FINAL_HOME_PATH = '__FINAL_HOME_PATH__' -FINAL_WWW_PATH = '__FINAL_WWW_PATH__' +FINAL_HOME_PATH = __Path('__FINAL_HOME_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 -SECRET_KEY = '__KEY__' - DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', @@ -62,7 +66,6 @@ DEFAULT_FROM_EMAIL = '__ADMINMAIL__' # List of URLs your site is supposed to serve ALLOWED_HOSTS = ['__DOMAIN__'] - # _____________________________________________________________________________ # Configuration for caching CACHES = { @@ -86,10 +89,10 @@ CACHES = { # Static files (CSS, JavaScript, Images) STATIC_URL = '/static/' -STATIC_ROOT = str(__Path(FINAL_WWW_PATH, 'static')) +STATIC_ROOT = str(FINAL_WWW_PATH / 'static') MEDIA_URL = '/media/' -MEDIA_ROOT = str(__Path(FINAL_WWW_PATH, 'media')) +MEDIA_ROOT = str(FINAL_WWW_PATH / 'media') # ----------------------------------------------------------------------------- @@ -111,18 +114,17 @@ LOGGING = { }, 'syslog': { 'level': 'DEBUG', - 'class': 'logging.handlers.SysLogHandler', + 'class': 'logging.handlers.WatchedFileHandler', 'formatter': 'verbose', - 'address': '/dev/log', - 'facility': SysLogHandler.LOG_LOCAL2, + 'filename': str(LOG_FILE), }, }, '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}, 'axes': {'handlers': ['syslog', 'mail_admins'], 'level': 'WARNING', '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}, }, } diff --git a/manifest.json b/manifest.json index f55a7b8..a081c8a 100644 --- a/manifest.json +++ b/manifest.json @@ -5,19 +5,16 @@ "description": { "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", "license": "GPL-3.0", "maintainer": { - "name": "", - "email": "" - }, - "previous_maintainers": [{ "name": "Jens Diemer", "email": "pyinventory_yng@jensdiemer.de" - }], + }, + "previous_maintainers": [], "requirements": { - "yunohost": ">= 4.0.8" + "yunohost": ">= 4.0" }, "multi_instance": true, "services": [ @@ -35,14 +32,13 @@ "example": "domain.org" }, { - "name": "path", - "type": "path", + "name": "admin", + "type": "user", "ask": { - "en": "Choose a path for PyInventory", - "fr": "Choisissez un chemin pour PyInventory" + "en": "Choose an admin user for PyInventory", + "fr": "Choisissez l'administrateur pour PyInventory" }, - "example": "/pyinventory", - "default": "/pyinventory" + "example": "johndoe" }, { "name": "is_public", @@ -56,15 +52,6 @@ "fr": "Tout utilisateur YunoHost et les personnes anonymes pourront accéder à l'application" }, "default": false - }, - { - "name": "admin", - "type": "user", - "ask": { - "en": "Choose an admin user for PyInventory", - "fr": "Choisissez l'administrateur pour PyInventory" - }, - "example": "johndoe" } ] } diff --git a/scripts/_common.sh b/scripts/_common.sh index a1a741d..8db757c 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -5,20 +5,19 @@ #================================================= domain=$YNH_APP_ARG_DOMAIN -path_url=$YNH_APP_ARG_PATH +path_url="/" admin=$YNH_APP_ARG_ADMIN is_public=$YNH_APP_ARG_IS_PUBLIC - app=$YNH_APP_INSTANCE_NAME - #================================================= # SET CONSTANTS #================================================= public_path=/var/www/$app final_path=/opt/yunohost/$app - +log_path=/var/log/$app +log_file="${log_path}/pyinventory.log" #================================================= # COMMON VARIABLES @@ -28,77 +27,10 @@ final_path=/opt/yunohost/$app pkg_dependencies="build-essential python3-dev python3-pip python3-virtualenv virtualenv git \ postgresql postgresql-contrib" -python_version="$(python3 -V | cut -d' ' -f2 | cut -d. -f1-2)" - # PyInventory's version for PIP and settings file 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 #================================================= @@ -139,8 +71,4 @@ ynh_redis_get_free_db() { ynh_redis_remove_db() { local db=$1 redis-cli -n "$db" flushall -} - - - - +} \ No newline at end of file diff --git a/scripts/install b/scripts/install index 301b3a2..c4b20fa 100755 --- a/scripts/install +++ b/scripts/install @@ -1,7 +1,5 @@ #!/bin/bash -#================================================= -# GENERIC START #================================================= # 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" # 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 "$final_path" +sudo mkdir -p "$log_path" +sudo touch "${log_file}" + #================================================= # 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=public_path --value="$public_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 @@ -75,7 +87,7 @@ ynh_script_progression --message="Configuring nginx web server..." # Create a dedicated nginx config # 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 @@ -85,15 +97,13 @@ ynh_script_progression --message="Configuring system user..." # A home directory for venv and settings etc. ynh_system_user_create --username="$app" --home_dir="$final_path" --use_shell -#================================================= -# SPECIFIC SETUP #================================================= # PIP INSTALLATION #================================================= ynh_script_progression --message="Install PyInventory using PIP..." --weight=80 virtualenv --python=python3 "${final_path}/venv" -chown -R "$app": "$final_path" +sudo chown -R "$app" "$final_path" #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..." -# 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" 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" 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" 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="__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="__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="__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" +touch "$final_path/local_settings.py" + #================================================= # 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 -ynh_store_file_checksum --file="$settings" +# Use logrotate to manage app-specific logfile(s) +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 -ynh_add_systemd_config --service="$app" --template="pyinventory.service" +yunohost service add "$app" --log "${log_file}" #================================================= # GENERIC FINALIZATION @@ -194,22 +196,17 @@ ynh_add_systemd_config --service="$app" --template="pyinventory.service" #================================================= # 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) -ynh_use_logrotate --non-append - -#================================================= -# INTEGRATE SERVICE IN YUNOHOST -#================================================= -ynh_script_progression --message="Integrating service in YunoHost..." - -yunohost service add "$app" --log "/var/log/$app/pyinventory.log" +# https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/systemd +ynh_add_systemd_config --service="$app" --template="pyinventory.service" #================================================= # SETUP SSOWAT @@ -224,7 +221,7 @@ then fi #================================================= -# Start pyinventory +# Start pyinventory via systemd #================================================= 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" -#================================================= -# 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 diff --git a/scripts/remove b/scripts/remove index 1f156c3..5e630a4 100755 --- a/scripts/remove +++ b/scripts/remove @@ -51,8 +51,8 @@ ynh_psql_remove_db --db_user=$db_user --db_name=$db_name ##================================================= ## REMOVE REDIS DB ##================================================= -# -#ynh_redis_remove_db + +ynh_redis_remove_db #================================================= # REMOVE DEPENDENCIES diff --git a/scripts/restore b/scripts/restore index 27da747..d1a21aa 100755 --- a/scripts/restore +++ b/scripts/restore @@ -68,8 +68,8 @@ ynh_system_user_create --username=$app --home_dir="$final_path" --use_shell #================================================= # Restore permissions on app files -chown -R "$app": "$public_path" -chown -R "$app": "$final_path" +chown -R "$app" "$public_path" +chown -R "$app" "$final_path" #================================================= # SPECIFIC RESTORATION diff --git a/scripts/upgrade b/scripts/upgrade index 0ed260d..8c27a58 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,10 +1,9 @@ #!/bin/bash -#================================================= -# GENERIC START #================================================= # IMPORT GENERIC HELPERS #================================================= + source _common.sh source /usr/share/yunohost/helpers @@ -13,18 +12,17 @@ source /usr/share/yunohost/helpers #================================================= 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) +admin=$(ynh_app_setting_get --app="$app" --key=admin) is_public=$(ynh_app_setting_get --app="$app" --key=is_public) public_path=$(ynh_app_setting_get --app="$app" --key=public_path) final_path=$(ynh_app_setting_get --app="$app" --key=final_path) -db_name=$(ynh_app_setting_get --app="$app" --key=db_name) -db_user=$db_name -domain=$(ynh_app_setting_get --app="$app" --key=domain) +log_path=$(ynh_app_setting_get --app="$app" --key=log_path) + 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) -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 @@ -40,25 +38,6 @@ ynh_clean_setup () { # Exit if an error occurs during the execution of the script 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 #================================================= @@ -74,7 +53,8 @@ ynh_systemd_action --service_name="$app" --action="stop" ynh_script_progression --message="Upgrading nginx web server configuration..." # 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 @@ -104,94 +84,84 @@ ynh_add_systemd_config --service="$app" --template="pyinventory.service" # UPGRADE PYINVENTORY #================================================= -upgrade() { - new_version=$1 - settings_template=$2 - #================================================= - # PIP INSTALLATION - #================================================= - ynh_script_progression --message="Install pyinventory using PIP..." --weight=15 +ynh_script_progression --message="Install pyinventory using PIP..." --weight=15 - virtualenv --python=python3 "${final_path}/venv" - chown -R "$app": "$final_path/venv" +virtualenv --python=python3 "${final_path}/venv" +sudo 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=="$new_version" - ) +#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" +) - #================================================= - # MODIFY A CONFIG FILE - #================================================= - ynh_script_progression --message="Create pyinventory configuration file..." +#================================================= +# copy config files +# ================================================ +ynh_script_progression --message="Create pyinventory configuration file..." - ynh_backup_if_checksum_is_different --file="$final_path/wsgi.py" - cp ../conf/wsgi.py "$final_path/wsgi.py" +gunicorn_conf="$final_path/gunicorn.conf.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" - cp ../conf/wsgi.py "$final_path/manage.py" - chmod +x "$final_path/manage.py" +ynh_backup_if_checksum_is_different --file="$final_path/manage.py" +cp ../conf/manage.py "$final_path/manage.py" +chmod +x "$final_path/manage.py" - # save old settings file - settings="$final_path/ynh_pyinventory_settings.py" +ynh_backup_if_checksum_is_different --file="$final_path/wsgi.py" +cp ../conf/wsgi.py "$final_path/wsgi.py" - ynh_backup_if_checksum_is_different --file="$settings" - cp "$settings_template" "$settings" +# save old settings file +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" - 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" +cp "../conf/ynh_pyinventory_settings.py" "$settings" - # remove last "/" of $path_url - ynh_replace_string --match_string="__PATHURL__" --replace_string="${path_url%/}" --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="__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 - ynh_store_file_checksum --file="$settings" +# Recalculate and store the config file checksum into the app settings +ynh_store_file_checksum --file="$settings" - touch "$final_path/local_settings.py" +touch "$final_path/local_settings.py" - #================================================= - # MIGRATE PYINVENTORY - #================================================= - ynh_script_progression --message="Run migration scripts..." --weight=10 +#================================================= +# MIGRATE PYINVENTORY +#================================================= +ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight=10 - ( - set +o nounset - source "${final_path}/venv/bin/activate" - set -o nounset - cd "${final_path}" +( + set +o nounset + source "${final_path}/venv/bin/activate" + set -o nounset + cd "${final_path}" - ./manage.py migrate --noinput - ./manage.py collectstatic --noinput - - # Check the configuration - # 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 - fi - ) -} + # Just for debugging: + ./manage.py diffsettings -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 + ./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 -upgrade $pyinventory_version "../conf/ynh_pyinventory_settings.py" + # Check the configuration + # This may fail in some cases with errors, etc., but the app works and the user can fix issues later. + ./manage.py check --deploy || true +) #================================================= # SETUP LOGROTATE @@ -206,7 +176,7 @@ ynh_use_logrotate --non-append #================================================= 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 @@ -214,15 +184,10 @@ yunohost service add "$app" --log "/var/log/$app/pyinventory.log" # SECURE FILES AND DIRECTORIES #================================================= -# Set right permissions for curl installation -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" +# 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 @@ -236,6 +201,13 @@ then ynh_app_setting_set --app="$app" --key=unprotected_uris --value="/" 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 #=================================================