-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap-install.sh
136 lines (114 loc) · 3.82 KB
/
bootstrap-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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bash
set -euo pipefail
# tput is used to color shell messages
# Not all systems have it, and if they don't, we can safely noop it
type tput >/dev/null 2>&1 || alias tput=:
log_info() {
echo "$(tput setaf 6)INFO $1 $(tput sgr0)"
}
log_warn() {
echo "$(tput setaf 3)WARN $1 $(tput sgr0)"
}
log_error() {
echo "$(tput setaf 1)ERROR $1 $(tput sgr0)"
}
flush_stdin() {
while read -ret 0.1; do :; done
}
# Args:
# - checked command
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# Args:
# - target directory
is_brewblox_dir() {
[[ -f "$1/.env" ]] && grep -q "BREWBLOX_RELEASE" "$1/.env"
return
}
install() {
# We should not be creating config files with root permissions
if [[ $EUID -eq 0 ]]; then
log_error "This script must not be run as root, or using sudo."
exit 1
fi
# Use BREWBLOX_DIR from env if set, otherwise find out
if [ -z "${BREWBLOX_DIR:-}" ]; then
if is_brewblox_dir "${PWD}"; then
BREWBLOX_DIR="${PWD}"
else
BREWBLOX_DIR="${PWD}/brewblox"
fi
fi
# Determine BREWBLOX_RELEASE
# Priority is given to:
# - Environment variable
# - Value for previous install
# - Default
if [ -z "${BREWBLOX_RELEASE:-}" ]; then
if is_brewblox_dir "${BREWBLOX_DIR}"; then
BREWBLOX_RELEASE=$(grep BREWBLOX_RELEASE "${BREWBLOX_DIR}/.env" | cut -d '=' -f2)
else
BREWBLOX_RELEASE="edge"
fi
fi
# Install system packages
if command_exists "apt-get"; then
log_info "Installing Apt packages..."
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y python3-pip python3-venv
else
# We also list packages installed in brewblox-ctl install
# This is a duplication of the list in brewblox_ctl.const
log_warn "apt-get not found. You may need to manually install system packages:"
log_warn ""
log_warn "python3-pip python3-venv curl libssl-dev libffi-dev avahi-daemon"
log_warn ""
fi
if ! command_exists python3; then
log_error "Python3 not found."
log_error "Install Python >=3.6 manually, or add the existing installation to PATH."
exit 1
fi
log_info "Brewblox dir is \"${BREWBLOX_DIR}\""
log_info "Brewblox release is \"${BREWBLOX_RELEASE}\""
# Check if dir exists, but is not a brewblox dir, and is not empty
if [[ -d "${BREWBLOX_DIR}" ]] &&
! is_brewblox_dir "${BREWBLOX_DIR}" &&
[ -n "$(ls -A "${BREWBLOX_DIR:?}")" ]; then
log_warn "${BREWBLOX_DIR} already exists, but is not a Brewblox directory."
flush_stdin
read -rp "Remove all files in this directory and continue? (y/N)" response
if [[ "$response" =~ ^y(es)? ]]; then
rm -rf "${BREWBLOX_DIR:?}/*"
else
exit 1
fi
fi
# Ensure that the Brewblox dir is cwd
if [[ ${BREWBLOX_DIR} != "${PWD}" ]]; then
mkdir -p "${BREWBLOX_DIR}"
pushd "${BREWBLOX_DIR}" >/dev/null
fi
# Create virtual env
log_info "Creating Python virtual env..."
python3 -m venv .venv
# Download the sdist tarball
log_info "Downloading brewblox-ctl..."
wget -q \
-O ./brewblox-ctl.tar.gz \
"https://brewblox.blob.core.windows.net/ctl/${BREWBLOX_RELEASE}/brewblox-ctl.tar.gz"
# Activate virtual env
# shellcheck source=/dev/null
source .venv/bin/activate
# Install packages into the virtual env
log_info "Installing Python packages..."
python3 -m pip install pip setuptools wheel
python3 -m pip install --prefer-binary ./brewblox-ctl.tar.gz
# Init the config file
echo "release: ${BREWBLOX_RELEASE}" >./brewblox.yml
exec python3 -m brewblox_ctl install
}
# Protect against incomplete file downloads
install