0
0
Fork 0
mirror of https://github.com/boltgolt/howdy.git synced 2024-09-19 09:51:19 +02:00
howdy/debian/postinst

155 lines
4.8 KiB
Text
Raw Normal View History

2018-11-09 12:43:10 +01:00
#!/usr/bin/python3
# Installation script to install howdy
2018-04-05 21:12:36 +02:00
# Executed after primary apt install
def col(id):
"""Add color escape sequences"""
if id == 1: return "\033[32m"
if id == 2: return "\033[33m"
if id == 3: return "\033[31m"
return "\033[0m"
# Import required modules
2018-02-05 23:43:32 +01:00
import subprocess
import time
import sys
import os
import re
2018-02-05 23:43:32 +01:00
import signal
import fileinput
import urllib.parse
# Don't run unless we need to configure the install
# Will also happen on upgrade but we will catch that later on
if "configure" not in sys.argv:
sys.exit(0)
2018-02-05 23:43:32 +01:00
def log(text):
"""Print a nicely formatted line to stdout"""
2018-04-13 01:00:36 +02:00
print("\n>>> " + col(1) + text + col(0) + "\n")
2018-02-05 23:43:32 +01:00
def handleStatus(status):
"""Abort if a command fails"""
2018-02-08 01:11:47 +01:00
if (status != 0):
print(col(3) + "Error while running last command" + col(0))
2018-04-05 21:12:36 +02:00
sys.exit(1)
2018-02-05 23:43:32 +01:00
2018-04-05 21:46:11 +02:00
# We're not in fresh configuration mode so don't continue the setup
if not os.path.exists("/tmp/howdy_picked_device"):
# Check if we have an older config we can restore
if len(sys.argv) > 2:
if os.path.exists("/tmp/howdy_config_backup_v" + sys.argv[2] + ".ini"):
# Get the config parser
import configparser
# Load th old and new config files
oldConf = configparser.ConfigParser()
oldConf.read("/tmp/howdy_config_backup_v" + sys.argv[2] + ".ini")
newConf = configparser.ConfigParser()
newConf.read("/lib/security/howdy/config.ini")
# Go through every setting in the old config and apply it to the new file
for section in oldConf.sections():
for (key, value) in oldConf.items(section):
2018-12-09 12:47:29 +01:00
# MIGRATION 2.3.1 -> 2.4.0
# If config is still using the old device_id parameter, convert it to a path
2018-11-09 20:02:30 +01:00
if key == "device_id":
key = "device_path"
value = "/dev/video" + value
2018-12-09 12:47:29 +01:00
# MIGRATION 2.4.0 -> 2.5.0
# Finally correct typo in "timout" config value
if key == "timout":
key = "timeout"
try:
newConf.set(section, key, value)
# Add a new section where needed
except configparser.NoSectionError as e:
newConf.add_section(section)
newConf.set(section, key, value)
# Write it all to file
with open("/lib/security/howdy/config.ini", "w") as configfile:
newConf.write(configfile)
sys.exit(0)
2018-04-05 21:12:36 +02:00
# Open the temporary file containing the device ID
in_file = open("/tmp/howdy_picked_device", "r")
# Load it in, it should be a string
2018-04-12 23:45:52 +02:00
picked = in_file.read()
in_file.close()
2018-04-05 21:12:36 +02:00
# Remove the temporary file
subprocess.call(["rm /tmp/howdy_picked_device"], shell=True)
2018-04-05 21:12:36 +02:00
2018-04-12 23:45:52 +02:00
log("Upgrading pip to the latest version")
# Update pip
handleStatus(subprocess.call(["pip3 install --upgrade pip"], shell=True))
2018-02-05 23:43:32 +01:00
log("Cloning dlib")
# Clone the dlib git to /tmp, but only the last commit
handleStatus(subprocess.call(["git", "clone", "--depth", "1", "https://github.com/davisking/dlib.git", "/tmp/dlib_clone"]))
log("Building dlib")
# Start the build without GPU
handleStatus(subprocess.call(["cd /tmp/dlib_clone/; python3 setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA"], shell=True))
log("Cleaning up dlib")
# Remove the no longer needed git clone
handleStatus(subprocess.call(["rm", "-rf", "/tmp/dlib_clone"]))
print("Temporary dlib files removed")
2018-04-12 23:45:52 +02:00
log("Installing python dependencies")
2018-11-09 19:39:16 +01:00
# Install direct dependencies so pip does not freak out with the manual dlib install
handleStatus(subprocess.call(["pip3", "install", "--cache-dir", "/tmp/pip_howdy", "face_recognition_models==0.3.0", "Click>=6.0", "numpy", "Pillow"]))
2018-04-12 23:45:52 +02:00
2018-02-05 23:43:32 +01:00
log("Installing face_recognition")
# Install face_recognition though pip
handleStatus(subprocess.call(["pip3", "install", "--cache-dir", "/tmp/pip_howdy", "--no-deps", "face_recognition==1.2.2"]))
try:
import cv2
except Exception as e:
log("Reinstalling opencv2")
handleStatus(subprocess.call(["pip3", "install", "opencv-python"]))
2018-02-05 23:43:32 +01:00
2018-04-07 19:15:00 +02:00
log("Configuring howdy")
2018-02-05 23:43:32 +01:00
# Manually change the camera id to the one picked
2018-02-05 23:43:32 +01:00
for line in fileinput.input(["/lib/security/howdy/config.ini"], inplace = 1):
2018-11-09 19:39:16 +01:00
print(line.replace("device_path = none", "device_path = " + picked), end="")
2018-11-09 16:33:19 +01:00
print("Camera ID saved")
2018-02-05 23:43:32 +01:00
2018-02-10 17:03:42 +01:00
# Secure the howdy folder
handleStatus(subprocess.call(["chmod 744 -R /lib/security/howdy/"], shell=True))
2018-02-10 17:03:42 +01:00
# Allow anyone to execute the python CLI
handleStatus(subprocess.call(["chmod 755 /lib/security/howdy"], shell=True))
handleStatus(subprocess.call(["chmod 755 /lib/security/howdy/cli.py"], shell=True))
handleStatus(subprocess.call(["chmod 755 -R /lib/security/howdy/cli"], shell=True))
2018-04-07 19:15:00 +02:00
print("Permissions set")
# Make the CLI executable as howdy
handleStatus(subprocess.call(["ln -s /lib/security/howdy/cli.py /usr/local/bin/howdy"], shell=True))
handleStatus(subprocess.call(["chmod +x /usr/local/bin/howdy"], shell=True))
print("Howdy command installed")
2018-02-10 17:03:42 +01:00
2018-02-05 23:43:32 +01:00
log("Adding howdy as PAM module")
# Activate the pam-config file
handleStatus(subprocess.call(["pam-auth-update --package"], shell=True))
2018-02-05 23:43:32 +01:00
# Sign off
2018-02-05 23:43:32 +01:00
print("Installation complete.")