forked from lanikai/alohacam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·115 lines (100 loc) · 3.21 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash -e
# Check for dependencies.
hash curl grep openssl
DASHBOARD_URL=https://alohacam.io
API_URL=https://api.alohacam.io
DOWNLOAD_URL=http://get.alohacam.io
VERSION=latest
KEYFILE=key.pem
CERTFILE=cert.pem
CSRFILE=req.pem
REBOOT_REQUIRED=false
[ -e lanikai.env ] && source lanikai.env
# Identify platform and architecture.
if [ -z "$PLAT" ]; then
case "$OSTYPE" in
linux*)
PLAT=linux ;;
*)
echo "Unsupported platform: $OSTYPE"
exit 1 ;;
esac
fi
if [ -z "$ARCH" ]; then
case "$(uname -m)" in
armv6*)
ARCH=armv6 ;;
armv7*)
ARCH=armv7 ;;
aarch64*)
ARCH=aarch64 ;;
*)
echo "Unsupported architecture: $(uname -m)"
exit 1 ;;
esac
fi
# Download latest alohacam binary.
if [ -z "$SKIP_DOWNLOAD" ]; then
curl -L -o alohacam $DOWNLOAD_URL/release/$VERSION/alohacam-$PLAT-$ARCH
chmod a+x alohacam
fi
# Install rng-tools to ensure have sufficient entropy for certificates
if [ ! -e /usr/sbin/rngd ]; then
echo "Installing rng-tools to ensure sufficient entropy for generating"
echo "certificates (requires sudo)."
sudo apt-get install -y rng-tools
fi
# Generate a private key, if we don't already have one.
if [ ! -e $KEYFILE ]; then
echo "Generating private key"
openssl ecparam -name prime256v1 -genkey -out $KEYFILE
rm -f $CERTFILE
fi
# Request a certificate, if we don't already have one.
if [ ! -e $CERTFILE ]; then
if [ -z "$TOKEN" ]; then
echo "Visit the Alohacam dashboard to obtain an activation token for this device:"
echo " $DASHBOARD_URL/devices/"
while [ -z "$TOKEN" ]; do
read -r -p "Enter activation token: "
TOKEN=$(echo "$REPLY" | tr -cd '0-9A-Za-z')
done
fi
# Generate a certificate signing request. The activation token is included as
# the Subject Common Name.
openssl req -new -key $KEYFILE -subj "/CN=Activation:$TOKEN" -out $CSRFILE
# Submit CSR with activation request. If successful, we'll receive a PEM-encoded
# certificate in response.
if curl -fsSL -F "csr=<$CSRFILE" -o $CERTFILE "$API_URL/activate-device/$TOKEN"; then
echo "Success. You may now run Alohacam."
rm -f $CSRFILE
else
echo "Unable to activate your device."
echo "Try requesting a new activation code."
fi
fi
# Load v4l2 driver on boot (if not already enabled).
if ! grep -q "^bcm2835-v4l2$" /etc/modules; then
echo "Modifying /etc/modules to enable camera driver (requires sudo)"
sudo sh -c 'echo "bcm2835-v4l2" >> /etc/modules'
fi
if ! lsmod | grep -q "^bcm2835_v4l2"; then
echo "Loading camera driver (requires sudo)"
sudo modprobe bcm2835-v4l2
fi
# Ensure Raspberry Pi bootloader configured to enable camera.
if ! grep -q "^start_x=1$" /boot/config.txt; then
echo "Modifying /boot/config.txt to enable camera (requires sudo)"
sudo sh -c 'echo "start_x=1" >> /boot/config.txt'
REBOOT_REQUIRED=true
fi
# Reboot prompt.
if $REBOOT_REQUIRED; then
echo "Some changes require you to reboot your Raspberry Pi."
read -p "Reboot now (y/N)? " yesno
case $yesno in
[Yy]* ) sudo reboot;
esac
else
./alohacam
fi