forked from jasonacox/Powerwall-Dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·198 lines (172 loc) · 5.51 KB
/
setup.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
#
# Interactive Setup Script for Powerwall Dashboard
# by Jason Cox - 21 Jan 2022
# Stop on Errors
set -e
if [ ! -f VERSION ]; then
echo "ERROR: Missing VERSION file. Setup must run from installation directory."
echo ""
exit 1
fi
VERSION=`cat VERSION`
echo "Powerwall Dashboard (v${VERSION}) - SETUP"
echo "-----------------------------------------"
# Verify not running as root
if [ "$EUID" -eq 0 ]; then
echo "ERROR: Running this as root will cause permission issues."
echo ""
echo "Please ensure your local user in in the docker group and run without sudo."
echo " sudo usermod -aG docker \$USER"
echo " $0"
echo ""
exit 1
fi
# Service Running Helper Function
running() {
local url=${1:-http://localhost:80}
local code=${2:-200}
local status=$(curl --head --location --connect-timeout 5 --write-out %{http_code} --silent --output /dev/null ${url})
[[ $status == ${code} ]]
}
# Docker Dependency Check
if ! docker info > /dev/null 2>&1; then
echo "ERROR: docker is not available or not running."
echo "This script requires docker, please install and try again."
exit 1
fi
if ! docker-compose version > /dev/null 2>&1; then
if ! docker compose version > /dev/null 2>&1; then
echo "ERROR: docker-compose is not available or not running."
echo "This script requires docker-compose or docker compose."
echo "Please install and try again."
exit 1
fi
fi
# Check for RPi Issue with Buster
if [[ -f "/etc/os-release" ]]; then
OS_VER=`grep PRETTY /etc/os-release | cut -d= -f2 | cut -d\" -f2`
if [[ "$OS_VER" == "Raspbian GNU/Linux 10 (buster)" ]]
then
echo "WARNING: You are running ${OS_VER}"
echo " This OS version has a bug in the libseccomp2 library that"
echo " causes the pypowerwall container to fail."
echo " See details: https://github.com/jasonacox/Powerwall-Dashboard/issues/56"
echo ""
read -r -p "Setup - Proceed? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
then
echo ""
else
echo "Cancel"
exit 1
fi
fi
fi
PW_ENV_FILE="pypowerwall.env"
COMPOSE_ENV_FILE="compose.env"
TELEGRAF_LOCAL="telegraf.local"
GF_ENV_FILE="grafana.env"
CURRENT=`cat tz`
echo "Timezone (leave blank for ${CURRENT})"
read -p 'Enter Timezone: ' TZ
echo ""
# Powerwall Credentials
if [ -f ${PW_ENV_FILE} ]; then
echo "Current Powerwall Credentials:"
echo ""
cat ${PW_ENV_FILE}
echo ""
read -r -p "Update these credentials? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
rm ${PW_ENV_FILE}
else
echo "Using existing ${PW_ENV_FILE}."
fi
fi
# Create Powerwall Settings
if [ ! -f ${PW_ENV_FILE} ]; then
echo "Enter credentials for Powerwall..."
read -p 'Password: ' PASSWORD
read -p 'Email: ' EMAIL
read -p 'IP Address: ' IP
echo "PW_EMAIL=${EMAIL}" > ${PW_ENV_FILE}
echo "PW_PASSWORD=${PASSWORD}" >> ${PW_ENV_FILE}
echo "PW_HOST=${IP}" >> ${PW_ENV_FILE}
echo "PW_TIMEZONE=America/Los_Angeles" >> ${PW_ENV_FILE}
echo "TZ=America/Los_Angeles" >> ${PW_ENV_FILE}
echo "PW_DEBUG=no" >> ${PW_ENV_FILE}
fi
# Create Grafana Settings if missing (required in 2.4.0)
if [ ! -f ${GF_ENV_FILE} ]; then
cp "${GF_ENV_FILE}.sample" "${GF_ENV_FILE}"
fi
# Create default docker compose env file if needed.
if [ ! -f ${COMPOSE_ENV_FILE} ]; then
cp "${COMPOSE_ENV_FILE}.sample" "${COMPOSE_ENV_FILE}"
fi
# Create default telegraf local file if needed.
if [ ! -f ${TELEGRAF_LOCAL} ]; then
cp "${TELEGRAF_LOCAL}.sample" "${TELEGRAF_LOCAL}"
fi
echo ""
if [ -z "${TZ}" ]; then
echo "Using ${CURRENT} timezone...";
./tz.sh "${CURRENT}";
else
echo "Setting ${TZ} timezone...";
./tz.sh "${TZ}";
fi
echo "-----------------------------------------"
echo ""
# Optional - Setup Weather Data
if [ -f weather.sh ]; then
./weather.sh setup
fi
# Build Docker in current environment
./compose-dash.sh up -d
echo "-----------------------------------------"
# Set up Influx
echo "Waiting for InfluxDB to start..."
until running http://localhost:8086/ping 204 2>/dev/null; do
printf '.'
sleep 5
done
echo " up!"
sleep 2
echo "Setup InfluxDB Data for Powerwall..."
docker exec --tty influxdb sh -c "influx -import -path=/var/lib/influxdb/influxdb.sql"
sleep 2
# Execute Run-Once queries for initial setup.
cd influxdb
for f in run-once*.sql; do
if [ ! -f "${f}.done" ]; then
echo "Executing single run query $f file...";
docker exec --tty influxdb sh -c "influx -import -path=/var/lib/influxdb/${f}"
echo "OK" > "${f}.done"
fi
done
cd ..
# Restart weather411 to force a sample
if [ -f weather/weather411.conf ]; then
echo "Fetching local weather..."
docker restart weather411
fi
# Display Final Instructions
cat << EOF
------------------[ Final Setup Instructions ]-----------------
Open Grafana at http://localhost:9000/ ... use admin/admin for login.
Follow these instructions for *Grafana Setup*:
* From 'Configuration\Data Sources' add 'InfluxDB' database with:
- Name: 'InfluxDB'
- URL: 'http://influxdb:8086'
- Database: 'powerwall'
- Min time interval: '5s'
- Click "Save & test" button
* From 'Configuration\Data Sources' add 'Sun and Moon' database with:
- Name: 'Sun and Moon'
- Enter your latitude and longitude (tool here: https://bit.ly/3wYNaI1 )
- Click "Save & test" button
* From 'Dashboard\Browse', select 'New/Import', and upload 'dashboard.json' from
the ${PWD}/dashboards folder.
EOF