Browse Source

[enh] Add package script

zamentur 8 years ago
commit
7e71a9365e
13 changed files with 387 additions and 0 deletions
  1. 3 0
      .gitmodules
  2. 13 0
      README.md
  3. 16 0
      conf/gunicorn.service
  4. 6 0
      conf/gunicorn_config.py
  5. 11 0
      conf/local.py
  6. 25 0
      conf/nginx.conf
  7. 67 0
      manifest.json
  8. 20 0
      scripts/backup
  9. 133 0
      scripts/install
  10. 25 0
      scripts/remove
  11. 66 0
      scripts/restore
  12. 1 0
      scripts/upgrade
  13. 1 0
      sources

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "sources"]
+	path = sources
+	url = https://code.ffdn.org/FFDN/wifi-with-me.git

+ 13 - 0
README.md

@@ -0,0 +1,13 @@
+wifiwithme_ynh
+===============
+
+Wifi With Me for YunoHost
+
+A form and a map to take a census of people who want create a radio network.
+
+More information on https://code.ffdn.org/FFDN/wifi-with-me
+
+## TODO
+- Add log in gunicorn conf
+- Test
+- Modify wifi-with-me to be able to give the name of the group, latitude, etc.

+ 16 - 0
conf/gunicorn.service

@@ -0,0 +1,16 @@
+[Unit]
+Description=YNH_APP_INSTANCE_NAME gunicorn daemon
+After=network.target
+
+[Service]
+PIDFile=/run/gunicorn/YNH_APP_INSTANCE_NAME-pid
+User=YNH_APP_INSTANCE_NAME
+Group=www-data
+WorkingDirectory=/opt/YNH_APP_INSTANCE_NAME
+ExecStart=/opt/YNH_APP_INSTANCE_NAME/venv/bin/gunicorn -c /opt/YNH_APP_INSTANCE_NAME/gunicorn_config.py wifiwithme.wsgi
+ExecReload=/bin/kill -s HUP $MAINPID
+ExecStop=/bin/kill -s TERM $MAINPID
+PrivateTmp=true
+
+[Install]
+WantedBy=multi-user.target

+ 6 - 0
conf/gunicorn_config.py

@@ -0,0 +1,6 @@
+command = '/opt/YNH_APP_INSTANCE_NAME/venv/bin/gunicorn'
+pythonpath = '/opt/YNH_APP_INSTANCE_NAME'
+workers = 4
+user = 'YNH_APP_INSTANCE_NAME'
+bind = 'unix:/opt/YNH_APP_INSTANCE_NAME/sock'
+pid = '/run/gunicorn/YNH_APP_INSTANCE_NAME-pid'

+ 11 - 0
conf/local.py

@@ -0,0 +1,11 @@
+# -*- coding: utf-8 -*-
+DEBUG=True
+
+ISP_NAME='YNH_APP_ARG_ISP_NAME'
+ISP_LATITUDE='YNH_APP_ARG_LATITUDE'
+ISP_LONGITUDE='YNH_APP_ARG_LONGITUDE'
+URL_PREFIX='YNH_APP_PREFIX/'
+NOTIFICATION_EMAILS=['YNH_APP_ARG_EMAIL']
+DEFAULT_FROM_EMAIL='notifier@YNH_APP_ARG_DOMAIN'
+SITE_URL="https://YNH_APP_ARG_DOMAINYNH_APP_ARG_PATH"
+SECRET_KEY = 'YNH_APP_SECRET_KEY'

+ 25 - 0
conf/nginx.conf

@@ -0,0 +1,25 @@
+location PATHTOCHANGE {
+        if ($scheme = http) {
+                rewrite ^ https://$server_name$request_uri? permanent;
+        }
+        try_files $uri @YNH_APP_INSTANCE_NAME;
+}
+
+location PATHTOCHANGE/media  {
+    alias /opt/YNH_APP_INSTANCE_NAME/media;
+}
+
+location PATHTOCHANGE/static {
+    alias /opt/YNH_APP_INSTANCE_NAME/static;
+}
+
+location PATHTOCHANGE/assets {
+    alias /opt/YNH_APP_INSTANCE_NAME/static;
+}
+
+location @YNH_APP_INSTANCE_NAME {
+    proxy_set_header Host $host;
+    proxy_set_header X-Real-IP $remote_addr;
+    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+    proxy_pass http://unix:/opt/YNH_APP_INSTANCE_NAME/sock;
+}

