-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathstart-local-podman.sh
More file actions
executable file
·1583 lines (1337 loc) · 46.2 KB
/
start-local-podman.sh
File metadata and controls
executable file
·1583 lines (1337 loc) · 46.2 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
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# --------------------------------------------------------
# Run Elasticsearch and Kibana for local testing
# Note: do not use this script in a production environment
# --------------------------------------------------------
#
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
set -eu
script_dir="$(cd "$(dirname "$0")" && pwd)"
### Helper functions ###############################################################################
# Returns 0 if command exists in PATH.
available() {
command -v "$1" >/dev/null 2>&1
}
# Checks the available disk space in GB
# parameter: required size in GB
check_disk_space_gb() {
required=$1
available_gb=$(($(df -k / | awk 'NR==2 {print $4}') / 1024 / 1024))
if [ "$available_gb" -lt "$required" ]; then
echo "Error: Only '${available_gb}' GB disk space available; '${required}' GB is required."
exit 1
fi
}
# Check if a container is runnning
# parameter: the name of the container
check_container_running() {
container_name=$1
if "$container_cli" ps -a --format '{{.Names}}' 2>/dev/null | grep -qxF "${container_name}"; then
echo "The container '$container_name' does already exist!"
echo "To remove the container run the following command:"
echo
echo "$container_cli rm $container_name"
exit 1
fi
}
# Generates a random password with letters and numbers
# parameter: size of the password (default is 8 characters)
random_password() {
LENGTH="${1:-8}"
LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom 2>/dev/null | head -c "${LENGTH}"
}
# Check for ARM64 architecture
is_arm64() {
arch="$(uname -m)"
if [ "$arch" = "arm64" ] || [ "$arch" = "aarch64" ]; then
return 0 # Return 0 (true)
else
return 1 # Return 1 (false)
fi
}
# Alternative to sort -V, which is not available in BSD-based systems (e.g., macOS)
version_sort() {
awk -F'.' '
{
printf("%d %d %d %s\n", $1, $2, $3, $0)
}' | sort -n -k1,1 -k2,2 -k3,3 | awk '{print $4}'
}
# Function to check if the format is a valid semantic version (major.minor.patch)
is_valid_version() {
echo "$1" | grep -E -q '^[0-9]+\.[0-9]+\.[0-9]+$'
}
# Compare versions
# parameter 1: version to compare
# parameter 2: version to compare
compare_versions() {
v1=$1
v2=$2
original_ifs="$IFS"
IFS='.'
# shellcheck disable=SC2086
set -- $v1; v1_major=${1:-0}; v1_minor=${2:-0}; v1_patch=${3:-0}
IFS='.'
# shellcheck disable=SC2086
set -- $v2; v2_major=${1:-0}; v2_minor=${2:-0}; v2_patch=${3:-0}
IFS="$original_ifs"
[ "$v1_major" -lt "$v2_major" ] && echo "lt" && return 0
[ "$v1_major" -gt "$v2_major" ] && echo "gt" && return 0
[ "$v1_minor" -lt "$v2_minor" ] && echo "lt" && return 0
[ "$v1_minor" -gt "$v2_minor" ] && echo "gt" && return 0
[ "$v1_patch" -lt "$v2_patch" ] && echo "lt" && return 0
[ "$v1_patch" -gt "$v2_patch" ] && echo "gt" && return 0
echo "eq"
}
# Get the latest stable version of Elasticsearch
# Note: It removes all the beta or candidate releases from the list
# but includes the GA releases (e.g. new major)
get_latest_version() {
versions="$(curl -s "https://elastic-release-api.s3.us-west-2.amazonaws.com/public/past-releases.json")"
latest_version=$(echo "$versions" | awk -F'"' '/"version": *"/ {print $4}' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+( GA)?$' | version_sort | tail -n 1)
# Remove the GA prefix from the version, if present
latest_version=$(echo "$latest_version" | awk '{ gsub(/ GA$/, "", $0); print }')
# Check if the latest version is empty
if [ -z "$latest_version" ]; then
echo "Error: the latest Elasticsearch version is empty"
exit 1
fi
# Check if the latest version is valid
if ! is_valid_version "$latest_version"; then
echo "Error: {$latest_version} is not a valid Elasticsearch stable version"
exit 1
fi
echo "$latest_version"
}
# Revert the status, removing containers, volumes, network and folder
cleanup() {
if [ -d "$installation_folder" ]; then
if [ -f "uninstall.sh" ]; then
"$installation_folder/stop.sh" >/dev/null 2>&1 || true
"$installation_folder/uninstall.sh" >/dev/null 2>&1 || true
fi
rm -rf "$installation_folder"
fi
}
get_os_info() {
if [ -f /etc/os-release ]; then
# Most modern Linux distributions have this file
. /etc/os-release
echo "Distribution: $NAME"
echo "Version: $VERSION"
elif [ -f /etc/lsb-release ]; then
# For older distributions using LSB (Linux Standard Base)
. /etc/lsb-release
echo "Distribution: $DISTRIB_ID"
echo "Version: $DISTRIB_RELEASE"
elif [ -f /etc/debian_version ]; then
# For Debian-based distributions without os-release or lsb-release
echo "Distribution: Debian"
echo "Version: $(cat /etc/debian_version)"
elif [ -f /etc/redhat-release ]; then
# For Red Hat-based distributions
echo "Distribution: $(cat /etc/redhat-release)"
elif [ -n "${OSTYPE+x}" ]; then
if [ "${OSTYPE#darwin}" != "$OSTYPE" ]; then
# macOS detection
echo "Distribution: macOS"
echo "Version: $(sw_vers -productVersion)"
elif [ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ] || [ "$OSTYPE" = "win32" ]; then
# Windows detection in environments like Git Bash, Cygwin, or MinGW
echo "Distribution: Windows"
echo "Version: $(cmd.exe /c ver | tr -d '\r')"
elif [ "$OSTYPE" = "linux-gnu" ] && uname -r | grep -q "Microsoft"; then
# Windows Subsystem for Linux (WSL) detection
echo "Distribution: Windows (WSL)"
echo "Version: $(uname -r)"
fi
else
echo "Unknown operating system"
fi
if [ -f /proc/version ]; then
# Check if running on WSL2 or WSL1 for Microsoft
if grep -q "WSL2" /proc/version; then
echo "Running on WSL2"
elif grep -q "microsoft" /proc/version; then
echo "Running on WSL1"
fi
fi
}
# Generate the error log
# parameter 1: error message
# parameter 2: the container names to retrieve, separated by comma
generate_error_log() {
msg="$1"
services="$2"
error_file="$script_dir/$error_log"
if [ -n "${msg}" ]; then
echo "${msg}" > "$error_file"
fi
{
echo "Start-local version: ${version}"
echo "$container_cli engine version: $container_runtime_version"
echo "Elastic Stack version: ${es_version}"
if [ "$esonly" = "true" ]; then
echo "--esonly parameter used"
fi
if [ "$edot" = "true" ]; then
echo "--edot parameter used"
fi
get_os_info
} >> "$error_file"
for service in $services; do
echo "-- Logs of service ${service}:" >> "$error_file"
$container_cli logs "${service}" >> "$error_file" 2> /dev/null
done
echo "An error log has been generated in '${error_log}' file."
echo "If you need assistance, open an issue at https://github.com/elastic/start-local/issues"
}
### Main functions #################################################################################
startup() {
echo
echo ' ______ _ _ _ '
echo ' | ____| | | | (_) '
echo ' | |__ | | __ _ ___| |_ _ ___ '
echo ' | __| | |/ _` / __| __| |/ __|'
echo ' | |____| | (_| \__ \ |_| | (__ '
echo ' |______|_|\__,_|___/\__|_|\___|'
echo '-------------------------------------------------'
echo '🚀 Run Elasticsearch and Kibana for local testing'
echo '-------------------------------------------------'
echo
echo 'ℹ️ Do not use this script in a production environment'
echo
# Version
version="0.14.0"
# Folder name for the installation
installation_folder="${ES_LOCAL_DIR:-"$script_dir/elastic-start-local"}"
# API key name for Elasticsearch
api_key_name="elastic-start-local"
# Name of the error log
error_log="error-start-local.log"
container_network_name="es-local-dev-net${ES_LOCAL_DIR:+-${ES_LOCAL_DIR}}"
# Elasticsearch container name
elasticsearch_container_name="es-local-dev${ES_LOCAL_DIR:+-${ES_LOCAL_DIR}}"
# Kibana container name
kibana_container_name="kibana-local-dev${ES_LOCAL_DIR:+-${ES_LOCAL_DIR}}"
# EDOT container name
edot_container_name="edot-collector${ES_LOCAL_DIR:+-${ES_LOCAL_DIR}}"
# Minimum disk space required for docker images + services (in GB)
min_disk_space_required=5
# TODO:
container_runtime_version=0.0.0
}
parse_args() {
# Parameters
esonly=false
edot=false
# Parse the script parameters.
while [ "$#" -gt 0 ]; do
case "$1" in
-v)
# Check that there is another argument for the version.
if [ $# -lt 2 ]; then
echo "Error: -v requires a version value (eg. -v 8.17.0)"
exit 1
fi
es_version="$2"
shift 2
;;
--esonly)
esonly=true
shift
;;
--edot)
edot=true
shift
;;
--)
# End of options; shift and exit the loop.
shift
break
;;
-*)
# Unknown or unsupported option.
echo "Error: Unknown option '$1'"
exit 1
;;
*)
# We've hit a non-option argument; stop parsing options.
break
;;
esac
done
}
check_requirements() {
# Check required commands.
requirements='
curl|https://curl.se/download.html
grep|https://www.gnu.org/software/grep/
head|https://www.gnu.org/software/coreutils/
tail|https://www.gnu.org/software/coreutils/
tr|https://www.gnu.org/software/coreutils/
mkdir|https://www.gnu.org/software/coreutils/
uname|https://www.gnu.org/software/coreutils/
df|https://www.gnu.org/software/coreutils/
awk|https://www.gnu.org/software/gawk/
date|https://www.gnu.org/software/coreutils/
cat|https://www.gnu.org/software/coreutils/
'
missing=0
while IFS= read -r entry; do
case "$entry" in
''|\#*) continue ;;
esac
case "$entry" in
*'|'*)
cmd=${entry%%|*}
url=${entry#*|}
;;
*)
cmd=$entry
url=
;;
esac
if ! available "$cmd"; then
printf 'Error: %s command is required\n' "$cmd"
if [ -n "$url" ]; then
printf 'You can install it from %s\n' "$url"
fi
missing=1
fi
done <<EOF
$requirements
EOF
if [ "$missing" -ne 0 ]; then
exit 1
fi
# Check available disk space.
check_disk_space_gb ${min_disk_space_required}
}
initialize_container_runtime() {
# For development purposes, allow to force Docker even in this script.
prefer_docker=false
available "docker" && has_docker=true || has_docker=false
available "podman" && has_podman=true || has_podman=false
if [ "$prefer_docker" = "false" ] && [ "$has_podman" = "false" ]; then
if [ "$has_docker" = "true" ]; then
echo "Podman is not installed, but Docker was detected."
echo "You can use the Docker-specific script instead:"
echo
echo " curl -fsSL https://elastic.co/start-local | sh"
exit 1
fi
echo "Error: Podman is not installed."
echo "You can install Podman from https://podman.io/getting-started/installation/."
exit 1
fi
if [ "$has_docker" = "false" ] && [ "$has_podman" = "false" ]; then
echo "Error: Either Docker or Podman must be installed."
echo "You can install Docker from https://docs.docker.com/engine/install/."
echo "You can install Podman from https://podman.io/getting-started/installation/."
exit 1
fi
if [ "$prefer_docker" = "true" ] && [ "$has_docker" = "true" ]; then
container_cli="docker"
return 0
fi
container_cli="podman"
}
check_installation_folder() {
# Check if $installation_folder exists
folder=$installation_folder
if [ -d "$folder" ]; then
if [ -n "$(ls -A "$folder")" ]; then
echo "It seems you already have a 'start-local' installation in '${folder}'."
if [ -f "$folder/uninstall.sh" ]; then
echo "I cannot proceed unless you uninstall it, using the following command:"
echo "$folder/uninstall.sh"
else
echo "I did not find the 'uninstall.sh' file, you need to proceed manually."
if [ -f "$folder/.env" ]; then
echo "Execute the following commands:"
# TODO:
fi
fi
exit 1
fi
fi
}
check_container_services() {
check_container_running "$elasticsearch_container_name"
check_container_running "$kibana_container_name"
check_container_running "$edot_container_name"
}
create_installation_folder() {
if [ ! -d "$folder" ]; then
mkdir "$folder"
fi
cd "$folder"
}
generate_passwords() {
# Generate random passwords.
es_password="${ES_LOCAL_PASSWORD:-$(random_password)}"
if [ "$esonly" = "false" ]; then
kibana_password="$(random_password)"
kibana_encryption_key="$(random_password 32)"
fi
}
create_scripts() {
write_up_script
write_start_script
write_stop_script
write_down_script
write_uninstall_script
}
choose_es_version() {
if [ -z "${es_version:-}" ]; then
# Get the latest Elasticsearch version
es_version="$(get_latest_version)"
fi
# Fix for ARM64: add suffix "-arm64"
if is_arm64 && [ "${es_version##*-arm64}" = "$es_version" ]; then
es_version="${es_version}-arm64"
fi
}
create_env_file() {
today=$(date +%s)
license_expire=$((today + 3600*24*30))
# Create the .env file
cat > .env <<- EOM
START_LOCAL_VERSION=$version
CONTAINER_CLI=$container_cli
CONTAINER_NETWORK_NAME=$container_network_name
ES_LOCAL_VERSION=$es_version
ES_LOCAL_CONTAINER_NAME=$elasticsearch_container_name
ES_LOCAL_PASSWORD=$es_password
ES_LOCAL_PORT=9200
ES_LOCAL_URL=http://localhost:\${ES_LOCAL_PORT}
ES_LOCAL_API_KEY_NAME=$api_key_name
ES_LOCAL_DISK_SPACE_REQUIRED=1gb
ES_LOCAL_LICENSE_EXPIRE_DATE=$license_expire
EOM
if [ "$edot" = "true" ]; then
cat >> .env <<- EOM
ES_LOCAL_JAVA_OPTS="-Xms2g -Xmx2g"
EDOT_LOCAL_CONTAINER_NAME=$edot_container_name
EOM
else
cat >> .env <<- EOM
ES_LOCAL_JAVA_OPTS="-Xms128m -Xmx2g"
EOM
fi
if [ "$esonly" = "false" ]; then
cat >> .env <<- EOM
KIBANA_LOCAL_CONTAINER_NAME=$kibana_container_name
KIBANA_LOCAL_PORT=5601
KIBANA_LOCAL_PASSWORD=$kibana_password
KIBANA_ENCRYPTION_KEY=$kibana_encryption_key
EOM
fi
}
create_kibana_config() {
if [ "$esonly" = "true" ]; then
return 0
fi
if [ ! -d "config" ]; then
mkdir config
fi
# Create telemetry
cat > config/telemetry.yml <<- EOM
start-local:
version: ${version}
EOM
}
create_edot_config() {
if [ "$edot" = "false" ]; then
return 0
fi
if [ ! -d "$installation_folder/config/edot-collector" ]; then
mkdir -p "$installation_folder/config/edot-collector"
fi
trace_processor_name="elasticapm"
if [ "$(compare_versions "$es_version" "9.2.0")" = "lt" ];then
trace_processor_name="elastictrace"
fi
# shellcheck disable=SC2016
es_local_api_key_var='${ES_LOCAL_API_KEY}'
# shellcheck disable=SC2016
es_local_password_var='${ES_LOCAL_PASSWORD}'
cat > "$installation_folder/config/edot-collector/config.yaml" <<EOM
extensions:
apmconfig:
source:
elasticsearch:
endpoint: http://elastic:${es_local_password_var}@elasticsearch:9200
cache_duration: 10s
opamp:
protocols:
http:
endpoint: 0.0.0.0:4320
receivers:
# Receives data from other Collectors in Agent mode
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317 # Listen on all interfaces
http:
endpoint: 0.0.0.0:4318 # Listen on all interfaces
connectors:
elasticapm: {} # Elastic APM Connector
processors:
batch:
send_batch_size: 1000
timeout: 1s
send_batch_max_size: 1500
batch/metrics:
send_batch_max_size: 0 # Explicitly set to 0 to avoid splitting metrics requests
timeout: 1s
${trace_processor_name}: {}
exporters:
debug: {}
elasticsearch/otel:
endpoints:
- http://elasticsearch:9200
api_key: ${es_local_api_key_var}
tls:
insecure_skip_verify: true
mapping:
mode: otel
service:
extensions: [ apmconfig ]
pipelines:
metrics:
receivers: [otlp]
processors: [batch/metrics]
exporters: [debug, elasticsearch/otel]
logs:
receivers: [otlp]
processors: [batch]
exporters: [debug, elasticapm, elasticsearch/otel]
traces:
receivers: [otlp]
processors: [batch, ${trace_processor_name}]
exporters: [debug, elasticapm, elasticsearch/otel]
metrics/aggregated-otel-metrics:
receivers:
- elasticapm
processors: [] # No processors defined in the original for this pipeline
exporters:
- debug
- elasticsearch/otel
EOM
}
# --- Inline script writers -----------------------------------------------------------------------
write_up_script() {
cat > ./up.sh <<'EOM'
#!/bin/sh
# --------------------------------------------------------
# Run Elasticsearch and Kibana for local testing
# Note: do not use this script in a production environment
# --------------------------------------------------------
#
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
set -eu
script_dir="$(cd "$(dirname "$0")" && pwd)"
# shellcheck disable=SC1091
. "$script_dir/.env"
[ -n "${CONTAINER_CLI:-}" ] || { echo "CONTAINER_CLI not set"; exit 1; }
command -v "$CONTAINER_CLI" >/dev/null 2>&1 || { echo "Error: '$CONTAINER_CLI' not found."; exit 1; }
# Detect if running on LXC container
detect_lxc() {
# Check /proc/1/environ for LXC container identifier
if grep -qa "container=lxc" /proc/1/environ 2>/dev/null; then
return 0
fi
# Check /proc/self/cgroup for LXC references
if grep -q "lxc" /proc/self/cgroup 2>/dev/null; then
return 0
fi
# Check for LXC in /sys/fs/cgroup
if grep -q "lxc" /sys/fs/cgroup/* 2>/dev/null; then
return 0
fi
# Use systemd-detect-virt if available
if command -v systemd-detect-virt >/dev/null 2>&1; then
if [ "$(systemd-detect-virt)" = "lxc" ]; then
return 0
fi
fi
return 1
}
create_bridged_network() {
[ -n "${CONTAINER_NETWORK_NAME:-}" ] || { echo "CONTAINER_NETWORK_NAME not set"; return 1; }
# shellcheck disable=SC2059
printf "Creating network '$CONTAINER_NETWORK_NAME' ... "
# If network already exists, nothing to do.
if "$CONTAINER_CLI" network inspect "$CONTAINER_NETWORK_NAME" >/dev/null 2>&1; then
echo "done (already exists)."
return 0
fi
# Create a bridged network and let the CLI/CNI pick the default subnet/gateway.
if "$CONTAINER_CLI" network create --driver bridge "$CONTAINER_NETWORK_NAME" >/dev/null 2>&1; then
echo "done (created)."
return 0
fi
# Some Podman versions use CNI and accept 'podman network create' without --driver.
if [ "$CONTAINER_CLI" = "podman" ]; then
if "$CONTAINER_CLI" network create "$CONTAINER_NETWORK_NAME" >/dev/null 2>&1; then
echo "done (created)."
return 0
fi
fi
echo "failed."
return 1
}
create_elasticsearch_container() {
[ -n "${CONTAINER_NETWORK_NAME:-}" ] || { echo "CONTAINER_NETWORK_NAME not set"; return 1; }
[ -n "${ES_LOCAL_VERSION:-}" ] || { echo "ES_LOCAL_VERSION not set"; return 1; }
[ -n "${ES_LOCAL_CONTAINER_NAME:-}" ] || { echo "ES_LOCAL_CONTAINER_NAME not set"; return 1; }
[ -n "${ES_LOCAL_PORT:-}" ] || { echo "ES_LOCAL_PORT not set"; return 1; }
[ -n "${ES_LOCAL_PASSWORD:-}" ] || { echo "ES_LOCAL_PASSWORD not set"; return 1; }
ES_LOCAL_JAVA_OPTS=${ES_LOCAL_JAVA_OPTS:-}
ES_LOCAL_DISK_SPACE_REQUIRED=${ES_LOCAL_DISK_SPACE_REQUIRED:-85%}
image="docker.elastic.co/elasticsearch/elasticsearch:${ES_LOCAL_VERSION}"
# shellcheck disable=SC2059
printf "Creating container '${ES_LOCAL_CONTAINER_NAME}' from image '${image}' ... "
# If container exists, do nothing.
if "$CONTAINER_CLI" ps -a --format '{{.Names}}' 2>/dev/null | grep -qxF "${ES_LOCAL_CONTAINER_NAME}"; then
echo "done (already exist)."
return 0
fi
# Add ulimit for memlock on non-LXC hosts.
ulimit_args=""
if [ "${CONTAINER_CLI}" != "podman" ] && ! detect_lxc >/dev/null 2>&1; then
ulimit_args="--ulimit memlock=-1:-1"
fi
# Create container.
# shellcheck disable=SC2086
if ! output=$("$CONTAINER_CLI" create \
--name "${ES_LOCAL_CONTAINER_NAME}" \
--network "${CONTAINER_NETWORK_NAME}" \
--hostname elasticsearch \
--network-alias elasticsearch \
-p "127.0.0.1:${ES_LOCAL_PORT}:9200" \
-v "dev-elasticsearch:/usr/share/elasticsearch/data" \
-e "discovery.type=single-node" \
-e "ELASTIC_PASSWORD=${ES_LOCAL_PASSWORD}" \
-e "xpack.security.enabled=true" \
-e "xpack.security.http.ssl.enabled=false" \
-e "xpack.license.self_generated.type=trial" \
-e "xpack.ml.use_auto_machine_memory_percent=true" \
-e "ES_JAVA_OPTS=${ES_LOCAL_JAVA_OPTS}" \
-e "cluster.routing.allocation.disk.watermark.low=${ES_LOCAL_DISK_SPACE_REQUIRED}" \
-e "cluster.routing.allocation.disk.watermark.high=${ES_LOCAL_DISK_SPACE_REQUIRED}" \
-e "cluster.routing.allocation.disk.watermark.flood_stage=${ES_LOCAL_DISK_SPACE_REQUIRED}" \
$ulimit_args \
"${image}" 2>&1); then
echo "failed."
printf '%s\n' "$output"
return 1
fi
echo "done (created)."
return 0
}
create_kibana_container() {
[ -n "${CONTAINER_NETWORK_NAME:-}" ] || { echo "CONTAINER_NETWORK_NAME not set"; return 1; }
[ -n "${ES_LOCAL_VERSION:-}" ] || { echo "ES_LOCAL_VERSION not set"; return 1; }
[ -n "${KIBANA_LOCAL_CONTAINER_NAME:-}" ] || { echo "KIBANA_LOCAL_CONTAINER_NAME not set"; return 1; }
[ -n "${KIBANA_LOCAL_PORT:-}" ] || { echo "KIBANA_LOCAL_PORT not set"; return 1; }
[ -n "${KIBANA_LOCAL_PASSWORD:-}" ] || { echo "KIBANA_LOCAL_PASSWORD not set"; return 1; }
ES_LOCAL_PORT=${ES_LOCAL_PORT:-9200}
telemetry_host_path="$script_dir/config/telemetry.yml"
image="docker.elastic.co/kibana/kibana:${ES_LOCAL_VERSION}"
# shellcheck disable=SC2059
printf "Creating container '${KIBANA_LOCAL_CONTAINER_NAME}' from image '${image}' ... "
# Do nothing if container already exists.
if "$CONTAINER_CLI" ps -a --format '{{.Names}}' 2>/dev/null | grep -qxF "${KIBANA_LOCAL_CONTAINER_NAME}"; then
echo "done (already exists)."
return 0
fi
# Optional telemetry mount if file exists.
if [ -f "${telemetry_host_path}" ]; then
telemetry_mount="-v ${telemetry_host_path}:/usr/share/kibana/config/telemetry.yml:ro"
else
telemetry_mount=""
fi
# shellcheck disable=SC2086
if ! output=$("$CONTAINER_CLI" create \
--name "${KIBANA_LOCAL_CONTAINER_NAME}" \
--network "${CONTAINER_NETWORK_NAME}" \
--hostname kibana \
--network-alias kibana \
-p "127.0.0.1:${KIBANA_LOCAL_PORT}:5601" \
-v "dev-kibana:/usr/share/kibana/data" \
$telemetry_mount \
-e "SERVER_NAME=kibana" \
-e "ELASTICSEARCH_HOSTS=http://elasticsearch:9200" \
-e "ELASTICSEARCH_USERNAME=kibana_system" \
-e "ELASTICSEARCH_PASSWORD=${KIBANA_LOCAL_PASSWORD}" \
-e "XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY=${KIBANA_ENCRYPTION_KEY:-}" \
-e "ELASTICSEARCH_PUBLICBASEURL=http://localhost:${ES_LOCAL_PORT}" \
-e "XPACK_SPACES_DEFAULTSOLUTION=es" \
"${image}" 2>&1); then
echo "failed."
printf '%s\n' "$output"
return 1
fi
echo "done (created)."
return 0
}
create_edot_container() {
[ -n "${CONTAINER_NETWORK_NAME:-}" ] || { echo "CONTAINER_NETWORK_NAME not set"; return 1; }
[ -n "${ES_LOCAL_VERSION:-}" ] || { echo "ES_LOCAL_VERSION not set"; return 1; }
[ -n "${EDOT_LOCAL_CONTAINER_NAME:-}" ] || { echo "EDOT_LOCAL_CONTAINER_NAME not set"; return 1; }
edot_config_host_path="$script_dir/config/edot-collector/config.yaml"
image="docker.elastic.co/elastic-agent/elastic-otel-collector:${ES_LOCAL_VERSION}"
# shellcheck disable=SC2059
printf "Creating container '${EDOT_LOCAL_CONTAINER_NAME}' from image '${image}' ... "
# If container exists, do nothing.
if "$CONTAINER_CLI" ps -a --format '{{.Names}}' 2>/dev/null | grep -qxF "${EDOT_LOCAL_CONTAINER_NAME}"; then
echo "done (already exists)."
return 0
fi
# We use --env-file to allow the container to access ES_LOCAL_API_KEY - which is not initialized
# at this point.
if ! output=$("$CONTAINER_CLI" create \
--name "${EDOT_LOCAL_CONTAINER_NAME}" \
--network "${CONTAINER_NETWORK_NAME}" \
--hostname edot-collector \
--network-alias edot-collector \
-p "4317:4317" \
-p "4318:4318" \
-p "4320:4320" \
-v "${edot_config_host_path}:/etc/otelcol-contrib/config.yaml:ro" \
--env-file "$script_dir/.env" \
"${image}" \
--config=/etc/otelcol-contrib/config.yaml 2>&1); then
echo "failed."
printf '%s\n' "$output"
return 1
fi
echo "done (created)."
return 0
}
main() {
create_bridged_network
create_elasticsearch_container
if [ -n "${KIBANA_LOCAL_CONTAINER_NAME:-}" ]; then
create_kibana_container
fi
if [ -n "${EDOT_LOCAL_CONTAINER_NAME:-}" ]; then
create_edot_container
fi
}
main
EOM
chmod +x ./up.sh
}
write_start_script() {
cat > ./start.sh <<'EOM'
#!/bin/sh
# --------------------------------------------------------
# Run Elasticsearch and Kibana for local testing
# Note: do not use this script in a production environment
# --------------------------------------------------------
#
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
set -eu
script_dir="$(cd "$(dirname "$0")" && pwd)"
# shellcheck disable=SC1091
. "$script_dir/.env"
[ -n "${CONTAINER_CLI:-}" ] || { echo "CONTAINER_CLI not set"; exit 1; }
command -v "$CONTAINER_CLI" >/dev/null 2>&1 || { echo "Error: '$CONTAINER_CLI' not found."; exit 1; }
wait_for_healthcheck() {
# wait_for_healthcheck <container-name> <check-cmd>
name=${1:?name required}
shift
check_cmd="$*"
timeout_seconds=${HEALTHCHECK_TIMEOUT:-300}
delay_seconds=${HEALTHCHECK_DELAY:-10}
start_time=$(date +%s)
# shellcheck disable=SC2059
printf "Waiting for '${name}' ... "
while :; do
# Execute the check command inside the container; it should return 0 on success.
if $CONTAINER_CLI exec "${name}" sh -c "$check_cmd" >/dev/null 2>&1 ; then
echo "healthy."
return 0
fi
now=$(date +%s)
if [ $((now - start_time)) -ge "${timeout_seconds}" ]; then
echo "timed out."
return 1
fi
sleep "${delay_seconds}"
done
}
start_container() {
# start_container <container-name>
cname=${1:?container name required}
# shellcheck disable=SC2059
printf "Starting container '${cname}' ... "
if ! $CONTAINER_CLI inspect "${cname}" >/dev/null 2>&1; then
echo "failed (does not exist)."
return 1
fi
if $CONTAINER_CLI ps --format '{{.Names}}' 2>/dev/null | grep -qxF "${cname}"; then
echo "done (already running)."
return 0
fi
if ! output=$("$CONTAINER_CLI" start "${cname}" 2>&1); then
echo "failed."
printf '%s\n' "$output"
return 1
fi
echo "done."
return 0
}
configure_kibana_system_user_password() {
printf "Setting up 'kibana_system' user password ... "
start_time=$(date +%s)
timeout_seconds=60
until \
curl \
-s \
-X POST \
"${ES_LOCAL_URL}/_security/user/kibana_system/_password" \
-d "{\"password\":\"${KIBANA_LOCAL_PASSWORD}\"}" \
-H "Authorization: ApiKey ${ES_LOCAL_API_KEY}" \
-H "Content-Type: application/json" \
| grep -q "^{}"; \
do
now=$(date +%s)
if [ $((now - start_time)) -ge "${timeout_seconds}" ]; then
printf "\n"
echo "timed out.";
exit 1
fi
sleep 2
done
echo "done."
}
create_elasticsearch_api_key() {
printf "Creating Elasticsearch API key ... "
status=0
response=$(curl \
-s \