forked from tiny-pilot/tinypilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick-install
executable file
·179 lines (157 loc) · 5.65 KB
/
quick-install
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
# Exit on first error.
set -e
# Echo commands to stdout.
set -x
#######################################
# Adds a setting to the YAML settings file, if it's not yet defined.
# Globals:
# TINYPILOT_SETTINGS_FILE, a path.
# Arguments:
# Key to define.
# Value to set.
# Outputs:
# The line appended to the settings file, if the variable wasn't yet defined.
#######################################
add_setting_if_undefined() {
local key="$1"
local value="$2"
if ! grep --silent "^${key}:" "${TINYPILOT_SETTINGS_FILE}"; then
echo "${key}: ${value}" | tee --append "${TINYPILOT_SETTINGS_FILE}"
fi
}
readonly DEFAULT_TINYPILOT_SETTINGS_FILE="/home/tinypilot/settings.yml"
if [[ -n "${TINYPILOT_INSTALL_VARS}" ]]; then
echo "TINYPILOT_INSTALL_VARS is no longer supported." >&2
echo "Please specify extra settings via the ${DEFAULT_TINYPILOT_SETTINGS_FILE} file." >&2
exit 255
fi
# Treat undefined environment variables as errors.
set -u
# Create a temporary settings file that will be used throughout this script to
# avoid repeatedly using sudo.
# HACK: If we let mktemp use the default /tmp directory, the system purges the file
# before the end of the script for some reason. We use /var/tmp as a workaround.
readonly TINYPILOT_SETTINGS_FILE="$(mktemp --tmpdir="/var/tmp" --suffix ".yml")"
# Check if there's already a settings file with extra installation settings.
if [[ -f "${DEFAULT_TINYPILOT_SETTINGS_FILE}" ]]; then
echo "Using settings file at: ${DEFAULT_TINYPILOT_SETTINGS_FILE}"
sudo cp "${DEFAULT_TINYPILOT_SETTINGS_FILE}" "${TINYPILOT_SETTINGS_FILE}"
else
echo "No pre-existing settings file found at: ${DEFAULT_TINYPILOT_SETTINGS_FILE}"
fi
readonly EXTRA_VARS_PATH="@${TINYPILOT_SETTINGS_FILE}"
# Set default installation settings
add_setting_if_undefined "ustreamer_port" "8001"
# Check if this system uses the TC358743 HDMI to CSI capture bridge.
USE_TC358743_DEFAULTS=''
if grep --silent "^ustreamer_capture_device:" "${TINYPILOT_SETTINGS_FILE}"; then
if grep --silent "^ustreamer_capture_device: tc358743$" "${TINYPILOT_SETTINGS_FILE}"; then
USE_TC358743_DEFAULTS='y'
fi
# Only check the existing config file if user has not set
# ustreamer_capture_device install variable.
elif [ -f /home/ustreamer/config.yml ] && grep --silent 'capture_device: "tc358743"' /home/ustreamer/config.yml; then
USE_TC358743_DEFAULTS='y'
fi
# If this system does not use a TC358743 capture chip, set defaults for any
# unset install variables.
if [ -z "$USE_TC358743_DEFAULTS" ]; then
add_setting_if_undefined "ustreamer_encoder" "hw"
add_setting_if_undefined "ustreamer_format" "jpeg"
add_setting_if_undefined "ustreamer_resolution" "1920x1080"
add_setting_if_undefined "ustreamer_persistent" "true"
add_setting_if_undefined "ustreamer_desired_fps" "30"
fi
echo "Final install settings:"
cat "${TINYPILOT_SETTINGS_FILE}"
# Check if the user is accidentally downgrading from TinyPilot Pro.
HAS_PRO_INSTALLED=0
SCRIPT_DIR="$(dirname "$0")"
# If they're piping this script in from stdin, guess that TinyPilot is
# in the default location.
if [ "$SCRIPT_DIR" = "." ]; then
SCRIPT_DIR="/opt/tinypilot"
fi
# Detect TinyPilot Pro if the README file has a TinyPilot Pro header.
TINYPILOT_README="${SCRIPT_DIR}/README.md"
if [ -f "$TINYPILOT_README" ]; then
if [ "$(head -n 1 $TINYPILOT_README)" = "# TinyPilot Pro" ]; then
HAS_PRO_INSTALLED=1
fi
fi
if [ "$HAS_PRO_INSTALLED" = 1 ]; then
set +u # Don't exit if FORCE_DOWNGRADE is unset.
if [ "$FORCE_DOWNGRADE" = 1 ]; then
echo "Downgrading from TinyPilot Pro to TinyPilot Community Edition"
set -u
else
set +x
printf "You are trying to downgrade from TinyPilot Pro to TinyPilot "
printf "Community Edition.\n\n"
printf "You probably want to update to the latest version of TinyPilot "
printf "Pro instead:\n\n"
printf " /opt/tinypilot/scripts/upgrade && sudo reboot\n"
printf "\n"
printf "If you *really* want to downgrade to TinyPilot Community Edition, "
printf "type the following:\n\n"
printf " export FORCE_DOWNGRADE=1\n\n"
printf "And then run your previous command again.\n"
exit 255
fi
fi
sudo apt-get update
sudo apt-get install -y \
git \
libffi-dev \
libssl-dev \
python3-dev \
python3-venv
INSTALLER_DIR="/opt/tinypilot-updater"
sudo mkdir -p "$INSTALLER_DIR"
sudo chown "$(whoami):$(whoami)" --recursive "$INSTALLER_DIR"
pushd "$INSTALLER_DIR"
python3 -m venv venv
. venv/bin/activate
# For some reason, wheel has to be installed before anything else.
pip install wheel==0.34.2
echo 'ansible==2.9.10
cffi==1.14.4
cryptography==3.3.1
Jinja2==2.11.2
MarkupSafe==1.1.1
pkg-resources==0.0.0
pycparser==2.20
pyOpenSSL==20.0.1
PyYAML==5.3.1
six==1.15.0' > requirements.txt
pip install -r requirements.txt
echo "[defaults]
roles_path = $PWD
interpreter_python = /usr/bin/python3
" > ansible.cfg
TINYPILOT_ROLE_NAME="mtlynch.tinypilot"
if [ -d "$TINYPILOT_ROLE_NAME" ]; then
pushd "$TINYPILOT_ROLE_NAME"
git pull origin master
popd
else
TINYPILOT_ROLE_REPO="https://github.com/mtlynch/ansible-role-tinypilot.git"
git clone "$TINYPILOT_ROLE_REPO" "$TINYPILOT_ROLE_NAME"
fi
ansible-galaxy install \
--force \
--role-file "${TINYPILOT_ROLE_NAME}/requirements.yml"
echo "- hosts: localhost
connection: local
become: true
become_method: sudo
roles:
- role: $TINYPILOT_ROLE_NAME" > install.yml
ansible-playbook -i localhost, install.yml \
--extra-vars "${EXTRA_VARS_PATH}"
# Copy the final install settings used in this installation back to the default
# settings location.
chmod +r "${TINYPILOT_SETTINGS_FILE}"
sudo cp "${TINYPILOT_SETTINGS_FILE}" "${DEFAULT_TINYPILOT_SETTINGS_FILE}"
sudo chown tinypilot:tinypilot "${DEFAULT_TINYPILOT_SETTINGS_FILE}"