+ 67 - 0
manifest.json

@@ -0,0 +1,67 @@
+{
+    "name": "Wifi with me",
+    "id": "wifiwithme",
+    "packaging_format": 1,
+    "description": {
+        "en": "A form and a map to take a census of people who want create a radio network.",
+        "fr": "Formulaire & carte pour recenser les aspirant-e-s connecté-e-s à un réseau radio."
+    },
+    "maintainer": {
+        "name": "ljf",
+        "email": "ljf+yunohost@grimaud.me"
+    },
+    "requirements": {
+        "yunohost": ">> 2.4.0"
+    },
+    "multi_instance": "false",
+    "services": [
+        "nginx"
+    ],
+    "arguments": {
+        "install" : [
+            {
+                "name": "domain",
+                "type": "domain",
+                "ask": {
+                    "en": "Choose a domain for Wifi With Me",
+                    "fr": "Choisissez un nom de domaine pour Wifi With Me",
+                    "de": "Wählen Sie bitte einen Domain für Wifi With Me"
+                },
+                "example": "domain.org"
+            },
+            {
+                "name": "path",
+                "type": "path",
+                "ask": {
+                    "en": "Choose a path for Wifi With Me",
+                    "fr": "Choisissez l'adresse pour Wifi With Me",
+                    "de": "Tragen Sie bitte den Pfad für Wifi With Me ein"
+                },
+                "example": "/wifi-with-me",
+                "default": "/wifi-with-me"
+            },
+            {
+                "name": "admin",
+                "type": "user",
+                "ask": {
+                    "en": "Choose the Wifi With Me administrator (must be an existing YunoHost user)",
+                    "fr": "Choisissez l'administrateur de Wifi With Me (doit être un utilisateur YunoHost)",
+                    "de": "Wählen Sie bitte den WifiWithMeadministrator (muss ein vorhandener YunoHost Nutzer sein)"
+                },
+                "example": "johndoe"
+            },
+            {
+                "name": "email",
+                "type": "email",
+                "ask": {
+                    "en": "Choose email were send notification",
+                    "fr": "Choisissez l'email vers lequel envoyer les notifications",
+                    "de": "Wählen Sie bitte den email"
+                },
+                "example": "admin@example.tld"
+            }
+
+
+        ]
+    }
+}

+ 20 - 0
scripts/backup

@@ -0,0 +1,20 @@
+#!/bin/bash
+
+# Exit on command errors and treat unset variables as an error
+set -eu
+
+# See comments in install script
+app=$YNH_APP_INSTANCE_NAME
+
+# Source YunoHost helpers
+. /usr/share/yunohost/helpers
+
+# Backup sources & data
+ynh_backup "/opt/$APP" "sources"
+
+# Copy NGINX configuration
+domain=$(ynh_app_setting_get "$app" domain)
+ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "nginx.conf"
+
+# Copy Gunicorn pool configuration
+ynh_backup "/etc/systemd/system/$APP.service" "gunicorn.service"

+ 133 - 0
scripts/install

