Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FreeBSD support #1616

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions extra/motioneye.rc-bsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/sh

# $FreeBSD$
#
# PROVIDE: motioneye
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following line(s) to /etc/rc.conf to enable motioneye:
#
# motioneye_enable="YES"
# # optional
# motioneye_flags="-l startserver -c $motioneye_config"
# motioneye_user="root"
# motioneye_config="/usr/local/etc/motioneye/motioneye.conf"

. /etc/rc.subr

name=motioneye
rcvar=motioneye_enable
load_rc_config $name

# Set defaults
motioneye_enable=${motioneye_enable:-"NO"}
motioneye_config=${motioneye_config:-"/usr/local/etc/motioneye/motioneye.conf"}
motioneye_flags=${motioneye_flags:-"startserver -l -c $motioneye_config"}
motioneye_user=${motioneye_user:-"root"}

pidfile=/var/run/motioneye.pid
command_interpreter=/usr/local/bin/python2.7
command=/usr/local/bin/meyectl

start_cmd=motioneye_start
stop_postcmd=motioneye_cleanup

motioneye_start() {
echo "Starting motioneye."
/usr/sbin/daemon -cf -p ${pidfile} -u ${motioneye_user} ${command} ${motioneye_flags}
}

motioneye_cleanup() {
[ -f ${pidfile} ] && rm ${pidfile}
}

run_rc_command "$1"

56 changes: 53 additions & 3 deletions motioneye/diskctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,48 @@
import subprocess
import utils


def _list_mounts():
if os.path.exists('/proc/mounts'):
return _list_mounts_proc_mounts()
else: # Use mount command
return _list_mounts_mount()

def _list_mounts_mount():
try:
output = subprocess.check_output(['mount'], stderr=utils.DEV_NULL)

except Exception as e:
logging.error('failed to list mounts using "mount": %s' % e, exc_info=True)

return []

mounts = []
# Line should be something like (on FreeBSD):
# target on mountpoint (fstype, option1, option2...)
# On Linux it would look different, but linux has /proc/mounts so we should not get here
# So regex out the target, mountpoint, fstype and whatever options
regex = re.compile('(\S+)\s+on\s+(\S+)\s+\((\w+),\s+([\S\s]+).*\)')
for line in output.splitlines():
line = line.strip()
if not line:
continue
parts = regex.split(line)

target = parts[1]
mount_point = parts[2]
fstype = parts[3]
opts = parts[4]

mounts.append({
'target': target,
'mount_point': mount_point,
'fstype': fstype,
'opts': opts
})
return mounts


def _list_mounts_proc_mounts():
logging.debug('listing mounts...')

seen_targets = set()
Expand Down Expand Up @@ -68,10 +108,20 @@ def _list_mounts():
def _list_disks():
if os.path.exists('/dev/disk/by-id/'):
return _list_disks_dev_by_id()

else: # fall back to fdisk -l
if os.path.exists('/proc/mounts'):
return _list_disks_fdisk()
else: # fall back to gpart command - Linux should never get here
return _list_disks_gpart()

def _list_disks_gpart():
try:
output = subprocess.check_output(['gpart', 'status'], stderr=utils.DEV_NULL)
except Exception as e:
logging.error('failed to list disks using "gpart status": %s' % e, exc_info=True)

disks = []

return disks
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here is something wrong since you return an empty array? Isn't it intended to return output and make it an empty array only on except?


def _list_disks_dev_by_id():
logging.debug('listing disks using /dev/disk/by-id/')
Expand Down
4 changes: 3 additions & 1 deletion motioneye/scripts/relayevent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ filename="$4"

uri="/_relay_event/?_username=$username&event=$event&motion_camera_id=$motion_camera_id"
data="{\"filename\": \"$filename\"}"
signature=$(echo -n "POST:$uri:$data:$password" | sha1sum | cut -d ' ' -f 1)
# allow for sha1 usage as well as sha1sum
sha1=$(which sha1sum sha1)
Copy link
Member

@MichaIng MichaIng Mar 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problematic if both commands exist. Also, please use command -v:

Suggested change
sha1=$(which sha1sum sha1)
sha1=$(command -v sha1sum sha1 | head -1)

signature=$(echo -n "POST:$uri:$data:$password" | $sha1 | cut -d ' ' -f 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To assure compatibility with any kind of shell:

Suggested change
signature=$(echo -n "POST:$uri:$data:$password" | $sha1 | cut -d ' ' -f 1)
signature=$(printf '%s' "POST:$uri:$data:$password" | "$sha1" | cut -d ' ' -f 1)


curl -s -S -m $timeout -H "Content-Type: application/json" -X POST "http://127.0.0.1:$port$uri&_signature=$signature" -d "$data" >/dev/null

24 changes: 15 additions & 9 deletions motioneye/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,23 @@ def get_os_version():


def _get_os_version_lsb_release():
try:
output = subprocess.check_output('lsb_release -sri', shell=True)
lines = output.strip().split()
name, version = lines
if version.lower() == 'rolling':
version = ''
# check if it exists before calling to avoid log spam
rc = subprocess.call(['which', 'lsb_release'])

if rc == 0:
try:
output = subprocess.check_output('lsb_release -sri', shell=True)
lines = output.strip().split()
name, version = lines
if version.lower() == 'rolling':
version = ''

return name, version
return name, version

except:
return _get_os_version_uname()
except:
return _get_os_version_uname()
else:
return _get_os_version_uname()
Comment on lines +37 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong indentation? Simpler may be:

    # check if it exists before calling to avoid log spam
    if subprocess.call(['which', 'lsb_release']):
        try:
            output = subprocess.check_output('lsb_release -sri', shell=True)
            lines = output.strip().split()
            name, version = lines
            if version.lower() == 'rolling':
                version = ''

            return name, version

        except:
            pass

    return _get_os_version_uname()

Copy link
Member

@cclauss cclauss Mar 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please lose the bare except as discussed in PEP8 and
https://realpython.com/the-most-diabolical-python-antipattern

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also see annotations on this in CodeFactor. What is the exception type in case of a failing subprocess.check_output?

Copy link
Member

@MichaIng MichaIng Mar 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subprocess.CalledProcessError it seems. So:

except subprocess.CalledProcessError as e:
    logging.warning(f'Running lsb_release failed with: {e.output}')

Or something like that?



def _get_os_version_uname():
Expand Down