Skip to content

Commit 8042eab

Browse files
committed
Replace compose with custom scripts
1 parent 2a82f73 commit 8042eab

16 files changed

+1128
-849
lines changed

.github/workflows/lint.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on: # yamllint disable-line rule:truthy
44
pull_request:
55
types: [opened, synchronize, reopened]
66
push:
7+
branches:
8+
- main
79
paths-ignore:
810
- 'README.md'
911
- '.editorconfig'

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ jobs:
3737
sudo snap remove docker
3838
sudo apt-get remove -y docker docker-engine docker.io containerd runc docker-ce docker-ce-cli
3939
sudo apt-get -y install podman
40-
sudo pip3 install podman-compose
4140
4241
- name: Run the tests
42+
env:
43+
TEST_CONTAINER_CLI: ${{ matrix.podman && 'podman' || 'docker' }}
4344
run: |
4445
./lib/bashunit

container-create.sh

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
#!/bin/sh
2+
# --------------------------------------------------------
3+
# Run Elasticsearch and Kibana for local testing
4+
# Note: do not use this script in a production environment
5+
# --------------------------------------------------------
6+
#
7+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
8+
# or more contributor license agreements. See the NOTICE file distributed with
9+
# this work for additional information regarding copyright
10+
# ownership. Elasticsearch B.V. licenses this file to you under
11+
# the Apache License, Version 2.0 (the "License"); you may
12+
# not use this file except in compliance with the License.
13+
# You may obtain a copy of the License at
14+
#
15+
# http://www.apache.org/licenses/LICENSE-2.0
16+
#
17+
# Unless required by applicable law or agreed to in writing,
18+
# software distributed under the License is distributed on an
19+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
# KIND, either express or implied. See the License for the
21+
# specific language governing permissions and limitations
22+
# under the License.
23+
24+
set -eu
25+
26+
script_dir="$(cd "$(dirname "$0")" && pwd)"
27+
. "$script_dir/.env"
28+
29+
[ -n "${CONTAINER_CLI:-}" ] || { echo "CONTAINER_CLI not set"; exit 1; }
30+
command -v "$CONTAINER_CLI" >/dev/null 2>&1 || { echo "Error: '$CONTAINER_CLI' not found."; exit 1; }
31+
32+
create_bridged_network() {
33+
[ -n "${CONTAINER_NETWORK_NAME:-}" ] || { echo "CONTAINER_NETWORK_NAME not set"; return 1; }
34+
35+
printf "Creating network '$CONTAINER_NETWORK_NAME' ... "
36+
37+
# If network already exists, nothing to do.
38+
if "$CONTAINER_CLI" network inspect "$CONTAINER_NETWORK_NAME" >/dev/null 2>&1; then
39+
echo "done (already exists)."
40+
return 0
41+
fi
42+
43+
# Create a bridged network and let the CLI/CNI pick the default subnet/gateway.
44+
if "$CONTAINER_CLI" network create --driver bridge "$CONTAINER_NETWORK_NAME" >/dev/null 2>&1; then
45+
echo "done (created)."
46+
return 0
47+
fi
48+
49+
# Some Podman versions use CNI and accept 'podman network create' without --driver.
50+
if [ "$CONTAINER_CLI" = "podman" ]; then
51+
if "$CONTAINER_CLI" network create "$CONTAINER_NETWORK_NAME" >/dev/null 2>&1; then
52+
echo "done (created)."
53+
return 0
54+
fi
55+
fi
56+
57+
echo "failed."
58+
return 1
59+
}
60+
61+
create_elasticsearch_container() {
62+
[ -n "${CONTAINER_NETWORK_NAME:-}" ] || { echo "CONTAINER_NETWORK_NAME not set"; return 1; }
63+
[ -n "${ES_LOCAL_VERSION:-}" ] || { echo "ES_LOCAL_VERSION not set"; return 1; }
64+
[ -n "${ES_LOCAL_CONTAINER_NAME:-}" ] || { echo "ES_LOCAL_CONTAINER_NAME not set"; return 1; }
65+
[ -n "${ES_LOCAL_PORT:-}" ] || { echo "ES_LOCAL_PORT not set"; return 1; }
66+
[ -n "${ES_LOCAL_PASSWORD:-}" ] || { echo "ES_LOCAL_PASSWORD not set"; return 1; }
67+
68+
ES_LOCAL_JAVA_OPTS=${ES_LOCAL_JAVA_OPTS:-}
69+
ES_LOCAL_DISK_SPACE_REQUIRED=${ES_LOCAL_DISK_SPACE_REQUIRED:-85%}
70+
71+
image="docker.elastic.co/elasticsearch/elasticsearch:${ES_LOCAL_VERSION}"
72+
73+
printf "Creating container '${ES_LOCAL_CONTAINER_NAME}' from image '${image}' ... "
74+
75+
# If container exists, do nothing.
76+
if "$CONTAINER_CLI" ps -a --format '{{.Names}}' 2>/dev/null | grep -qxF "${ES_LOCAL_CONTAINER_NAME}"; then
77+
echo "done (already exist)."
78+
return 0
79+
fi
80+
81+
# Create container.
82+
"$CONTAINER_CLI" create \
83+
--name "${ES_LOCAL_CONTAINER_NAME}" \
84+
--network "${CONTAINER_NETWORK_NAME}" \
85+
--hostname elasticsearch \
86+
--network-alias elasticsearch \
87+
-p "127.0.0.1:${ES_LOCAL_PORT}:9200" \
88+
-v "dev-elasticsearch:/usr/share/elasticsearch/data" \
89+
-e "discovery.type=single-node" \
90+
-e "ELASTIC_PASSWORD=${ES_LOCAL_PASSWORD}" \
91+
-e "xpack.security.enabled=true" \
92+
-e "xpack.security.http.ssl.enabled=false" \
93+
-e "xpack.license.self_generated.type=trial" \
94+
-e "xpack.ml.use_auto_machine_memory_percent=true" \
95+
-e "ES_JAVA_OPTS=${ES_LOCAL_JAVA_OPTS}" \
96+
-e "cluster.routing.allocation.disk.watermark.low=${ES_LOCAL_DISK_SPACE_REQUIRED}" \
97+
-e "cluster.routing.allocation.disk.watermark.high=${ES_LOCAL_DISK_SPACE_REQUIRED}" \
98+
-e "cluster.routing.allocation.disk.watermark.flood_stage=${ES_LOCAL_DISK_SPACE_REQUIRED}" \
99+
"${image}" >/dev/null 2>&1 || {
100+
echo "failed."
101+
return 1
102+
}
103+
104+
echo "done (created)."
105+
return 0
106+
}
107+
108+
create_kibana_container() {
109+
[ -n "${CONTAINER_NETWORK_NAME:-}" ] || { echo "CONTAINER_NETWORK_NAME not set"; return 1; }
110+
[ -n "${ES_LOCAL_VERSION:-}" ] || { echo "ES_LOCAL_VERSION not set"; return 1; }
111+
[ -n "${KIBANA_LOCAL_CONTAINER_NAME:-}" ] || { echo "KIBANA_LOCAL_CONTAINER_NAME not set"; return 1; }
112+
[ -n "${KIBANA_LOCAL_PORT:-}" ] || { echo "KIBANA_LOCAL_PORT not set"; return 1; }
113+
[ -n "${KIBANA_LOCAL_PASSWORD:-}" ] || { echo "KIBANA_LOCAL_PASSWORD not set"; return 1; }
114+
115+
ES_LOCAL_PORT=${ES_LOCAL_PORT:-9200}
116+
telemetry_host_path="$script_dir/config/telemetry.yml"
117+
118+
image="docker.elastic.co/kibana/kibana:${ES_LOCAL_VERSION}"
119+
120+
printf "Creating container '${KIBANA_LOCAL_CONTAINER_NAME}' from image '${image}' ... "
121+
122+
# Do nothing if container already exists.
123+
if "$CONTAINER_CLI" ps -a --format '{{.Names}}' 2>/dev/null | grep -qxF "${KIBANA_LOCAL_CONTAINER_NAME}"; then
124+
echo "done (already exists)."
125+
return 0
126+
fi
127+
128+
# Optional telemetry mount if file exists.
129+
if [ -f "${telemetry_host_path}" ]; then
130+
telemetry_mount="-v ${telemetry_host_path}:/usr/share/kibana/config/telemetry.yml:ro"
131+
else
132+
telemetry_mount=""
133+
fi
134+
135+
$CONTAINER_CLI create \
136+
--name "${KIBANA_LOCAL_CONTAINER_NAME}" \
137+
--network "${CONTAINER_NETWORK_NAME}" \
138+
--hostname kibana \
139+
--network-alias kibana \
140+
-p "127.0.0.1:${KIBANA_LOCAL_PORT}:5601" \
141+
-v "dev-kibana:/usr/share/kibana/data" \
142+
$telemetry_mount \
143+
-e "SERVER_NAME=kibana" \
144+
-e "ELASTICSEARCH_HOSTS=http://elasticsearch:9200" \
145+
-e "ELASTICSEARCH_USERNAME=kibana_system" \
146+
-e "ELASTICSEARCH_PASSWORD=${KIBANA_LOCAL_PASSWORD}" \
147+
-e "XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY=${KIBANA_ENCRYPTION_KEY:-}" \
148+
-e "ELASTICSEARCH_PUBLICBASEURL=http://localhost:${ES_LOCAL_PORT}" \
149+
-e "XPACK_SPACES_DEFAULTSOLUTION=es" \
150+
"${image}" >/dev/null 2>&1 || {
151+
echo "failed."
152+
return 1
153+
}
154+
155+
echo "done (created)."
156+
return 0
157+
}
158+
159+
create_edot_container() {
160+
[ -n "${CONTAINER_NETWORK_NAME:-}" ] || { echo "CONTAINER_NETWORK_NAME not set"; return 1; }
161+
[ -n "${ES_LOCAL_VERSION:-}" ] || { echo "ES_LOCAL_VERSION not set"; return 1; }
162+
[ -n "${EDOT_LOCAL_CONTAINER_NAME:-}" ] || { echo "EDOT_LOCAL_CONTAINER_NAME not set"; return 1; }
163+
164+
edot_config_host_path="$script_dir/config/edot-collector/config.yaml"
165+
image="docker.elastic.co/elastic-agent/elastic-otel-collector:${ES_LOCAL_VERSION}"
166+
167+
printf "Creating container '${EDOT_LOCAL_CONTAINER_NAME}' from image '${image}' ... "
168+
169+
# If container exists, do nothing.
170+
if "$CONTAINER_CLI" ps -a --format '{{.Names}}' 2>/dev/null | grep -qxF "${EDOT_LOCAL_CONTAINER_NAME}"; then
171+
echo "done (already exists)."
172+
return 0
173+
fi
174+
175+
# We use --env-file to allow the container to access ES_LOCAL_API_KEY - which is not initialized
176+
# at this point.
177+
178+
$CONTAINER_CLI create \
179+
--name "${EDOT_LOCAL_CONTAINER_NAME}" \
180+
--network "${CONTAINER_NETWORK_NAME}" \
181+
--hostname edot-collector \
182+
--network-alias edot-collector \
183+
-p "4317:4317" \
184+
-p "4318:4318" \
185+
-v "${edot_config_host_path}:/etc/otelcol-contrib/config.yaml:ro" \
186+
--env-file "$script_dir/.env" \
187+
"${image}" \
188+
--config=/etc/otelcol-contrib/config.yaml >/dev/null 2>&1 || {
189+
echo "failed."
190+
return 1
191+
}
192+
193+
echo "done (created)."
194+
return 0
195+
}
196+
197+
main() {
198+
create_bridged_network
199+
create_elasticsearch_container
200+
201+
if [ -n "${KIBANA_LOCAL_CONTAINER_NAME:-}" ]; then
202+
create_kibana_container
203+
fi
204+
205+
if [ -n "${EDOT_LOCAL_CONTAINER_NAME:-}" ]; then
206+
create_edot_container
207+
fi
208+
}
209+
210+
main