@@ -0,0 +1,133 @@
+#!/bin/bash
+
+set -eu
+
+app=$YNH_APP_INSTANCE_NAME
+
+# Retrieve arguments
+domain=$YNH_APP_ARG_DOMAIN
+path=$YNH_APP_ARG_PATH
+admin=$YNH_APP_ARG_ADMIN
+email=$YNH_APP_ARG_EMAIL
+
+# Source YunoHost helpers
+. /usr/share/yunohost/helpers
+
+# Check if admin exists
+ynh_user_exists $admin \
+    || ynh_die "Wrong admin"
+
+ynh_app_setting_set "$app" admin "$admin"
+ynh_app_setting_set "$app" email "$email"
+
+# Check domain/path availability
+sudo yunohost app checkurl "${domain}${path}" -a "$app" \
+    || ynh_die "Path not available: ${domain}${path}"
+
+# Dependencies
+ynh_package_install python3-pip python3-virtualenv
+
+
+# Delete db and user if exit with an error
+function exit_properly
+{
+    set +e
+  	sudo rm -Rf /opt/$app
+  	sudo rm -f /etc/nginx/conf.d/$domain.d/$app.conf
+  	sudo service nginx reload
+  	sudo yunohost service remove $app
+  	sudo rm -f /etc/systemd/system/$app.service
+  	sudo systemctl daemon-reload
+    sudo userdel $app
+    sudo rm -Rf $final_path
+    exit 1
+}
+trap exit_properly ERR
+
+
+# Copy files to the right place
+final_path=/opt/$app
+sudo mkdir -p $final_path
+sudo cp -a ../sources/. $final_path
+
+# Install venv
+# It seems that python3.4 venv is broken, we need to setup pip manually to fix that
+# http://stackoverflow.com/questions/26215790/venv-doesnt-create-activate-script-python3
+python3 -m venv --without-pip $final_path/venv
+
+set +o nounset
+source $final_path/venv/bin/activate
+set -o nounset
+curl https://bootstrap.pypa.io/get-pip.py | python
+set +o nounset
+deactivate
+set -o nounset
+
+alias py='$final_path/venv/bin/python3'
+set +o nounset
+source $final_path/venv/bin/activate
+set -o nounset
+$final_path/venv/bin/pip install gunicorn
+$final_path/venv/bin/pip install -r $final_path/requirements/base.txt
+set +o nounset
+deactivate
+set -o nounset
+
+
+# Configuration Django
+sed -i "s@YNH_APP_ARG_DOMAIN@$domain@g" ../conf/local.py
+sed -i "s@YNH_APP_ARG_PATH@$path@g" ../conf/local.py
+sed -i "s@YNH_APP_PREFIX@${path#"/"}@g" ../conf/local.py
+sed -i "s#YNH_APP_ARG_EMAIL#$email#g" ../conf/local.py
+sed -i "s#YNH_APP_SECRET_KEY#ssss#g" ../conf/local.py #TODO
+sed -i "s#YNH_APP_ARG_ISP_NAME#ARN#g" ../conf/local.py #TODO
+sed -i "s#YNH_APP_ARG_ISP_LATITUDE#47.218371#g" ../conf/local.py #TODO
+sed -i "s#YNH_APP_ARG_ISP_LONGITUDE#-1.553621#g" ../conf/local.py #TODO
+sed -i "s#YNH_APP_ARG_ISP_ZOOM#13#g" ../conf/local.py #TODO
+sudo cp ../conf/local.py $final_path/wifiwithme/settings/local.py
+
+# Set production
+sudo sed -i "s@wifiwithme\.settings\.dev@wifiwithme.settings.prod@g" $final_path/manage.py
+sudo sed -i "s@wifiwithme\.settings@wifiwithme.settings.prod@g" $final_path/wifiwithme/wsgi.py
+sudo ln -s $final_path/wifiwithme/static $final_path/static
+
+# Set permissions
+sudo useradd $app -d $final_path || echo "User already exists"
+sudo chown -R $app:www-data $final_path
+
+# Reload Nginx and regenerate SSOwat conf
+sudo yunohost app addaccess $app -u $admin
+
+old_pwd=$(pwd)
+cd $final_path
+set +o nounset
+source $final_path/venv/bin/activate
+sudo $final_path/venv/bin/python3 manage.py migrate --noinput
+set -o nounset
+cd $old_pwd
+
+# Modify Nginx configuration file and copy it to Nginx conf directory
+sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
+sed -i "s@ALIASTOCHANGE@$final_path/@g" ../conf/nginx.conf
+sed -i "s@YNH_APP_INSTANCE_NAME@$app@g" ../conf/nginx.conf
+sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
+
+# Service gunicorn
+sudo sed -i "s@YNH_APP_INSTANCE_NAME@$app@g" ../conf/gunicorn_config.py
+sudo sed -i "s@YNH_APP_INSTANCE_NAME@$app@g" ../conf/gunicorn.service
+sudo cp ../conf/gunicorn.service /etc/systemd/system/$app.service
+sudo systemctl daemon-reload
+sudo cp ../conf/gunicorn_config.py /opt/$app/
+
+# Set permissions to directory
+sudo useradd $app -d $final_path || echo "User already exists"
+sudo chown $app:www-data -R $final_path
+
+## Reload Nginx and regenerate SSOwat conf
+sudo service nginx reload
+sudo systemctl start $app
+sudo systemctl enable $app
+sudo yunohost service add $app -l /var/log/gunicorn/$app.log
+
+ynh_app_setting_set "$app" skipped_uris "/"
+sudo yunohost app ssowatconf

