-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
167 lines (140 loc) · 4.44 KB
/
install.sh
File metadata and controls
167 lines (140 loc) · 4.44 KB
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
#!/bin/bash
# Set variables
REPO_URL="https://github.com/ryanpulley/DiscoverAEStoPylontech.git"
APP_NAME="DiscoverBMS"
INSTALL_DIR=~/$APP_NAME
OS_USER=`whoami`
FIRMWARE_CONFIG_FILE=/boot/firmware/config.txt
MOSQUITTO_CONFIG_FILE=/etc/mosquitto/mosquitto.conf
# Function to search for a specific entry in a config file
search_config() {
config_file="$1"
search_term="$2"
# Check if the config file exists
if [[ ! -f "$config_file" ]]; then
return 1
fi
# Search for the entry using grep
if grep -q "$search_term" "$config_file"; then
return 0
else
return 1
fi
}
# Function to add waveshare configs
WaveShare_config() {
config_file=$1
cat <<EOF | sudo tee -a $1
dtparam=spi=on
dtoverlay=spi1-3cs
dtoverlay=mcp251xfd,spi0-0,interrupt=25
dtoverlay=mcp251xfd,spi1-0,interrupt=24
EOF
}
Mosquitto_config() {
config_file=$1
cat << EOF | sudo tee -a $1
listener 1883 0.0.0.0
allow_anonymous true
EOF
}
echo "$(date) : searching for WaveShare CAN FD HAT configs"
# Check WaveShare configs
if search_config ${FIRMWARE_CONFIG_FILE} "dtoverlay=mcp251xfd"; then
echo "$(date) : WaveShare CAN FD HAT firmware configs found"
else
echo "$(date) : No WaveShare CAN FD HAT firmware configs found"
echo "Do you wish to install WaveShare CAN FD HAT firmware configs and reboot?(Y/N)"
select yn in "Y" "N"; do
case $yn in
[Yy]* ) WaveShare_config ${FIRMWARE_CONFIG_FILE}; sudo reboot; break;;
[Nn]* ) break;;
esac
done
fi
echo "$(date) : installing mosquitto and CAN utilities"
sudo apt install can-utils mosquitto mosquitto-clients
# append configs to mosquitto to allow anonymous listener
if search_config ${MOSQUITTO_CONFIG_FILE} "listener 1883 0.0.0.0"; then
echo "$(date) : Mosquitto configs found, skipping"
else
echo "$(date) : Adding Mosquitto Configs"
Mosquitto_config ${MOSQUITTO_CONFIG_FILE}
sudo systemctl restart mosquitto
fi
# If directory exists, make a backup and restore config after clone
if [ -d ${INSTALL_DIR} ]; then
echo "$(date) : Install directory exists, making backup"
if [ -d ${INSTALL_DIR}.backup ]; then
rm -fr ${INSTALL_DIR}.backup
fi
echo "$(date) : Backing up prior application directory from ${INSTALL_DIR} to ${INSTALL_DIR}.backup"
if cp -r ${INSTALL_DIR} $INSTALL_DIR.backup; then
rm -fr ${INSTALL_DIR}
else
echo "$(date): failure in making a backup of the current installation, exiting..."
exit 1
fi
fi
echo "$(date) : cloning DiscoverBMS repository to ${INSTALL_DIR}"
# Clone the repository
git clone $REPO_URL $INSTALL_DIR
# Change directory to the application directory
cd $INSTALL_DIR
echo "$(date) : creating python virtual environment"
# Create a virtual environment
python3 -m venv venv
# Activate the virtual environment
source venv/bin/activate
echo "$(date) : installing python dependencies"
# Install dependencies
pip install -r requirements.txt
echo "$(date) : creating log directory"
# Create a directory for logs
mkdir -p ${INSTALL_DIR}/log
echo "$(date) : creating systemd services"
sudo rm /etc/systemd/system/${APP_NAME}-CAN.service
#cat <<EOF |sudo tee -a /etc/systemd/system/${APP_NAME}-CAN.service
sudo bash -c "cat <<EOF > /etc/systemd/system/${APP_NAME}-CAN.service
[Unit]
Description=${APP_NAME} CAN Initialization Service
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=${INSTALL_DIR}/startcan.sh
WorkingDirectory=${INSTALL_DIR}
User=${OS_USER}
StandardOutput=append:${INSTALL_DIR}/log/${APP_NAME}-CAN.service.log
StandardError=append:${INSTALL_DIR}/log/${APP_NAME}-CAN.service.log
[Install]
WantedBy=multi-user.target
EOF
"
sudo rm /etc/systemd/system/${APP_NAME}.service
#cat <<EOF |sudo tee -a /etc/systemd/system/${APP_NAME}.service
sudo bash -c "cat <<EOF > /etc/systemd/system/${APP_NAME}.service
[Unit]
Description=${APP_NAME} Service
After=${APP_NAME}-CAN.service
Wants=network-online.target
[Service]
Restart=always
RestartSec=10
User=${OS_USER}
ExecStart=${INSTALL_DIR}/start.sh
WorkingDirectory=${INSTALL_DIR}
User=${OS_USER}
StandardOutput=append:${INSTALL_DIR}/log/${APP_NAME}.service.log
StandardError=append:${INSTALL_DIR}/log/${APP_NAME}.service.log
[Install]
WantedBy=multi-user.target
EOF
"
echo "$(date) : enabling and starting systemd services"
# Enable and start the service (optional)
sudo systemctl daemon-reload
sudo systemctl enable ${APP_NAME}-CAN.service
sudo systemctl start ${APP_NAME}-CAN.service
sudo systemctl enable ${APP_NAME}.service
sudo systemctl start ${APP_NAME}.service
echo "$APP_NAME installed successfully!"