container-destroy.sh

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/bin/sh
2+
# --------------------------------------------------------
3+
# Run Elasticsearch and Kibana for local testing
4+
# Note: do not use this script in a production environment
5+
# --------------------------------------------------------
6+
#
7+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
8+
# or more contributor license agreements. See the NOTICE file distributed with
9+
# this work for additional information regarding copyright
10+
# ownership. Elasticsearch B.V. licenses this file to you under
11+
# the Apache License, Version 2.0 (the "License"); you may
12+
# not use this file except in compliance with the License.
13+
# You may obtain a copy of the License at
14+
#
15+
# http://www.apache.org/licenses/LICENSE-2.0
16+
#
17+
# Unless required by applicable law or agreed to in writing,
18+
# software distributed under the License is distributed on an
19+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
# KIND, either express or implied. See the License for the
21+
# specific language governing permissions and limitations
22+
# under the License.
23+
24+
set -eu
25+
26+
script_dir="$(cd "$(dirname "$0")" && pwd)"
27+
. "$script_dir/.env"
28+
29+
[ -n "${CONTAINER_CLI:-}" ] || { echo "CONTAINER_CLI not set"; exit 1; }
30+
command -v "$CONTAINER_CLI" >/dev/null 2>&1 || { echo "Error: '$CONTAINER_CLI' not found."; exit 1; }
31+
32+
remove_bridged_network() {
33+
[ -n "${CONTAINER_NETWORK_NAME:-}" ] || { echo "CONTAINER_NETWORK_NAME not set"; return 1; }
34+
35+
printf "Removing network '${CONTAINER_NETWORK_NAME}' ... "
36+
37+
if ! "$CONTAINER_CLI" network inspect "${CONTAINER_NETWORK_NAME}" >/dev/null 2>&1; then
38+
echo "done (does not exist)."
39+
return 0
40+
fi
41+
42+
# Try graceful removal the network; if it's in use this will typically fail.
43+
"$CONTAINER_CLI" network rm "${CONTAINER_NETWORK_NAME}" >/dev/null 2>&1 || true
44+
45+
# Force remove.
46+
if "$CONTAINER_CLI" network rm -f "${CONTAINER_NETWORK_NAME}" >/dev/null 2>&1; then
47+
echo "done (removed)."
48+
return 0
49+
fi
50+
51+
echo "failed."
52+
53+
return 1
54+
}
55+
56+
remove_volume() {
57+
# remove_volumes <volume>
58+
[ -n "${1:-}" ] || { echo "Usage: remove_volume <volume-name>"; return 1; }
59+
60+
vol="$1"
61+
62+
printf "Removing volume '$vol' ... "
63+
64+
if ! "$CONTAINER_CLI" volume inspect "$vol" >/dev/null 2>&1; then
65+
echo "done (does not exist)."
66+
return 0
67+
fi
68+
69+
if "$CONTAINER_CLI" volume rm -f "$vol" >/dev/null 2>&1; then
70+
echo "done (removed)."
71+
return 0
72+
fi
73+
74+
echo "failed."
75+
76+
# If removal failed, attempt to list attached containers to help the user.
77+
echo "Volume '${vol}' may be in use."
78+
echo "Attached containers (if any):"
79+
"$CONTAINER_CLI" volume inspect "${vol}" 2>/dev/null || true
80+
echo "Disconnect or stop containers and retry: '${CONTAINER_CLI} volume rm -f ${vol}'"
81+
82+
return 1
83+
}
84+
85+
remove_container() {
86+
# remove_container <container-name>
87+
name=${1:?container name required}
88+
89+
printf "Removing container '$name' ... "
90+
91+
# If container doesn't exist, nothing to do.
92+
if ! "$CONTAINER_CLI" inspect "$name" >/dev/null 2>&1; then
93+
echo "done (does not exist)."
94+
return 0
95+
fi
96+
97+
# Try graceful stop (ignore errors if not running).
98+
"$CONTAINER_CLI" stop "$name" >/dev/null 2>&1 || true
99+
100+
# Force remove (will stop if still running).
101+
if "$CONTAINER_CLI" rm -f "$name" >/dev/null 2>&1; then
102+
echo "done (removed)."
103+
return 0
104+
fi
105+
106+
echo "failed."
107+
return 1
108+
}
109+
110+
remove_elasticsearch_container() {
111+
[ -n "${ES_LOCAL_CONTAINER_NAME:-}" ] || { echo "ES_LOCAL_CONTAINER_NAME not set"; return 1; }
112+
113+
remove_container "${ES_LOCAL_CONTAINER_NAME}"
114+
remove_volume dev-elasticsearch
115+
}
116+
117+
remove_kibana_container() {
118+
[ -n "${KIBANA_LOCAL_CONTAINER_NAME:-}" ] || { echo "KIBANA_LOCAL_CONTAINER_NAME not set"; return 1; }
119+
120+
remove_container "${KIBANA_LOCAL_CONTAINER_NAME}"
121+
remove_volume dev-kibana
122+
}
123+
124+
remove_edot_container() {
125+
[ -n "${EDOT_LOCAL_CONTAINER_NAME:-}" ] || { echo "EDOT_LOCAL_CONTAINER_NAME not set"; return 1; }
126+
127+
remove_container "${EDOT_LOCAL_CONTAINER_NAME}"
128+
}
129+
130+
main() {
131+
if [ -n "${EDOT_LOCAL_CONTAINER_NAME:-}" ]; then
132+
remove_edot_container
133+
fi
134+
135+
if [ -n "${KIBANA_LOCAL_CONTAINER_NAME:-}" ]; then
136+
remove_kibana_container
137+
fi
138+
139+
remove_elasticsearch_container
140+
remove_bridged_network
141+
}
142+
143+
main

0 commit comments

Comments
 (0)