-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
envsetup.sh
293 lines (248 loc) · 7.21 KB
/
envsetup.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/sh
if [ -f local.sh ]; then
echo "reading local settings"
. ./local.sh
fi
RECOMMENDED_ELM_VERSION=0.19.1
# map tools from project go modules
air() {
go run github.com/cosmtrek/air "$@"
}
siot_install_proto_gen_go() {
cd ~ && go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
cd - || exit
}
siot_install_frontend_deps() {
(cd "frontend" && npm install)
(cd "frontend" && npx elm-tooling install)
(cd "frontend/lib" && npm ci)
}
siot_check_elm() {
# this no longer works with the way we are installing elm
if ! npx elm --version >/dev/null 2>&1; then
echo "Please install elm >= 0.19"
echo "https://guide.elm-lang.org/install.html"
return 1
fi
version=$(npx elm --version)
if [ "$version" != "$RECOMMENDED_ELM_VERSION" ]; then
echo "found elm $version, recommend elm version $RECOMMENDED_ELM_VERSION"
echo "not sure what will happen otherwise"
fi
return 0
}
siot_check_go() {
# Get the installed Go version
go_version=$(go version | awk '{print $3}' | sed 's/go//g')
# Split the version into major, minor, and patch components
major=$(echo "$go_version" | awk -F'.' '{print $1}')
minor=$(echo "$go_version" | awk -F'.' '{print $2}')
patch=$(echo "$go_version" | awk -F'.' '{print $3}')
# Check if the version is greater than 1.22
if [ "$major" -gt 1 ] || { [ "$major" -eq 1 ] && [ "$minor" -gt 22 ]; } || { [ "$major" -eq 1 ] && [ "$minor" -eq 22 ] && [ "$patch" -gt 0 ]; }; then
echo "Go version $go_version is greater than 1.22"
return 0
else
echo "Go version $go_version is not greater than 1.22"
return 1
fi
}
siot_setup() {
siot_check_go || return 1
siot_install_frontend_deps
# the following is to work around a race condition
# where the first time you run npx elm, you get an error:
# elm: Text file busy
(cd frontend && (npx elm || true))
# make sure elm-spa auto-generated stuff is set up
(cd frontend && npx elm-spa build)
return 0
}
siot_build_frontend() {
(cd "frontend" && npx elm-spa build) || return 1
gzip -f frontend/public/dist/elm.js
return 0
}
siot_version() {
git describe --tags HEAD
}
siot_build_backend() {
BINARY_NAME=siot
if [ "${GOOS}" = "windows" ]; then
BINARY_NAME=siot.exe
fi
CGO_ENABLED=0 go build -ldflags="-s -w -X main.version=$(siot_version)" -o $BINARY_NAME cmd/siot/main.go || return 1
return 0
}
siot_build() {
siot_build_frontend || return 1
siot_build_backend || return 1
}
siot_build_arm() {
siot_build_frontend || return 1
GOARCH=arm GOARM=7 go build -ldflags="-s -w -X main.version=$(siot_version)" -o siot_arm cmd/siot/main.go || return 1
return 0
}
siot_build_arm64() {
siot_build_frontend || return 1
GOARCH=arm64 go build -ldflags="-s -w -X main.version=$(siot_version)" -o siot_arm64 cmd/siot/main.go || return 1
return 0
}
siot_build_arm_debug() {
siot_build_frontend || return 1
GOARCH=arm GOARM=7 go build -ldflags="-s -w -X main.version=$(siot_version)" -o siot_arm cmd/siot/main.go || return 1
return 0
}
siot_deploy() {
siot_build_frontend || return 1
gcloud app deploy cmd/portal || return 1
return 0
}
siot_run() {
siot_build_frontend || return 1
go build -ldflags="-X main.version=$(siot_version)" -o siot -race cmd/siot/main.go || return 1
./siot "$@"
return 0
}
# run siot_mkcert first
siot_run_tls() {
export SIOT_NATS_TLS_CERT=server-cert.pem
export SIOT_NATS_TLS_KEY=server-key.pem
siot_build_frontend || return 1
go run cmd/siot/main.go "$@" || return 1
return 0
}
# please install mkcert and run mkcert -install first
siot_mkcert() {
mkcert -cert-file server-cert.pem -key-file server-key.pem localhost ::1
}
find_src_files() {
find . -not \( -path ./frontend/src/Spa/Generated -prune \) -not \( -path ./assets -prune \) -name "*.go"
}
siot_watch_go() {
echo "watch args: $*"
air serve -dev "$*"
}
siot_watch_elm() {
(cd frontend && npx elm-watch hot) || false
}
siot_watch() {
npx run-pty \
% /bin/sh -c ". ./envsetup.sh && siot_watch_elm" \
% /bin/sh -c ". ./envsetup.sh && siot_watch_go $*"
}
# TODO finish this and add to siot_test ...
check_go_format() {
gofiles=$(find . -name "*.go")
unformatted=$(gofmt -l "$gofiles")
if [ -n "$unformatted" ]; then
return 1
fi
return 0
}
siot_test_frontend() {
(cd frontend && npx elm-test || return 1) || return 1
(cd frontend && npx elm-review || return 1) || return 1
}
siot_test_frontend_lib() {
(cd ./frontend/lib && npm run lint || return 1) || return 1
echo "Starting SimpleIOT..."
./siot serve --store siot_test_frontend_lib.sqlite --resetStore 2>/dev/null &
PID=$!
sleep 1
(cd ./frontend/lib && npm run test || return 1)
CODE=$?
echo "Stopping SimpleIOT..."
kill -s SIGINT $PID
wait $PID
echo "SimpleIOT Stopped"
if [ "$CODE" = "0" ]; then
rm siot_test_frontend_lib.sqlite
fi
}
siot_frontend_fix() {
(cd frontend && npx elm-review --fix-all)
}
# please run the following before pushing -- best if your editor can be set up
# to do this automatically.
siot_test() {
echo "Build frontend ..."
siot_build_frontend || return 1
echo "Test frontend ..."
siot_test_frontend || return 1
echo "Test backend ..."
go test -p=1 -race "$@" ./... || return 1
echo "Lint backend ..."
golangci-lint run || return 1
echo "Testing passed :-)"
return 0
}
# following can be used to set up influxdb for local testing
siot_setup_influx() {
export SIOT_INFLUX_URL=http://localhost:8086
#export SIOT_INFLUX_USER=admin
#export SIOT_INFLUX_PASS=admin
export SIOT_INFLUX_DB=siot
}
siot_protobuf_go() {
protoc --proto_path=internal/pb internal/pb/*.proto --go_out=./ || return 1
}
siot_protobuf_js() {
protoc --proto_path=internal/pb internal/pb/*.proto --js_out=import_style=commonjs,binary:./frontend/lib/protobuf/ || return 1
}
siot_protobuf() {
echo "generating protobufs"
siot_protobuf_go
siot_protobuf_js
}
siot_edge_run() {
go run cmd/edge/main.go "$*"
}
# download goreleaser from https://github.com/goreleaser/goreleaser/releases/
# and put in /usr/local/bin
# This can be useful to test/debug the release process locally
siot_goreleaser_build() {
goreleaser build --skip-validate --rm-dist
}
# before releasing, you need to tag the release
# you need to provide GITHUB_TOKEN in env or ~/.config/goreleaser/github_token
# generate tokens: https://github.com/settings/tokens/new
# enable repo and workflow sections
siot_release() {
VERSION=$1
if [ -z "$VERSION" ]; then
echo "must provide version in format vX.Y.Z"
return 1
fi
# update elm.js.gz
siot_build_frontend || return 1
git commit -m "update FE assets" frontend/public/dist/elm.js.gz || return 1
git push || return 1
git tag -f "$VERSION" || return 1
goreleaser release --clean || return 1
siot_deploy_docs || return 1
# refresh godocs site
wget "https://proxy.golang.org/github.com/simpleiot/simpleiot/@v/${VERSION}.info" || return 1
rm "${VERSION}.info"
}
# dblab keyboard shortcuts
# - Ctrl+space execute query
# - Ctrl+H,J,K,L move to panel left,below,above,right
# see more keybindings here: https://github.com/danvergara/dblab#key-bindings
siot_dblab() {
STORE=siot.sqlite
if [ "$1" != "" ]; then
STORE=$1
fi
go run github.com/danvergara/dblab@latest --db "$STORE" --driver sqlite3
}
siot_mdbook() {
mdbook serve -p 3333
}
siot_mdbook_cleanup() {
rm -rf book
}
siot_deploy_docs() {
(cd /scratch/bec/ops/ &&
ansible-playbook -i production all.yml --limit tmpdir --tags docs.simpleiot.org)
}