-
Notifications
You must be signed in to change notification settings - Fork 6
/
placeos
executable file
·899 lines (752 loc) · 25.3 KB
/
placeos
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
#! /usr/bin/env bash
# shellcheck disable=1003,1090
# The bash hail mary (exit on failure, unset variables, fail pipe composition early)
set -euo pipefail
# Kill child processes on signals
trap 'tput cnorm; pkill -P $$;' SIGINT SIGTERM
# Name of the environment
PLACEOS_PRODUCT="PlaceOS Local"
# Whether to write to STDOUT or not
VERBOSE="${VERBOSE:-"false"}"
EMPTY_FILE="$(touch /tmp/placeos-empty-env; echo "/tmp/placeos-empty-env")"
cleanup() (
rm "$EMPTY_FILE" || true
)
banner() (
echo
echo '░░░░░░░\ ░░\ ░░░░░░\ ░░░░░░\'
echo '░░ __░░\ ░░ | ░░ __░░\ ░░ __░░\'
echo '░░ | ░░ |░░ | ░░░░░░\ ░░░░░░░\ ░░░░░░\ ░░ / ░░ |░░ / \__|'
echo '░░░░░░░ |░░ | \____░░\ ░░ _____|░░ __░░\ ░░ | ░░ |\░░░░░░\'
echo '░░ ____/ ░░ | ░░░░░░░ |░░ / ░░░░░░░░ |░░ | ░░ | \____░░\'
echo '░░ | ░░ |░░ __░░ |░░ | ░░ ____|░░ | ░░ |░░\ ░░ |'
echo '░░ | ░░ |\░░░░░░░ |\░░░░░░░\ \░░░░░░░\ ░░░░░░ |\░░░░░░ |'
echo '\__| \__| \_______| \_______| \_______| \______/ \______/'
echo
)
# Checks that files used for `docker-compose.yml` to function are present.
ensure_env_files() (
for expected_file in .env.chronograf .env.influxdb .env.public_key .env.secret_key; do
[[ ! -f "$expected_file" ]] && touch "$expected_file" || true
done
)
# This functions mutates the outer scope, hence {} are used
load_environment () {
eval "$("${base_path}/scripts/shdotenv" --dialect ruby --env "${base_path}/.env")"
}
# Path of directory containing the script
base_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
# Terminal spinner
source "${base_path}/scripts/spinner.sh"
# Create a logfile
mkdir -p "${base_path}/.logs"
logfile="${base_path}/.logs/$(date +"%Y%m%d%H%M").log"
export COMPOSE_PROJECT_NAME=placeos
# Helpers
###################################################################################################
PROMPT="░░░"
# Colours
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
reset='\033[0m'
bold='\033[1m'
normal='\033[22m'
unknown_argument() (
unknown_arg="${1}"
usage="${2}"
[ -n "${unknown_arg}" ] && prompt "${red}Unknown option:${reset} ${unknown_arg}"
eval "${usage}"
exit 1
)
prompt() (
echo -e "${PROMPT} ${1}"
)
abort() (
prompt "${red}${1}${reset}"
prompt "Logs can be found in ${logfile}"
exit 1
)
run_or_abort() {
task="${1}"
start="${2}"
failure="${3}"
ignore_failure="${4-"false"}"
exit_code=0
if [ "${VERBOSE}" == "false" ]; then
start_spinner "${PROMPT} ${start}"
eval "${task} >>${logfile} 2>&1" || exit_code=$?
stop_spinner $exit_code
else
prompt "${start}"
eval "${task} 2>&1 | tee -a ${logfile}" || exit_code=$?
fi
if [ "$ignore_failure" != "true" ]; then
[ $exit_code -eq 0 ] || abort "${failure:-"${task} failed with ${exit_code}"}"
fi
}
# Version Definitions
###################################################################################################
PLACEOS_RELEASE_REPO="PlaceOS/PlaceOS"
PLACEOS_RELEASE_BRANCH="release"
CALVER_FULL_REGEX='[0-9]+\.[0-9]{4}\.[0-9]+(-rc[0-9]+)?'
CALVER_MONTH_REGEX='[0-9]+\.[0-9]{4}'
VERSION_CHANNEL_REGEX='(nightly|preview|latest)'
fetch_release_tags() (
git ls-remote https://github.com/${PLACEOS_RELEASE_REPO} |
cut -f2 |
grep '^refs/tags/' |
cut -d'/' -f3 |
sort --version-sort --reverse |
sed -E "s/($CALVER_FULL_REGEX|$CALVER_MONTH_REGEX)/placeos-\1/g"
)
# Start
###################################################################################################
hard_reset() (
# TODO: Drop influxdb tables
# TODO: Clear redis
run_or_abort \
"${base_path}/scripts/run-sam-task drop:db" \
"Dropping PostgreSQL tables..." \
"Failed to drop PostgreSQL tables."
run_or_abort \
"${base_path}/scripts/run-sam-task drop:elastic" \
"Dropping Elasticsearch indices..." \
"Failed to drop Elasticsearch indices."
run_or_abort \
"docker compose --env-file='${EMPTY_FILE}' --project-directory='${base_path}' restart nginx" \
"Restarting nginx..." \
"Failed to restart nginx." \
"true"
)
start_environment__usage() (
cat <<EOF
Usage: placeos start [-h|--help]
Start the ${PLACEOS_PRODUCT} environment.
Arguments:
--hard-reset Reset the environment to a default state.
--email EMAIL Email to setup an admin account for. [default: support@place.tech]
--password PASSWORD Password for created admin account. [default: development]
--domain DOMAIN Domain to configure. [default: localhost:8443]
--application APP Application to configure. [default: backoffice]
--analytics Set-up analytics stack, including MQTT & InfluxDB & Chronograf
-v, --verbose Write logs to STDOUT in addition to the log file.
-h, --help Display this message.
EOF
)
start_environment() (
PROFILES=""
hard_reset=false
enable_analytics=false
enable_lgtm=false
email_argument=""
password_argument=""
domain_argument=""
application_argument=""
while [ ${#} -gt 0 ]; do
command="${1}"
shift
case ${command} in
--hard-reset)
read -rp "${PROMPT} $(echo -e "${yellow}Warning:${reset}") This will reset your environment. Would you like to continue (y/n)? " choice
case "${choice}" in
y | Y | YES | yes)
hard_reset=true
;;
*)
abort "Exiting."
;;
esac
;;
--email)
email_argument="${1}"
shift
;;
--password)
password_argument="${1}"
shift
;;
--domain)
domain_argument="${1}"
shift
;;
--application)
application_argument="${1}"
shift
;;
--analytics)
enable_analytics=true
;;
--lgtm)
enable_lgtm=true
;;
-v | --verbose)
VERBOSE="true"
;;
-h | --help | help)
start_environment__usage
exit 0
;;
*)
unknown_argument "${command}" "start_environment__usage"
;;
esac
done
banner
load_environment
prompt "Starting $PLACEOS_PRODUCT <${PLACEOS_TAG}>"
if [[ $VERBOSE == "false" ]]; then
prompt "For detailed logging, run..."
prompt "${bold}tail -f ${logfile}${normal}"
fi
EMAIL_ENV="${base_path}/.env.email"
set -a
if [ -f "${EMAIL_ENV}" ]; then
. "${EMAIL_ENV}"
elif [ -n "${email_argument}" ]; then
# Override env with CLI arguments
PLACE_EMAIL=${email_argument}
# Write the email so as to not prompt the user again.
echo "PLACE_EMAIL=${PLACE_EMAIL}" >"${EMAIL_ENV}"
fi
set +a
if [ -n "${password_argument}" ]; then
PLACE_PASSWORD=${password_argument}
fi
if [ -n "${domain_argument}" ]; then
export PLACE_DOMAIN=${domain_argument}
fi
if [ -n "${application_argument}" ]; then
export PLACE_APPLICATION=${application_argument}
fi
# Attempt to set the username to that in git config if username not in env
if [ -z ${PLACE_USERNAME+x} ]; then
PLACE_USERNAME=${PLACE_USERNAME:-"$(git config user.name 2>/dev/null || exit 0)"}
PLACE_USERNAME=${PLACE_USERNAME:-"Place Support"}
fi
# Prompt the user for their email if not already set via `.env` or argument
if [ -z ${PLACE_EMAIL+x} ]; then
PLACE_EMAIL=${PLACE_EMAIL:-"$(git config user.email 2>/dev/null || exit 0)"}
PLACE_EMAIL=${PLACE_EMAIL:-"support@place.tech"}
read -rp "${PROMPT} Please enter an email ($(echo -e "${green}default:${reset}") ${PLACE_EMAIL}): " choice
if [ -n "${choice}" ]; then
PLACE_EMAIL=${choice}
fi
fi
# Prompt the user for their password if not already set via `.env`
# TODO: Check for the user before asking for a password
# TODO: Autogenerate the default password
while [ -z ${PLACE_PASSWORD+x} ]; do
read -srp "${PROMPT} Please enter a password: " choice
PLACE_PASSWORD="${choice}"
# Newline after response
echo ""
done
# Write the email so as to not prompt the user again.
echo "PLACE_EMAIL=${PLACE_EMAIL}" >"${EMAIL_ENV}"
# TODO: Use init check instead of writing the password.
echo "PLACE_PASSWORD=${PLACE_PASSWORD}" >>"${EMAIL_ENV}"
if [[ $ENABLE_ANALYTICS == "true" ]] || [[ $enable_analytics == "true" ]]; then
PROFILES+=" --profile analytics"
fi
if [[ $ENABLE_LGTM == "true" ]] || [[ $enable_lgtm == "true" ]]; then
PROFILES+=" --profile lgtm"
export LOG_FORMAT=JSON
fi
run_or_abort \
"${base_path}/scripts/generate-secrets" \
"Generating secrets..." \
"Failed to generate secrets."
run_or_abort \
"docker compose --env-file='${EMPTY_FILE}' --project-directory='${base_path}' ${PROFILES} pull --quiet" \
"Pulling images..." \
"Failed to pull images."
[ ${hard_reset} == "true" ] && hard_reset
run_or_abort \
"${base_path}/scripts/run-init-container" \
"Initialising $PLACEOS_PRODUCT with default domain ($PLACE_DOMAIN)..." \
"Failed to create user entity."
run_or_abort \
"docker compose --env-file='${EMPTY_FILE}' --project-directory='${base_path}' ${PROFILES} up --detach" \
"Bringing up services..." \
"Failed to start services."
if [[ $PROFILES == *"analytics"* ]]; then
run_or_abort \
"${base_path}/scripts/init-influxdb" \
"Configuring InfluxDB..." \
"Failed to configure InfluxDB."
fi
prompt "${green}$PLACEOS_PRODUCT initialised!${reset}"
prompt "Login to https://$PLACE_DOMAIN/backoffice/ with..."
prompt "${yellow}$PLACE_EMAIL${reset}:${yellow}$PLACE_PASSWORD${reset}"
)
# Stop
###################################################################################################
stop_environment__usage() (
cat <<EOF
Usage: placeos stop [-h|--help]
Stop the ${PLACEOS_PRODUCT} environment.
Arguments:
-v, --verbose Write logs to STDOUT in addition to the log file.
-h, --help Display this message.
EOF
)
stop_environment() (
while [ ${#} -gt 0 ]; do
command="${1}"
shift
case ${command} in
-h | --help | help)
stop_environment__usage
exit 0
;;
-v | --verbose)
VERBOSE="true"
;;
*)
unknown_argument "${command}" "stop_environment__usage"
;;
esac
done
run_or_abort \
"docker compose --env-file='${EMPTY_FILE}' --project-directory='${base_path}' down --remove-orphans" \
"Tearing down $PLACEOS_PRODUCT" \
"Failed to teardown $PLACEOS_PRODUCT"
)
# Task
###################################################################################################
task__usage() (
cat <<EOF
Usage: placeos task [-h|--help|help] [--list] <task> [help|...] [arguments...]
Run a task in the ${PLACEOS_PRODUCT} environment.
Arguments:
--list Display list of available tasks.
-h, --help Display this message.
EOF
)
task() (
load_environment
PARAMS=""
while [ ${#} -gt 0 ]; do
command="${1}"
shift
case ${command} in
-h | --help)
task__usage
exit 0
;;
-t | --tasks | --list)
./scripts/run-sam-task help
exit 0
;;
*)
PARAMS="${PARAMS} ${command}"
shift
;;
esac
done
# Print help when no task passed
if [[ -z ${PARAMS} ]]; then
./scripts/run-sam-task help
exit 0
fi
# shellcheck disable=2086
./scripts/run-sam-task ${PARAMS}
)
# Changelog
###################################################################################################
changelog__usage() (
cat <<EOF
Usage: placeos changelog [-h|--help] [--full] [VERSION]
Changelog for the PlaceOS environment.
If called without a VERSION, fetches the latest CHANGELOG entry.
Otherwise, fetches the CHANGELOG entry for passed VERSION.
Arguments:
--full Include all prior CHANGELOG entries.
-h, --help Display this message
EOF
)
changelog() (
load_environment
if [[ $PLACEOS_TAG < "placeos-1.2202.3" ]]; then
abort "Please see https://github.com/${PLACEOS_RELEASE_REPO}/blob/${PLACEOS_RELEASE_BRANCH}/CHANGELOG.md for the platform changelog."
fi
version=""
full_changelog="false"
while [ ${#} -gt 0 ]; do
command="${1}"
shift
case ${command} in
-h | --help | help)
changelog__usage
exit 0
;;
--full)
full_changelog="true"
;;
*)
if [[ ${#} == 0 ]]; then
version="$command"
else
changelog__usage
exit 1
fi
;;
esac
done
temporary_changelog="$(mktemp -d)/CHANGELOG.md"
curl \
--silent \
--location \
--proto-redir -all,https \
"https://raw.githubusercontent.com/${PLACEOS_RELEASE_REPO}/${PLACEOS_RELEASE_BRANCH}/CHANGELOG.md" >"$temporary_changelog"
if [[ $version != "" ]]; then
version=$(sed -E 's/placeos-//g' <<<"$version")
version_header="$(grep --extended-regexp "^## ${version}$" "$temporary_changelog" | head -1)"
# Check version is present in changelog
if [[ ! $version_header ]]; then
abort "${red}${version} not found in CHANGELOG${reset}"
fi
else
# Extract the latest version
version_header="$(grep --extended-regexp "^## ${CALVER_FULL_REGEX}$" "$temporary_changelog" | head -1)"
version=$(echo "${version_header}" | sed -E 's/^## (.*)$/\1/')
fi
# Remove lines before the version header (simplifies range below)
sed -i '.bak' -n "/^${version_header}$/,\$p" "$temporary_changelog"
previous_version_header="$(grep --extended-regexp "^## ${CALVER_FULL_REGEX}$" "$temporary_changelog" | head -2 | tail -1)"
# Create a URL that directs to the header of the version in the CHANGELOG
anchor="$(echo "$version_header" | tr -d "[:blank:]" | tr -d '.#')"
changelog_uri="https://github.com/${PLACEOS_RELEASE_REPO}/blob/${PLACEOS_RELEASE_BRANCH}/CHANGELOG.md#${anchor}"
if [[ $full_changelog == "true" ]]; then
output="$(cat "$temporary_changelog")"
else
output="$(sed -n "/^$previous_version_header/q;p" "$temporary_changelog")"
fi
echo "# Changelog"
echo -e "\nFor the full PlaceOS release notes, see ${changelog_uri}\n"
echo "$output"
)
# Update
###################################################################################################
update_environment__usage() (
cat <<EOF
Usage: placeos update [-h|--help] [VERSION]
Modifies PLACEOS_TAG to the selected PlaceOS platform version.
If VERSION is omitted, defaults to the most recent stable version.
Arguments:
--list List the available versions.
--restart Restart services after updating, loading the new version.
-v, --verbose Write logs to STDOUT in addition to the log file.
-h, --help Display this message.
EOF
)
update_environment() (
version=""
restart_services="false"
while [ ${#} -gt 0 ]; do
command="${1}"
shift
case ${command} in
-h | --help | help)
update_environment__usage
exit 0
;;
--list)
fetch_release_tags
exit 0
;;
--restart)
restart_services="true"
;;
-v | --verbose)
VERBOSE="true"
;;
-*)
unknown_argument "${command}" "update_environment__usage"
;;
*)
version="$command"
[[ ${#} = 0 ]] || abort "Expected a single VERSION argument."
break
;;
esac
done
tags=$(fetch_release_tags)
if [[ -z "$version" ]]; then
version=$(grep -v --extended-regexp "$VERSION_CHANNEL_REGEX" <<<"${tags}" | head -1)
fi
# shellcheck disable=2076
if ! grep -q --extended-regexp "$CALVER_FULL_REGEX|$CALVER_MONTH_REGEX|$VERSION_CHANNEL_REGEX" <<<"$version"; then
prompt "Valid versions are...\n\n$tags\n"
abort "${version} is not a valid version."
fi
run_or_abort \
"sed -i '.bak' -E 's/^PLACEOS_TAG=.*$/PLACEOS_TAG=${version}/g' '.env'" \
"Updating to ${version}" \
"Failed to update to ${version}"
if [[ $restart_services == "true" ]]; then
run_or_abort \
"docker compose --env-file='${EMPTY_FILE}' --project-directory='${base_path}' restart" \
"Restarting services on ${version}..." \
"Failed to restart services"
fi
changelog "${version}"
)
# Upgrade
###################################################################################################
PLACEOS_PRODUCT_REPO="placeos/local"
fetch_environment_tags() (
git ls-remote "https://github.com/${PLACEOS_PRODUCT_REPO}" |
cut -f2 |
grep --extended-regexp '^refs/tags/|HEAD' |
cut -d'/' -f3 |
sort --version-sort --reverse
)
upgrade_environment__usage() (
cat <<EOF
Usage: placeos upgrade [-h|--help] [VERSION]
Upgrades {PLACEOS_PRODUCT}.
If VERSION is omitted, defaults to the latest version.
Arguments:
--list Lists versions of {PLACEOS_PRODUCT}.
-q, --quiet Do not write command logs to STDOUT.
-h, --help Display this message.
EOF
)
upgrade_environment() (
version="HEAD"
tags="$(fetch_environment_tags)"
VERBOSE="true"
while [ ${#} -gt 0 ]; do
command="${1}"
shift
case ${command} in
-h | --help | help)
upgrade_environment__usage
exit 0
;;
--list)
echo "$tags"
exit 0
;;
-q | --quiet)
VERBOSE="false"
;;
-*)
unknown_argument "${command}" "upgrade_environment__usage"
;;
*)
version="$command"
[[ ${#} = 0 ]] || abort "Expected a single VERSION argument."
break
;;
esac
done
# shellcheck disable=2076
if [[ ! " $tags " =~ "$version" ]]; then
prompt "Valid versions are...\n\n$tags\n"
abort "$version is not a valid version."
fi
# Set git-config values known to fix git errors
# Line endings
git config --local core.eol lf
git config --local core.autocrlf false
# zeroPaddedFilemode fsck errors
git config --local fsck.zeroPaddedFilemode ignore
git config --local fetch.fsck.zeroPaddedFilemode ignore
git config --local receive.fsck.zeroPaddedFilemode ignore
last_commit=$(git rev-parse "$version")
if ! git diff --quiet; then
(
git stash --include-untracked \
&& git checkout --quiet "$version" \
&& git stash apply
) || abort "Failed to upgrade, you may need to resolve merge conflicts"
else
run_or_abort \
"git checkout --quiet $version" \
"Upgrading ${PLACEOS_PRODUCT}..." \
"Failed to upgrade ${PLACEOS_PRODUCT}"
fi
# Check if it was really updated or not
if [[ "$(git rev-parse HEAD)" = "$last_commit" ]]; then
prompt "${PLACEOS_PRODUCT} is already at $version"
else
prompt "${PLACEOS_PRODUCT} is now at $version"
fi
)
# Uninstall
###################################################################################################
uninstall_environment__usage() (
cat <<EOF
Usage: placeos uninstall
Removes $PLACEOS_PRODUCT containers, removes scripts from paths, and finally deletes the installation path.
Arguments:
--force Skip confirmation of uninstall.
-h, --help Display this message.
EOF
)
uninstall_environment() (
force="false"
VERBOSE="true"
while [ ${#} -gt 0 ]; do
command="${1}"
shift
case ${command} in
-h | --help | help)
uninstall_environment__usage
exit 0
;;
--force)
force="true"
;;
*)
unknown_argument "${command}" "uninstall_environment__usage"
;;
esac
done
# Get confirmation.
if [ "${force}" == "false" ]; then
read -rp "${PROMPT} $(echo -e "${yellow}Warning:${reset}") This will remove your $PLACEOS_PRODUCT installation. Would you like to continue (y/n)? " choice
case "${choice}" in
y | Y | YES | yes) ;;
*)
abort "Exiting."
;;
esac
fi
# Stop environment and remove containers, volumes, and images.
run_or_abort \
"docker compose --env-file='${EMPTY_FILE}' --project-directory='${base_path}' down --volumes --networks --rmi all" \
"Stopping ${PLACEOS_PRODUCT}..." \
"Failed to stop ${PLACEOS_PRODUCT}"
PROFILE_PATH="${PROFILE_PATH:-"${HOME}/.profile"}"
# Remove scripts from paths.
run_or_abort \
"sed -i '.bak' -E 's/^.*# added by PlaceOS installer$//g' \"${PROFILE_PATH}\"" \
"Removing $PLACEOS_PRODUCT from ${bold}${PROFILE_PATH}${normal}..." \
"Failed to remove $PLACEOS_PRODUCT from ${bold}${PROFILE_PATH}${normal}"
# Delete the installation path.
PLACEOS_HOME="${PLACEOS_HOME:-"$HOME/.placeos"}"
if [ "${force}" == "false" ]; then
read -rp "${PROMPT} $(echo -e "${yellow}Warning:${reset}") Removing ${bold}${PLACEOS_HOME}${normal}. Would you like to continue (y/n)? " choice
case "${choice}" in
y | Y | YES | yes) ;;
*)
abort "Exiting."
;;
esac
fi
rm -rf "$PLACEOS_HOME"
prompt "${green}Uninstalled $PLACEOS_PRODUCT${reset}"
)
# Version
###################################################################################################
version() (
load_environment
echo "${PLACEOS_TAG}"
)
# Migrate
###################################################################################################
migrate__usage() (
cat <<EOF
Usage: placeos migrate
Run a RethinkDB data migration in the ${PLACEOS_PRODUCT} environment.
Arguments:
-a, --appends Don't clean tables before loading RethinkDB data. Defaults to True
-h, --help Display this message.
EOF
)
migrate() (
load_environment
clean="true"
while [ ${#} -gt 0 ]; do
command="${1}"
shift
case ${command} in
-h | --help)
migrate__usage
exit 0
;;
-a | --clean)
clean="false"
;;
*)
unknown_argument "${command}" "migrate__usage"
;;
esac
done
echo "Running migration"
# shellcheck disable=2086
./scripts/run-migration ${clean}
run_or_abort \
"docker compose --env-file='${EMPTY_FILE}' --project-directory='${base_path}' up --detach --remove-orphans" \
"Bringing up services..." \
"Failed to start services."
)
# Root Command
###################################################################################################
usage() (
cat <<EOF
Usage: placeos [-h|--help] [--version] [command]
Helper script for interfacing with ${PLACEOS_PRODUCT}.
Command:
start Start the environment.
stop Stops the environment.
task Runs a task in the environment.
changelog Displays platform changelog (in markdown).
update Update the platform version.
upgrade Upgrade {PLACEOS_PRODUCT}.
migrate Migrate RethinkDB data to PostgreSQL
uninstall Uninstalls {PLACEOS_PRODUCT}.
version Render PlaceOS version in use.
help Display this message.
Arguments:
--version Render PlaceOS version in use.
-h, --help Display this message.
EOF
)
if [ $# -eq 0 ]; then
usage
exit 1
fi
ensure_env_files
command="$1"
shift
case $command in
start)
start_environment "$@"
;;
stop)
stop_environment "$@"
;;
task)
task "$@"
;;
changelog)
changelog "$@"
;;
update)
update_environment "$@"
;;
upgrade)
upgrade_environment "$@"
;;
migrate)
migrate "$@"
;;
uninstall)
uninstall_environment "$@"
;;
--version | version)
version
;;
-h | --help | help)
usage
;;
*)
unknown_argument "${command}" "usage"
;;
esac
cleanup