-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.bash
More file actions
executable file
·112 lines (92 loc) · 3.76 KB
/
start.bash
File metadata and controls
executable file
·112 lines (92 loc) · 3.76 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
#!/usr/bin/env bash
# stop script on error
set -e
LOG_FILE="/tmp/extend_iot_start.log"
# Redirect stdout and stderr through tee to log file and terminal
exec > >(tee -a "$LOG_FILE") 2>&1
# Wait for network to be available
printf "\nWaiting for network to be available...\n"
while ! ping -c 1 -W 1 8.8.8.8 &> /dev/null; do
sleep 2
done
printf "\nNetwork is available. Proceeding...\n"
if ping -q -c1 -W1 google.com >/dev/null && command -v chronyc >/dev/null 2>&1; then
while true; do
refid=$(chronyc tracking | grep "Reference ID" | awk '{print $5}')
leap=$(chronyc tracking | grep "Leap status" | awk '{print $4}')
if [ "$refid" != "()" ] && [ "$leap" = "Normal" ]; then
echo "Chrony is synchronized."
break
else
echo "Waiting for chrony to synchronize..."
sleep 0.5
fi
done
fi
# Check for python 3
if ! python3 --version &> /dev/null; then
printf "\nERROR: python3 must be installed.\n"
exit 1
fi
# Check for pip, install if missing (with version-specific bootstrap)
if ! python3 -m pip --version &> /dev/null; then
PYTHON_VERSION=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
printf "\nInstalling pip for Python $PYTHON_VERSION...\n"
if [ "$PYTHON_VERSION" = "3.8" ]; then
curl -s https://bootstrap.pypa.io/pip/3.8/get-pip.py -o /tmp/get-pip.py
else
curl -s https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
fi
python3 /tmp/get-pip.py --user
fi
# Create virtual environment if it doesn't exist using venv (built-in to Python 3)
# Use --system-site-packages to allow access to system packages like python3-apt
if [ ! -d "$HOME/.iot_kit/iot_manager_env" ]; then
echo "Creating virtual environment with system site packages access..."
python3 -m venv --system-site-packages $HOME/.iot_kit/iot_manager_env
else
echo "Virtual environment already exists."
fi
# Activate it
echo "Activating virtual environment..."
source $HOME/.iot_kit/iot_manager_env/bin/activate
echo "Virtual environment ready!"
# Check to see if root CA file exists, download if not
if [ ! -f $HOME/.iot_kit/root-CA.crt ]; then
printf "\nDownloading AWS IoT Root CA certificate from AWS...\n"
curl https://www.amazontrust.com/repository/AmazonRootCA1.pem > $HOME/.iot_kit/root-CA.crt
fi
# Clone or pull the latest changes from Extend-Robotics/er_iot_manager repository
if [ ! -d $HOME/er_iot_manager ]; then
printf "\nCloning the Extend-Robotics er_iot_manager repository...\n"
git clone https://github.com/Extend-Robotics/er_iot_manager.git $HOME/er_iot_manager --recursive
else
printf "\nPulling the latest changes from the er_iot_manager repository...\n"
cd $HOME/er_iot_manager
git pull origin main
git checkout main
cd $HOME
fi
# Run the setup script to install required dependencies using setup.py
printf "\nRunning setup.py to install dependencies for er_iot_manager...\n"
pip install $HOME/er_iot_manager # Uses venv's pip since it's activated
source $HOME/.iot_kit/device.env
# Run the connection and jobs python scripts
printf "\nRunning IoT manager application...\n"
# Define the base directory and connection script path
CONNECTION_SCRIPT_DIR="$HOME/er_iot_manager"
CONNECTION_SCRIPT="$CONNECTION_SCRIPT_DIR/connection.py"
CONNECTION_SCRIPT_PYC="$CONNECTION_SCRIPT_DIR/connection.pyc"
# Choose the correct script (source or compiled)
if [ -f "$CONNECTION_SCRIPT" ]; then
SCRIPT_TO_RUN="$CONNECTION_SCRIPT"
else
SCRIPT_TO_RUN="$CONNECTION_SCRIPT_PYC"
fi
# Run the chosen script
python3 "$SCRIPT_TO_RUN" \
--endpoint a34wwkbw0n00uf-ats.iot.eu-west-2.amazonaws.com \
--key "$HOME/.iot_kit/$thingName.private.key" \
--cert "$HOME/.iot_kit/$thingName.cert.pem" \
--thing_name "$thingName" \
--ca_file "$HOME/.iot_kit/root-CA.crt"