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

162 lines
4.5 KiB
Python
Raw Normal View History

2018-02-05 23:43:32 +01:00
import subprocess
import time
import sys
import os
import signal
import fileinput
import urllib.parse
def log(text):
2018-02-08 01:11:47 +01:00
print("\n>>> \033[32m" + text + "\033[0m\n")
2018-02-05 23:43:32 +01:00
def handleStatus(status):
2018-02-08 01:11:47 +01:00
if (status != 0):
print("\033[31mError while running last command\033[0m")
sys.exit()
2018-02-05 23:43:32 +01:00
user = os.getenv("SUDO_USER")
if user is None:
2018-02-08 01:11:47 +01:00
print("Please run this script as a sudo user")
sys.exit()
2018-02-05 23:43:32 +01:00
print("\n\033[33m HOWDY INSTALLER FOR UBUNTU\033[0m")
print(" Version 1, 2016/02/05\n")
time.sleep(.5)
log("Installing required apt packages")
handleStatus(subprocess.call(["apt", "install", "-y", "libpam-python", "fswebcam", "libopencv-dev", "python-opencv"]))
log("Starting camera check")
devices = os.listdir("/dev")
picked = False
for dev in devices:
2018-02-08 01:11:47 +01:00
if (dev[:5] == "video"):
time.sleep(.5)
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
print("Trying /dev/" + dev)
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
sub = subprocess.Popen(["fswebcam -S 9999999999 -d /dev/" + dev + " /dev/null 2>/dev/null"], shell=True, preexec_fn=os.setsid)
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
print("\033[33mOne of your cameras should now be on.\033[0m")
ans = input("Did your IR emitters turn on? [y/N]: ")
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
os.killpg(os.getpgid(sub.pid), signal.SIGTERM)
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
if (ans.lower() == "y"):
picked = dev[5:]
break
else:
print("Inerpeting as a \"NO\"\n")
2018-02-05 23:43:32 +01:00
if (picked == False):
2018-02-08 01:11:47 +01:00
print("\033[31mNo suitable IR camera found\033[0m")
2018-02-05 23:43:32 +01:00
log("Cloning dlib")
handleStatus(subprocess.call(["git", "clone", "https://github.com/davisking/dlib.git", "/tmp/dlib_clone"]))
log("Building dlib")
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")
handleStatus(subprocess.call(["rm", "-rf", "/tmp/dlib_clone"]))
log("Installing face_recognition")
handleStatus(subprocess.call(["pip3", "install", "face_recognition"]))
log("Cloning howdy")
if not os.path.exists("/lib/security"):
2018-02-08 01:11:47 +01:00
os.makedirs("/lib/security")
2018-02-05 23:43:32 +01:00
handleStatus(subprocess.call(["git", "clone", "https://github.com/Boltgolt/howdy.git", "/lib/security/howdy"]))
for line in fileinput.input(["/lib/security/howdy/config.ini"], inplace = 1):
2018-02-08 01:11:47 +01:00
print(line.replace("device_id = 1", "device_id = " + picked), end="")
2018-02-05 23:43:32 +01:00
2018-02-10 17:03:42 +01:00
# Secure the howdy folder
2018-02-05 23:43:32 +01:00
handleStatus(subprocess.call(["chmod 600 -R /lib/security/howdy/"], shell=True))
2018-02-10 17:03:42 +01:00
# Make the CLI executable as howdy
2018-02-08 01:11:47 +01:00
handleStatus(subprocess.call(["ln -s /lib/security/howdy/cli.py /usr/bin/howdy"], shell=True))
handleStatus(subprocess.call(["chmod +x /usr/bin/howdy"], shell=True))
2018-02-05 23:43:32 +01:00
2018-02-10 17:03:42 +01:00
# Install the command autocomplete
handleStatus(subprocess.call(["sudo cp /lib/security/howdy/autocomplete.sh /etc/bash_completion.d/howdy"], shell=True))
2018-02-05 23:43:32 +01:00
log("Adding howdy as PAM module")
outlines = []
printlines = []
inserted = False
with open("/etc/pam.d/common-auth") as fp:
2018-02-08 01:11:47 +01:00
line = fp.readline()
cnt = 1
while line:
outlines.append(line)
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
if line[:1] != "#":
printlines.append(line)
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
if not inserted:
line_comment = "# Howdy IR face recognition\n"
line_Link = "auth sufficient pam_python.so /lib/security/howdy/pam.py\n\n"
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
outlines.append(line_comment)
outlines.append(line_Link)
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
printlines.append("\033[33m" + line_comment + "\033[0m")
printlines.append("\033[33m" + line_Link + "\033[0m")
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
inserted = True
else:
printlines.append("\033[37m" + line + "\033[0m")
2018-02-05 23:43:32 +01:00
2018-02-08 01:11:47 +01:00
line = fp.readline()
cnt += 1
2018-02-05 23:43:32 +01:00
print("\033[33m" + ">>> START OF /etc/pam.d/common-auth" + "\033[0m")
for line in printlines:
2018-02-08 01:11:47 +01:00
print(line, end="")
2018-02-05 23:43:32 +01:00
print("\033[33m" + ">>> END OF /etc/pam.d/common-auth" + "\033[0m" + "\n")
print("Lines will be insterted in /etc/pam.d/common-auth as shown above")
ans = input("Apply this change? [y/N]: ")
if (ans.lower() != "y"):
2018-02-08 01:11:47 +01:00
print("Inerpeting as a \"NO\", aborting")
sys.exit()
2018-02-05 23:43:32 +01:00
print("Adding lines to PAM\n")
common_auth = open("/etc/pam.d/common-auth", "w")
common_auth.write("".join(outlines))
common_auth.close()
diag_out = ""
diag_out += "Video devices (IR:" + picked + ")\n"
diag_out += "```\n"
diag_out += subprocess.check_output(['ls /dev/ | grep video'], shell=True).decode("utf-8")
diag_out += "```\n"
diag_out += "Lsusb output\n"
diag_out += "```\n"
diag_out += subprocess.check_output(['lsusb -vvvv | grep -i "Camera\|iFunction"'], shell=True).decode("utf-8")
diag_out += "```"
print("https://github.com/Boltgolt/howdy/issues/new?title=%5Bdiag%5D%20Post-installation%20camera%20information&body=" + urllib.parse.quote_plus(diag_out) + "\n")
print("Installation complete.")
print("If you want to help the development, please use the link above to post some camera-related information as a new github issue")