From 3d69c744cc5912c01d254b364619822a480f3b64 Mon Sep 17 00:00:00 2001 From: antonmolodykh Date: Fri, 27 Apr 2018 20:26:03 +0600 Subject: [PATCH] Service for wifi-connect --- .../roles/network/files/wifi-connect.service | 17 ++++++++ ansible/roles/network/tasks/main.yml | 8 ++++ ansible/roles/network/vars/main.yml | 2 +- ansible/roles/nginx/files/nginx.conf | 26 +++++++++++- ansible/roles/nginx/tasks/main.yml | 38 ----------------- bin/disable_nginx.sh | 7 ---- bin/install.sh | 4 +- server.py | 37 +++++++++------- settings.py | 3 +- start_resin_wifi.py | 34 +++++++++++++++ static/js/{backup.coffee => settings.coffee} | 8 ++++ static/js/{backup.js => settings.js} | 12 +++++- templates/settings.html | 42 ++++++++----------- viewer.py | 11 +++-- 14 files changed, 150 insertions(+), 99 deletions(-) create mode 100644 ansible/roles/network/files/wifi-connect.service delete mode 100755 bin/disable_nginx.sh create mode 100644 start_resin_wifi.py rename static/js/{backup.coffee => settings.coffee} (88%) rename static/js/{backup.js => settings.js} (86%) diff --git a/ansible/roles/network/files/wifi-connect.service b/ansible/roles/network/files/wifi-connect.service new file mode 100644 index 00000000..da0780fc --- /dev/null +++ b/ansible/roles/network/files/wifi-connect.service @@ -0,0 +1,17 @@ +[Unit] +Description=Wifi Connect +Wants=network-online.target +After=network-online.target + +[Service] +WorkingDirectory=/home/pi/screenly +User=pi +Type=oneshot + +ConditionPathExists=!/home/pi/.screenly/initialized + +ExecStart=/usr/bin/python /home/pi/screenly/start_resin_wifi.py +ExecStartPost=/usr/bin/touch /home/pi/.screenly/initialized + +[Install] +WantedBy=multi-user.target diff --git a/ansible/roles/network/tasks/main.yml b/ansible/roles/network/tasks/main.yml index 9f90b5f9..9db34ff1 100644 --- a/ansible/roles/network/tasks/main.yml +++ b/ansible/roles/network/tasks/main.yml @@ -96,3 +96,11 @@ - ui - resin-wifi-connect.tar.gz when: ansible_distribution_major_version|int >= 9 and not resin_wifi_version_file_exist + +- name: Copy wifi-connect systemd unit + copy: + src: "wifi-connect.service" + dest: "/etc/systemd/system/wifi-connect.service" + +- name: Enable wifi-connect systemd service + command: systemctl enable wifi-connect.service chdir=/etc/systemd/system diff --git a/ansible/roles/network/vars/main.yml b/ansible/roles/network/vars/main.yml index 1cc9fffe..80c35011 100644 --- a/ansible/roles/network/vars/main.yml +++ b/ansible/roles/network/vars/main.yml @@ -1 +1 @@ -resin_wifi_connect_version: 4.0.3 +resin_wifi_connect_version: 4.1.1 diff --git a/ansible/roles/nginx/files/nginx.conf b/ansible/roles/nginx/files/nginx.conf index a7ecd14d..2395b9c9 100644 --- a/ansible/roles/nginx/files/nginx.conf +++ b/ansible/roles/nginx/files/nginx.conf @@ -2,15 +2,39 @@ upstream ose { server 127.0.0.1:8080; } +upstream wifi-connect { + server 192.168.42.1:9090; +} + server { server_tokens off; listen 80 default_server; location / { + if (-f /home/pi/.screenly/initialized) { + proxy_pass http://ose; + } + if (!-f /home/pi/.screenly/initialized) { + proxy_pass http://wifi-connect; + } + client_max_body_size 4G; - proxy_pass http://ose; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; } + + location /static { + proxy_pass http://ose/static; + } + + location /hotspot { + allow 127.0.0.1; + deny all; + + if (!-f /home/pi/.screenly/initialized) { + root /tmp; + rewrite ^ /hotspot.html break; + } + } } diff --git a/ansible/roles/nginx/tasks/main.yml b/ansible/roles/nginx/tasks/main.yml index bcaf8c9a..dc2561c9 100644 --- a/ansible/roles/nginx/tasks/main.yml +++ b/ansible/roles/nginx/tasks/main.yml @@ -24,7 +24,6 @@ mode: 644 owner: root group: root - force: no - name: Modifies screenly-web service to only listen on localhost lineinfile: @@ -32,40 +31,3 @@ state: absent dest: /etc/systemd/system/screenly-web.service when: no_ssl - -# Disable Nginx -- name: Remove Nginx - apt: - name: nginx-light - state: absent - update_cache: yes - tags: - - disable-nginx - -- name: Remove nginx config - file: - path: /etc/nginx/sites-enabled/screenly.conf - state: absent - tags: - - disable-nginx - -- name: Turns off the ssl mode - replace: - replace: 'use_ssl = False' - regexp: '^.*use_ssl.*' - dest: /home/pi/.screenly/screenly.conf - tags: - - disable-nginx - -- name: Modifies screenly-web service to listen on default gateway - lineinfile: - insertafter: '^\[Service\]' - regexp: '^\[Service\]*;' - line: Environment=LISTEN=0.0.0.0 - dest: /etc/systemd/system/screenly-web.service - notify: - - reload systemctl - - restart-screenly-websocket_server_layer - - restart-screenly-server - tags: - - disable-nginx diff --git a/bin/disable_nginx.sh b/bin/disable_nginx.sh deleted file mode 100755 index 24ead8b5..00000000 --- a/bin/disable_nginx.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -ex - -cd ~/screenly/ansible -ansible-playbook -t disable-nginx site.yml - -set +x -echo "You should be all set. You should be able to access Screenly's management interface at http://:" diff --git a/bin/install.sh b/bin/install.sh index e1b277c2..314f4071 100755 --- a/bin/install.sh +++ b/bin/install.sh @@ -44,9 +44,9 @@ fi echo && read -p "Would you like to perform a full system upgrade as well? (y/N)" -n 1 -r -s UPGRADE && echo if [ "$UPGRADE" != 'y' ]; then - EXTRA_ARGS="--skip-tags enable-ssl,disable-nginx,system-upgrade" + EXTRA_ARGS="--skip-tags enable-ssl,system-upgrade" else - EXTRA_ARGS="--skip-tags enable-ssl,disable-nginx" + EXTRA_ARGS="--skip-tags enable-ssl" fi set -x diff --git a/server.py b/server.py index 69cc6a21..4cde0b82 100755 --- a/server.py +++ b/server.py @@ -640,6 +640,26 @@ class Recover(Resource): return "Recovery successful." +class ResetWifiConfig(Resource): + method_decorators = [api_response, auth_basic] + + @swagger.doc({ + 'responses': { + '204': { + 'description': 'Deleted' + } + } + }) + def get(self): + home = getenv('HOME') + file_path = path.join(home, '.screenly/initialized') + + if path.isfile(file_path): + remove(file_path) + + return '', 204 + + class Info(Resource): method_decorators = [api_response, auth_basic] @@ -703,6 +723,7 @@ api.add_resource(Backup, '/api/v1/backup') api.add_resource(Recover, '/api/v1/recover') api.add_resource(AssetsControl, '/api/v1/assets/control/') api.add_resource(Info, '/api/v1/info') +api.add_resource(ResetWifiConfig, '/api/v1/reset_wifi') try: my_ip = get_node_ip() @@ -844,22 +865,6 @@ def splash_page(): return template('splash_page.html', ip_lookup=ip_lookup, msg=msg) -@app.route('/hotspot') -def hotspot_page(): - if LISTEN == '127.0.0.1': - sh.sudo('nginx', '-s', 'stop') - - ssid = "ScreenlyOSE-{}".format(pwgen(4, symbols=False)) - ssid_password = pwgen(8, symbols=False) - - wifi_connect = sh.sudo('wifi-connect', '-s', ssid, '-p', ssid_password, _bg=True, _err_to_out=True) - - while 'Starting HTTP server' not in wifi_connect.process.stdout: - sleep(1) - - return template('hotspot.html', network=ssid, ssid_pswd=ssid_password, address='screenly.io/wifi') - - @app.errorhandler(403) def mistake403(code): return 'The parameter you passed has the wrong format!' diff --git a/settings.py b/settings.py index f52d26d7..e3218905 100644 --- a/settings.py +++ b/settings.py @@ -31,8 +31,7 @@ DEFAULTS = { 'default_duration': '10', 'default_streaming_duration': '300', 'debug_logging': False, - 'verify_ssl': True, - 'enable_offline_mode': False + 'verify_ssl': True }, 'auth': { 'user': '', diff --git a/start_resin_wifi.py b/start_resin_wifi.py new file mode 100644 index 00000000..210f82f8 --- /dev/null +++ b/start_resin_wifi.py @@ -0,0 +1,34 @@ +#!/usr/bin/python + +from jinja2 import Template +from netifaces import gateways +from os import getenv, path +from pwgen import pwgen +import sh + + +def generate_page(ssid, pswd, address): + home = getenv('HOME') + template_path = path.join(home, 'screenly/templates/hotspot.html') + with open(template_path) as f: + template = Template(f.read()) + + context = { + 'network': ssid, + 'ssid_pswd': pswd, + 'address': address + } + + with open('/tmp/hotspot.html', 'w') as out_file: + out_file.write(template.render(context=context)) + + +if __name__ == "__main__": + if not gateways().get('default'): + ssid = 'ScreenlyOSE-{}'.format(pwgen(4, symbols=False)) + ssid_password = pwgen(8, symbols=False) + generate_page(ssid, ssid_password, 'screenly.io/wifi') + + wifi_connect = sh.sudo('wifi-connect', '-s', ssid, '-p', ssid_password, '-o', '9090') + else: + pass diff --git a/static/js/backup.coffee b/static/js/settings.coffee similarity index 88% rename from static/js/backup.coffee rename to static/js/settings.coffee index 4a905be4..2bdd4e2a 100644 --- a/static/js/backup.coffee +++ b/static/js/settings.coffee @@ -64,3 +64,11 @@ $().ready -> $('.progress').hide() $('#btn-upload').show() $('#btn-backup').show() + + $('#btn-reset').click (e) -> + $.get '/api/v1/reset_wifi' + .done (e) -> + $('#request-error .alert').show() + $('#request-error .alert').addClass 'alert-success' + $('#request-error .alert').removeClass 'alert-error' + ($ '#request-error .msg').text 'Reset was successful. Please reboot the device.' diff --git a/static/js/backup.js b/static/js/settings.js similarity index 86% rename from static/js/backup.js rename to static/js/settings.js index 48dea7d5..53b453fd 100644 --- a/static/js/backup.js +++ b/static/js/settings.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.12.6 +// Generated by CoffeeScript 2.2.4 (function() { $().ready(function() { $('#request-error .close').click(function(e) { @@ -34,7 +34,7 @@ e.preventDefault(); return $('[name="backup_upload"]').click(); }); - return $('[name="backup_upload"]').fileupload({ + $('[name="backup_upload"]').fileupload({ url: "api/v1/recover", progressall: function(e, data) { var valuenow; @@ -76,6 +76,14 @@ return $('#btn-backup').show(); } }); + return $('#btn-reset').click(function(e) { + return $.get('/api/v1/reset_wifi').done(function(e) { + $('#request-error .alert').show(); + $('#request-error .alert').addClass('alert-success'); + $('#request-error .alert').removeClass('alert-error'); + return ($('#request-error .msg')).text('Reset was successful. Please reboot the device.'); + }); + }); }); }).call(this); diff --git a/templates/settings.html b/templates/settings.html index ef8526a4..6c12fcf4 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -17,7 +17,7 @@ - + @@ -119,24 +119,6 @@ -
- -
- -
-
-
@@ -209,15 +191,27 @@
+
+

+ Reset wifi config +

+

Run it, and if the next boot does not have a network connection, you will be prompted to select a WiFi network.
Warning: after pressing, a reboot is required. Web interface will not be available until reboot.

+
+ +
+
+

Backup

- - - -
diff --git a/viewer.py b/viewer.py index b158e4ba..8e36f958 100755 --- a/viewer.py +++ b/viewer.py @@ -34,6 +34,7 @@ __license__ = "Dual License: GPLv2 and Commercial License" SPLASH_DELAY = 60 # secs EMPTY_PL_DELAY = 5 # secs +INITIALIZED_FILE = '/.screenly/initialized' BLACK_PAGE = '/tmp/screenly_html/black_page.html' WATCHDOG_PATH = '/tmp/screenly.watchdog' SCREENLY_HTML = '/tmp/screenly_html/' @@ -426,14 +427,12 @@ def setup(): def main(): setup() - if not gateways().get('default') and not settings['enable_offline_mode']: - url = 'http://{0}:{1}/hotspot'.format(LISTEN, PORT) + if not path.isfile(HOME + INITIALIZED_FILE) and not gateways().get('default'): + url = 'http://{0}/hotspot'.format(LISTEN) load_browser(url=url) - while not gateways().get('default'): - sleep(2) - if LISTEN == '127.0.0.1': - sh.sudo('nginx') + while not path.isfile(HOME + INITIALIZED_FILE): + sleep(1) url = 'http://{0}:{1}/splash_page'.format(LISTEN, PORT) if settings['show_splash'] else 'file://' + BLACK_PAGE browser_url(url=url)