+ 25 - 0
scripts/remove

@@ -0,0 +1,25 @@
+#!/bin/bash
+
+set -eu
+
+app=$YNH_APP_INSTANCE_NAME
+# Source YunoHost helpers
+. /usr/share/yunohost/helpers
+
+# Retrieve app settings
+domain=$(ynh_app_setting_get "$app" domain)
+
+# Remove sources
+sudo rm -Rf /opt/$app
+
+# Remove configuration files
+sudo rm -f /etc/nginx/conf.d/$domain.d/$app.conf
+
+sudo service $app stop
+sudo killall $app
+sudo yunohost service remove $app
+sudo rm -f /etc/systemd/system/$app.service
+sudo systemctl daemon-reload
+sudo service nginx reload
+sudo yunohost app ssowatconf
+sudo userdel $app

+ 66 - 0
scripts/restore

@@ -0,0 +1,66 @@
+#!/bin/bash
+
+# Exit on command errors and treat unset variables as an error
+set -eu
+
+# See comments in install script
+app=$YNH_APP_INSTANCE_NAME
+
+# Source YunoHost helpers
+. /usr/share/yunohost/helpers
+
+# Retrieve old app settings
+domain=$(ynh_app_setting_get "$app" domain)
+path=$(ynh_app_setting_get "$app" path)
+admin=$(ynh_app_setting_get "$app" admin)
+email=$(ynh_app_setting_get "$app" email)
+
+# Check domain/path availability
+sudo yunohost app checkurl $domain$path -a $app \
+    || ynh_die "Path not available: ${domain}${path}"
+
+
+final_path=/opt/$app
+if [ -d $final_path ]; then
+     ynh_die "There is already a directory: $final_path "
+fi
+
+conf=/etc/nginx/conf.d/$domain.d/$app.conf
+if [ -f $conf ]; then
+    ynh_die "There is already a nginx conf file at this path: $conf "
+fi
+
+gunicorn_path=/etc/systemd/system/$app.service
+if [ -f $gunicorn_path ]; then
+    ynh_die "There is already a gunicorn service file at this path: $gunicorn_path "
+fi
+
+# Dependences
+ynh_package_install python3-pip python3-virtualenv
+
+# Restore sources & data
+sudo cp -a ./sources $final_path
+
+
+# Set permissions
+sudo chown -R www-data:www-data $final_path
+
+# Restore conf files
+sudo cp -a ./nginx.conf $conf
+sudo cp -a ./gunicorn.service $gunicorn_path
+sudo chown root: $gunicorn_path
+sudo chmod 644 $gunicorn_path
+
+# Set Administrator
+if ynh_user_exists $admin; then
+    sudo yunohost app addaccess $app -u $admin
+fi
+
+# Reload Nginx
+sudo service nginx reload
+sudo systemctl start $app
+sudo systemctl enable $app
+sudo yunohost service add $app -l /var/log/gunicorn/$app.log
+
+ynh_app_setting_set "$app" skipped_uris "/"
+sudo yunohost app ssowatconf

+ 1 - 0
scripts/upgrade

@@ -0,0 +1 @@
+#!/bin/bash

+ 1 - 0
sources

@@ -0,0 +1 @@
+Subproject commit d3535f918572e04be7321cbeb458c65cc80273b7