forked from iotempire/iotempower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·178 lines (155 loc) · 4.85 KB
/
run
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
#!/usr/bin/env bash
# This file might be not executable as is (for example when run in Termux on
# Android). This file is best run as: bash run
# (after changing into this iotempower directory)
#
# At first start, it installs the local iotempower-environment
#
# Author: ulno
# Create date: 2017-05-17
#
# if parameters are given and the first is "exec", iotempower will execute
# the rest of the parameters as command in iotempower environment
function welcome() {
cat << EOF
IoTempower
==========
Your friendly and affordable Internet of Things environment.
More at http://github.com/iotempire/iotempower
EOF
}
function usage() {
welcome
cat << EOF
Syntax: iot [command command_parameters]
The following commands are possible:
- welcome:
Print the welcome message again.
- exec [cmd+arguments]:
Execute cmd inside the iotempower environment. Check out all the helper
scripts in iotempower/bin
- upgrade:
Update current version iotempower with newest version from git repository.
- help [cmd]:
Display help on a specific command given in cmd. If cmd is empty,
show this help.
After entering the IoTempower environment, this help is also available just
via typing uhelp. Calling uhelp with no parameters will list all available
commands.
- [command]:
Execute iot_[command] with the given parameters.
If no command is given, execute the bash shell in the IoTempower environment.
The first time this is run, external dependencies are downloaded and
installed here too.
Also try out the following commands for device management (add help to each of
them to get a longer description):
- initialize (reflashing device)
- deploy (update a device remotely)
- console_serial show serial output of connected node
Commands for interacting via mqtt (node topic is added when in node folder):
- mqtt_all (show all mqtt messages_
- mqtt_listen (listen for a specific topic)
- mqtt_send (directly send payload to specific topic)
- mqtt_action (react on specific payload on specific mqtt topic)
EOF
}
function check_files() {
for file in "$@"; do
if [ ! -e "$file" ]; then
echo "Can't find $file."
return 1
fi
done
return 0
}
function check_root() {
if [ -e "$IOTEMPOWER_ROOT/lib/shell_starter/iotempower.bash" ]; then
cd "$IOTEMPOWER_ROOT"
fi
# else we should assume that we were started from the root-directory
#echo -n "Checking now that we are in the iotempower root directory. "
check_files "lib/shell_starter/iotempower.bash" \
"bin/shebang_run" \
"etc/iotempower.conf.example"
if [ $? -ne 0 ]; then
echo
echo "Cannot find IoTempower files."
echo "Stopping now."
return 1
fi
# Yes, found them.
echo
return 0
}
function check_install() {
# Check if all local directories and paths have been set up
check_files .local/bin/shebang_run .local/vp/bin/activate
return $?
}
function install() {
echo "Installing now."
echo
bash "$IOTEMPOWER_ROOT/bin/iot_install"
# run upgrade should not be necessary anymore - TODO: verify
}
function run() {
case "$1" in
welcome)
welcome
;;
help | -h | --help)
shift
if [[ "$1" ]]; then
exec "$IOTEMPOWER_LOCAL/bin/iot_help" "$@"
else
usage
fi
;;
"")
welcome
exec bash --noprofile --norc # do not read again any of the environments
;;
exec | x | run | -x | -c | --exec)
shift
exec "$@"
;;
*)
# try to execute that with iot_ prefix
cmd="$IOTEMPOWER_LOCAL/bin/iot_$1"
if [[ -x "$cmd" ]]; then
shift
exec "$cmd" "$@"
else
usage
fi
esac
}
######## Main #########
current_path="$(pwd)" # save to restore later
if check_root; then
export IOTEMPOWER_ROOT=$(realpath "$(pwd)")
source "$IOTEMPOWER_ROOT/lib/shell_starter/iotempower.bash" \
|| { echo "Can't read configuration. Aborting."; exit 1; }
check_install
installed=$?
cd "$current_path" # back to where we have been called
# If installed is detected or any argument is given
if [ $installed -eq 0 ] || [ $# -gt 0 ]; then
run "$@"
else
# check if we are in iotempower docker
if [[ -d "$HOME/data" && -e "$HOME/iot-local-docker.tar.xz" ]]; then
echo "Initializing docker container on first run..."
cd "$HOME/data"
tar xaf "$HOME/iot-local-docker.tar.xz"
cd "$current_path" # back to where we have been called
echo "... done."
echo
run "$@"
else
if install; then # if it failed, try to install first
run "$@"
fi
fi
fi
fi