Skip to content

Commit

Permalink
Add offline docker example for air gapped networks (#160)
Browse files Browse the repository at this point in the history
This adds an example of how to use heartbeat / synthetics in an air
gapped network, where our calls to NPM won't work. Users will have to
pre-bake a container on a machine with internet access and ship that
behind their firewall.
  • Loading branch information
andrewvc authored Dec 18, 2020
1 parent b07443a commit c54f980
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini
ENTRYPOINT ["/tini", "--", "/usr/local/bin/docker-entrypoint"]

ENV SUITES_DIR=/usr/share/heartbeat/suites

# or docker run your-image /your/program ...
RUN echo /usr/share/heartbeat/.node \\
/usr/share/heartbeat/.npm \\
/usr/share/heartbeat/.cache \\
/usr/share/heartbeat/.config \\
/opt/elastic-synthetics | xargs -IDIR sh -c "mkdir DIR && chown -R heartbeat DIR"
$SUITES_DIR \\
/opt/elastic-synthetics | xargs -IDIR sh -c "mkdir DIR && chown -R heartbeat:heartbeat DIR"
ENV NODE_PATH=/usr/share/heartbeat/.node
USER heartbeat
RUN cd /usr/share/heartbeat/.node \\
Expand Down
2 changes: 1 addition & 1 deletion examples/docker/run-build-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
pushd ../../
./build-docker.sh $1
popd
./run.sh heartbeat-synthetics-local $@
./run.sh heartbeat-synthetics-local "${@:2}"
4 changes: 3 additions & 1 deletion examples/docker/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ else
IMAGE=$VERSION
fi

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

echo "Using image '$IMAGE' with extra args: $HEARTBEAT_ARGS"
docker run \
--rm \
--name=heartbeat \
--user=heartbeat \
--net=host \
--security-opt seccomp=seccomp_profile.json \
--security-opt seccomp=$SCRIPT_DIR/seccomp_profile.json \
--volume="$(pwd)/heartbeat.docker.yml:/usr/share/heartbeat/heartbeat.yml:ro" \
--volume="$(pwd)/../../:/opt/elastic-synthetics:rw" \
$IMAGE \
Expand Down
30 changes: 30 additions & 0 deletions examples/todos/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This Dockerfile illustrates the usage of synthetics in an air gapped environment, where
# public internet access is not available. In this situation you'll want to create
# a custom image using our official docker image as a base.

# Use our synthetics image as a base.
ARG STACK_VERSION=latest
FROM docker.elastic.co/experimental/synthetics:${STACK_VERSION}-synthetics
# Use the line below if you're using a custom base image, usually only for
# developers
#FROM heartbeat-synthetics-local

# This flag variable will prevent heartbeat from running `npm i` or
# similar commands that depend on an internet connection.
# We'll have to do that work now, when we bake the image.
ENV ELASTIC_SYNTHETICS_OFFLINE=true

# Copy our heartbeat config directly to the image.
# This could be done as a shared mount instead, but if we're
# baking an image anyway, this may be something that may be easier
# to do in this manner.
COPY heartbeat.docker.yml /usr/share/heartbeat/heartbeat.yml

RUN mkdir -p $SUITES_DIR/todos
# Copy your custom synthetics tests into a folder on the image
COPY . $SUITES_DIR/todos/

# Install NPM deps locally on this image
# Please note that it's important to run both `npm install` AND `npm install playwright`
# for more see this issue: https://github.com/microsoft/playwright/issues/3712
RUN cd $SUITES_DIR/todos && npm install
7 changes: 7 additions & 0 deletions examples/todos/build-offline-dockerfile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
# This demonstrates building and tagging a custom offline docker heartbeat image with synthetics
# with all dependencies pre-bundled.

# You'll want to run this in an environment with internet access so that NPM deps can be installed,
# then, take the resultant image and transfer that to your air gapped network.
docker build --build-arg STACK_VERSION=7.10.0 -t my-custom-heartbeat .
12 changes: 12 additions & 0 deletions examples/todos/heartbeat.docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
seccomp.enabled: false
heartbeat.config.monitors:
path: "${path.config}/monitors.d/*.yml"
reload.enabled: false
reload.period: 5s

heartbeat.synthetic_suites:
- name: Todos
# SUITES_DIR is an environment var provided by the docker container
path: "${SUITES_DIR}/todos"
schedule: "@every 1m"

0 comments on commit c54f980

Please sign in to comment.