-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·141 lines (119 loc) · 3.25 KB
/
run.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
137
138
139
140
141
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
source ./.env
VENVPATH="./venv"
venv() {
local _bin="${VENVPATH}/bin"
if [ -d "${_bin}" ]; then
echo "source ${VENVPATH}/bin/activate"
else
echo "source ${VENVPATH}/Scripts/activate"
fi
}
make-venv() {
python -m venv "${VENVPATH}"
}
reset-venv() {
rm -rf "${VENVPATH}"
make-venv
}
wrapped-python() {
local _bin="${VENVPATH}/bin"
if [ -d "${_bin}" ]; then
"${VENVPATH}"/bin/python "$@"
else
"${VENVPATH}"/Scripts/python "$@"
fi
}
wrapped-pip() {
wrapped-python -m pip "$@"
}
python-deps() {
wrapped-pip install --upgrade pip setuptools wheel
local pip_extras="${1:-}"
if [ -z "${pip_extras}" ]; then
wrapped-pip install --upgrade -e .
else
wrapped-pip install --upgrade -e ".[${pip_extras}]"
fi
}
install() {
if [ -d "${VENVPATH}" ]; then
python-deps "$@"
else
make-venv && python-deps "$@"
fi
}
build() {
wrapped-python -m build
}
get-wheel(){
# return the .whl file in dist/
local _files=(dist/*.whl)
basename -- "${_files[0]}"
}
clean() {
rm -rf dist/
rm -rf .eggs/
rm -rf build/
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
find . -name '__pycache__' -exec rm -fr {} +
find . -name '.mypy_cache' -exec rm -fr {} +
find . -name '.pytest_cache' -exec rm -fr {} +
find . -name '*.egg-info' -exec rm -fr {} +
}
tests() {
wrapped-python -m pytest -rA tests
}
deploy-sensor() {
scp config/* "${PI_USER}@${PI_SSH_HOST}:/home/${PI_USER}/.config/systemd/user/"
scp src/garage/sensor.py "${PI_USER}@${PI_SSH_HOST}:/home/${PI_USER}"
local commands=(
"systemctl --user stop sensor.service;"
"systemctl --user daemon-reload;"
"systemctl --user enable --now sensor.service;"
"sleep 1;"
"systemctl --user status sensor.service;"
)
ssh "${PI_USER}@${PI_SSH_HOST}" "${commands[@]}"
}
deploy-server() {
clean && build
local _wheel
_wheel=$(get-wheel)
if [[ ! -f "${_wheel}" ]]; then
echo "No wheel file! Cannot deploy-server"
return 1
fi
scp config/* "${SERVER_USER}@${SERVER_SSH_HOST}:/home/${SERVER_USER}/.config/systemd/user/"
scp dist/"${_wheel}" "${SERVER_SSH_HOST}:/tmp/"
local commands=(
"systemctl --user stop garage-server.service;"
"rm -rf /home/${SERVER_USER}/venv;"
"python -m venv venv;"
"./venv/bin/python -m pip install --upgrade wheel setuptools /tmp/${_wheel};"
"systemctl --user daemon-reload;"
"systemctl --user enable --now garage-server.service;"
"sleep 1;"
"systemctl --user status garage-server.service;"
)
ssh "${SERVER_USER}@${SERVER_SSH_HOST}" "${commands[@]}"
}
fake-sensor() {
wrapped-python -m garage.fake_sensor
}
prod-server() {
GARAGE_API_KEY=testkey wrapped-python -m hypercorn garage.server:app --bind '0.0.0.0:8081' --debug
}
default() {
GARAGE_API_KEY=testkey QUART_APP=garage.server:app \
wrapped-python -m quart --debug \
run --host 0.0.0.0 --port 8081 \
--reload
}
TIMEFORMAT="Task completed in %3lR"
time "${@:-default}"