forked from sixfab/sixfab-power-python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
125 lines (100 loc) · 3.66 KB
/
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
#!/bin/bash
clear
cat <<"EOF"
.&@&. %% .%@%
#@@@% *&@@@&. %@@@#
&@@&. .&@@% .%@@@@@%/. .%@@&. ,&@@%
%@@&. /@@@# *&@@@@&&&@@@@&, %@@&* .&@@&
/@@@* .@@@# &@@&( (@@@% #@@&, *@@@*
%@@&. #@@&. (@@@, ,&@@( .@@@( &@@#
%@@&. #@@&. /@@@, *&@@( .@@@( &@@#
*@@@* .&@@# %@@@# %@@@# #@@&, /@@@,
%@@&, *@@@% .&@@@@@@@@@@@%. .&@@&, ,&@@%
%@@&* %@@# *#%%%#* #@@% *&@@%
(@@@&. .&@@&/
#@# #@#
_____ _ __ _ ______
/ ___(_) / _| | | | ___ \
\ `--. ___ _| |_ __ _| |__ | |_/ /____ _____ _ __
`--. \ \ \/ / _/ _` | '_ \ | __/ _ \ \ /\ / / _ \ '__|
/\__/ / |> <| || (_| | |_) | | | | (_) \ V V / __/ |
\____/|_/_/\_\_| \__,_|_.__/ \_| \___/ \_/\_/ \___|_|
==========================================================
EOF
print_info() {
YELLOW='\033[0;33m'
NC='\033[0m'
printf "${YELLOW}[INFO]${NC} $1\n"
}
print_error() {
RED='\033[0;31m'
NC='\033[0m'
printf "${RED}[ERROR]${NC} $1\n"
}
if [ "$EUID" -ne 0 ]; then
print_error "Please run as root"
exit
fi
print_info "Updating package index..."
sudo apt-get update > /dev/null 2>&1
print_info "Enabling i2c..."
sudo raspi-config nonint do_i2c 0 > /dev/null 2>&1
print_info "Looking for dependencies..."
# Check if git installed
if ! [ -x "$(command -v git)" ]; then
print_info 'Git is not installed, installing...'
apt-get install git -y > /dev/null 2>&1
fi
# Check if pip3 installed
if ! [ -x "$(command -v pip3)" ]; then
print_info 'Pip for python3 is not installed, installing...'
apt-get install python3-pip -y > /dev/null 2>&1
fi
print_info "Cloning source to /tmp/sixfab-power-api"
rm -r /tpm/sixfab-power-api > /dev/null 2>&1
git clone https://github.com/sixfab/sixfab-power-python-api.git /tmp/sixfab-power-api > /dev/null 2>&1
print_info "Installing package"
cd /tmp/sixfab-power-api
python3 setup.py install > /dev/null 2>&1
cd $HOME
rm -r /tmp/sixfab-power-api
if [ ! -d "/opt/sixfab" ]; then
mkdir /opt/sixfab
fi
if [ ! -d "/opt/sixfab/pms" ]; then
mkdir /opt/sixfab/pms
fi
if [ ! -d "/opt/sixfab/pms/api" ]; then
print_info "Downloading service source"
git clone https://github.com/sixfab/power_distribution-service.git /opt/sixfab/pms/api > /dev/null 2>&1
print_info "Installing service dependencies"
pip3 install -r /opt/sixfab/pms/api/requirements.txt > /dev/null 2>&1
else
print_info "Updating service source"
cd /opt/sixfab/pms/api && git pull > /dev/null 2>&1 && cd - > /dev/null 2>&1
fi
if [ ! -f "/etc/systemd/system/power_request.service" ]; then
print_info "Initializing systemd service for request service..."
echo "[Unit]
Description=Sixfab UPS HAT Distributed API
[Service]
User=pi
ExecStart=/usr/bin/python3 /opt/sixfab/pms/api/run_server.py
[Install]
WantedBy=multi-user.target" | sudo tee /etc/systemd/system/power_request.service > /dev/null 2>&1
print_info "Enabling and starting service."
sudo systemctl daemon-reload
sudo systemctl enable power_request > /dev/null 2>&1
sudo systemctl start power_request
print_info "Service initialized successfully."
else
print_info "Request service installed already, restarting..."
sudo systemctl restart power_request
fi
STATUS="$(systemctl is-active power_request.service)"
if [ "${STATUS}" = "active" ]; then
print_info "Package installed and service running, visit http://localhost:6060/docs"
else
print_info "Couldn't initialize service. Please re-try"
exit 1
fi