From 6f92b503cb7ac9b618b289371bfecacc0fbb1827 Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Mon, 23 Jun 2014 21:52:15 +0300 Subject: [PATCH] initial work on smb shares --- doc/requirements.txt | 1 + doc/todo.txt | 4 ++++ src/config.py | 21 ++++++++++++++++++--- src/handlers.py | 2 -- src/smbctl.py | 44 ++++++++++++++++++++++++++++++-------------- 5 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 doc/todo.txt diff --git a/doc/requirements.txt b/doc/requirements.txt index bea5c22..9a71525 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -6,3 +6,4 @@ pil ffmpeg motion v4l-utils +cifs-utils diff --git a/doc/todo.txt b/doc/todo.txt new file mode 100644 index 0000000..0c770cf --- /dev/null +++ b/doc/todo.txt @@ -0,0 +1,4 @@ +-> in continuare o camera remote moarta blocheaza intreg sistemul la load +-> use minimum_frame_time +-> implement custom camera frame sizes +-> add a separate full-screen camera link diff --git a/src/config.py b/src/config.py index b4ea82e..a5fa4cd 100644 --- a/src/config.py +++ b/src/config.py @@ -24,6 +24,7 @@ from collections import OrderedDict import motionctl import settings +import smbctl import update import utils import v4l2ctl @@ -483,7 +484,6 @@ def camera_ui_to_dict(ui): '@network_share_name': ui['network_share_name'], '@network_username': ui['network_username'], '@network_password': ui['network_password'], - 'target_dir': ui['root_directory'], # text overlay 'text_left': '', @@ -560,6 +560,15 @@ def camera_ui_to_dict(ui): else: data['hue'] = max(1, int(round(int(ui['hue']) * 2.55))) + if ui['storage_device'] == 'network-share': + mount_point = smbctl.make_mount_point(ui['network_server'], ui['network_share_name'], ui['network_username']) + if ui['root_directory'].startswith('/'): + ui['root_directory'] = ui['root_directory'][1:] + data['target_dir'] = os.path.join(mount_point, ui['root_directory']) + + else: + data['target_dir'] = ui['root_directory'] + if ui['text_overlay']: left_text = ui['left_text'] if left_text == 'camera-name': @@ -617,7 +626,7 @@ def camera_ui_to_dict(ui): ui['sunday_from'] + '-' + ui['sunday_to']) return data - + def camera_dict_to_ui(data): usage = utils.get_disk_usage(data['target_dir']) @@ -654,7 +663,6 @@ def camera_dict_to_ui(data): 'network_share_name': data['@network_share_name'], 'network_username': data['@network_username'], 'network_password': data['@network_password'], - 'root_directory': data['target_dir'], 'disk_used': disk_used, 'disk_total': disk_total, @@ -745,6 +753,13 @@ def camera_dict_to_ui(data): else: ui['hue'] = 50 + + if data['@storage_device'] == 'network-share': + mount_point = smbctl.make_mount_point(data['@network_server'], data['@network_share_name'], data['@network_username']) + ui['root_directory'] = data['target_dir'][len(mount_point):] + + else: + ui['root_directory'] = data['target_dir'] text_left = data['text_left'] text_right = data['text_right'] diff --git a/src/handlers.py b/src/handlers.py index ffa4f3b..c82252d 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -74,11 +74,9 @@ class BaseHandler(RequestHandler): main_config = config.get_main() if user == main_config.get('@admin_username') and pwd == main_config.get('@admin_password'): - return 'admin' elif user == main_config.get('@normal_username') and pwd == main_config.get('@normal_password'): - return 'normal' else: diff --git a/src/smbctl.py b/src/smbctl.py index 166c427..009f69f 100644 --- a/src/smbctl.py +++ b/src/smbctl.py @@ -19,6 +19,7 @@ import logging import os import re import subprocess +import time def find_mount_cifs(): @@ -29,7 +30,7 @@ def find_mount_cifs(): return None -def _make_mount_point(server, share, username): +def make_mount_point(server, share, username): server = re.sub('[^a-zA-Z0-9]', '_', server).lower() share = re.sub('[^a-zA-Z0-9]', '_', share).lower() @@ -39,12 +40,13 @@ def _make_mount_point(server, share, username): else: mount_point = '/media/motioneye_%s_%s' % (server, share) - logging.debug('making sure mount point "%s" exists' % mount_point) - os.makedirs(mount_point) - return mount_point +def _is_motioneye_mount(mount_point): + return bool(re.match('^/media/motioneye_\w+$', mount_point)) + + def list_mounts(): logging.debug('listing smb mounts...') @@ -67,6 +69,9 @@ def list_mounts(): if fstype != 'cifs': continue + if not _is_motioneye_mount(mount_point): + continue + match = re.match('//([^/]+)/(.+)', target) if not match: continue @@ -82,7 +87,7 @@ def list_mounts(): else: username = None - + logging.debug('found smb mount "//%s/%s" at "%s"' % (server, share, mount_point)) mounts.append({ @@ -91,18 +96,17 @@ def list_mounts(): 'username': username, 'mount_point': mount_point }) - - return mounts - -def is_motioneye_mount(mount_point): - return bool(re.match('^/media/motioneye_\w+$', mount_point)) + return mounts def mount(server, share, username, password): - mount_point = _make_mount_point(server, share, username) + mount_point = make_mount_point(server, share, username) logging.debug('mounting "//%s/%s" at "%s"' % (server, share, mount_point)) + logging.debug('making sure mount point "%s" exists' % mount_point) + os.makedirs(mount_point) + if username: opts = 'username=%s,password=%s' % (username, password) @@ -112,16 +116,28 @@ def mount(server, share, username, password): try: subprocess.check_call('mount.cifs //%s/%s %s -o %s' % (server, share, mount_point, opts), shell=True) - return mount_point - except subprocess.CalledProcessError: logging.error('failed to mount smb share "//%s/%s" at "%s"' % (server, share, mount_point)) return False + + # test to see if mount point is writable + try: + path = os.path.join(mount_point, '.motioneye_' + str(int(time.time()))) + os.mkdir(path) + os.rmdir(path) + logging.debug('directory at "%s" is writable' % mount_point) + + except: + logging.error('directory at "%s" is not writable' % mount_point) + + return False + + return mount_point def umount(server, share, username): - mount_point = _make_mount_point(server, share, username) + mount_point = make_mount_point(server, share, username) logging.debug('unmounting "//%s/%s" from "%s"' % (server, share, mount_point)) try: -- 2.39.5