Pull changes from upstream repository.

This commit is contained in:
Marek Skalický 2018-10-29 09:35:21 +00:00
commit c620ae86fe
No known key found for this signature in database
GPG key ID: ADCAA1DEE100A66D
51 changed files with 4074 additions and 862 deletions

View file

@ -1,26 +1,30 @@
FROM registry.fedoraproject.org/fedora:rawhide
LABEL MAINTAINER SoftwareCollections.org <sclorg@redhat.com>
FROM registry.fedoraproject.org/f28/s2i-base:latest
ENV NAME=mongodb \
VERSION=0 \
RELEASE=1 \
ARCH=x86_64
LABEL com.redhat.component="$NAME" \
name="$FGC/$NAME" \
version="$VERSION" \
release="$RELEASE.$DISTTAG" \
architecture="$ARCH" \
usage="docker run -d -e MONGODB_ADMIN_PASSWORD=my_pass $FGC/$NAME" \
help="help.1"
ENV SUMMARY="MongoDB NoSQL database server" \
DESCRIPTION="MongoDB (from humongous) is a free and open-source \
cross-platform document-oriented database program. Classified as a NoSQL \
database program, MongoDB uses JSON-like documents with schemas. This \
container image contains programs to run mongod server."
LABEL io.k8s.description="MongoDB is a scalable, high-performance, open source NoSQL database." \
io.k8s.display-name="MongoDB 3.4" \
LABEL summary="$SUMMARY" \
description="$DESCRIPTION" \
io.k8s.description="$DESCRIPTION" \
io.k8s.display-name="MongoDB 3.6" \
io.openshift.expose-services="27017:mongodb" \
io.openshift.tags="database,mongodb"
io.openshift.tags="database,mongodb" \
com.redhat.component="$NAME" \
name="$FGC/$NAME" \
usage="docker run -d -e MONGODB_ADMIN_PASSWORD=my_pass $FGC/$NAME" \
version="$VERSION" \
maintainer="SoftwareCollections.org <sclorg@redhat.com>"
ENV MONGODB_VERSION=3.4 \
ENV MONGODB_VERSION=3.6 \
# Set paths to avoid hard-coding them in scripts.
APP_DATA=/opt/app-root/src \
HOME=/var/lib/mongodb \
CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/mongodb
@ -29,20 +33,23 @@ EXPOSE 27017
ENTRYPOINT ["container-entrypoint"]
CMD ["run-mongod"]
RUN INSTALL_PKGS="bind-utils gettext iproute rsync tar findutils python3 mongodb mongodb-server mongo-tools" && \
RUN INSTALL_PKGS="bind-utils gettext iproute rsync tar hostname findutils shadow-utils mongodb mongodb-server mongo-tools groff-base" && \
dnf install -y --setopt=tsflags=nodocs $INSTALL_PKGS && \
rpm -V $INSTALL_PKGS && \
dnf clean all
ADD root /
COPY s2i/bin/ $STI_SCRIPTS_PATH
COPY root /
# Container setup
RUN : > /etc/mongod.conf && \
mkdir -p ${HOME}/data && \
# Set owner 'mongodb:0' and 'g+rw(x)' permission - to avoid problems running container with arbitrary UID
/usr/libexec/fix-permissions /etc/mongod.conf ${CONTAINER_SCRIPTS_PATH}/mongodb.conf.template \
${HOME}
/usr/libexec/fix-permissions /etc/mongod.conf ${CONTAINER_SCRIPTS_PATH}/mongod.conf.template \
${HOME} ${APP_DATA}/.. && \
usermod -a -G root mongodb
VOLUME ["/var/lib/mongodb/data"]

1
Dockerfile.fedora Symbolic link
View file

@ -0,0 +1 @@
Dockerfile

1
help.md Symbolic link
View file

@ -0,0 +1 @@
README.md

View file

@ -1,153 +1,290 @@
.\"t
.\" WARNING: Do not edit this file manually, it is generated from README.md automatically.
.\"
.\"t
.\" Automatically generated by Pandoc 1.16.0.2
.\"
.TH "MONGODB\-32\-RHEL7" "1" "February 22, 2017" "Container Image Pages" ""
.hy
.SH MongoDB Docker image
.TH MongoDB 3.6 NoSQL Database Server container image
.PP
This repository contains Dockerfiles for MongoDB images for general
usage and OpenShift.
This repository contains Dockerfiles for MongoDB images for general usage and OpenShift.
Users can choose between RHEL and CentOS based images.
.SS Environment variables
The RHEL image is available in the Red Hat Container Catalog
\[la]https://access.redhat.com/containers/#/registry.access.redhat.com/rhscl/mongodb-36-rhel7\[ra]
as registry.access.redhat.com/rhscl/mongodb\-36\-rhel7.
The CentOS image is then available on Docker Hub
\[la]https://hub.docker.com/r/centos/mongodb-36-centos7/\[ra]
as centos/mongodb\-36\-centos7.
.SH Description
.PP
The image recognizes the following environment variables that you can
set during initialization by passing \f[C]\-e\ VAR=VALUE\f[] to the
Docker run command.
This container image provides a containerized packaging of the MongoDB mongod daemon
and client application. The mongod server daemon accepts connections from clients
and provides access to content from MongoDB databases on behalf of the clients.
You can find more information on the MongoDB project from the project Web site
(
\[la]https://www.mongodb.com/\[ra]).
.SH Usage
.PP
.TS
tab(@);
l l.
T{
Variable name
T}@T{
Description
T}
_
T{
\f[C]MONGODB_USER\f[]
T}@T{
User name for MONGODB account to be created
T}
T{
\f[C]MONGODB_PASSWORD\f[]
T}@T{
Password for the user account
T}
T{
\f[C]MONGODB_DATABASE\f[]
T}@T{
Database name
T}
T{
\f[C]MONGODB_ADMIN_PASSWORD\f[]
T}@T{
Password for the admin user
T}
.TE
For this, we will assume that you are using the \fB\fCrhscl/mongodb\-36\-rhel7\fR image.
If you want to set only the mandatory environment variables and store the database
in the \fB\fC/home/user/database\fR directory on the host filesystem, execute the following command:
.PP
The following environment variables influence the MongoDB configuration
file.
They are all optional.
.PP
.TS
tab(@);
lw(13.6n) lw(45.8n) lw(10.5n).
T{
Variable name
T}@T{
Description
T}@T{
Default
T}
_
T{
\f[C]MONGODB_QUIET\f[]
T}@T{
Runs MongoDB in a quiet mode that attempts to limit the amount of
output.
T}@T{
true
T}
.TE
.PP
You can also set the following mount points by passing the
\f[C]\-v\ /host:/container\f[] flag to Docker.
.PP
.TS
tab(@);
l l.
T{
Volume mount point
T}@T{
Description
T}
_
T{
\f[C]/var/lib/mongodb/data\f[]
T}@T{
MongoDB data directory
T}
.TE
.PP
\f[B]Notice: When mouting a directory from the host into the container,
ensure that the mounted directory has the appropriate permissions and
that the owner and group of the directory matches the user UID or name
which is running inside the container.\f[]
.SS Usage
.PP
For this, we will assume that you are using the
\f[C]centos/mongodb\-32\-centos7\f[] image.
If you want to set only the mandatory environment variables and store
the database in the \f[C]/home/user/database\f[] directory on the host
filesystem, execute the following command:
.IP
.RS
.nf
\f[C]
$\ docker\ run\ \-d\ \-e\ MONGODB_USER=<user>\ \-e\ MONGODB_PASSWORD=<password>\ \-e\ MONGODB_DATABASE=<database>\ \-e\ MONGODB_ADMIN_PASSWORD=<admin_password>\ \-v\ /home/user/database:/var/lib/mongodb/data\ centos/mongodb\-32\-centos7
\f[]
$ docker run \-d \-e MONGODB\_USER=<user> \-e MONGODB\_PASSWORD=<password> \-e MONGODB\_DATABASE=<database> \-e MONGODB\_ADMIN\_PASSWORD=<admin\_password> \-v /home/user/database:/var/lib/mongodb/data rhscl/mongodb\-36\-rhel7
.fi
.RE
.PP
If you are initializing the database and it\[aq]s the first time you are
using the specified shared volume, the database will be created with two
users: \f[C]admin\f[] and \f[C]MONGODB_USER\f[].
After that the MongoDB daemon will be started.
If you are re\-attaching the volume to another container, the creation
of the database user and admin user will be skipped and only the MongoDB
daemon will be started.
.SS Custom configuration file
If you are initializing the database and it's the first time you are using the
specified shared volume, the database will be created with two users: \fB\fCadmin\fR
and \fB\fCMONGODB\_USER\fR\&. After that the MongoDB daemon will be started. If you are
re\-attaching the volume to another container, the creation of the database
user and admin user will be skipped, passwords of users will be changed and
only the MongoDB daemon will be started.
.SH Environment variables and volumes
.PP
It is allowed to use custom configuration file for mongod server.
Providing a custom configuration file supercedes the individual
configuration environment variable values.
The image recognizes the following environment variables that you can set
during initialization by passing \fB\fC\-e VAR=VALUE\fR to the Docker run command.
.PP
To use custom configuration file in container it has to be mounted into
\f[C]/etc/mongod.conf\f[].
For example to use configuration file stored in \f[C]/home/user\f[]
directory use this option for \f[C]docker\ run\f[] command:
\f[C]\-v\ /home/user/mongod.conf:/etc/mongod.conf:Z\f[].
\fB\fB\fCMONGODB\_ADMIN\_PASSWORD\fR\fP
.br
Password for the admin user
.PP
\f[B]Notice: Custom config file does not affect name of replica set. It
has to be set in \f[C]MONGODB_REPLICA_NAME\f[] environment variable.\f[]
.SS MongoDB admin user
Optionally you can provide settings for user with 'readWrite' role.
(Note you MUST specify all three of these settings)
.PP
The admin user name is set to \f[C]admin\f[] and you have to to specify
the password by setting the \f[C]MONGODB_ADMIN_PASSWORD\f[] environment
variable.
This process is done upon database initialization.
.SS Changing passwords
\fB\fB\fCMONGODB\_USER\fR\fP
.br
User name for MONGODB account to be created
.PP
Since passwords are part of the image configuration, the only supported
method to change passwords for the database user (\f[C]MONGODB_USER\f[])
and admin user is by changing the environment variables
\f[C]MONGODB_PASSWORD\f[] and \f[C]MONGODB_ADMIN_PASSWORD\f[],
respectively.
\fB\fB\fCMONGODB\_PASSWORD\fR\fP
.br
Password for the user account
.PP
Changing database passwords directly in MongoDB will cause a mismatch
between the values stored in the variables and the actual passwords.
Whenever a database container starts it will reset the passwords to the
values stored in the environment variables.
.SH AUTHORS
Red Hat.
\fB\fB\fCMONGODB\_DATABASE\fR\fP
.br
Database name
.PP
The following environment variables influence the MongoDB configuration file.
They are all optional.
.PP
\fB\fB\fCMONGODB\_QUIET (default: true)\fR\fP
.br
Runs MongoDB in a quiet mode that attempts to limit the amount of output.
.PP
You can also set the following mount points by passing the \fB\fC\-v
/host:/container\fR flag to Docker.
.PP
\fB\fB\fC/var/lib/mongodb/data\fR\fP
.br
MongoDB data directory
.PP
\fBNotice: When mounting a directory from the host into the container, ensure
that the mounted directory has the appropriate permissions and that the owner
and group of the directory matches the user UID or name which is running
inside the container.\fP
.SH MongoDB admin user
.PP
The admin user name is set to \fB\fCadmin\fR and you have to to specify the password by
setting the \fB\fCMONGODB\_ADMIN\_PASSWORD\fR environment variable.
.PP
This user has 'dbAdminAnyDatabase', 'userAdminAnyDatabase',
'readWriteAnyDatabase', 'clusterAdmin' roles (for more information see
MongoDB
reference
\[la]https://docs.mongodb.com/manual/reference/built-in-roles/\[ra]).
.SH Optional unprivileged user
.PP
The user with \fB\fC$MONGODB\_USER\fR name is created in database \fB\fC$MONGODB\_DATABASE\fR
and you have to to specify the password by setting the \fB\fCMONGODB\_PASSWORD\fR
environment variable.
.PP
This user has only 'readWrite' role in the database.
.SH Changing passwords
.PP
Since passwords are part of the image configuration, the only supported method
to change passwords for the database user (\fB\fCMONGODB\_USER\fR) and admin user is
by changing the environment variables \fB\fCMONGODB\_PASSWORD\fR and
\fB\fCMONGODB\_ADMIN\_PASSWORD\fR, respectively.
.PP
Changing database passwords directly in MongoDB will cause a mismatch between
the values stored in the variables and the actual passwords. Whenever a
database container starts it will reset the passwords to the values stored in
the environment variables.
.SH Extending image
.PP
This image can be extended using
source\-to\-image
\[la]https://github.com/openshift/source-to-image\[ra]\&.
.PP
For example to build customized MongoDB database image \fB\fCmy\-mongodb\-centos7\fR
with configuration in \fB\fC\~/image\-configuration/\fR run:
.PP
.RS
.nf
$ s2i build \~/image\-configuration/ centos/mongodb\-36\-centos7 my\-mongodb\-centos7
.fi
.RE
.PP
The directory passed to \fB\fCs2i build\fR should contain one or more of the
following directories:
.ti 0
\l'\n(.lu'
.SS \fB\fCmongodb\-cfg/\fR
.PP
when running \fB\fCrun\-mongod\fR or \fB\fCrun\-mongod\-replication\fR commands contained
\fB\fCmongod.conf\fR file is used for \fB\fCmongod\fR configuration
.PP
.RS
.nf
\- `envsubst` command is run on this file to still allow customization of
the image using environment variables
\- custom configuration file does not affect name of replica set \- it has
to be set in `MONGODB\_REPLICA\_NAME` environment variable
\- it is not possible to configure SSL using custom configuration file
.fi
.RE
.SS \fB\fCmongodb\-ssl/\fR
.PP
SSL/TLS certificates used to configure MongoDB server SSL/TLS support
.PP
\fBNotice: To allow connections from internal scripts it is required to have \fB\fClocalhost\fR specified in SAN filed of SSL certificate.\fP
.PP
.RS
.nf
\- `mongodb.pem` \- file containing a public key certificate and its
associated private key. See [upstream
documentation](https://docs.mongodb.com/manual/tutorial/configure\-ssl/#pem\-file)
(encryption of private key is not supported).
\- `ca.pem` \- optional file containing the root certificate chain from
the Certificate Authority. See [upstream
documentation](https://docs.mongodb.com/manual/tutorial/configure\-ssl/#set\-up\-mongod\-and\-mongos\-with\-certificate\-validation)
.fi
.RE
.SS \fB\fCmongodb\-pre\-init/\fR
.PP
contained shell scripts (\fB\fC*.sh\fR) are sourced before \fB\fCmongod\fR server is
started
.SS \fB\fCmongodb\-init/\fR
.PP
contained shell scripts (\fB\fC*.sh\fR) are sourced when \fB\fCmongod\fR server is
started for the first time (= datadir is empty)
.PP
.RS
.nf
\- `run\-mongod` command doesn't have enabled authentication in this phase
\- `run\-mongod\-replication` command has enabled authentication in this phase
.fi
.RE
.SS \fB\fCmongodb\-start/\fR
.PP
same sematics as \fB\fCmongodb\-init/\fR, except that these scripts are always sourced
(after \fB\fCmongodb\-init/\fR scripts, if they exist)
.PP
these scripts are skipped if \fB\fCrun\-mongod\-replication\fR is run with already
initialized data directory
.ti 0
\l'\n(.lu'
.PP
Variables that can be used in the scripts provided to s2i:
.PP
.RS
.nf
\- `mongo\_common\_args` \-\- contains arguments for the `mongod` server (changing
this can break existing customization scripts, e.g. default scripts)
\- `shell\_args` \-\- arguments to mongo shell which should be used with every `mongo`
invocation (e.g. it's used to set parameters for SSL connection). Same as in
internal scripts, `mongo\_cmd` function, which uses `shell\_args`, can be used.
\- `$MEMBER\_ID` \-\- contains 'id' of the container. It is defined only in
scripts for replication (`run\-mongod\-replication` command) and has different
value for each container in a replicaset cluster. Customization scripts are
run by all containers in replicaset \- `MEMBER\_ID` can be used to write scripts
which are run only by some container.
.fi
.RE
.PP
During \fB\fCs2i build\fR all provided files are copied into \fB\fC/opt/app\-root/src\fR
directory in the new image. If some configuration files are present in
destination directory, files with the same name are overwritten. Also only one
file with the same name can be used for customization and user provided files
are preferred over default files in \fB\fC/usr/share/container\-scripts/mongodb/\fR\-
so it is possible to overwrite them.
.PP
Same configuration directory structure can be used to customize the image
every time the image is started using \fB\fCdocker run\fR\&. The directory have to be
mounted into \fB\fC/opt/app\-root/src/\fR in the image (\fB\fC\-v
./image\-configuration/:/opt/app\-root/src/\fR). This overwrites customization
built into the image.
.SH Troubleshooting
.PP
The mongod deamon in the container logs to the standard output, so the log is available in the container log. The log can be examined by running:
.PP
.RS
.nf
docker logs <container>
.fi
.RE
.SH See also
.PP
Dockerfile and other sources for this container image are available on
\[la]https://github.com/sclorg/mongodb-container\[ra]\&.
In that repository, Dockerfile for CentOS is called Dockerfile, Dockerfile
for RHEL is called Dockerfile.rhel7.

View file

@ -15,39 +15,26 @@ function cleanup() {
trap 'cleanup' SIGINT SIGTERM
check_env_vars
mongo_common_args=
setup_wiredtiger_cache ${CONTAINER_SCRIPTS_PATH}/mongodb.conf.template
# If user provides own config file use it and do not generate new one
if [ ! -s $MONGODB_CONFIG_PATH ]; then
# Generate config file for MongoDB
envsubst < ${CONTAINER_SCRIPTS_PATH}/mongodb.conf.template > $MONGODB_CONFIG_PATH
fi
# pre-init files
process_extending_files ${APP_DATA}/mongodb-pre-init/ ${CONTAINER_SCRIPTS_PATH}/pre-init/
mongo_common_args="-f $MONGODB_CONFIG_PATH"
setup_default_datadir
datadir_files="$(ls -A $MONGODB_DATADIR)"
# Must bring up MongoDB on localhost only until it has an admin password set.
mongod $mongo_common_args --bind_ip 127.0.0.1 &
mongod $mongo_common_args --bind_ip localhost --noauth &
wait_for_mongo_up
js_command="db.system.users.count({'user':'admin', 'db':'admin'})"
if [ "$(mongo admin --quiet --eval "$js_command")" == "1" ]; then
echo "=> Admin user is already created. Resetting password ..."
mongo_reset_admin
else
mongo_create_admin
fi
if [[ -v CREATE_USER ]]; then
js_command="db.system.users.count({'user':'${MONGODB_USER}', 'db':'${MONGODB_DATABASE}'})"
if [ "$(mongo admin --quiet --eval "$js_command")" == "1" ]; then
echo "=> MONGODB_USER user is already created. Resetting password ..."
mongo_reset_user
else
mongo_create_user
fi
fi
# init files - on first start
[ "$datadir_files" ] || process_extending_files ${APP_DATA}/mongodb-init/ ${CONTAINER_SCRIPTS_PATH}/init/
# start files - on every start
process_extending_files ${APP_DATA}/mongodb-start/ ${CONTAINER_SCRIPTS_PATH}/start/
# Restart the MongoDB daemon to bind on all interfaces
mongod $mongo_common_args --shutdown
wait_for_mongo_down

View file

@ -26,30 +26,36 @@ function cleanup() {
trap 'cleanup' SIGINT SIGTERM
REPLICATION=1 check_env_vars
# StatefulSet pods are named with a predictable name, following the pattern:
# $(statefulset name)-$(zero-based index)
# MEMBER_ID is computed by removing the prefix matching "*-", i.e.:
# "mongodb-0" -> "0"
# "mongodb-1" -> "1"
# "mongodb-2" -> "2"
export readonly MEMBER_ID="${HOSTNAME##*-}"
setup_wiredtiger_cache ${CONTAINER_SCRIPTS_PATH}/mongodb.conf.template
mongo_common_args=
# If user provides own config file use it and do not generate new one
if [ ! -s "${MONGODB_CONFIG_PATH}" ]; then
# Generate config file for MongoDB
envsubst < "${CONTAINER_SCRIPTS_PATH}/mongodb.conf.template" > "${MONGODB_CONFIG_PATH}"
fi
mongo_common_args="-f ${MONGODB_CONFIG_PATH}"
# pre-init files
process_extending_files ${APP_DATA}/mongodb-pre-init/ ${CONTAINER_SCRIPTS_PATH}/pre-init/
# Attention: setup_keyfile may modify value of mongo_common_args!
setup_keyfile
setup_default_datadir
${CONTAINER_SCRIPTS_PATH}/init-replset.sh &
# TODO: capture exit code of `init-petset-replset.sh` and exit with an error if
# the initialization failed, so that the container will be restarted and the
# user can gain more visibility that there is a problem in a way other than just
# inspecting log messages.
datadir_files="$(ls -A $MONGODB_DATADIR)"
# Make sure env variables don't propagate to mongod process.
unset MONGODB_USER MONGODB_PASSWORD MONGODB_DATABASE MONGODB_ADMIN_PASSWORD
mongod ${mongo_common_args} --replSet "${MONGODB_REPLICA_NAME}" &
(unset MONGODB_USER MONGODB_PASSWORD MONGODB_DATABASE MONGODB_ADMIN_PASSWORD
mongod ${mongo_common_args} --replSet "${MONGODB_REPLICA_NAME}") &
# Initialize the replicaset or add container to it
${CONTAINER_SCRIPTS_PATH}/init-replset.sh
# init files - on first start
[ -f /tmp/initialized ] || [ "$datadir_files" ] || process_extending_files ${APP_DATA}/mongodb-init/ ${CONTAINER_SCRIPTS_PATH}/init/
# start files - on every start
[ -f /tmp/initialized ] || process_extending_files ${APP_DATA}/mongodb-start/ ${CONTAINER_SCRIPTS_PATH}/start/
>/tmp/initialized
wait

4
root/usr/bin/usage Executable file
View file

@ -0,0 +1,4 @@
#!/bin/bash
cat /usr/share/container-scripts/mongodb/README.md

View file

@ -1,4 +1,4 @@
#!/usr/bin/python3
#!/usr/bin/python
"""
Script for parsing cgroup information

View file

@ -1,90 +1,240 @@
MongoDB Docker image
MongoDB 3.6 NoSQL Database Server container image
====================
This repository contains Dockerfiles for MongoDB images for general usage and OpenShift.
Users can choose between RHEL and CentOS based images.
Environment variables
---------------------------------
The image recognizes the following environment variables that you can set during
initialization by passing `-e VAR=VALUE` to the Docker run command.
| Variable name | Description |
| :------------------------ | ----------------------------------------- |
| `MONGODB_ADMIN_PASSWORD` | Password for the admin user |
Optionally you can provide settings for user with 'readWrite' role.
| Variable name | Description |
| :------------------------ | ----------------------------------------- |
| `MONGODB_USER` | User name for MONGODB account to be created |
| `MONGODB_PASSWORD` | Password for the user account |
| `MONGODB_DATABASE` | Database name |
The RHEL image is available in the [Red Hat Container Catalog](https://access.redhat.com/containers/#/registry.access.redhat.com/rhscl/mongodb-36-rhel7)
as registry.access.redhat.com/rhscl/mongodb-36-rhel7.
The CentOS image is then available on [Docker Hub](https://hub.docker.com/r/centos/mongodb-36-centos7/)
as centos/mongodb-36-centos7.
The following environment variables influence the MongoDB configuration file. They are all optional.
Description
-----------
| Variable name | Description | Default
| :-------------------- | ------------------------------------------------------------------------- | ----------------
| `MONGODB_QUIET` | Runs MongoDB in a quiet mode that attempts to limit the amount of output. | true
You can also set the following mount points by passing the `-v /host:/container` flag to Docker.
| Volume mount point | Description |
| :-------------------------- | ---------------------- |
| `/var/lib/mongodb/data` | MongoDB data directory |
**Notice: When mouting a directory from the host into the container, ensure that the mounted
directory has the appropriate permissions and that the owner and group of the directory
matches the user UID or name which is running inside the container.**
This container image provides a containerized packaging of the MongoDB mongod daemon
and client application. The mongod server daemon accepts connections from clients
and provides access to content from MongoDB databases on behalf of the clients.
You can find more information on the MongoDB project from the project Web site
(https://www.mongodb.com/).
Usage
---------------------------------
-----
For this, we will assume that you are using the `centos/mongodb-32-centos7` image.
For this, we will assume that you are using the `rhscl/mongodb-36-rhel7` image.
If you want to set only the mandatory environment variables and store the database
in the `/home/user/database` directory on the host filesystem, execute the following command:
```
$ docker run -d -e MONGODB_USER=<user> -e MONGODB_PASSWORD=<password> -e MONGODB_DATABASE=<database> -e MONGODB_ADMIN_PASSWORD=<admin_password> -v /home/user/database:/var/lib/mongodb/data centos/mongodb-32-centos7
$ docker run -d -e MONGODB_USER=<user> -e MONGODB_PASSWORD=<password> -e MONGODB_DATABASE=<database> -e MONGODB_ADMIN_PASSWORD=<admin_password> -v /home/user/database:/var/lib/mongodb/data rhscl/mongodb-36-rhel7
```
If you are initializing the database and it's the first time you are using the
specified shared volume, the database will be created with two users: `admin` and `MONGODB_USER`. After that the MongoDB daemon
will be started. If you are re-attaching the volume to another container, the
creation of the database user and admin user will be skipped and only the
MongoDB daemon will be started.
specified shared volume, the database will be created with two users: `admin`
and `MONGODB_USER`. After that the MongoDB daemon will be started. If you are
re-attaching the volume to another container, the creation of the database
user and admin user will be skipped, passwords of users will be changed and
only the MongoDB daemon will be started.
Custom configuration file
Environment variables and volumes
---------------------------------
It is allowed to use custom configuration file for mongod server. Providing a custom configuration file supercedes the individual configuration environment variable values.
The image recognizes the following environment variables that you can set
during initialization by passing `-e VAR=VALUE` to the Docker run command.
To use custom configuration file in container it has to be mounted into `/etc/mongod.conf`. For example to use configuration file stored in `/home/user` directory use this option for `docker run` command: `-v /home/user/mongod.conf:/etc/mongod.conf:Z`.
**`MONGODB_ADMIN_PASSWORD`**
Password for the admin user
Optionally you can provide settings for user with 'readWrite' role.
(Note you MUST specify all three of these settings)
**`MONGODB_USER`**
User name for MONGODB account to be created
**`MONGODB_PASSWORD`**
Password for the user account
**`MONGODB_DATABASE`**
Database name
The following environment variables influence the MongoDB configuration file.
They are all optional.
**`MONGODB_QUIET (default: true)`**
Runs MongoDB in a quiet mode that attempts to limit the amount of output.
You can also set the following mount points by passing the `-v
/host:/container` flag to Docker.
**`/var/lib/mongodb/data`**
MongoDB data directory
**Notice: When mounting a directory from the host into the container, ensure
that the mounted directory has the appropriate permissions and that the owner
and group of the directory matches the user UID or name which is running
inside the container.**
**Notice: Custom config file does not affect name of replica set. It has to be set in `MONGODB_REPLICA_NAME` environment variable.**
MongoDB admin user
---------------------------------
The admin user name is set to `admin` and you have to to specify the password by
setting the `MONGODB_ADMIN_PASSWORD` environment variable. This process is done
upon database initialization.
setting the `MONGODB_ADMIN_PASSWORD` environment variable.
This user has 'dbAdminAnyDatabase', 'userAdminAnyDatabase',
'readWriteAnyDatabase', 'clusterAdmin' roles (for more information see
[MongoDB
reference](https://docs.mongodb.com/manual/reference/built-in-roles/)).
Optional unprivileged user
---------------------------------
The user with `$MONGODB_USER` name is created in database `$MONGODB_DATABASE`
and you have to to specify the password by setting the `MONGODB_PASSWORD`
environment variable.
This user has only 'readWrite' role in the database.
Changing passwords
------------------
---------------------------------
Since passwords are part of the image configuration, the only supported method
to change passwords for the database user (`MONGODB_USER`) and admin user is by
changing the environment variables `MONGODB_PASSWORD` and
to change passwords for the database user (`MONGODB_USER`) and admin user is
by changing the environment variables `MONGODB_PASSWORD` and
`MONGODB_ADMIN_PASSWORD`, respectively.
Changing database passwords directly in MongoDB will cause a mismatch between
the values stored in the variables and the actual passwords. Whenever a database
container starts it will reset the passwords to the values stored in the
environment variables.
the values stored in the variables and the actual passwords. Whenever a
database container starts it will reset the passwords to the values stored in
the environment variables.
Extending image
---------------------------------
This image can be extended using
[source-to-image](https://github.com/openshift/source-to-image).
For example to build customized MongoDB database image `my-mongodb-centos7`
with configuration in `~/image-configuration/` run:
```
$ s2i build ~/image-configuration/ centos/mongodb-36-centos7 my-mongodb-centos7
```
The directory passed to `s2i build` should contain one or more of the
following directories:
----------------------------------------------
##### `mongodb-cfg/`
when running `run-mongod` or `run-mongod-replication` commands contained
`mongod.conf` file is used for `mongod` configuration
~~~~~
- `envsubst` command is run on this file to still allow customization of
the image using environment variables
- custom configuration file does not affect name of replica set - it has
to be set in `MONGODB_REPLICA_NAME` environment variable
- it is not possible to configure SSL using custom configuration file
~~~~~
##### `mongodb-ssl/`
SSL/TLS certificates used to configure MongoDB server SSL/TLS support
**Notice: To allow connections from internal scripts it is required to have `localhost` specified in SAN filed of SSL certificate.**
~~~~~
- `mongodb.pem` - file containing a public key certificate and its
associated private key. See [upstream
documentation](https://docs.mongodb.com/manual/tutorial/configure-ssl/#pem-file)
(encryption of private key is not supported).
- `ca.pem` - optional file containing the root certificate chain from
the Certificate Authority. See [upstream
documentation](https://docs.mongodb.com/manual/tutorial/configure-ssl/#set-up-mongod-and-mongos-with-certificate-validation)
~~~~~
##### `mongodb-pre-init/`
contained shell scripts (`*.sh`) are sourced before `mongod` server is
started
##### `mongodb-init/`
contained shell scripts (`*.sh`) are sourced when `mongod` server is
started for the first time (= datadir is empty)
~~~~~
- `run-mongod` command doesn't have enabled authentication in this phase
- `run-mongod-replication` command has enabled authentication in this phase
~~~~~
##### `mongodb-start/`
same sematics as `mongodb-init/`, except that these scripts are always sourced
(after `mongodb-init/` scripts, if they exist)
these scripts are skipped if `run-mongod-replication` is run with already
initialized data directory
----------------------------------------------
Variables that can be used in the scripts provided to s2i:
~~~~~
- `mongo_common_args` -- contains arguments for the `mongod` server (changing
this can break existing customization scripts, e.g. default scripts)
- `shell_args` -- arguments to mongo shell which should be used with every `mongo`
invocation (e.g. it's used to set parameters for SSL connection). Same as in
internal scripts, `mongo_cmd` function, which uses `shell_args`, can be used.
- `$MEMBER_ID` -- contains 'id' of the container. It is defined only in
scripts for replication (`run-mongod-replication` command) and has different
value for each container in a replicaset cluster. Customization scripts are
run by all containers in replicaset - `MEMBER_ID` can be used to write scripts
which are run only by some container.
~~~~~
During `s2i build` all provided files are copied into `/opt/app-root/src`
directory in the new image. If some configuration files are present in
destination directory, files with the same name are overwritten. Also only one
file with the same name can be used for customization and user provided files
are preferred over default files in `/usr/share/container-scripts/mongodb/`-
so it is possible to overwrite them.
Same configuration directory structure can be used to customize the image
every time the image is started using `docker run`. The directory have to be
mounted into `/opt/app-root/src/` in the image (`-v
./image-configuration/:/opt/app-root/src/`). This overwrites customization
built into the image.
Troubleshooting
---------------
The mongod deamon in the container logs to the standard output, so the log is available in the container log. The log can be examined by running:
docker logs <container>
See also
--------
Dockerfile and other sources for this container image are available on
https://github.com/sclorg/mongodb-container.
In that repository, Dockerfile for CentOS is called Dockerfile, Dockerfile
for RHEL is called Dockerfile.rhel7.

View file

@ -5,7 +5,7 @@ set -o nounset
set -o pipefail
# Data directory where MongoDB database files live. The data subdirectory is here
# because mongodb.conf lives in /var/lib/mongodb/ and we don't want a volume to
# because mongod.conf lives in /var/lib/mongodb/ and we don't want a volume to
# override it.
export MONGODB_DATADIR=/var/lib/mongodb/data
export CONTAINER_PORT=27017
@ -19,6 +19,13 @@ MONGODB_KEYFILE_PATH="${HOME}/keyfile"
readonly MAX_ATTEMPTS=60
readonly SLEEP_TIME=1
# function to use by extension scripts instead of mongo shell binary
# - to be able to change shell params in all scripts
# for example to use SSL certificate
function mongo_cmd() {
mongo ${shell_args:-} $@;
}
# wait_for_mongo_up waits until the mongo server accepts incomming connections
function wait_for_mongo_up() {
_wait_for_mongo 1 "$@"
@ -39,12 +46,10 @@ function _wait_for_mongo() {
message="down"
fi
local mongo_cmd="mongo admin --host ${2:-localhost} "
local i
for i in $(seq $MAX_ATTEMPTS); do
echo "=> ${2:-} Waiting for MongoDB daemon ${message}"
if ([[ ${operation} -eq 1 ]] && ${mongo_cmd} --eval "quit()" &>/dev/null) || ([[ ${operation} -eq 0 ]] && ! ${mongo_cmd} --eval "quit()" &>/dev/null); then
if ([[ ${operation} -eq 1 ]] && mongo_cmd ${2:-localhost} <<<"quit()") || ([[ ${operation} -eq 0 ]] && ! mongo_cmd ${2:-localhost} <<<"quit()"); then
echo "=> MongoDB daemon is ${message}"
return 0
fi
@ -56,7 +61,7 @@ function _wait_for_mongo() {
# endpoints returns list of IP addresses with other instances of MongoDB
# To get list of endpoints, you need to have headless Service named 'mongodb'.
# NOTE: This won't work with standalone Docker container.
# NOTE: This won't work with standalone container.
function endpoints() {
service_name=${MONGODB_SERVICE_NAME:-mongodb}
dig ${service_name} A +search +short 2>/dev/null
@ -64,163 +69,20 @@ function endpoints() {
# replset_addr return the address of the current replSet
function replset_addr() {
local current_endpoints
local current_endpoints db
db="${1:-}"
current_endpoints="$(endpoints)"
if [ -z "${current_endpoints}" ]; then
echo >&2 "Cannot get address of replica set: no nodes are listed in service"
info "Cannot get address of replica set: no nodes are listed in service!"
info "CAUSE: DNS lookup for '${MONGODB_SERVICE_NAME:-mongodb}' returned no results."
return 1
fi
echo "${MONGODB_REPLICA_NAME}/${current_endpoints//[[:space:]]/,}"
}
# mongo_create_admin creates the MongoDB admin user with password: MONGODB_ADMIN_PASSWORD
# $1 - login parameters for mongo (optional)
# $2 - host where to connect (localhost by default)
function mongo_create_admin() {
if [[ -z "${MONGODB_ADMIN_PASSWORD:-}" ]]; then
echo >&2 "=> MONGODB_ADMIN_PASSWORD is not set. Authentication can not be set up."
exit 1
fi
# Set admin password
local js_command="db.createUser({user: 'admin', pwd: '${MONGODB_ADMIN_PASSWORD}', roles: ['dbAdminAnyDatabase', 'userAdminAnyDatabase' , 'readWriteAnyDatabase','clusterAdmin' ]});"
if ! mongo admin ${1:-} --host ${2:-"localhost"} --eval "${js_command}"; then
echo >&2 "=> Failed to create MongoDB admin user."
exit 1
fi
}
# mongo_create_user creates the MongoDB database user: MONGODB_USER,
# with password: MONGDOB_PASSWORD, inside database: MONGODB_DATABASE
# $1 - login parameters for mongo (optional)
# $2 - host where to connect (localhost by default)
function mongo_create_user() {
# Ensure input variables exists
if [[ -z "${MONGODB_USER:-}" ]]; then
echo >&2 "=> MONGODB_USER is not set. Failed to create MongoDB user"
exit 1
fi
if [[ -z "${MONGODB_PASSWORD:-}" ]]; then
echo "=> MONGODB_PASSWORD is not set. Failed to create MongoDB user: ${MONGODB_USER}"
exit >&2 1
fi
if [[ -z "${MONGODB_DATABASE:-}" ]]; then
echo >&2 "=> MONGODB_DATABASE is not set. Failed to create MongoDB user: ${MONGODB_USER}"
exit 1
fi
# Create database user
local js_command="db.getSiblingDB('${MONGODB_DATABASE}').createUser({user: '${MONGODB_USER}', pwd: '${MONGODB_PASSWORD}', roles: [ 'readWrite' ]});"
if ! mongo admin ${1:-} --host ${2:-"localhost"} --eval "${js_command}"; then
echo >&2 "=> Failed to create MongoDB user: ${MONGODB_USER}"
exit 1
fi
}
# mongo_reset_user sets the MongoDB MONGODB_USER's password to match MONGODB_PASSWORD
function mongo_reset_user() {
if [[ -n "${MONGODB_USER:-}" && -n "${MONGODB_PASSWORD:-}" && -n "${MONGODB_DATABASE:-}" ]]; then
local js_command="db.changeUserPassword('${MONGODB_USER}', '${MONGODB_PASSWORD}')"
if ! mongo ${MONGODB_DATABASE} --eval "${js_command}"; then
echo >&2 "=> Failed to reset password of MongoDB user: ${MONGODB_USER}"
exit 1
fi
fi
}
# mongo_reset_admin sets the MongoDB admin password to match MONGODB_ADMIN_PASSWORD
function mongo_reset_admin() {
if [[ -n "${MONGODB_ADMIN_PASSWORD:-}" ]]; then
local js_command="db.changeUserPassword('admin', '${MONGODB_ADMIN_PASSWORD}')"
if ! mongo admin --eval "${js_command}"; then
echo >&2 "=> Failed to reset password of MongoDB user: ${MONGODB_USER}"
exit 1
fi
fi
}
# setup_keyfile fixes the bug in mounting the Kubernetes 'Secret' volume that
# mounts the secret files with 'too open' permissions.
# add --keyFile argument to mongo_common_args
function setup_keyfile() {
# If user specify keyFile in config file do not use generated keyFile
if grep -q "^\s*keyFile" ${MONGODB_CONFIG_PATH}; then
exit 0
fi
if [ -z "${MONGODB_KEYFILE_VALUE-}" ]; then
echo >&2 "ERROR: You have to provide the 'keyfile' value in MONGODB_KEYFILE_VALUE"
exit 1
fi
local keyfile_dir
keyfile_dir="$(dirname "$MONGODB_KEYFILE_PATH")"
if [ ! -w "$keyfile_dir" ]; then
echo >&2 "ERROR: Couldn't create ${MONGODB_KEYFILE_PATH}"
echo >&2 "CAUSE: current user doesn't have permissions for writing to ${keyfile_dir} directory"
echo >&2 "DETAILS: current user id = $(id -u), user groups: $(id -G)"
echo >&2 "DETAILS: directory permissions: $(stat -c '%A owned by %u:%g' "${keyfile_dir}")"
exit 1
fi
echo ${MONGODB_KEYFILE_VALUE} > ${MONGODB_KEYFILE_PATH}
chmod 0600 ${MONGODB_KEYFILE_PATH}
mongo_common_args+=" --keyFile ${MONGODB_KEYFILE_PATH}"
}
# setup_default_datadir checks permissions of mounded directory into default
# data directory MONGODB_DATADIR
function setup_default_datadir() {
if [ ! -w "$MONGODB_DATADIR" ]; then
echo >&2 "ERROR: Couldn't write into ${MONGODB_DATADIR}"
echo >&2 "CAUSE: current user doesn't have permissions for writing to ${MONGODB_DATADIR} directory"
echo >&2 "DETAILS: current user id = $(id -u), user groups: $(id -G)"
echo >&2 "DETAILS: directory permissions: $(stat -c '%A owned by %u:%g, SELinux: %C' "${MONGODB_DATADIR}")"
exit 1
fi
}
# setup_wiredtiger_cache checks amount of available RAM (it has to use cgroups in container)
# and if there are any memory restrictions set storage.wiredTiger.engineConfig.cacheSizeGB
# in MONGODB_CONFIG_PATH to upstream default size
# it is intended to update mongodb.conf.template, with custom config file it might create conflict
function setup_wiredtiger_cache() {
local config_file
config_file=${1:-$MONGODB_CONFIG_PATH}
declare $(cgroup-limits)
if [[ ! -v MEMORY_LIMIT_IN_BYTES || "${NO_MEMORY_LIMIT:-}" == "true" ]]; then
return 0;
fi
cache_size=$(python3 -c "min=1; limit=int(($MEMORY_LIMIT_IN_BYTES / pow(2,30) - 1) * 0.6); print( min if limit < min else limit)")
echo "storage.wiredTiger.engineConfig.cacheSizeGB: ${cache_size}" >> ${config_file}
info "wiredTiger cacheSizeGB set to ${cache_size}"
}
# check_env_vars checks environmental variables
# if variables to create non-admin user are provided, sets CREATE_USER=1
# if REPLICATION variable is set, checks also replication variables
function check_env_vars() {
local readonly database_regex='^[^/\. "$]*$'
[[ -v MONGODB_ADMIN_PASSWORD ]] || usage "MONGODB_ADMIN_PASSWORD has to be set."
if [[ -v MONGODB_USER || -v MONGODB_PASSWORD || -v MONGODB_DATABASE ]]; then
[[ -v MONGODB_USER && -v MONGODB_PASSWORD && -v MONGODB_DATABASE ]] || usage "You have to set all or none of variables: MONGODB_USER, MONGODB_PASSWORD, MONGODB_DATABASE"
[[ "${MONGODB_DATABASE}" =~ $database_regex ]] || usage "Database name must match regex: $database_regex"
[ ${#MONGODB_DATABASE} -le 63 ] || usage "Database name too long (maximum 63 characters)"
export CREATE_USER=1
fi
if [[ -v REPLICATION ]]; then
[[ -v MONGODB_KEYFILE_VALUE && -v MONGODB_REPLICA_NAME ]] || usage "MONGODB_KEYFILE_VALUE and MONGODB_REPLICA_NAME have to be set"
fi
echo "mongodb://${current_endpoints//[[:space:]]/,}/${db}?replicaSet=${MONGODB_REPLICA_NAME}"
}
# usage prints info about required enviromental variables
# if $1 is passed, prints error message containing $1
# if REPLICATION variable is set, prints also info about replication variables
# if MEMBER_ID variable is set, prints also info about replication variables
function usage() {
if [ $# == 1 ]; then
echo >&2 "error: $1"
@ -237,7 +99,7 @@ Optionally you can provide settings for a user with 'readWrite' role:
Optional settings:
MONGODB_QUIET (default: true)"
if [[ -v REPLICATION ]]; then
if [[ -v MEMBER_ID ]]; then
echo "
For replication you must also specify the following environment variables:
MONGODB_KEYFILE_VALUE
@ -248,11 +110,64 @@ Optional settings:
fi
echo "
For more information see /usr/share/container-scripts/mongodb/README.md
within the container or visit https://github.com/sclorgk/mongodb-container/."
within the container or visit https://github.com/sclorg/mongodb-container/."
exit 1
}
# log_info MESSAGE
# ---------------------------------------
# System log information message.
function log_info() {
printf "\xE2\x9E\xA1 [%s INFO] %s\n" "$(date +'%a %b %d %T')" "${1:-}"
}
# log_fail MESSAGE
# ---------------------------------------
# System log failure message.
function log_fail() {
printf "\xe2\x9c\x98 [%s FAIL] %s\n" "$(date +'%a %b %d %T')" "${1:-}"
}
# log_pass MESSAGE
# ---------------------------------------
# System log success message.
function log_pass() {
printf "\xE2\x9C\x94 [%s PASS] %s\n" "$(date +'%a %b %d %T')" "${1:-}"
}
# get_matched_files PATTERN DIR [DIR ...]
# ---------------------------------------
# Print all basenames for files matching PATTERN in DIRs.
get_matched_files ()
{
local pattern=$1 dir
shift
for dir; do
test -d "$dir" || continue
find "$dir" -maxdepth 1 -type f -name "$pattern" -printf "%f\n"
done
}
# process_extending_files DIR [DIR ...]
# -------------------------------------
# Source all *.sh files in DIRs in alphabetical order, but if the file exists in
# more then one DIR, source only the first occurrence (first found wins).
process_extending_files()
{
local filename dir
while read filename ; do
for dir in "$@"; do
local file="$dir/$filename"
if test -f "$file"; then
echo "=> sourcing $file ..."
source "$file"
break
fi
done
done <<<"$(get_matched_files '*.sh' "$@" | sort -u)"
}
# info prints a message prefixed by date and time.
function info() {
printf "=> [%s] %s\n" "$(date +'%a %b %d %T')" "$*"

View file

@ -10,18 +10,6 @@ source "${CONTAINER_SCRIPTS_PATH}/common.sh"
# (for example, "replica-2.mongodb.myproject.svc.cluster.local")
readonly MEMBER_HOST="$(hostname -f)"
# Outputs available endpoints (hostnames) to stdout.
# This also includes hostname of the current pod.
#
# Uses the following global variables:
# - MONGODB_SERVICE_NAME (optional, defaults to 'mongodb')
function find_endpoints() {
local service_name="${MONGODB_SERVICE_NAME:-mongodb}"
# Extract host names from lines like this: "10 33 0 mongodb-2.mongodb.myproject.svc.cluster.local."
dig "${service_name}" SRV +search +short | cut -d' ' -f4 | rev | cut -c2- | rev
}
# Initializes the replica set configuration.
#
# Arguments:
@ -36,14 +24,10 @@ function initiate() {
local config="{_id: '${MONGODB_REPLICA_NAME}', members: [{_id: 0, host: '${host}'}]}"
info "Initiating MongoDB replica using: ${config}"
mongo --eval "quit(rs.initiate(${config}).ok ? 0 : 1)" --quiet
mongo_cmd --host localhost --quiet <<<"quit(rs.initiate(${config}).ok ? 0 : 1)"
info "Waiting for PRIMARY status ..."
mongo --eval "while (!rs.isMaster().ismaster) { sleep(100); }" --quiet
info "Creating MongoDB users ..."
mongo_create_admin
[[ -v CREATE_USER ]] && mongo_create_user "-u admin -p ${MONGODB_ADMIN_PASSWORD}"
mongo_cmd --host localhost --quiet <<<"while (!rs.isMaster().ismaster) { sleep(100); }"
info "Successfully initialized replica set"
}
@ -60,47 +44,27 @@ function add_member() {
local host="$1"
info "Adding ${host} to replica set ..."
# TODO: replace this with a call to `replset_addr` from common.sh, once it returns host names.
local endpoints
endpoints="$(find_endpoints | paste -s -d,)"
if [ -z "${endpoints}" ]; then
info "ERROR: couldn't add host to replica set!"
info "CAUSE: DNS lookup for '${MONGODB_SERVICE_NAME:-mongodb}' returned no results."
return 1
fi
local replset_addr
replset_addr="${MONGODB_REPLICA_NAME}/${endpoints}"
if ! mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --host "${replset_addr}" --eval "while (!rs.add('${host}').ok) { sleep(100); }" --quiet; then
if ! mongo_cmd "$(replset_addr admin)" -u admin -p"${MONGODB_ADMIN_PASSWORD}" --quiet <<<"while (!rs.add('${host}').ok) { sleep(100); }"; then
info "ERROR: couldn't add host to replica set!"
return 1
fi
info "Waiting for PRIMARY/SECONDARY status ..."
mongo --eval "while (!rs.isMaster().ismaster && !rs.isMaster().secondary) { sleep(100); }" --quiet
mongo_cmd --host localhost --quiet <<<"while (!rs.isMaster().ismaster && !rs.isMaster().secondary) { sleep(100); }"
info "Successfully joined replica set"
}
info "Waiting for local MongoDB to accept connections ..."
wait_for_mongo_up &>/dev/null
if [[ $(mongo --eval 'db.isMaster().setName' --quiet) == "${MONGODB_REPLICA_NAME}" ]]; then
if [[ $(mongo_cmd --host localhost --quiet <<<'db.isMaster().setName') == "${MONGODB_REPLICA_NAME}" ]]; then
info "Replica set '${MONGODB_REPLICA_NAME}' already exists, skipping initialization"
>/tmp/initialized
exit 0
fi
# StatefulSet pods are named with a predictable name, following the pattern:
# $(statefulset name)-$(zero-based index)
# MEMBER_ID is computed by removing the prefix matching "*-", i.e.:
# "mongodb-0" -> "0"
# "mongodb-1" -> "1"
# "mongodb-2" -> "2"
readonly MEMBER_ID="${HOSTNAME##*-}"
# Initialize replica set only if we're the first member
if [ "${MEMBER_ID}" = '0' ]; then
initiate "${MEMBER_HOST}"
@ -108,4 +72,3 @@ else
add_member "${MEMBER_HOST}"
fi
>/tmp/initialized

View file

@ -13,6 +13,8 @@ systemLog:
net:
# Specify port number (27017 by default)
port: ${CONTAINER_PORT}
# Specify on which interfaces start listening
bindIp: "0.0.0.0"
# storage Options - How and Where to store data

View file

@ -0,0 +1,25 @@
# check_env_vars checks environment variables
# if variables to create non-admin user are provided, sets CREATE_USER=1
# if MEMBER_ID variable is set, checks also replication variables
function check_env_vars() {
local readonly database_regex='^[^/\. "$]*$'
[[ -v MONGODB_ADMIN_PASSWORD ]] || usage "MONGODB_ADMIN_PASSWORD has to be set."
if [[ -v MONGODB_USER || -v MONGODB_PASSWORD || -v MONGODB_DATABASE ]]; then
[[ -v MONGODB_USER && -v MONGODB_PASSWORD && -v MONGODB_DATABASE ]] || usage "You have to set all or none of variables: MONGODB_USER, MONGODB_PASSWORD, MONGODB_DATABASE"
[[ "${MONGODB_DATABASE}" =~ $database_regex ]] || usage "Database name must match regex: $database_regex"
[ ${#MONGODB_DATABASE} -le 63 ] || usage "Database name too long (maximum 63 characters)"
export CREATE_USER=1
fi
if [[ -v MEMBER_ID ]]; then
[[ -v MONGODB_KEYFILE_VALUE && -v MONGODB_REPLICA_NAME ]] || usage "MONGODB_KEYFILE_VALUE and MONGODB_REPLICA_NAME have to be set"
fi
}
# Can export CREATE_USER=1 to indicate that variables for optional user
# are provided
check_env_vars

View file

@ -0,0 +1,24 @@
# New systems by default use only python3, so select python on runtime
PYTHON=python3
command -v $PYTHON &>/dev/null || PYTHON=python
# setup_wiredtiger_cache checks amount of available RAM (it has to use cgroups in container)
# and if there are any memory restrictions set storage.wiredTiger.engineConfig.cacheSizeGB
# in MONGODB_CONFIG_PATH to upstream default size
# it is intended to update mongod.conf.template, with custom config file it might create conflict
function setup_wiredtiger_cache() {
local config_file
config_file=${1:-$MONGODB_CONFIG_PATH}
declare $($PYTHON /usr/libexec/cgroup-limits)
if [[ ! -v MEMORY_LIMIT_IN_BYTES || "${NO_MEMORY_LIMIT:-}" == "true" ]]; then
return 0;
fi
cache_size=$($PYTHON -c "min=1; limit=int(($MEMORY_LIMIT_IN_BYTES / pow(2,30) - 1) * 0.6); print( min if limit < min else limit)")
echo "storage.wiredTiger.engineConfig.cacheSizeGB: ${cache_size}" >> ${config_file}
info "wiredTiger cacheSizeGB set to ${cache_size}"
}
setup_wiredtiger_cache ${CONTAINER_SCRIPTS_PATH}/mongod.conf.template

View file

@ -0,0 +1,12 @@
# If user provides own config file use it and do not generate new one
if [ ! -s $MONGODB_CONFIG_PATH ]; then
# If no configuration is provided use template
cp ${CONTAINER_SCRIPTS_PATH}/mongod.conf.template $MONGODB_CONFIG_PATH
fi
[ -r "${APP_DATA}/mongodb-cfg/mongod.conf" ] && cp "${APP_DATA}/mongodb-cfg/mongod.conf" $MONGODB_CONFIG_PATH
# Substitute environment variables in configuration file
TEMP=`mktemp`; cp ${MONGODB_CONFIG_PATH} $TEMP; envsubst > ${MONGODB_CONFIG_PATH} < $TEMP
mongo_common_args+="-f ${MONGODB_CONFIG_PATH}"

View file

@ -0,0 +1,13 @@
# setup_default_datadir checks permissions of mounded directory into default
# data directory MONGODB_DATADIR
function setup_default_datadir() {
if [ ! -w "$MONGODB_DATADIR" ]; then
echo >&2 "ERROR: Couldn't write into ${MONGODB_DATADIR}"
echo >&2 "CAUSE: current user doesn't have permissions for writing to ${MONGODB_DATADIR} directory"
echo >&2 "DETAILS: current user id = $(id -u), user groups: $(id -G)"
echo >&2 "DETAILS: directory permissions: $(stat -c '%A owned by %u:%g, SELinux: %C' "${MONGODB_DATADIR}")"
exit 1
fi
}
setup_default_datadir

View file

@ -0,0 +1,30 @@
# setup_keyfile fixes the bug in mounting the Kubernetes 'Secret' volume that
# mounts the secret files with 'too open' permissions.
# add --keyFile argument to mongo_common_args
function setup_keyfile() {
# If user specify keyFile in config file do not use generated keyFile
if grep -q "^\s*keyFile" ${MONGODB_CONFIG_PATH}; then
exit 0
fi
if [ -z "${MONGODB_KEYFILE_VALUE-}" ]; then
echo >&2 "ERROR: You have to provide the 'keyfile' value in MONGODB_KEYFILE_VALUE"
exit 1
fi
local keyfile_dir
keyfile_dir="$(dirname "$MONGODB_KEYFILE_PATH")"
if [ ! -w "$keyfile_dir" ]; then
echo >&2 "ERROR: Couldn't create ${MONGODB_KEYFILE_PATH}"
echo >&2 "CAUSE: current user doesn't have permissions for writing to ${keyfile_dir} directory"
echo >&2 "DETAILS: current user id = $(id -u), user groups: $(id -G)"
echo >&2 "DETAILS: directory permissions: $(stat -c '%A owned by %u:%g' "${keyfile_dir}")"
exit 1
fi
echo ${MONGODB_KEYFILE_VALUE} > ${MONGODB_KEYFILE_PATH}
chmod 0600 ${MONGODB_KEYFILE_PATH}
mongo_common_args+=" --keyFile ${MONGODB_KEYFILE_PATH}"
}
if [[ -v MEMBER_ID ]]; then
# Attention: setup_keyfile may modify value of mongo_common_args!
setup_keyfile
fi

View file

@ -0,0 +1,16 @@
SSL_PEM_FILE=${SSL_PEM_FILE:-${APP_DATA}/mongodb-ssl/mongodb.pem}
SSL_CA_FILE=${SSL_CA_FILE:-${APP_DATA}/mongodb-ssl/ca.pem}
if [ -f "${SSL_PEM_FILE}" ]; then
log_info "SSL/TLS enabled"
mongo_common_args+=" --sslMode requireSSL --sslPEMKeyFile ${SSL_PEM_FILE}"
shell_args+=" --ssl --sslPEMKeyFile ${SSL_PEM_FILE}"
if [ ! -f "${SSL_CA_FILE:-}" ]; then
log_info "A certificate authority was not set. Assuming self-signed"
shell_args+=" --sslAllowInvalidCertificates"
else
mongo_common_args+=" --sslCAFile ${SSL_CA_FILE}"
shell_args+=" --sslCAFile ${SSL_CA_FILE}"
fi
fi

View file

@ -0,0 +1,101 @@
# mongo_create_admin creates the MongoDB admin user with password: MONGODB_ADMIN_PASSWORD
# $1 - login parameters for mongo (optional)
# $2 - host where to connect (localhost by default)
function mongo_create_admin() {
if [[ -z "${MONGODB_ADMIN_PASSWORD:-}" ]]; then
echo >&2 "=> MONGODB_ADMIN_PASSWORD is not set. Authentication can not be set up."
exit 1
fi
# Set admin password
local js_command="db.createUser({user: 'admin', pwd: '${MONGODB_ADMIN_PASSWORD}', roles: ['dbAdminAnyDatabase', 'userAdminAnyDatabase' , 'readWriteAnyDatabase','clusterAdmin' ]});"
if ! mongo_cmd --host "localhost" admin ${1:-} <<<"$js_command"; then
echo >&2 "=> Failed to create MongoDB admin user."
exit 1
fi
}
# mongo_create_user creates the MongoDB database user: MONGODB_USER,
# with password: MONGDOB_PASSWORD, inside database: MONGODB_DATABASE
# $1 - login parameters for mongo (optional)
# $2 - host where to connect (localhost by default)
function mongo_create_user() {
# Ensure input variables exists
if [[ -z "${MONGODB_USER:-}" ]]; then
echo >&2 "=> MONGODB_USER is not set. Failed to create MongoDB user"
exit 1
fi
if [[ -z "${MONGODB_PASSWORD:-}" ]]; then
echo "=> MONGODB_PASSWORD is not set. Failed to create MongoDB user: ${MONGODB_USER}"
exit >&2 1
fi
if [[ -z "${MONGODB_DATABASE:-}" ]]; then
echo >&2 "=> MONGODB_DATABASE is not set. Failed to create MongoDB user: ${MONGODB_USER}"
exit 1
fi
# Create database user
local js_command="db.getSiblingDB('${MONGODB_DATABASE}').createUser({user: '${MONGODB_USER}', pwd: '${MONGODB_PASSWORD}', roles: [ 'readWrite' ]});"
if ! mongo_cmd --host "localhost" admin ${1:-} <<<"$js_command"; then
echo >&2 "=> Failed to create MongoDB user: ${MONGODB_USER}"
exit 1
fi
}
# mongo_reset_user sets the MongoDB MONGODB_USER's password to match MONGODB_PASSWORD
function mongo_reset_user() {
if [[ -n "${MONGODB_USER:-}" && -n "${MONGODB_PASSWORD:-}" && -n "${MONGODB_DATABASE:-}" ]]; then
local js_command="db.changeUserPassword('${MONGODB_USER}', '${MONGODB_PASSWORD}')"
if ! mongo_cmd --host localhost ${MONGODB_DATABASE} <<<"${js_command}"; then
echo >&2 "=> Failed to reset password of MongoDB user: ${MONGODB_USER}"
exit 1
fi
fi
}
# mongo_reset_admin sets the MongoDB admin password to match MONGODB_ADMIN_PASSWORD
function mongo_reset_admin() {
if [[ -n "${MONGODB_ADMIN_PASSWORD:-}" ]]; then
local js_command="db.changeUserPassword('admin', '${MONGODB_ADMIN_PASSWORD}')"
if ! mongo_cmd --host localhost admin <<<"${js_command}"; then
echo >&2 "=> Failed to reset password of MongoDB user: ${MONGODB_USER}"
exit 1
fi
fi
}
# update_users creates default users (see usage)
# if users are already created, updates passwords to match
# environment variables
function update_users() {
js_command="db.system.users.count({'user':'admin', 'db':'admin'})"
if [ "$(mongo_cmd --host localhost admin --quiet <<<$js_command)" == "1" ]; then
info "Admin user is already created. Resetting password ..."
mongo_reset_admin
else
info "Creating MongoDB admin user ..."
mongo_create_admin
fi
if [[ -v CREATE_USER ]]; then
js_command="db.system.users.count({'user':'${MONGODB_USER}', 'db':'${MONGODB_DATABASE}'})"
if [ "$(mongo_cmd --host localhost admin --quiet <<<$js_command)" == "1" ]; then
info "MONGODB_USER user is already created. Resetting password ..."
mongo_reset_user
else
info "Creating MongoDB $MONGODB_USER user ..."
mongo_create_user
fi
fi
}
if ! [[ -v MEMBER_ID ]]; then
update_users
else
if [ "${MEMBER_ID}" -eq 0 ]; then
info "Creating MongoDB users ..."
mongo_create_admin
[[ -v CREATE_USER ]] && mongo_create_user "-u admin -p${MONGODB_ADMIN_PASSWORD}"
fi
fi

View file

@ -34,7 +34,7 @@ function insert_and_wait_for_replication() {
printjson(rs.status());
quit(1);"
mongo admin --host "${host}" -u admin -p "${MONGODB_ADMIN_PASSWORD}" --eval "${script}"
mongo "${host}" -u admin -p"${MONGODB_ADMIN_PASSWORD}" <<<"${script}"
}
# wait_replicaset_members waits till replset has specified number of members
@ -57,5 +57,5 @@ function wait_replicaset_members() {
printjson(rs.status());
quit(1);"
mongo admin --host "${host}" -u admin -p "${MONGODB_ADMIN_PASSWORD}" --eval "${script}"
mongo "${host}" -u admin -p"${MONGODB_ADMIN_PASSWORD}" <<<"${script}"
}

12
s2i/bin/assemble Executable file
View file

@ -0,0 +1,12 @@
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
shopt -s dotglob
echo "---> Installing application source ..."
mv /tmp/src/* ./ 2>/dev/null || true
# Fix source directory permissions
/usr/libexec/fix-permissions ./

1
s2i/bin/run Symbolic link
View file

@ -0,0 +1 @@
/bin/run-mongod

7
s2i/bin/usage Executable file
View file

@ -0,0 +1,7 @@
#!/bin/sh
set -o errexit
set -o nounset
set -o pipefail
groff -t -man -ETascii /help.1

View file

507
test/common-lib Normal file
View file

@ -0,0 +1,507 @@
#
# Test a container image.
#
# Always use sourced from a specific container testfile
#
# reguires definition of CID_FILE_DIR
# CID_FILE_DIR=$(mktemp --suffix=<container>_test_cidfiles -d)
# reguires definition of TEST_LIST
# TEST_LIST="\
# ctest_container_creation
# ctest_doc_content"
# Container CI tests
# abbreviated as "ct"
# may be redefined in the specific container testfile
EXPECTED_EXIT_CODE=0
# ct_cleanup
# --------------------
# Cleans up containers used during tests. Stops and removes all containers
# referenced by cid_files in CID_FILE_DIR. Dumps logs if a container exited
# unexpectedly. Removes the cid_files and CID_FILE_DIR as well.
# Uses: $CID_FILE_DIR - path to directory containing cid_files
# Uses: $EXPECTED_EXIT_CODE - expected container exit code
function ct_cleanup() {
for cid_file in $CID_FILE_DIR/* ; do
local container=$(cat $cid_file)
: "Stopping and removing container $container..."
docker stop $container
exit_status=$(docker inspect -f '{{.State.ExitCode}}' $container)
if [ "$exit_status" != "$EXPECTED_EXIT_CODE" ]; then
: "Dumping logs for $container"
docker logs $container
fi
docker rm -v $container
rm $cid_file
done
rmdir $CID_FILE_DIR
: "Done."
}
# ct_enable_cleanup
# --------------------
# Enables automatic container cleanup after tests.
function ct_enable_cleanup() {
trap ct_cleanup EXIT SIGINT
}
# ct_get_cid [name]
# --------------------
# Prints container id from cid_file based on the name of the file.
# Argument: name - name of cid_file where the container id will be stored
# Uses: $CID_FILE_DIR - path to directory containing cid_files
function ct_get_cid() {
local name="$1" ; shift || return 1
echo $(cat "$CID_FILE_DIR/$name")
}
# ct_get_cip [id]
# --------------------
# Prints container ip address based on the container id.
# Argument: id - container id
function ct_get_cip() {
local id="$1" ; shift
docker inspect --format='{{.NetworkSettings.IPAddress}}' $(ct_get_cid "$id")
}
# ct_wait_for_cid [cid_file]
# --------------------
# Holds the execution until the cid_file is created. Usually run after container
# creation.
# Argument: cid_file - name of the cid_file that should be created
function ct_wait_for_cid() {
local cid_file=$1
local max_attempts=10
local sleep_time=1
local attempt=1
local result=1
while [ $attempt -le $max_attempts ]; do
[ -f $cid_file ] && [ -s $cid_file ] && return 0
: "Waiting for container start..."
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
return 1
}
# ct_assert_container_creation_fails [container_args]
# --------------------
# The invocation of docker run should fail based on invalid container_args
# passed to the function. Returns 0 when container fails to start properly.
# Argument: container_args - all arguments are passed directly to dokcer run
# Uses: $CID_FILE_DIR - path to directory containing cid_files
function ct_assert_container_creation_fails() {
local ret=0
local max_attempts=10
local attempt=1
local cid_file=assert
set +e
local old_container_args="${CONTAINER_ARGS-}"
CONTAINER_ARGS="$@"
ct_create_container $cid_file
if [ $? -eq 0 ]; then
local cid=$(ct_get_cid $cid_file)
while [ "$(docker inspect -f '{{.State.Running}}' $cid)" == "true" ] ; do
sleep 2
attempt=$(( $attempt + 1 ))
if [ $attempt -gt $max_attempts ]; then
docker stop $cid
ret=1
break
fi
done
exit_status=$(docker inspect -f '{{.State.ExitCode}}' $cid)
if [ "$exit_status" == "0" ]; then
ret=1
fi
docker rm -v $cid
rm $CID_FILE_DIR/$cid_file
fi
[ ! -z $old_container_args ] && CONTAINER_ARGS="$old_container_args"
set -e
return $ret
}
# ct_create_container [name, command]
# --------------------
# Creates a container using the IMAGE_NAME and CONTAINER_ARGS variables. Also
# stores the container id to a cid_file located in the CID_FILE_DIR, and waits
# for the creation of the file.
# Argument: name - name of cid_file where the container id will be stored
# Argument: command - optional command to be executed in the container
# Uses: $CID_FILE_DIR - path to directory containing cid_files
# Uses: $CONTAINER_ARGS - optional arguments passed directly to docker run
# Uses: $IMAGE_NAME - name of the image being tested
function ct_create_container() {
local cid_file="$CID_FILE_DIR/$1" ; shift
# create container with a cidfile in a directory for cleanup
docker run --cidfile="$cid_file" -d ${CONTAINER_ARGS:-} $IMAGE_NAME "$@"
ct_wait_for_cid $cid_file || return 1
: "Created container $(cat $cid_file)"
}
# ct_scl_usage_old [name, command, expected]
# --------------------
# Tests three ways of running the SCL, by looking for an expected string
# in the output of the command
# Argument: name - name of cid_file where the container id will be stored
# Argument: command - executed inside the container
# Argument: expected - string that is expected to be in the command output
# Uses: $CID_FILE_DIR - path to directory containing cid_files
# Uses: $IMAGE_NAME - name of the image being tested
function ct_scl_usage_old() {
local name="$1"
local command="$2"
local expected="$3"
local out=""
: " Testing the image SCL enable"
out=$(docker run --rm ${IMAGE_NAME} /bin/bash -c "${command}")
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[/bin/bash -c "${command}"] Expected '${expected}', got '${out}'" >&2
return 1
fi
out=$(docker exec $(ct_get_cid $name) /bin/bash -c "${command}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/bash -c "${command}"] Expected '${expected}', got '${out}'" >&2
return 1
fi
out=$(docker exec $(ct_get_cid $name) /bin/sh -ic "${command}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/sh -ic "${command}"] Expected '${expected}', got '${out}'" >&2
return 1
fi
}
# ct_doc_content_old [strings]
# --------------------
# Looks for occurence of stirngs in the documentation files and checks
# the format of the files. Files examined: help.1
# Argument: strings - strings expected to appear in the documentation
# Uses: $IMAGE_NAME - name of the image being tested
function ct_doc_content_old() {
local tmpdir=$(mktemp -d)
local f
: " Testing documentation in the container image"
# Extract the help files from the container
for f in help.1 ; do
docker run --rm ${IMAGE_NAME} /bin/bash -c "cat /${f}" >${tmpdir}/$(basename ${f})
# Check whether the files contain some important information
for term in $@ ; do
if ! cat ${tmpdir}/$(basename ${f}) | grep -F -q -e "${term}" ; then
echo "ERROR: File /${f} does not include '${term}'." >&2
return 1
fi
done
# Check whether the files use the correct format
for term in TH PP SH ; do
if ! grep -q "^\.${term}" ${tmpdir}/help.1 ; then
echo "ERROR: /help.1 is probably not in troff or groff format, since '${term}' is missing." >&2
return 1
fi
done
done
: " Success!"
}
# ct_npm_works
# --------------------
# Checks existance of the npm tool and runs it.
function ct_npm_works() {
local tmpdir=$(mktemp -d)
: " Testing npm in the container image"
docker run --rm ${IMAGE_NAME} /bin/bash -c "npm --version" >${tmpdir}/version
if [ $? -ne 0 ] ; then
echo "ERROR: 'npm --version' does not work inside the image ${IMAGE_NAME}." >&2
return 1
fi
docker run --rm ${IMAGE_NAME} /bin/bash -c "npm install jquery && test -f node_modules/jquery/src/jquery.js"
if [ $? -ne 0 ] ; then
echo "ERROR: npm could not install jquery inside the image ${IMAGE_NAME}." >&2
return 1
fi
: " Success!"
}
# ct_path_append PATH_VARNAME DIRECTORY
# -------------------------------------
# Append DIRECTORY to VARIABLE of name PATH_VARNAME, the VARIABLE must consist
# of colon-separated list of directories.
ct_path_append ()
{
if eval "test -n \"\${$1-}\""; then
eval "$1=\$2:\$$1"
else
eval "$1=\$2"
fi
}
# ct_path_foreach PATH ACTION [ARGS ...]
# --------------------------------------
# For each DIR in PATH execute ACTION (path is colon separated list of
# directories). The particular calls to ACTION will look like
# '$ ACTION directory [ARGS ...]'
ct_path_foreach ()
{
local dir dirlist action save_IFS
save_IFS=$IFS
IFS=:
dirlist=$1
action=$2
shift 2
for dir in $dirlist; do "$action" "$dir" "$@" ; done
IFS=$save_IFS
}
# ct_run_test_list
# --------------------
# Execute the tests specified by TEST_LIST
# Uses: $TEST_LIST - list of test names
function ct_run_test_list() {
for test_case in $TEST_LIST; do
: "Running test $test_case"
[ -f test/$test_case ] && source test/$test_case
[ -f ../test/$test_case ] && source ../test/$test_case
$test_case
done;
}
# ct_gen_self_signed_cert_pem
# ---------------------------
# Generates a self-signed PEM certificate pair into specified directory.
# Argument: output_dir - output directory path
# Argument: base_name - base name of the certificate files
# Resulted files will be those:
# <output_dir>/<base_name>-cert-selfsigned.pem -- public PEM cert
# <output_dir>/<base_name>-key.pem -- PEM private key
ct_gen_self_signed_cert_pem() {
local output_dir=$1 ; shift
local base_name=$1 ; shift
mkdir -p ${output_dir}
openssl req -newkey rsa:2048 -nodes -keyout ${output_dir}/${base_name}-key.pem -subj '/C=GB/ST=Berkshire/L=Newbury/O=My Server Company' > ${base_name}-req.pem
openssl req -new -x509 -nodes -key ${output_dir}/${base_name}-key.pem -batch > ${output_dir}/${base_name}-cert-selfsigned.pem
}
# ct_obtain_input FILE|DIR|URL
# --------------------
# Either copies a file or a directory to a tmp location for local copies, or
# downloads the file from remote location.
# Resulted file path is printed, so it can be later used by calling function.
# Arguments: input - local file, directory or remote URL
function ct_obtain_input() {
local input=$1
local extension="${input##*.}"
# Try to use same extension for the temporary file if possible
[[ "${extension}" =~ ^[a-z0-9]*$ ]] && extension=".${extension}" || extension=""
local output=$(mktemp "/var/tmp/test-input-XXXXXX$extension")
if [ -f "${input}" ] ; then
cp -f "${input}" "${output}"
elif [ -d "${input}" ] ; then
rm -f "${output}"
cp -r -LH "${input}" "${output}"
elif echo "${input}" | grep -qe '^http\(s\)\?://' ; then
curl "${input}" > "${output}"
else
echo "ERROR: file type not known: ${input}" >&2
return 1
fi
echo "${output}"
}
# ct_test_response
# ----------------
# Perform GET request to the application container, checks output with
# a reg-exp and HTTP response code.
# Argument: url - request URL path
# Argument: expected_code - expected HTTP response code
# Argument: body_regexp - PCRE regular expression that must match the response body
# Argument: max_attempts - Optional number of attempts (default: 20), three seconds sleep between
# Argument: ignore_error_attempts - Optional number of attempts when we ignore error output (default: 10)
ct_test_response() {
local url="$1"
local expected_code="$2"
local body_regexp="$3"
local max_attempts=${4:-20}
local ignore_error_attempts=${5:-10}
: " Testing the HTTP(S) response for <${url}>"
local sleep_time=3
local attempt=1
local result=1
local status
local response_code
local response_file=$(mktemp /tmp/ct_test_response_XXXXXX)
while [ ${attempt} -le ${max_attempts} ]; do
curl --connect-timeout 10 -s -w '%{http_code}' "${url}" >${response_file} && status=0 || status=1
if [ ${status} -eq 0 ]; then
response_code=$(cat ${response_file} | tail -c 3)
if [ "${response_code}" -eq "${expected_code}" ]; then
result=0
fi
cat ${response_file} | grep -qP -e "${body_regexp}" || result=1;
# Some services return 40x code until they are ready, so let's give them
# some chance and not end with failure right away
# Do not wait if we already have expected outcome though
if [ ${result} -eq 0 -o ${attempt} -gt ${ignore_error_attempts} -o ${attempt} -eq ${max_attempts} ] ; then
break
fi
fi
attempt=$(( ${attempt} + 1 ))
sleep ${sleep_time}
done
rm -f ${response_file}
return ${result}
}
# ct_registry_from_os OS
# ----------------
# Transform operating system string [os] into registry url
# Argument: OS - string containing the os version
ct_registry_from_os() {
local registry=""
case $1 in
rhel7)
registry=registry.access.redhat.com
;;
*)
registry=docker.io
;;
esac
echo "$registry"
}
# ct_assert_cmd_success CMD
# ----------------
# Evaluates [cmd] and fails if it does not succeed.
# Argument: CMD - Command to be run
function ct_assert_cmd_success() {
echo "Checking '$*' for success ..."
if ! eval "$@" &>/dev/null; then
echo " FAIL"
return 1
fi
echo " PASS"
return 0
}
# ct_assert_cmd_failure CMD
# ----------------
# Evaluates [cmd] and fails if it succeeds.
# Argument: CMD - Command to be run
function ct_assert_cmd_failure() {
echo "Checking '$*' for failure ..."
if eval "$@" &>/dev/null; then
echo " FAIL"
return 1
fi
echo " PASS"
return 0
}
# ct_random_string [LENGTH=10]
# ----------------------------
# Generate pseudorandom alphanumeric string of LENGTH bytes, the
# default length is 10. The string is printed on stdout.
ct_random_string()
(
export LC_ALL=C
dd if=/dev/urandom count=1 bs=10k 2>/dev/null \
| tr -dc 'a-z0-9' \
| fold -w "${1-10}" \
| head -n 1
)
# ct_s2i_usage IMG_NAME [S2I_ARGS]
# ----------------------------
# Create a container and run the usage script inside
# Argument: IMG_NAME - name of the image to be used for the container run
# Argument: S2I_ARGS - Additional list of source-to-image arguments, currently unused.
ct_s2i_usage()
{
local img_name=$1; shift
local s2i_args="$*";
local usage_command="/usr/libexec/s2i/usage"
docker run --rm "$img_name" bash -c "$usage_command"
}
# ct_s2i_build_as_df APP_PATH SRC_IMAGE DST_IMAGE [S2I_ARGS]
# ----------------------------
# Create a new s2i app image from local sources in a similar way as source-to-image would have used.
# Argument: APP_PATH - local path to the app sources to be used in the test
# Argument: SRC_IMAGE - image to be used as a base for the s2i build
# Argument: DST_IMAGE - image name to be used during the tagging of the s2i build result
# Argument: S2I_ARGS - Additional list of source-to-image arguments.
# Only used to check for pull-policy=never and environment variable definitions.
ct_s2i_build_as_df()
{
local app_path=$1; shift
local src_image=$1; shift
local dst_image=$1; shift
local s2i_args="$*";
local local_app=upload/src/
local local_scripts=upload/scripts/
local user_id=
local df_name=
local tmpdir=
# Use /tmp to not pollute cwd
tmpdir=$(mktemp -d)
df_name=$(mktemp -p "$tmpdir" Dockerfile.XXXX)
pushd "$tmpdir"
# Check if the image is available locally and try to pull it if it is not
docker images "$src_image" &>/dev/null || echo "$s2i_args" | grep -q "pull-policy=never" || docker pull "$src_image"
user_id=$(docker inspect -f "{{.ContainerConfig.User}}" "$src_image")
# Strip file:// from APP_PATH and copy its contents into current context
mkdir -p "$local_app"
cp -r "${app_path/file:\/\//}/." "$local_app"
[ -d "$local_app/.s2i/bin/" ] && mv "$local_app/.s2i/bin" "$local_scripts"
# Create a Dockerfile named df_name and fill it with proper content
#FIXME: Some commands could be combined into a single layer but not sure if worth the trouble for testing purposes
cat <<EOF >"$df_name"
FROM $src_image
LABEL "io.openshift.s2i.build.image"="$src_image" \\
"io.openshift.s2i.build.source-location"="$app_path"
USER root
COPY $local_app /tmp/src
EOF
[ -d "$local_scripts" ] && echo "COPY $local_scripts /tmp/scripts" >> "$df_name" &&
echo "RUN chown -R $user_id:0 /tmp/scripts" >>"$df_name"
echo "RUN chown -R $user_id:0 /tmp/src" >>"$df_name"
# Check for custom environment variables inside .s2i/ folder
if [ -e "$local_app/.s2i/environment" ]; then
# Remove any comments and add the contents as ENV commands to the Dockerfile
sed '/^\s*#.*$/d' "$local_app/.s2i/environment" | while read -r line; do
echo "ENV $line" >>"$df_name"
done
fi
# Filter out env var definitions from $s2i_args and create Dockerfile ENV commands out of them
echo "$s2i_args" | grep -o -e '\(-e\|--env\)[[:space:]=]\S*=\S*' | sed -e 's/-e /ENV /' -e 's/--env[ =]/ENV /' >>"$df_name"
echo "USER $user_id" >>"$df_name"
# If exists, run the custom assemble script, else default to /usr/libexec/s2i/assemble
if [ -x "$local_scripts/assemble" ]; then
echo "RUN /tmp/scripts/assemble" >>"$df_name"
else
echo "RUN /usr/libexec/s2i/assemble" >>"$df_name"
fi
# If exists, set the custom run script as CMD, else default to /usr/libexec/s2i/run
if [ -x "$local_scripts/run" ]; then
echo "CMD /tmp/scripts/run" >>"$df_name"
else
echo "CMD /usr/libexec/s2i/run" >>"$df_name"
fi
# Run the build and tag the result
docker build -f "$df_name" -t "$dst_image" .
popd
}

39
test/ctest_WT_cache Normal file
View file

@ -0,0 +1,39 @@
#
# WT cache size test for MongoDB image.
#
# Always use sourced from a specific container testfile
ctest_WT_cache() {
local name="WT_cache_size"
echo " Testing setting of WT cache size"
local database='db'
local user='user'
local password='password'
local admin_password='adminPassword'
local common_arguments="
-e MONGODB_DATABASE=${database}
-e MONGODB_USER=${user}
-e MONGODB_PASSWORD=${password}
-e MONGODB_ADMIN_PASSWORD=${admin_password}
"
# need to set these because `mongo_cmd` relies on global variables
USER=${user}
PASS=${password}
DB=${database}
ADMIN_PASS=${admin_password}
# minimum is 1G
CONTAINER_ARGS="$common_arguments -m 200M"
ct_create_container "${name}_200M"
test_connection ${name}_200M
mongo_admin_cmd "if (db.serverStatus()['wiredTiger']['cache']['maximum bytes configured'] == Math.pow(2,30)){quit(0)}; quit(1)"
# if greater that 1G, use 60% of (RAM - 1G)
CONTAINER_ARGS="$common_arguments -m 6G"
ct_create_container "${name}_6G"
test_connection ${name}_6G
mongo_admin_cmd "if (db.serverStatus()['wiredTiger']['cache']['maximum bytes configured'] == 3*Math.pow(2,30)){quit(0)}; quit(1)"
echo " Success!"
echo
}

View file

@ -0,0 +1,71 @@
#
# Change password test for MongoDB image.
#
# Always use sourced from a specific container testfile
function ctest_change_password() {
local name="change_password"
local database='db'
local user='user'
local password='password'
local admin_password='adminPassword'
local volume_dir
volume_dir=`mktemp -d --tmpdir mongodb-testdata.XXXXX`
chmod a+rwx ${volume_dir}
ct_path_append volumes_to_clean "$volume_dir"
CONTAINER_ARGS="
-e MONGODB_DATABASE=${database}
-e MONGODB_USER=${user}
-e MONGODB_PASSWORD=${password}
-e MONGODB_ADMIN_PASSWORD=${admin_password}
-v ${volume_dir}:/var/lib/mongodb/data:z
"
ct_create_container $name
# need to set these because `mongo_cmd` relies on global variables
USER=${user}
PASS=${password}
DB=${database}
# need this to wait for the container to start up
CONTAINER_IP=$(ct_get_cip ${name})
test_connection ${name}
echo " Testing login"
assert_login_access ${user} ${password} true
DB='admin' assert_login_access 'admin' ${admin_password} true
echo " Changing passwords"
docker stop $(ct_get_cid ${name})
CONTAINER_ARGS="
-e MONGODB_DATABASE=${database}
-e MONGODB_USER=${user}
-e MONGODB_PASSWORD=NEW_${password}
-e MONGODB_ADMIN_PASSWORD=NEW_${admin_password}
-v ${volume_dir}:/var/lib/mongodb/data:z
"
ct_create_container "${name}_NEW"
# need to set this because `mongo_cmd` relies on global variables
PASS="NEW_${password}"
# need this to wait for the container to start up
CONTAINER_IP=$(ct_get_cip "${name}_NEW")
test_connection "${name}_NEW"
echo " Testing login with new passwords"
assert_login_access ${user} "NEW_${password}" true
assert_login_access ${user} ${password} false
DB='admin' assert_login_access 'admin' "NEW_${admin_password}" true
DB='admin' assert_login_access 'admin' ${admin_password} false
echo " Success!"
}

View file

@ -0,0 +1,24 @@
#
# Container creation test for MongoDB image.
#
# Always use sourced from a specific container testfile
function ctest_container_creation() {
echo " Testing wrong user variables usage"
ct_assert_container_creation_fails -e MONGODB_USER=user -e MONGODB_PASSWORD=pass
ct_assert_container_creation_fails -e MONGODB_DATABASE=db -e MONGODB_PASSWORD=pass
ct_assert_container_creation_fails -e MONGODB_DATABASE=db -e MONGODB_USER=user
ct_assert_container_creation_fails -e MONGODB_USER=user -e MONGODB_DATABASE=db -e MONGODB_PASSWORD=pass
ct_assert_container_creation_fails -e MONGODB_ADMIN_PASSWORD=Pass -e MONGODB_USER=user
ct_assert_container_creation_fails -e MONGODB_ADMIN_PASSWORD=Pass -e MONGODB_PASSWORD=pass
ct_assert_container_creation_fails -e MONGODB_ADMIN_PASSWORD=Pass -e MONGODB_DATABASE=db
echo " Success!"
echo " Testing good user variables usage"
ct_assert_container_creation_fails -e MONGODB_ADMIN_PASSWORD=Pass || [ $? -eq 1 ]
ct_assert_container_creation_fails -e MONGODB_ADMIN_PASSWORD=Pass -e MONGODB_USER=user -e MONGODB_DATABASE=db -e MONGODB_PASSWORD=pass || [ $? -eq 1 ]
echo " Success!"
}

11
test/ctest_doc_content Normal file
View file

@ -0,0 +1,11 @@
#
# Documentation content test for MongoDB image.
#
# Always use sourced from a specific container testfile
function ctest_doc_content() {
: " Testing documentation content"
ct_doc_content_old MONGODB\\_ADMIN\\_PASSWORD volume
: " Success!"
}

11
test/ctest_general Normal file
View file

@ -0,0 +1,11 @@
#
# General test for MongoDB image.
#
# Always use sourced from a specific container testfile
function ctest_general() {
USER="user1" PASS="pass1" DB="test_db" ADMIN_PASS="r00t" test_general admin
# Test with random uid in container
CONTAINER_ARGS="-u 12345" USER="user1" PASS="pass1" DB="test_db" ADMIN_PASS="r00t" test_general admin_altuid
}

View file

@ -0,0 +1,67 @@
#
# Local replication test for MongoDB image.
#
# Always use sourced from a specific container testfile
function ctest_local_replication() {
function print_logs() {
for file in $CID_FILE_DIR/replset*; do
echo "INFO: printing logs for CID file ${file}"
docker logs $(cat ${file})
done
}
trap print_logs ERR
echo "Testing replication on local docker"
#Initializing replicaset
cat > variables <<EOF
MONGODB_DATABASE=db
MONGODB_USER=user
MONGODB_PASSWORD=password
MONGODB_ADMIN_PASSWORD=adminPassword
MONGODB_REPLICA_NAME=rs0
MONGODB_KEYFILE_VALUE=xxxxxxxxxxxx
MONGODB_SMALLFILES=true
MONGODB_SERVICE_NAME=mongodb
EOF
source variables
local network_name="mongodb-replset-$$"
docker network create ${network_name}
docker run -d --cidfile $CID_FILE_DIR/replset0 --name=replset-0 --hostname=replset-0 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-0 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
docker run -d --cidfile $CID_FILE_DIR/replset1 --name=replset-1 --hostname=replset-1 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-1 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
docker run -d --cidfile $CID_FILE_DIR/replset2 --name=replset-2 --hostname=replset-2 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-2 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
local host="$(docker run --rm --env-file=variables --network ${network_name} ${IMAGE_NAME} bash -c '. /usr/share/container-scripts/mongodb/common.sh && echo $(replset_addr admin)')"
# Storing document into replset and wait replication to finish ...
docker run --rm --env-file=variables --network ${network_name} ${IMAGE_NAME} bash -c "set -e
. /usr/share/container-scripts/mongodb/common.sh
. /usr/share/container-scripts/mongodb/test-functions.sh
wait_for_mongo_up '${host}'
wait_replicaset_members '${host}' 3
insert_and_wait_for_replication '${host}' '{a:5, b:10}'"
# Adding new container
docker run -d --cidfile $CID_FILE_DIR/replset3 --name=replset-3 --hostname=replset-3 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-3 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
# Storing document into replset and wait replication to finish ...
docker run --rm --env-file=variables --network ${network_name} ${IMAGE_NAME} bash -c "set -e
. /usr/share/container-scripts/mongodb/common.sh
. /usr/share/container-scripts/mongodb/test-functions.sh
wait_for_mongo_up '${host}'
wait_replicaset_members '${host}' 4
insert_and_wait_for_replication '${host}' '{a:5, b:10}'"
rm variables
trap ERR
echo " Success!"
}

40
test/ctest_ssl Normal file
View file

@ -0,0 +1,40 @@
function _ssl_test_image() {
local name="$1"
local CONTAINER_ARGS="${CONTAINER_ARGS:-} \
-e MONGODB_ADMIN_PASSWORD=adminPass \
-e MONGODB_USER=user \
-e MONGODB_PASSWORD=password \
-e MONGODB_DATABASE=db"
ADMIN_PASS=adminPass
ct_create_container $name
CONTAINER_IP=$(ct_get_cip ${name})
# mongod fails after connection with ssl, even when binded to localhost
# so waiting till initialization ends
sleep 10
echo " Testing connection without SSL args"
! DB=db USER=user PASS=password test_connection "${name}"
local shell_args='--ssl --sslAllowInvalidCertificates --sslPEMKeyFile /opt/app-root/src/mongodb-ssl/mongodb.pem'
echo " Testing SSL connection"
DB=db USER=user PASS=password test_connection "${name}"
shell_args=
}
function ctest_ssl() {
CONTAINER_ARGS=
echo " Testing SSL s2i"
ct_s2i_build_as_df file://${TEST_DIR}/examples/ssl/ ${IMAGE_NAME} ${IMAGE_NAME}-testapp
IMAGE_NAME=${IMAGE_NAME}-testapp _ssl_test_image "ssl_config_s2i"
echo " Testing SSL mount"
test_app_dir=$(mktemp -d)
cp -r ${TEST_DIR}/examples/ssl/ ${test_app_dir}/
chmod -R a+rX ${test_app_dir}
CONTAINER_ARGS="-v ${test_app_dir}/ssl/:/opt/app-root/src/:z" _ssl_test_image "ssl_config_mount"
rm -rf ${test_app_dir}
echo " Success!"
}

View file

@ -0,0 +1,18 @@
Image extending example
===================================
See image [README.md](https://github.com/sclorg/mongodb-container/blob/master/3.2/root/usr/share/container-scripts/mongodb/README.md) for decription how to extend image using s2i.
What this example configuration does:
```
├── mongodb-cfg
│   └── mongod.conf # Configuration file for mongod server
├── mongodb-init
│   ├── 11-init-backup-user.sh # Create special user for backups
│   └── 50-initial-store.sh # Store information in database
├── mongodb-pre-init
│   ├── 11-check-additional-user.sh # Check that credentials for backups are provided
│   └── 20-setup-wiredtiger-cache.sh # Overwrites default script for configuring wiredTiger cache
└── README.md
```

View file

@ -0,0 +1,46 @@
# This example adds socket and wiredtiger settings to default config file
##
## For list of options visit:
## https://docs.mongodb.org/manual/reference/configuration-options/
##
# systemLog Options - How to do logging
systemLog:
# Runs the mongod in a quiet mode that attempts to limit the amount of output
quiet: ${MONGODB_QUIET}
# net Options - Network interfaces settings
net:
# Specify port number (27017 by default)
port: ${CONTAINER_PORT}
# Specify on which interfaces start listening
bindIp: "0.0.0.0"
unixDomainSocket:
# Enable/disable listening on the UNIX domain socket (true by default)
enabled: true
# Alternative directory for UNIX domain sockets (defaults to /tmp)
pathPrefix: /var/lib/mongodb/socket
# storage Options - How and Where to store data
storage:
# Directory for datafiles (defaults to /data/db/)
dbPath: ${MONGODB_DATADIR}
wiredTiger:
engineConfig:
# The maximum size of the cache that WiredTiger will use for all data
# (max(60% of RAM - 1GB, 1GB) by default)
cacheSizeGB: 5
# replication Options - Configures replication
replication:
# Specifies a maximum size in megabytes for the replication operation log (i.e. the oplog,
# 5% of disk space by default)
oplogSizeMB: 128

View file

@ -0,0 +1,46 @@
# This example adds socket and wiredtiger settings to default config file
##
## For list of options visit:
## https://docs.mongodb.org/manual/reference/configuration-options/
##
# systemLog Options - How to do logging
systemLog:
# Runs the mongod in a quiet mode that attempts to limit the amount of output
quiet: ${MONGODB_QUIET}
# net Options - Network interfaces settings
net:
# Specify port number (27017 by default)
port: ${CONTAINER_PORT}
# Specify on which interfaces start listening
bindIp: "0.0.0.0"
unixDomainSocket:
# Enable/disable listening on the UNIX domain socket (true by default)
enabled: true
# Alternative directory for UNIX domain sockets (defaults to /tmp)
pathPrefix: /var/lib/mongodb/socket
# storage Options - How and Where to store data
storage:
# Directory for datafiles (defaults to /data/db/)
dbPath: ${MONGODB_DATADIR}
# Enable or disable the preallocation of data files (true by default)
preallocDataFiles: ${MONGODB_PREALLOC}
# Use a smaller default file size (false by default)
smallFiles: ${MONGODB_SMALLFILES}
# replication Options - Configures replication
replication:
# Specifies a maximum size in megabytes for the replication operation log (i.e. the oplog,
# 5% of disk space by default)
oplogSizeMB: 128

View file

@ -0,0 +1 @@
mongod-WT.conf

View file

@ -0,0 +1,3 @@
# Create directory for socket
mkdir ~/socket/

View file

@ -0,0 +1,3 @@
# Check that user credentials for backup is set
[[ -v MONGODB_BACKUP_USER && -v MONGODB_BACKUP_PASSWORD ]] || usage "You have to set all variables for user for doing backup: MONGODB_BACKUP_USER, MONGODB_BACKUP_PASSWORD"

View file

@ -0,0 +1,3 @@
# Do not set default size for WiredTiger. It is set in configuration file.
#
# (overwrites default scripts for setting cache size)

View file

@ -0,0 +1,8 @@
# Create backup user
js_command="db.createUser({user: '${MONGODB_BACKUP_USER}', pwd: '${MONGODB_BACKUP_PASSWORD}', roles: [ 'readAnyDatabase' ]});"
if ! mongo_cmd --host localhost admin -u admin -p$MONGODB_ADMIN_PASSWORD <<<"$js_command" ; then
echo >&2 "=> Failed to create MongoDB user: ${MONGODB_BACKUP_USER}"
exit 1
fi

View file

@ -0,0 +1,7 @@
# Store some initial information into database
# Connect database as optional user
mongo_cmd --host localhost $MONGODB_DATABASE -u $MONGODB_USER -p$MONGODB_PASSWORD <<<"db.constants.insert({author: \"sclorg@redhat.com\"})"
# Connect database as admin user
mongo_cmd --host localhost admin -u admin -p$MONGODB_ADMIN_PASSWORD <<<"db.getSiblingDB(\"$MONGODB_DATABASE\").constants.insert({subject: \"s2i build example\"})"

View file

@ -0,0 +1,272 @@
{
"kind": "Template",
"apiVersion": "v1",
"metadata": {
"name": "mongodb-ephemeral",
"annotations": {
"openshift.io/display-name": "MongoDB (Ephemeral)",
"description": "MongoDB database service, without persistent storage. For more information about using this template, including OpenShift considerations, see https://github.com/sclorg/mongodb-container/blob/master/3.2/README.md.\n\nWARNING: Any data stored will be lost upon pod destruction. Only use this template for testing",
"iconClass": "icon-mongodb",
"tags": "database,mongodb",
"openshift.io/long-description": "This template provides a standalone MongoDB server with a database created. The database is not stored on persistent storage, so any restart of the service will result in all data being lost. The database name, username, and password are chosen via parameters when provisioning this service.",
"openshift.io/provider-display-name": "Red Hat, Inc.",
"openshift.io/documentation-url": "https://docs.okd.io/latest/using_images/db_images/mongodb.html",
"openshift.io/support-url": "https://access.redhat.com"
}
},
"message": "The following service(s) have been created in your project: ${DATABASE_SERVICE_NAME}.\n\n Username: ${MONGODB_USER}\n Password: ${MONGODB_PASSWORD}\n Database Name: ${MONGODB_DATABASE}\n Connection URL: mongodb://${MONGODB_USER}:${MONGODB_PASSWORD}@${DATABASE_SERVICE_NAME}/${MONGODB_DATABASE}\n\nFor more information about using this template, including OpenShift considerations, see https://github.com/sclorg/mongodb-container/blob/master/3.2/README.md.",
"labels": {
"template": "mongodb-ephemeral-template"
},
"objects": [
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"annotations": {
"template.openshift.io/expose-username": "{.data['database-user']}",
"template.openshift.io/expose-password": "{.data['database-password']}",
"template.openshift.io/expose-admin_password": "{.data['database-admin-password']}",
"template.openshift.io/expose-database_name": "{.data['database-name']}"
}
},
"stringData" : {
"database-user" : "${MONGODB_USER}",
"database-password" : "${MONGODB_PASSWORD}",
"database-admin-password" : "${MONGODB_ADMIN_PASSWORD}",
"database-name" : "${MONGODB_DATABASE}"
}
},
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"annotations": {
"template.openshift.io/expose-uri": "mongodb://{.spec.clusterIP}:{.spec.ports[?(.name==\"mongo\")].port}"
}
},
"spec": {
"ports": [
{
"name": "mongo",
"protocol": "TCP",
"port": 27017,
"targetPort": 27017,
"nodePort": 0
}
],
"selector": {
"name": "${DATABASE_SERVICE_NAME}"
},
"type": "ClusterIP",
"sessionAffinity": "None"
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"annotations": {
"template.alpha.openshift.io/wait-for-ready": "true"
}
},
"spec": {
"strategy": {
"type": "Recreate"
},
"triggers": [
{
"type": "ImageChange",
"imageChangeParams": {
"automatic": true,
"containerNames": [
"mongodb"
],
"from": {
"kind": "ImageStreamTag",
"name": "mongodb:${MONGODB_VERSION}",
"namespace": "${NAMESPACE}"
},
"lastTriggeredImage": ""
}
},
{
"type": "ConfigChange"
}
],
"replicas": 1,
"selector": {
"name": "${DATABASE_SERVICE_NAME}"
},
"template": {
"metadata": {
"labels": {
"name": "${DATABASE_SERVICE_NAME}"
}
},
"spec": {
"containers": [
{
"name": "mongodb",
"image": " ",
"ports": [
{
"containerPort": 27017,
"protocol": "TCP"
}
],
"readinessProbe": {
"timeoutSeconds": 1,
"initialDelaySeconds": 3,
"exec": {
"command": [ "/bin/sh", "-i", "-c", "mongo 127.0.0.1:27017/$MONGODB_DATABASE -u $MONGODB_USER -p $MONGODB_PASSWORD --eval=\"quit()\""]
}
},
"livenessProbe": {
"timeoutSeconds": 1,
"initialDelaySeconds": 30,
"tcpSocket": {
"port": 27017
}
},
"env": [
{
"name": "MONGODB_USER",
"valueFrom": {
"secretKeyRef" : {
"name" : "${DATABASE_SERVICE_NAME}",
"key" : "database-user"
}
}
},
{
"name": "MONGODB_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${DATABASE_SERVICE_NAME}",
"key" : "database-password"
}
}
},
{
"name": "MONGODB_ADMIN_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${DATABASE_SERVICE_NAME}",
"key" : "database-admin-password"
}
}
},
{
"name": "MONGODB_DATABASE",
"valueFrom": {
"secretKeyRef" : {
"name" : "${DATABASE_SERVICE_NAME}",
"key" : "database-name"
}
}
}
],
"resources": {
"limits": {
"memory": "${MEMORY_LIMIT}"
}
},
"volumeMounts": [
{
"name": "${DATABASE_SERVICE_NAME}-data",
"mountPath": "/var/lib/mongodb/data"
}
],
"terminationMessagePath": "/dev/termination-log",
"imagePullPolicy": "IfNotPresent",
"capabilities": {},
"securityContext": {
"capabilities": {},
"privileged": false
}
}
],
"volumes": [
{
"name": "${DATABASE_SERVICE_NAME}-data",
"emptyDir": {
"medium": ""
}
}
],
"restartPolicy": "Always",
"dnsPolicy": "ClusterFirst"
}
}
},
"status": {}
}
],
"parameters": [
{
"name": "MEMORY_LIMIT",
"displayName": "Memory Limit",
"description": "Maximum amount of memory the container can use.",
"value": "512Mi",
"required": true
},
{
"name": "NAMESPACE",
"displayName": "Namespace",
"description": "The OpenShift Namespace where the ImageStream resides.",
"value": "openshift"
},
{
"name": "DATABASE_SERVICE_NAME",
"displayName": "Database Service Name",
"description": "The name of the OpenShift Service exposed for the database.",
"value": "mongodb",
"required": true
},
{
"name": "MONGODB_USER",
"displayName": "MongoDB Connection Username",
"description": "Username for MongoDB user that will be used for accessing the database.",
"generate": "expression",
"from": "user[A-Z0-9]{3}",
"required": true
},
{
"name": "MONGODB_PASSWORD",
"displayName": "MongoDB Connection Password",
"description": "Password for the MongoDB connection user.",
"generate": "expression",
"from": "[a-zA-Z0-9]{16}",
"required": true
},
{
"name": "MONGODB_DATABASE",
"displayName": "MongoDB Database Name",
"description": "Name of the MongoDB database accessed.",
"value": "sampledb",
"required": true
},
{
"name": "MONGODB_ADMIN_PASSWORD",
"displayName": "MongoDB Admin Password",
"description": "Password for the database admin user.",
"generate": "expression",
"from": "[a-zA-Z0-9]{16}",
"required": true
},
{
"name": "MONGODB_VERSION",
"displayName": "Version of MongoDB Image",
"description": "Version of MongoDB image to be used (3.6 or latest).",
"value": "3.6",
"required": true
}
]
}

View file

@ -0,0 +1,296 @@
{
"kind": "Template",
"apiVersion": "v1",
"metadata": {
"name": "mongodb-persistent",
"annotations": {
"openshift.io/display-name": "MongoDB",
"description": "MongoDB database service, with persistent storage. For more information about using this template, including OpenShift considerations, see https://github.com/sclorg/mongodb-container/blob/master/3.2/README.md.\n\nNOTE: Scaling to more than one replica is not supported. You must have persistent volumes available in your cluster to use this template.",
"iconClass": "icon-mongodb",
"tags": "database,mongodb",
"openshift.io/long-description": "This template provides a standalone MongoDB server with a database created. The database is stored on persistent storage. The database name, username, and password are chosen via parameters when provisioning this service.",
"openshift.io/provider-display-name": "Red Hat, Inc.",
"openshift.io/documentation-url": "https://docs.okd.io/latest/using_images/db_images/mongodb.html",
"openshift.io/support-url": "https://access.redhat.com"
}
},
"message": "The following service(s) have been created in your project: ${DATABASE_SERVICE_NAME}.\n\n Username: ${MONGODB_USER}\n Password: ${MONGODB_PASSWORD}\n Database Name: ${MONGODB_DATABASE}\n Connection URL: mongodb://${MONGODB_USER}:${MONGODB_PASSWORD}@${DATABASE_SERVICE_NAME}/${MONGODB_DATABASE}\n\nFor more information about using this template, including OpenShift considerations, see https://github.com/sclorg/mongodb-container/blob/master/3.2/README.md.",
"labels": {
"template": "mongodb-persistent-template"
},
"objects": [
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"annotations": {
"template.openshift.io/expose-username": "{.data['database-user']}",
"template.openshift.io/expose-password": "{.data['database-password']}",
"template.openshift.io/expose-admin_password": "{.data['database-admin-password']}",
"template.openshift.io/expose-database_name": "{.data['database-name']}"
}
},
"stringData" : {
"database-user" : "${MONGODB_USER}",
"database-password" : "${MONGODB_PASSWORD}",
"database-admin-password" : "${MONGODB_ADMIN_PASSWORD}",
"database-name" : "${MONGODB_DATABASE}"
}
},
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"annotations": {
"template.openshift.io/expose-uri": "mongodb://{.spec.clusterIP}:{.spec.ports[?(.name==\"mongo\")].port}"
}
},
"spec": {
"ports": [
{
"name": "mongo",
"protocol": "TCP",
"port": 27017,
"targetPort": 27017,
"nodePort": 0
}
],
"selector": {
"name": "${DATABASE_SERVICE_NAME}"
},
"type": "ClusterIP",
"sessionAffinity": "None"
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "PersistentVolumeClaim",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}"
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"resources": {
"requests": {
"storage": "${VOLUME_CAPACITY}"
}
}
}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "${DATABASE_SERVICE_NAME}",
"annotations": {
"template.alpha.openshift.io/wait-for-ready": "true"
}
},
"spec": {
"strategy": {
"type": "Recreate"
},
"triggers": [
{
"type": "ImageChange",
"imageChangeParams": {
"automatic": true,
"containerNames": [
"mongodb"
],
"from": {
"kind": "ImageStreamTag",
"name": "mongodb:${MONGODB_VERSION}",
"namespace": "${NAMESPACE}"
},
"lastTriggeredImage": ""
}
},
{
"type": "ConfigChange"
}
],
"replicas": 1,
"selector": {
"name": "${DATABASE_SERVICE_NAME}"
},
"template": {
"metadata": {
"labels": {
"name": "${DATABASE_SERVICE_NAME}"
}
},
"spec": {
"containers": [
{
"name": "mongodb",
"image": " ",
"ports": [
{
"containerPort": 27017,
"protocol": "TCP"
}
],
"readinessProbe": {
"timeoutSeconds": 1,
"initialDelaySeconds": 3,
"exec": {
"command": [ "/bin/sh", "-i", "-c", "mongo 127.0.0.1:27017/$MONGODB_DATABASE -u $MONGODB_USER -p $MONGODB_PASSWORD --eval=\"quit()\""]
}
},
"livenessProbe": {
"timeoutSeconds": 1,
"initialDelaySeconds": 30,
"tcpSocket": {
"port": 27017
}
},
"env": [
{
"name": "MONGODB_USER",
"valueFrom": {
"secretKeyRef" : {
"name" : "${DATABASE_SERVICE_NAME}",
"key" : "database-user"
}
}
},
{
"name": "MONGODB_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${DATABASE_SERVICE_NAME}",
"key" : "database-password"
}
}
},
{
"name": "MONGODB_ADMIN_PASSWORD",
"valueFrom": {
"secretKeyRef" : {
"name" : "${DATABASE_SERVICE_NAME}",
"key" : "database-admin-password"
}
}
},
{
"name": "MONGODB_DATABASE",
"valueFrom": {
"secretKeyRef" : {
"name" : "${DATABASE_SERVICE_NAME}",
"key" : "database-name"
}
}
}
],
"resources": {
"limits": {
"memory": "${MEMORY_LIMIT}"
}
},
"volumeMounts": [
{
"name": "${DATABASE_SERVICE_NAME}-data",
"mountPath": "/var/lib/mongodb/data"
}
],
"terminationMessagePath": "/dev/termination-log",
"imagePullPolicy": "IfNotPresent",
"capabilities": {},
"securityContext": {
"capabilities": {},
"privileged": false
}
}
],
"volumes": [
{
"name": "${DATABASE_SERVICE_NAME}-data",
"persistentVolumeClaim": {
"claimName": "${DATABASE_SERVICE_NAME}"
}
}
],
"restartPolicy": "Always",
"dnsPolicy": "ClusterFirst"
}
}
},
"status": {}
}
],
"parameters": [
{
"name": "MEMORY_LIMIT",
"displayName": "Memory Limit",
"description": "Maximum amount of memory the container can use.",
"value": "512Mi",
"required": true
},
{
"name": "NAMESPACE",
"displayName": "Namespace",
"description": "The OpenShift Namespace where the ImageStream resides.",
"value": "openshift"
},
{
"name": "DATABASE_SERVICE_NAME",
"displayName": "Database Service Name",
"description": "The name of the OpenShift Service exposed for the database.",
"value": "mongodb",
"required": true
},
{
"name": "MONGODB_USER",
"displayName": "MongoDB Connection Username",
"description": "Username for MongoDB user that will be used for accessing the database.",
"generate": "expression",
"from": "user[A-Z0-9]{3}",
"required": true
},
{
"name": "MONGODB_PASSWORD",
"displayName": "MongoDB Connection Password",
"description": "Password for the MongoDB connection user.",
"generate": "expression",
"from": "[a-zA-Z0-9]{16}",
"required": true
},
{
"name": "MONGODB_DATABASE",
"displayName": "MongoDB Database Name",
"description": "Name of the MongoDB database accessed.",
"value": "sampledb",
"required": true
},
{
"name": "MONGODB_ADMIN_PASSWORD",
"displayName": "MongoDB Admin Password",
"description": "Password for the database admin user.",
"generate": "expression",
"from": "[a-zA-Z0-9]{16}",
"required": true
},
{
"name": "VOLUME_CAPACITY",
"displayName": "Volume Capacity",
"description": "Volume space available for data, e.g. 512Mi, 2Gi.",
"value": "1Gi",
"required": true
},
{
"name": "MONGODB_VERSION",
"displayName": "Version of MongoDB Image",
"description": "Version of MongoDB image to be used (3.6 or latest).",
"value": "3.6",
"required": true
}
]
}

View file

@ -0,0 +1,183 @@
# MongoDB Replication Example Using a StatefulSet (ex-PetSet)
This [MongoDB replication](https://docs.mongodb.com/manual/replication/) example
uses a [StatefulSet](https://kubernetes.io/docs/concepts/abstractions/controllers/statefulsets/)
to manage replica set members.
It is supported by an example [OpenShift
template](https://docs.okd.io/latest/dev_guide/templates.html) and
scripts that automate replica set initiation, baked in the
[centos/mongodb-32-centos7](https://hub.docker.com/r/centos/mongodb-32-centos7/)
image (and its RHEL variant) built from this source repository.
## Getting Started
You will need an OpenShift cluster where you can deploy a template. If you don't
have an existing OpenShift installation yet, the easiest way to get started and
try out this example is using the
[`oc cluster up`](https://github.com/openshift/origin/blob/master/docs/cluster_up_down.md)
command.
This tutorial assumes you have the `oc` tool, are logged in and have 3
pre-created persistent volumes (or configured [persistent volume
provisioning](https://docs.okd.io/latest/install_config/persistent_storage/dynamically_provisioning_pvs.html)).
In the context of a project where you want to create a MongoDB cluster, run
`oc new-app` passing the template file as an argument:
```bash
oc new-app https://raw.githubusercontent.com/sclorg/mongodb-container/master/examples/petset/mongodb-petset-persistent.yaml
```
The command above will create a MongoDB cluster with 3 replica set members.
To list all pods:
```console
$ oc get pods -l name=mongodb
NAME READY STATUS RESTARTS AGE
mongodb-0 1/1 Running 0 50m
mongodb-1 1/1 Running 0 50m
mongodb-2 1/1 Running 0 49m
```
To see logs from the particular pod:
```console
$ oc logs mongodb-0
```
To log in to the pod:
```console
$ oc rsh mongodb-0
sh-4.2$
```
And later from one of the pods you can also login into MongoDB:
```console
sh-4.2$ mongo $MONGODB_DATABASE -u $MONGODB_USER -p$MONGODB_PASSWORD
MongoDB shell version: 3.2.6
connecting to: sampledb
rs0:PRIMARY>
```
## Example Working Scenarios
This section describes how this example is designed to work.
### Initial Deployment: 3-member Replica Set
After creating a cluster with the example template, we have a replica set with 3
members. That should be enough for most cases, as described in the
[official MongoDB documentation](https://docs.mongodb.com/manual/tutorial/deploy-replica-set/#overview).
During the lifetime of your OpenShift project, one or more of those members
might crash or fail. OpenShift automatically restarts unhealthy pods
(containers), and so will restart replica set members as necessary.
While a replica set member is down or being restarted, you may be in one of
these scenarios:
1. PRIMARY member is down
In this case, the other two members shall elect a new PRIMARY. Until then,
reads should NOT be affected, while writes will fail. After a successful
election, writes and reads will succeed normally.
2. One SECONDARY member is down
Reads and writes should be unaffected. Depending on the `oplogSize`
configuration and the write rate, the third member might fail to join back
the replica set, requiring manual intervention to re-sync its copy of the
database.
3. Any two members are down
When a three-member replica set member cannot reach any other member, it
will step down from the PRIMARY role if it had it. In this case, reads might
be served by a SECONDARY, and writes will fail. As soon as one more member
is back up, an election will pick a new PRIMARY and reads and writes will
succeed normally.
4. All members are down
In this extreme case, obviously reads and writes will fail. Once two or more
members are back up, an election will reestablish the replica set to have a
PRIMARY and a SECONDARY, such that reads and writes will succeed normally.
**Note**: for production usage, you should maintain as much separation between
members as possible. It is recommended to use one or more of the
[node selection features](http://kubernetes.io/docs/user-guide/node-selection/)
to schedule StatefulSet pods into different nodes, and to provide them storage backed
by independent volumes.
### Scaling Up
MongoDB recommends an odd number of members in a replica set. An admin may
decide to have, for instance, 5 members in the replica set. Given that there are
sufficient available persistent volumes, or a dynamic storage provisioner is
present, scaling up is done with the `oc scale` command:
```bash
oc scale --replicas=5 petset/mongodb
```
New pods (containers) are created and they connect to the replica set, updating
its configuration.
With five members, the scenarios described in the previous section should work
similarly, though now there is an added resilience to tolerate up to 2 members
being simultaneously unavailable.
**Note**: scaling up an existing database might require manual intervention. If
the database size is greater than the `oplogSize` configuration, a manual
initial sync of the new members will be required. Please consult the MongoDB
replication manual for more information.
### Scaling Down
An admin may decide to scale down a replica set to save resources or for any
other reason. For instance, it is possible to go from 5 to 3 members, or from 3
to 1 member.
While scaling up might be done without manual intervention when the
preconditions are met (storage availability, size of existing database and
`oplogSize`), scaling down always require manual intervention.
To scaling down, start with setting the new number of replicas, e.g.:
```bash
oc scale --replicas=3 petset/mongodb
```
Note that if the new number of replicas still constitutes a majority of the
previous number, it is guaranteed that the replica set may elect a new PRIMARY
in case one of the pods that was deleted had that role. For example, that is the
case when going from 5 to 3 members.
On the other hand, scaling down to a lower number will temporarily render the
replica set to have only SECONDARY members and be in read-only mode. That would
be the case when scaling from 5 down to 1 member.
The next step is to update the replica set configuration to
[remove members](https://docs.mongodb.com/manual/tutorial/remove-replica-set-member/)
that no longer exist. This may be improved in the future, a possible
implementation being setting a PreStop pod hook that inspects the number of
replicas (exposed via the downward API) and determines that the pod is being
removed from the StatefulSet, and not being restarted for some other reason.
Finally, the volumes used by the decommissioned pods may be manually purged.
Follow the [StatefulSet documentation](https://kubernetes.io/docs/tasks/manage-stateful-set/deleting-a-statefulset/)
for more details on how to clean up after scaling down.
### Known Limitations
* Only MongoDB 3.2 is supported.
* You have to manually update replica set configuration in case of scaling down.
* Changing a user's and admin's password is a manual process: it requires
updating values of environment variables in the StatefulSet configuration,
changing password in the database and restarting all the pods one by one.
See also [StatefulSet limitations](https://kubernetes.io/docs/concepts/abstractions/controllers/statefulsets/#limitations).

View file

@ -0,0 +1,165 @@
kind: Template
apiVersion: v1
metadata:
name: mongodb-petset-replication
annotations:
description: "MongoDB Replication Example (based on StatefulSet). You must have persistent volumes available in your cluster to use this template."
iconClass: "icon-mongodb"
tags: "database,mongodb,replication"
parameters:
- name: MONGODB_USER
displayName: "MongoDB Connection Username"
description: "Username for MongoDB user that will be used for accessing the database."
generate: expression
from: "[a-zA-Z0-9]{3}"
required: true
- name: MONGODB_PASSWORD
displayName: "MongoDB Connection Password"
description: "Password for the MongoDB connection user."
generate: expression
from: "[a-zA-Z0-9]{16}"
required: true
- name: MONGODB_DATABASE
displayName: "MongoDB Database Name"
description: "Name of the MongoDB database accessed."
value: sampledb
required: true
- name: MONGODB_ADMIN_PASSWORD
displayName: "MongoDB Admin Password"
description: "Password for the database admin user."
generate: expression
from: "[a-zA-Z0-9]{16}"
required: true
- name: MONGODB_REPLICA_NAME
displayName: "Replica Set Name"
description: "The name of the replica set."
value: rs0
required: true
- name: MONGODB_KEYFILE_VALUE
displayName: "Keyfile Content"
description: "The value of the MongoDB keyfile (https://docs.mongodb.com/manual/core/security-internal-authentication/#internal-auth-keyfile)."
generate: expression
from: "[a-zA-Z0-9]{255}"
required: true
- name: MONGODB_IMAGE
displayName: "MongoDB Container Image"
description: "A reference to a supported MongoDB container image."
value: "centos/mongodb-32-centos7"
required: true
- name: MONGODB_SERVICE_NAME
displayName: "OpenShift Service Name"
description: "The name of the OpenShift Service exposed for the database."
value: mongodb
required: true
- name: VOLUME_CAPACITY
displayName: "Volume Capacity"
description: "Volume space available for data, e.g. 512Mi, 2Gi."
value: "1Gi"
required: true
- name: MEMORY_LIMIT
displayName: "Memory Limit"
description: "Maximum amount of memory the container can use."
value: "512Mi"
objects:
# A non-headless service which takes pod readiness into consideration
- kind: Service
apiVersion: v1
metadata:
name: "${MONGODB_SERVICE_NAME}"
spec:
# the list of ports that are exposed by this service
ports:
- name: mongodb
port: 27017
# will route traffic to pods having labels matching this selector
selector:
name: "${MONGODB_SERVICE_NAME}"
# A headless service to create DNS records
- kind: Service
apiVersion: v1
metadata:
name: "${MONGODB_SERVICE_NAME}-internal"
annotations:
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
clusterIP: None
# the list of ports that are exposed by this service
ports:
- name: mongodb
port: 27017
# will route traffic to pods having labels matching this selector
selector:
name: "${MONGODB_SERVICE_NAME}"
- kind: StatefulSet
apiVersion: apps/v1beta1
metadata:
name: "${MONGODB_SERVICE_NAME}"
spec:
# pets get DNS/hostnames that follow the pattern: ${metadata.name}-NUM.${spec.serviceName}.default.svc.cluster.local
serviceName: "${MONGODB_SERVICE_NAME}-internal"
replicas: 3
# describes the pod that will be created if insufficient replicas are detected
template:
metadata:
# this label will be used for count running pods
labels:
name: "${MONGODB_SERVICE_NAME}"
spec:
containers:
- name: mongo-container
image: "${MONGODB_IMAGE}"
ports:
- containerPort: 27017
args:
- "run-mongod-pet"
volumeMounts:
- name: mongo-data
mountPath: "/var/lib/mongodb/data"
env:
- name: MONGODB_USER
value: "${MONGODB_USER}"
- name: MONGODB_PASSWORD
value: "${MONGODB_PASSWORD}"
- name: MONGODB_DATABASE
value: "${MONGODB_DATABASE}"
- name: MONGODB_ADMIN_PASSWORD
value: "${MONGODB_ADMIN_PASSWORD}"
- name: MONGODB_REPLICA_NAME
value: "${MONGODB_REPLICA_NAME}"
- name: MONGODB_KEYFILE_VALUE
value: "${MONGODB_KEYFILE_VALUE}"
- name: MONGODB_SERVICE_NAME
value: "${MONGODB_SERVICE_NAME}-internal"
resources:
limits:
memory: "${MEMORY_LIMIT}"
readinessProbe:
exec:
command:
- stat
- /tmp/initialized
volumeClaimTemplates:
- metadata:
name: mongo-data
annotations:
# Uncomment this if using dynamic volume provisioning.
# https://docs.okd.io/latest/install_config/persistent_storage/dynamically_provisioning_pvs.html
# volume.alpha.kubernetes.io/storage-class: anything
spec:
# the volume can be mounted as read-write by a single node
accessModes: [ ReadWriteOnce ]
resources:
requests:
storage: "${VOLUME_CAPACITY}"

View file

@ -0,0 +1,95 @@
# MongoDB Replication Example Using a Docker
This [MongoDB replication](https://docs.mongodb.com/manual/replication/) example
uses a [Docker engine](http://docker.com) to run replica set members.
**This platform is mainly for developing and testing.**
## Getting Started
You will need an Docker engine where you can run containers. If you want to avoid running all replset members on one host, you can also use [Docker Swarm](https://docs.docker.com/swarm/).
## Example Working Scenarios
This section describes how this example is designed to work.
**All practices for [MongoDB replication](https://docs.mongodb.com/manual/replication/) applies also to this example**
### Initial Deployment: 3-member Replica Set
To create replica set with three members you can use this script:
```bash
cat > variables <<EOF
MONGODB_DATABASE=db
MONGODB_USER=user
MONGODB_PASSWORD=password
MONGODB_ADMIN_PASSWORD=adminPassword
MONGODB_REPLICA_NAME=rs0
MONGODB_KEYFILE_VALUE=xxxxxxxxxxxx
MONGODB_SMALLFILES=true
MONGODB_SERVICE_NAME=mongodb"
EOF
source variables
IMAGE_NAME=centos/mongodb-32-centos7
network_name="mongodb-replset"
docker network create ${network_name}
docker run -d --cidfile $CIDFILE_DIR/replset0 --name=replset-0 --hostname=replset-0 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-0 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
docker run -d --cidfile $CIDFILE_DIR/replset1 --name=replset-1 --hostname=replset-1 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-1 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
docker run -d --cidfile $CIDFILE_DIR/replset2 --name=replset-2 --hostname=replset-2 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-2 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
```
`run-mongod-replication` command have to be run in container (same script as for [OpenShift StatefulSet replication example](https://github.com/sclorg/mongodb-container/tree/master/examples/petset).
Parameters for `docker run` command:
- `--name` and `--hostname` have to be set to the same value for each container to proper inter-container addressing
- same `--network-alias` has to be added to all containers to be able to automatically connect containers together (alias has to be equal to `$MONGODB_SERVICE_NAME`). This allows dynamic adding of members to replicaset.
- all environmental variables required for replication have to be set - see help of the image (**TODO** write it somewhere - [deprecated](https://github.com/sclorg/mongodb-container/tree/master/2.4/examples/replica))
To be able to select a container, which initialize the ReplicaSet, `HOSTNAME` of one container has to match this regular expression: `.*-0`. If this container does not use persistent storage (mounted directory into container) it can't be restarted.
And later from one of the containers you can easilly connect to MongoDB:
```console
sh-4.2$ mongo $MONGODB_DATABASE -u $MONGODB_USER -p$MONGODB_PASSWORD --host $MONGODB_REPLICA_NAME/localhost
MongoDB shell version: 3.2.6
connecting to: sampledb
rs0:PRIMARY>
```
Note: You can also use host version of mongo shell, but you have to substitute values of environmental variables and IP address instead of `localhost` by yourself.
During the lifetime of your deployment, one or more of those members might crash or fail. It is possible to configure container to get restarted automatically (see [docker run reference](https://docs.docker.com/engine/reference/run/#restart-policies---restart).
**Note**: for production usage, you should maintain as much separation between
members as possible. It is recommended to run containers on different hosts.
### Adding member
To add a new member into replicaset run:
```bash
docker run -d --cidfile $CIDFILE_DIR/replset3 --name=replset-3 --hostname=replset-3 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-3 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
```
New container is created and it automatically connects to the replica set.
### Removing member
To prevent possible data lost, automatic removing of members from replicaset is not supported.
To do it:
1. stop the container (for example `docker stop replset-2`)
2. connect to replica set and [remove the member](https://docs.mongodb.com/manual/tutorial/remove-replica-set-member/)
### Known Limitations
* Adding or removing new member to replica set takes some time (elections, syncing,...), so after your command finished it may take some time until replica set is ready

View file

@ -0,0 +1,49 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCzwzj+WRRenJu3
QnigU1a4cskHz+seArdMvmZt3H9Bn4WMQJJcYDlrsCPN3BMbOiSJxLa9w3nhDuYV
bhuyMce46t6AojCRR6OuIpRLQrjopmmGxOnkmHXaSrYgYVaFdwiD6Vl1aiivKtQh
jjkZ6iIRKMW9rjtN5Gq9epWbk8MX7vdvKZC7dSxH6/V2Maz5CeHMecpdr/TZVif0
sCouLgu8tovkcMZBsBwbSxBy7sgkMZ3915/AmJ0DD9X7K9AB2EteYBuRbcDBzZqt
DEbS/Ibt01NHaV+LDN6hQsfBRSsx/UT/BmV9yAHSrRC6ZmO1P704E2RU5LYoGG6L
NBjsagvXAgMBAAECggEAavOr5Sh6xg/HVbFms3ziC7u/f7Zk3OYgyLg/mP2neRPA
EUIZ0nwtrbSOmNxnQbvGIXG4iA7VZOHIYtewAsU7XvneiAn3e3h0GhbYxC5edY0N
G/HsduV+uPHaiklfgOjhWBpt7z3KFu4UaCKGNrTmr+7iN4iCIEXWfAWDHZ08Ze/4
Akte+VJzr2yHpCjv6JwyVg56SyOX//27HmUMBolXQwk+U8Z0nmLpfLLvodiLUX6z
DdJ6wUFl9h6uTqXPDgQN0RnTq7q5jMnEHezQzLI/gXSEYl3cS3hiaUF/MkWdlmVi
NCXo3I9RWFacSmhY4zPyJy15MipAmEtl7lfhwNdcgQKBgQDigicyz4BEaX3AhsHV
STDlUwSX3asWwKDzxGtQxsZUk5wwLw311GBzzCei+xSdd/4RyvqjsdTnUJoEgZWQ
veSocWxcB7ArXEu5B+ZE0IoppbDfpj+3+jGdBNJUTBXSf7oXckhO6nQqSMo8gzE4
tGNbnqRBWNGortU5GtonLV1I6wKBgQDLKvcDcfzNzgquQqFl6R+3Vq6M/NQ1yZHV
xszsBrn7XCcPW9q5EyGYWDIohpw1HIk8fZ7YbBJXvE3fCyfAqJEGRDI4Tktc1PDO
KS/JHdgTMv5h1NTJA79T4OeE3u+VcJaiTLcsuWfrcoNqbSvm9kWadj7q5E+lkLbg
mhHGuYcNxQKBgGMvjy6mwooyj4hw+gwFos2ozsgZn0O1UkWsaznR8SM0CB7pW/mb
6CiM64Zo4BwcYTs1YQ+xXYN1PhxfCjl5jdlc4ZBjJXe3UMlTdjgIAZg/H/avfBN6
EaOqatfTAXNNzxVZS+SSsg+aFETvReyIwUFrdI3Q8O4msOPyqDijmsfFAoGBALiH
xKWvcaNoAlF2mWubyz/VjmLl/MUB2OCnWVcDYQcKEzwi/M1esAPUD3K5mMWwroFv
w8fkmOBxWvU8B5Y4RMjBRi/YBSFDE5jc1ddAsRH3Hg+zpLUTH0NLIl2Ps2IzSc4O
Cc2odNfoQa53jAmSI3QA9HKafjDt+PyVU5IAzsbVAoGBAKNwzKu8CtEeWvwTOL0/
VYpIn7KVw3AqQUQoaOWUSPF9Z3zqUQn/YB+C1m1Yp/XEiOKIHtZ6HDDHlhUxTt2a
Q5vGJTL0ArRMF50mUbPvtI4fuZj3YVb7WSGIhTREhJ9h+yASU92SSXWpcSWKX4h9
0tgsUDjt2e2wm4KmTx8kILEQ
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
MIIDgjCCAmqgAwIBAgIJAIDRoOEfLaGfMA0GCSqGSIb3DQEBCwUAMFYxCzAJBgNV
BAYTAlhYMRUwEwYDVQQHDAxEZWZhdWx0IENpdHkxHDAaBgNVBAoME0RlZmF1bHQg
Q29tcGFueSBMdGQxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xODA1MDQxMDEwMjJa
Fw0yMzA1MDMxMDEwMjJaMFYxCzAJBgNVBAYTAlhYMRUwEwYDVQQHDAxEZWZhdWx0
IENpdHkxHDAaBgNVBAoME0RlZmF1bHQgQ29tcGFueSBMdGQxEjAQBgNVBAMMCWxv
Y2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALPDOP5ZFF6c
m7dCeKBTVrhyyQfP6x4Ct0y+Zm3cf0GfhYxAklxgOWuwI83cExs6JInEtr3DeeEO
5hVuG7Ixx7jq3oCiMJFHo64ilEtCuOimaYbE6eSYddpKtiBhVoV3CIPpWXVqKK8q
1CGOORnqIhEoxb2uO03kar16lZuTwxfu928pkLt1LEfr9XYxrPkJ4cx5yl2v9NlW
J/SwKi4uC7y2i+RwxkGwHBtLEHLuyCQxnf3Xn8CYnQMP1fsr0AHYS15gG5FtwMHN
mq0MRtL8hu3TU0dpX4sM3qFCx8FFKzH9RP8GZX3IAdKtELpmY7U/vTgTZFTktigY
bos0GOxqC9cCAwEAAaNTMFEwHQYDVR0OBBYEFGYgFlof4cyclaCNARLK+0lJXlgA
MB8GA1UdIwQYMBaAFGYgFlof4cyclaCNARLK+0lJXlgAMA8GA1UdEwEB/wQFMAMB
Af8wDQYJKoZIhvcNAQELBQADggEBAHseHweQtcA4Trk4EfxJfwkqgczuSQl4yWef
+G6cKtuK1YPYeBTVt/qjmJ4c+uLRRbKuHzI7bhbi0uIlZBH5MSBML7j75cZdy7vQ
d9OTFazAvz5ZkkQxjWodFj/+4oESqiUegfSOkUbJqoPPy2n0BzPdC0nIUdXu6xjF
SuvKMwFffd6Zd3IzK18E26Va65eXV36G1Gcy0+6nbYXQl7Fp+3kBjCiiGecTs/3D
YpVYhG7WYJ3SqGUWnL6iYz0vwGW4Q555ocGEPAXBzckJ6p2FmuFz3ZeOfx6TVBS7
2ZPqfadu4R+CSfC/oM4Z2Eh9C9qytzzivX1TeSF7IB09lsGIzRs=
-----END CERTIFICATE-----

143
test/lib Normal file
View file

@ -0,0 +1,143 @@
#
# Library of shared functions for MongoDB image tests.
#
# Always use sourced from a specific container testfile
test -n "${IMAGE_NAME-}" || false 'make sure $IMAGE_NAME is defined'
test -n "${VERSION-}" || false 'make sure $VERSION is defined'
# get_version_number [version_string]
# --------------------
# Extracts the version number from provided version string.
# e.g. 3.0upg => 3.0
function get_version_number() {
echo $1 | sed -e 's/^\([0-9.]*\).*/\1/'
}
function mongo_cmd() {
docker run --rm ${CONTAINER_ARGS:-} $IMAGE_NAME mongo ${shell_args:-} $CONTAINER_IP/$DB -u "$USER" -p"$PASS" --eval "${@}"
}
function mongo_admin_cmd() {
docker run --rm ${CONTAINER_ARGS:-} $IMAGE_NAME mongo ${shell_args:-} $CONTAINER_IP/admin -u admin -p"$ADMIN_PASS" --eval "${@}"
}
function test_connection() {
local name=$1 ; shift
CONTAINER_IP=$(ct_get_cip $name)
echo " Testing MongoDB connection to $CONTAINER_IP..."
local max_attempts=20
local sleep_time=2
for i in $(seq $max_attempts); do
echo " Trying to connect..."
set +e
mongo_cmd "db.getSiblingDB('test_database');"
status=$?
set -e
if [ $status -eq 0 ]; then
echo " Success!"
return 0
fi
sleep $sleep_time
done
echo " Giving up: Failed to connect. Logs:"
docker logs $(ct_get_cid $name)
return 1
}
function test_config_option() {
local env_var=$1 ; shift
local env_val=$1 ; shift
local config_part=$1 ; shift
local name="configuration_${env_var}"
# If $value is a string, it needs to be in simple quotes ''.
CONTAINER_ARGS="
-e MONGODB_DATABASE=db
-e MONGODB_USER=user
-e MONGODB_PASSWORD=password
-e MONGODB_ADMIN_PASSWORD=adminPassword
-e $env_var=${env_val}
"
ct_create_container $name
# need to set these because `mongo_cmd` relies on global variables
USER=user
PASS=password
DB=db
test_connection ${name}
# If nothing is found, grep returns 1 and test fails.
docker exec $(ct_get_cid $name) bash -c "cat /etc/mongod.conf" | grep -q "${config_part}"
docker stop $(ct_get_cid ${name})
}
function assert_login_access() {
local USER=$1 ; shift
local PASS=$1 ; shift
local success=$1 ; shift
if mongo_cmd 'db.version()' ; then
if $success ; then
echo " $USER($PASS) access granted as expected"
return
fi
else
if ! $success ; then
echo " $USER($PASS) access denied as expected"
return
fi
fi
echo " $USER($PASS) login assertion failed"
exit 1
}
function test_general() {
local name=$1 ; shift
CONTAINER_ARGS="-e MONGODB_USER=$USER -e MONGODB_PASSWORD=$PASS -e MONGODB_DATABASE=$DB"
if [ -v ADMIN_PASS ]; then
CONTAINER_ARGS="$CONTAINER_ARGS -e MONGODB_ADMIN_PASSWORD=$ADMIN_PASS"
fi
ct_create_container $name
CONTAINER_IP=$(ct_get_cip $name)
test_connection $name
echo " Testing scl usage"
ct_scl_usage_old $name 'mongo --version' $(get_version_number $VERSION)
test_mongo $name
}
function _s2i_test_image() {
local name="$1"
local mount_opts="$2"
echo " Testing s2i app image environment variable checking"
ct_assert_container_creation_fails $mount_opts -e MONGODB_USER=user -e MONGODB_PASSWORD=password -e MONGODB_DATABASE=db -e MONGODB_ADMIN_PASSWORD=adminPass
ct_assert_container_creation_fails $mount_opts -e MONGODB_USER=user -e MONGODB_PASSWORD=password -e MONGODB_DATABASE=db -e MONGODB_ADMIN_PASSWORD=adminPass -e MONGODB_BACKUP_USER=backup -e MONGODB_BACKUP_PASSWORD=pass || [ $? -eq 1 ]
echo " Testing s2i app image with correct configuration"
CONTAINER_ARGS="
-e MONGODB_ADMIN_PASSWORD=adminPass
-e MONGODB_USER=user
-e MONGODB_PASSWORD=password
-e MONGODB_DATABASE=db
-e MONGODB_BACKUP_USER=backup
-e MONGODB_BACKUP_PASSWORD=bPass
$mount_opts
"
ct_create_container $name
CONTAINER_IP=$(ct_get_cip ${name})
ADMIN_PASS=adminPass
echo " Testing s2i app image backup user"
DB=admin USER=backup PASS=bPass test_connection "${name}"
echo " Testing s2i app image mongodb-cfg/mongod.conf"
mongo_admin_cmd "if (db.serverCmdLineOpts()['parsed']['replication']['oplogSizeMB'] == 128){quit(0)}; quit(1)"
echo " Testing s2i app image reading initial data"
mongo_admin_cmd "if (db.getSiblingDB('db').constants.count({subject: \"s2i build example\"}) == 1){quit(0)}; quit(1)"
}

431
test/run
View file

@ -9,122 +9,54 @@
set -exo nounset
shopt -s nullglob
IMAGE_NAME=${IMAGE_NAME-centos/mongodb-32-centos7-candidate}
. test/common-lib
. test/lib
CIDFILE_DIR=$(mktemp --suffix=mongodb_test_cidfiles -d)
TEST_LIST="\
ctest_container_creation
ctest_configuration
ctest_general
ctest_change_password
ctest_mount_config
ctest_doc_content
ctest_local_replication
ctest_s2i
ctest_ssl"
VERSION_NUM=$(get_version_number $VERSION | sed -e 's/\.//g')
if test $VERSION_NUM -ge 32; then
TEST_LIST="${TEST_LIST}
ctest_WT_cache"
fi
test $# -eq 1 -a "${1-}" == --list && exit 0
CID_FILE_DIR=$(mktemp --suffix=mongodb_test_cidfiles -d)
TEST_DIR="$(readlink -zf $(dirname "${BASH_SOURCE[0]}"))"
S2I_ARGS="--pull-policy=never "
volumes_to_clean=
function cleanup() {
for cidfile in $CIDFILE_DIR/* ; do
CONTAINER=$(cat $cidfile)
local network_name="mongodb-replset-$$"
ct_cleanup
! docker network ls | grep -q ${network_name} || docker network rm ${network_name}
echo "Stopping and removing container $CONTAINER..."
docker stop $CONTAINER
docker rm $CONTAINER
rm $cidfile
echo "Done."
done
rmdir $CIDFILE_DIR
ct_path_foreach "$volumes_to_clean" cleanup_volume_dir
}
trap cleanup EXIT SIGINT
function get_cid() {
local id="$1" ; shift || return 1
echo -n $(cat "$CIDFILE_DIR/$id")
}
function get_container_ip() {
local id="$1" ; shift
docker inspect --format='{{.NetworkSettings.IPAddress}}' $(get_cid "$id")
}
function mongo_cmd() {
docker run --rm $IMAGE_NAME mongo "$DB" --host $CONTAINER_IP -u "$USER" -p"$PASS" --eval "${@}"
}
function mongo_admin_cmd() {
docker run --rm $IMAGE_NAME mongo admin --host $CONTAINER_IP -u admin -p"$ADMIN_PASS" --eval "${@}"
}
# Make sure the invocation of docker run fails.
function assert_container_creation_fails() {
# Time the docker run command. It should fail. If it doesn't fail,
# container will keep running so we kill it with SIGKILL to make sure
# timeout returns a non-zero value.
set +e
timeout -s 9 --preserve-status 10s docker run --rm "$@" $IMAGE_NAME
ret=$?
set -e
# Timeout will exit with a high number.
if [ $ret -gt 30 ]; then
return 1
fi
}
function run_container_creation_tests() {
echo " Testing wrong user variables usage"
assert_container_creation_fails -e MONGODB_USER=user -e MONGODB_PASSWORD=pass
assert_container_creation_fails -e MONGODB_DATABASE=db -e MONGODB_PASSWORD=pass
assert_container_creation_fails -e MONGODB_DATABASE=db -e MONGODB_USER=user
assert_container_creation_fails -e MONGODB_USER=user -e MONGODB_DATABASE=db -e MONGODB_PASSWORD=pass
assert_container_creation_fails -e MONGODB_ADMIN_PASSWORD=Pass -e MONGODB_USER=user
assert_container_creation_fails -e MONGODB_ADMIN_PASSWORD=Pass -e MONGODB_PASSWORD=pass
assert_container_creation_fails -e MONGODB_ADMIN_PASSWORD=Pass -e MONGODB_DATABASE=db
echo " Success!"
echo " Testing good user variables usage"
assert_container_creation_fails -e MONGODB_ADMIN_PASSWORD=Pass || [ $? -eq 1 ]
assert_container_creation_fails -e MONGODB_ADMIN_PASSWORD=Pass -e MONGODB_USER=user -e MONGODB_DATABASE=db -e MONGODB_PASSWORD=pass || [ $? -eq 1 ]
echo " Success!"
}
function test_connection() {
local name=$1 ; shift
CONTAINER_IP=$(get_container_ip $name)
echo " Testing MongoDB connection to $CONTAINER_IP..."
local max_attempts=20
local sleep_time=2
for i in $(seq $max_attempts); do
echo " Trying to connect..."
set +e
mongo_cmd "db.getSiblingDB('test_database');"
status=$?
set -e
if [ $status -eq 0 ]; then
echo " Success!"
return 0
fi
sleep $sleep_time
done
echo " Giving up: Failed to connect. Logs:"
docker logs $(get_cid $name)
return 1
}
test_scl_usage() {
local name="$1"
local run_cmd="$2"
local expected="$3"
echo " Testing the image SCL enable"
out=$(docker run --rm ${IMAGE_NAME} /bin/bash -c "${run_cmd}")
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[/bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
out=$(docker exec $(get_cid $name) /bin/bash -c "${run_cmd}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
out=$(docker exec $(get_cid $name) /bin/sh -ic "${run_cmd}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/sh -ic "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
cleanup_volume_dir ()
{
test ! -d "$1" && : "WARN: cleaned $1 for some reason" && return 0
# When we run this test script as non-root (we should?), the MongoDB server
# within container is still run under 'mongodb' user. It means that we are
# unable to remove files created by server. That's why we need to let docker
# escalate the privileges again.
local datadir=/var/lib/mongodb/data
docker run -v "$1:$datadir:z" --rm "$IMAGE_NAME" /bin/sh -c "/bin/rm -rf $datadir/*"
rmdir "$1"
}
function test_mongo() {
@ -146,169 +78,18 @@ function test_mongo() {
echo " Success!"
}
function test_config_option() {
local env_var=$1 ; shift
local env_val=$1 ; shift
local config_part=$1 ; shift
local name="configuration_${env_var}"
# If $value is a string, it needs to be in simple quotes ''.
DOCKER_ARGS="
-e MONGODB_DATABASE=db
-e MONGODB_USER=user
-e MONGODB_PASSWORD=password
-e MONGODB_ADMIN_PASSWORD=adminPassword
-e $env_var=${env_val}
"
create_container ${name} ${DOCKER_ARGS}
# need to set these because `mongo_cmd` relies on global variables
USER=user
PASS=password
DB=db
test_connection ${name}
# If nothing is found, grep returns 1 and test fails.
docker exec $(get_cid $name) bash -c "cat /etc/mongod.conf" | grep -q "${config_part}"
docker stop $(get_cid ${name})
}
function run_configuration_tests() {
function ctest_configuration() {
echo " Testing image configuration settings"
test_config_option MONGODB_QUIET true "quiet: true"
if test $VERSION_NUM -le 30; then
test_config_option MONGODB_PREALLOC false "preallocDataFiles: false"
test_config_option MONGODB_NOPREALLOC true "preallocDataFiles: false"
test_config_option MONGODB_SMALLFILES true "smallFiles: true"
fi
echo " Success!"
}
wait_for_cid() {
local max_attempts=10
local sleep_time=1
local attempt=1
local result=1
while [ $attempt -le $max_attempts ]; do
[ -f $cid_file ] && [ -s $cid_file ] && break
echo "Waiting for container start..."
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
}
function create_container() {
local name=$1 ; shift
local cargs=${CONTAINER_ARGS:-}
cid_file="$CIDFILE_DIR/$name"
# create container with a cidfile in a directory for cleanup
docker run --cidfile $cid_file -d $cargs "$@" $IMAGE_NAME
echo "Created container $(cat $cid_file)"
wait_for_cid
}
function assert_login_access() {
local USER=$1 ; shift
local PASS=$1 ; shift
local success=$1 ; shift
if mongo_cmd 'db.version()' ; then
if $success ; then
echo " $USER($PASS) access granted as expected"
return
fi
else
if ! $success ; then
echo " $USER($PASS) access denied as expected"
return
fi
fi
echo " $USER($PASS) login assertion failed"
exit 1
}
function run_tests() {
local name=$1 ; shift
envs="-e MONGODB_USER=$USER -e MONGODB_PASSWORD=$PASS -e MONGODB_DATABASE=$DB"
if [ -v ADMIN_PASS ]; then
envs="$envs -e MONGODB_ADMIN_PASSWORD=$ADMIN_PASS"
fi
create_container $name $envs
CONTAINER_IP=$(get_container_ip $name)
test_connection $name
echo " Testing scl usage"
test_scl_usage $name 'mongo --version' '3.2'
test_mongo $name
}
function run_change_password_test() {
local name="change_password"
local database='db'
local user='user'
local password='password'
local admin_password='adminPassword'
local volume_dir
volume_dir=`mktemp -d --tmpdir mongodb-testdata.XXXXX`
chmod a+rwx ${volume_dir}
DOCKER_ARGS="
-e MONGODB_DATABASE=${database}
-e MONGODB_USER=${user}
-e MONGODB_PASSWORD=${password}
-e MONGODB_ADMIN_PASSWORD=${admin_password}
-v ${volume_dir}:/var/lib/mongodb/data:Z
"
create_container ${name} ${DOCKER_ARGS}
# need to set these because `mongo_cmd` relies on global variables
USER=${user}
PASS=${password}
DB=${database}
# need this to wait for the container to start up
CONTAINER_IP=$(get_container_ip ${name})
test_connection ${name}
echo " Testing login"
assert_login_access ${user} ${password} true
DB='admin' assert_login_access 'admin' ${admin_password} true
echo " Changing passwords"
docker stop $(get_cid ${name})
DOCKER_ARGS="
-e MONGODB_DATABASE=${database}
-e MONGODB_USER=${user}
-e MONGODB_PASSWORD=NEW_${password}
-e MONGODB_ADMIN_PASSWORD=NEW_${admin_password}
-v ${volume_dir}:/var/lib/mongodb/data:Z
"
create_container "${name}_NEW" ${DOCKER_ARGS}
# need to set this because `mongo_cmd` relies on global variables
PASS="NEW_${password}"
# need this to wait for the container to start up
CONTAINER_IP=$(get_container_ip "${name}_NEW")
test_connection "${name}_NEW"
echo " Testing login with new passwords"
assert_login_access ${user} "NEW_${password}" true
assert_login_access ${user} ${password} false
DB='admin' assert_login_access 'admin' "NEW_${admin_password}" true
DB='admin' assert_login_access 'admin' ${admin_password} false
# need to remove volume_dir with sudo because of permissions of files written
# by the Docker container
sudo rm -rf ${volume_dir}
echo " Success!"
}
function run_mount_config_test() {
function ctest_mount_config() {
local name="mount_config"
echo " Testing config file mount"
local database='db'
@ -317,22 +98,31 @@ function run_mount_config_test() {
local admin_password='adminPassword'
local volume_dir
local config_content
volume_dir=`mktemp -d --tmpdir mongodb-testdata.XXXXX`
chmod a+rwx ${volume_dir}
ct_path_append volumes_to_clean "$volume_dir"
config_file=$volume_dir/mongod.conf
echo "dbpath=/var/lib/mongodb/dbpath
unixSocketPrefix = /var/lib/mongodb" > $config_file
chmod a+r ${config_file}
config_content="dbpath=/var/lib/mongodb/dbpath
unixSocketPrefix = /var/lib/mongodb
bind_ip = 0.0.0.0"
if test $VERSION_NUM -le 30; then
config_content="$config_content
smallfiles = true
noprealloc = true"
fi
echo "$config_content" > $config_file
chmod a+wr ${config_file}
DOCKER_ARGS="
CONTAINER_ARGS="
-e MONGODB_DATABASE=${database}
-e MONGODB_USER=${user}
-e MONGODB_PASSWORD=${password}
-e MONGODB_ADMIN_PASSWORD=${admin_password}
-v ${config_file}:/etc/mongod.conf:Z
-v ${volume_dir}:/var/lib/mongodb/dbpath:Z
-v ${config_file}:/etc/mongod.conf:z
-v ${volume_dir}:/var/lib/mongodb/dbpath:z
"
create_container ${name} ${DOCKER_ARGS}
ct_create_container $name
# need to set these because `mongo_cmd` relies on global variables
USER=${user}
@ -340,84 +130,43 @@ unixSocketPrefix = /var/lib/mongodb" > $config_file
DB=${database}
# need this to wait for the container to start up
CONTAINER_IP=$(get_container_ip ${name})
CONTAINER_IP=$(ct_get_cip ${name})
echo " Testing mongod is running"
test_connection ${name}
echo " Testing config file works"
docker exec $(get_cid ${name}) bash -c "test -S /var/lib/mongodb/mongodb-27017.sock"
# need to remove volume_dir with sudo because of permissions of files written
# by the Docker container
sudo rm -rf ${volume_dir}
docker exec $(ct_get_cid ${name}) bash -c "test -S /var/lib/mongodb/mongodb-27017.sock"
echo " Success!"
}
run_doc_test() {
local tmpdir=$(mktemp -d)
local f
echo " Testing documentation in the container image"
# Extract the help files from the container
for f in /usr/share/container-scripts/mongodb/README.md help.1 ; do
docker run --rm ${IMAGE_NAME} /bin/bash -c "cat /${f}" >${tmpdir}/$(basename ${f})
# Check whether the files include some important information
for term in MONGODB_ADMIN_PASSWORD volume ; do
if ! cat ${tmpdir}/$(basename ${f}) | grep -q -e "${term}" ; then
echo "ERROR: File /${f} does not include '${term}'."
return 1
fi
done
done
# Check whether the files use the correct format
if ! file ${tmpdir}/help.1 | grep -q roff ; then
echo "ERROR: /help.1 is not in troff or groff format"
return 1
fi
echo " Success!"
echo
}
function ctest_s2i() {
echo " Testing s2i usage"
ct_s2i_usage ${IMAGE_NAME} ${S2I_ARGS} &>/dev/null
run_WT_cache_test() {
local name="WT_cache_size"
echo " Testing setting of WT cache size"
local database='db'
local user='user'
local password='password'
local admin_password='adminPassword'
# Set configuration file (differs for mmapv1 storage engine)
mv ${TEST_DIR}/examples/extending-image/mongodb-cfg/mongod.conf ${TEST_DIR}/examples/extending-image/mongodb-cfg/mongod.conf.backup
DOCKER_ARGS="
-e MONGODB_DATABASE=${database}
-e MONGODB_USER=${user}
-e MONGODB_PASSWORD=${password}
-e MONGODB_ADMIN_PASSWORD=${admin_password}
"
# need to set these because `mongo_cmd` relies on global variables
USER=${user}
PASS=${password}
DB=${database}
ADMIN_PASS=${admin_password}
if test $VERSION_NUM -ge 32; then
ln -s mongod-WT.conf ${TEST_DIR}/examples/extending-image/mongodb-cfg/mongod.conf
else
ln -s mongod-mmapv1.conf ${TEST_DIR}/examples/extending-image/mongodb-cfg/mongod.conf
fi
# minimum is 1G
create_container "${name}_200M" ${DOCKER_ARGS} -m 200M
test_connection ${name}_200M
mongo_admin_cmd "if (db.serverStatus()['wiredTiger']['cache']['maximum bytes configured'] == Math.pow(2,30)){quit(0)}; quit(1)"
# if greater that 1G, use 60% of (RAM - 1G)
create_container "${name}_6G" ${DOCKER_ARGS} -m 6G
test_connection ${name}_6G
mongo_admin_cmd "if (db.serverStatus()['wiredTiger']['cache']['maximum bytes configured'] == 3*Math.pow(2,30)){quit(0)}; quit(1)"
echo " Testing s2i build"
ct_s2i_build_as_df file://${TEST_DIR}/examples/extending-image/ ${IMAGE_NAME} ${IMAGE_NAME}-testapp
local container_name=s2i_config_build
IMAGE_NAME=${IMAGE_NAME}-testapp _s2i_test_image "s2i_config_build" ""
echo " Testing s2i mount"
test_app_dir=$(mktemp -d)
cp -r ${TEST_DIR}/examples/extending-image/ ${test_app_dir}/
chmod -R a+rX ${test_app_dir}
_s2i_test_image "s2i_test_mount" "-v ${test_app_dir}/extending-image:/opt/app-root/src/:z"
rm -rf ${test_app_dir}
mv ${TEST_DIR}/examples/extending-image/mongodb-cfg/mongod.conf.backup ${TEST_DIR}/examples/extending-image/mongodb-cfg/mongod.conf
echo " Success!"
echo
}
# Tests.
run_container_creation_tests
run_configuration_tests
USER="user1" PASS="pass1" DB="test_db" ADMIN_PASS="r00t" run_tests admin
# Test with random uid in container
CONTAINER_ARGS="-u 12345" USER="user1" PASS="pass1" DB="test_db" ADMIN_PASS="r00t" run_tests admin_altuid
run_change_password_test
run_mount_config_test
run_doc_test
run_WT_cache_test
# Run the chosen tests
TEST_LIST=${@:-$TEST_LIST} ct_run_test_list

View file

@ -6,83 +6,99 @@
# The image has to be available before this script is executed.
#
THISDIR=$(dirname ${BASH_SOURCE[0]})
TEMPLATES="$THISDIR/examples/petset"
source "$THISDIR"/test-lib-openshift.sh
set -exo nounset
function prepare_oc_client() {
# Prepare oc command
if [[ ! -d ../openshift-client/ ]]; then
OC_RELEASE=$(curl -s https://api.github.com/repos/openshift/origin/releases | grep "browser_download_url" | grep "openshift-origin-client" | grep "linux-64bit" | sort | tail -n 1 | sed -e 's|"browser_download_url": ||' | tr -d ' "')
test -n "${IMAGE_NAME-}" || false 'make sure $IMAGE_NAME is defined'
test -n "${VERSION-}" || false 'make sure $VERSION is defined'
test -n "${OS-}" || false 'make sure $OS is defined'
curl -L ${OC_RELEASE} | tar xz
mv ./openshift-origin-client-tools-* ../openshift-client
fi
export PATH=${PATH}:`pwd`/../openshift-client/
}
prepare_oc_client
ct_os_enable_print_logs
function cleanup() {
echo "Stopping and removing OpenShift cluster..."
oc delete project --all
oc cluster down
}
trap cleanup EXIT SIGINT
function check_mongodb_os_service_connection() {
local util_image_name=$1 ; shift
local service_name=$1 ; shift
local user=$1 ; shift
local pass=$1 ; shift
local database=$1 ; shift
local timeout=${1:-60} ; shift || :
local pod_ip=$(ct_os_get_service_ip ${service_name})
function print_logs() {
oc get all
while read pod_info; do
pod=$(echo ${pod_info} | tr -s ' ' | cut -f1 -d' ')
echo "INFO: printing logs for pod ${pod}"
oc logs ${pod}
done < <(oc get pods --no-headers=true)
}
trap print_logs ERR
: " Service ${service_name} check ..."
# Start OpenShift cluster
[ -d openshift.pv ] || mkdir openshift.pv
oc cluster up --host-pv-dir=$(pwd)/openshift.pv
local cmd="mongo ${pod_ip}/${database} -u ${user} -p${pass} --eval 'quit()'"
SECONDS=0
# Prepare image - add tag different than :latest
# With :latest default imagePullPolicy is Always
# https://docs.openshift.com/enterprise/3.2/dev_guide/managing_images.html#image-pull-policy
docker tag ${IMAGE_NAME} ${IMAGE_NAME}:test-openshift
IMAGE_NAME=${IMAGE_NAME}:test-openshift
#
# General functions
#
# wait_for_ready_pods wait till number of pods with label get ready
function wait_for_ready_pods() {
local label="$1"
local count="$2"
for i in $(seq 30); do
if [[ "$(oc get pods --no-headers=true -l${label} | grep "1/1" | wc -l)" -eq ${count} ]]; then
return 0
fi
echo "Waiting for ${count} ready pods labeled with '${label}'..."
sleep 20
echo -n "Waiting for ${service_name} service becoming ready ..."
while ! docker run --rm ${util_image_name} bash -c "${cmd}" ; do
echo -n "."
[ ${SECONDS} -gt ${timeout} ] && echo " FAIL" && return 1
sleep 3
done
return 1
echo " PASS"
return 0
}
# Deploy MongoDB clustered application
USER=user
PASS=pass
ADMIN_PASS=adminPass
DB=db
function test_mongodb_pure_image() {
local image_name=$1
local image_name_no_namespace=${image_name##*/}
local service_name=${image_name_no_namespace}
oc new-project petset-example
oc new-app --file=../examples/petset/mongodb-petset-persistent.yaml -p MONGODB_USER=${USER} -p MONGODB_PASSWORD=${PASS} -p MONGODB_DATABASE=${DB} -p MONGODB_ADMIN_PASSWORD=${ADMIN_PASS} -p MONGODB_IMAGE=${IMAGE_NAME} -p VOLUME_CAPACITY=500M
ct_os_new_project
ct_os_upload_image "${image_name}"
# Create a specific imagestream tag for the image so that oc cannot use anything else
ct_os_upload_image "${image_name}" "$image_name_no_namespace:testing"
wait_for_ready_pods "app=mongodb-petset-replication" 3
wait_for_ready_pods "openshift.io/deployer-pod-for.name" 0
ct_os_deploy_pure_image "$image_name_no_namespace:testing" \
--name "${service_name}" \
--env MONGODB_ADMIN_PASSWORD=test
host="rs0/$(oc get endpoints mongodb --no-headers | tr -s ' ' | cut -f2 -d' ')"
ct_os_wait_pod_ready "${service_name}" 60
check_mongodb_os_service_connection "${image_name}" "${service_name}" admin test admin 120
docker exec origin kubectl run --attach --restart=Never mongodb-test --image ${IMAGE_NAME} --env="MONGODB_ADMIN_PASSWORD=${ADMIN_PASS}" --command -- bash -c "set -x
ct_os_delete_project
}
function test_mongodb_replication() {
local image_name=${1:-centos/mongodb-${VERSION//\.}-centos7}
local image_name_no_namespace=${image_name##*/}
local service_name=${image_name_no_namespace}
ADMIN_PASS=adminPass
ct_os_new_project
docker tag "${image_name}" "${image_name}:$VERSION"
ct_os_deploy_template_image "$TEMPLATES/mongodb-petset-persistent.yaml" \
MONGODB_IMAGE="${image_name}:$VERSION" \
MONGODB_SERVICE_NAME="${service_name}" \
MONGODB_USER=testu \
MONGODB_PASSWORD=testp \
MONGODB_DATABASE=testdb \
MONGODB_ADMIN_PASSWORD=${ADMIN_PASS} \
VOLUME_CAPACITY=500M
ct_os_wait_rc_ready "${service_name}" 600
for pod in $(ct_os_get_pod_name mongodb); do
ct_os_wait_pod_ready $pod 300
done
check_mongodb_os_service_connection "${image_name}:$VERSION" "${service_name}" admin ${ADMIN_PASS} admin
check_mongodb_os_service_connection "${image_name}:$VERSION" "${service_name}" testu testp testdb
ct_os_run_in_pod $(ct_os_get_pod_name mongodb | head -n 1) bash -c 'set -x
. /usr/share/container-scripts/mongodb/common.sh
. /usr/share/container-scripts/mongodb/test-functions.sh
wait_for_mongo_up '${host}'
wait_replicaset_members '${host}' 3
insert_and_wait_for_replication '${host}' '{a:5, b:10}'"
insert_and_wait_for_replication "$(replset_addr admin)" "{a:5, b:10}"'
ct_os_delete_project
}
ct_os_cluster_up
test_mongodb_pure_image "${IMAGE_NAME}"
test_mongodb_replication "${IMAGE_NAME}"
ct_os_cluster_down

925
test/test-lib-openshift.sh Normal file
View file

@ -0,0 +1,925 @@
# Set of functions for testing docker images in OpenShift using 'oc' command
# ct_os_get_status
# --------------------
# Returns status of all objects to make debugging easier.
function ct_os_get_status() {
oc get all
oc status
}
# ct_os_print_logs
# --------------------
# Returns status of all objects and logs from all pods.
function ct_os_print_logs() {
ct_os_get_status
while read pod_name; do
echo "INFO: printing logs for pod ${pod_name}"
oc logs ${pod_name}
done < <(oc get pods --no-headers=true -o custom-columns=NAME:.metadata.name)
}
# ct_os_enable_print_logs
# --------------------
# Enables automatic printing of pod logs on ERR.
function ct_os_enable_print_logs() {
set -E
trap ct_os_print_logs ERR
}
# ct_get_public_ip
# --------------------
# Returns best guess for the IP that the node is accessible from other computers.
# This is a bit funny heuristic, simply goes through all IPv4 addresses that
# hostname -I returns and de-prioritizes IP addresses commonly used for local
# addressing. The rest of addresses are taken as public with higher probability.
function ct_get_public_ip() {
local hostnames=$(hostname -I)
local public_ip=''
local found_ip
for guess_exp in '127\.0\.0\.1' '192\.168\.[0-9\.]*' '172\.[0-9\.]*' \
'10\.[0-9\.]*' '[0-9\.]*' ; do
found_ip=$(echo "${hostnames}" | grep -oe "${guess_exp}")
if [ -n "${found_ip}" ] ; then
hostnames=$(echo "${hostnames}" | sed -e "s/${found_ip}//")
public_ip="${found_ip}"
fi
done
if [ -z "${public_ip}" ] ; then
echo "ERROR: public IP could not be guessed." >&2
return 1
fi
echo "${public_ip}"
}
# ct_os_run_in_pod POD_NAME CMD
# --------------------
# Runs [cmd] in the pod specified by prefix [pod_prefix].
# Arguments: pod_name - full name of the pod
# Arguments: cmd - command to be run in the pod
function ct_os_run_in_pod() {
local pod_name="$1" ; shift
oc exec "$pod_name" -- "$@"
}
# ct_os_get_service_ip SERVICE_NAME
# --------------------
# Returns IP of the service specified by [service_name].
# Arguments: service_name - name of the service
function ct_os_get_service_ip() {
local service_name="${1}" ; shift
oc get "svc/${service_name}" -o yaml | grep clusterIP | \
cut -d':' -f2 | grep -oe '172\.30\.[0-9\.]*'
}
# ct_os_get_all_pods_status
# --------------------
# Returns status of all pods.
function ct_os_get_all_pods_status() {
oc get pods -o custom-columns=Ready:status.containerStatuses[0].ready,NAME:.metadata.name
}
# ct_os_get_all_pods_name
# --------------------
# Returns the full name of all pods.
function ct_os_get_all_pods_name() {
oc get pods --no-headers -o custom-columns=NAME:.metadata.name
}
# ct_os_get_pod_status POD_PREFIX
# --------------------
# Returns status of the pod specified by prefix [pod_prefix].
# Note: Ignores -build and -deploy pods
# Arguments: pod_prefix - prefix or whole ID of the pod
function ct_os_get_pod_status() {
local pod_prefix="${1}" ; shift
ct_os_get_all_pods_status | grep -e "${pod_prefix}" | grep -Ev "(build|deploy)$" \
| awk '{print $1}' | head -n 1
}
# ct_os_get_pod_name POD_PREFIX
# --------------------
# Returns the full name of pods specified by prefix [pod_prefix].
# Note: Ignores -build and -deploy pods
# Arguments: pod_prefix - prefix or whole ID of the pod
function ct_os_get_pod_name() {
local pod_prefix="${1}" ; shift
ct_os_get_all_pods_name | grep -e "^${pod_prefix}" | grep -Ev "(build|deploy)$"
}
# ct_os_get_pod_ip POD_NAME
# --------------------
# Returns the ip of the pod specified by [pod_name].
# Arguments: pod_name - full name of the pod
function ct_os_get_pod_ip() {
local pod_name="${1}"
oc get pod "$pod_name" --no-headers -o custom-columns=IP:status.podIP
}
# ct_os_check_pod_readiness POD_PREFIX STATUS
# --------------------
# Checks whether the pod is ready.
# Arguments: pod_prefix - prefix or whole ID of the pod
# Arguments: status - expected status (true, false)
function ct_os_check_pod_readiness() {
local pod_prefix="${1}" ; shift
local status="${1}" ; shift
test "$(ct_os_get_pod_status ${pod_prefix})" == "${status}"
}
# ct_os_wait_pod_ready POD_PREFIX TIMEOUT
# --------------------
# Wait maximum [timeout] for the pod becomming ready.
# Arguments: pod_prefix - prefix or whole ID of the pod
# Arguments: timeout - how many seconds to wait seconds
function ct_os_wait_pod_ready() {
local pod_prefix="${1}" ; shift
local timeout="${1}" ; shift
SECONDS=0
echo -n "Waiting for ${pod_prefix} pod becoming ready ..."
while ! ct_os_check_pod_readiness "${pod_prefix}" "true" ; do
echo -n "."
[ ${SECONDS} -gt ${timeout} ] && echo " FAIL" && return 1
sleep 3
done
echo " DONE"
}
# ct_os_wait_rc_ready POD_PREFIX TIMEOUT
# --------------------
# Wait maximum [timeout] for the rc having desired number of replicas ready.
# Arguments: pod_prefix - prefix of the replication controller
# Arguments: timeout - how many seconds to wait seconds
function ct_os_wait_rc_ready() {
local pod_prefix="${1}" ; shift
local timeout="${1}" ; shift
SECONDS=0
echo -n "Waiting for ${pod_prefix} pod becoming ready ..."
while ! test "$((oc get --no-headers statefulsets; oc get --no-headers rc) 2>/dev/null \
| grep "^${pod_prefix}" | awk '$2==$3 {print "ready"}')" == "ready" ; do
echo -n "."
[ ${SECONDS} -gt ${timeout} ] && echo " FAIL" && return 1
sleep 3
done
echo " DONE"
}
# ct_os_deploy_pure_image IMAGE [ENV_PARAMS, ...]
# --------------------
# Runs [image] in the openshift and optionally specifies env_params
# as environment variables to the image.
# Arguments: image - prefix or whole ID of the pod to run the cmd in
# Arguments: env_params - environment variables parameters for the images.
function ct_os_deploy_pure_image() {
local image="${1}" ; shift
# ignore error exit code, because oc new-app returns error when image exists
oc new-app ${image} "$@" || :
# let openshift cluster to sync to avoid some race condition errors
sleep 3
}
# ct_os_deploy_s2i_image IMAGE APP [ENV_PARAMS, ... ]
# --------------------
# Runs [image] and [app] in the openshift and optionally specifies env_params
# as environment variables to the image.
# Arguments: image - prefix or whole ID of the pod to run the cmd in
# Arguments: app - url or local path to git repo with the application sources.
# Arguments: env_params - environment variables parameters for the images.
function ct_os_deploy_s2i_image() {
local image="${1}" ; shift
local app="${1}" ; shift
# ignore error exit code, because oc new-app returns error when image exists
oc new-app "${image}~${app}" "$@" || :
# let openshift cluster to sync to avoid some race condition errors
sleep 3
}
# ct_os_deploy_template_image TEMPLATE [ENV_PARAMS, ...]
# --------------------
# Runs template in the openshift and optionally gives env_params to use
# specific values in the template.
# Arguments: template - prefix or whole ID of the pod to run the cmd in
# Arguments: env_params - environment variables parameters for the template.
# Example usage: ct_os_deploy_template_image mariadb-ephemeral-template.yaml \
# DATABASE_SERVICE_NAME=mysql-57-centos7 \
# DATABASE_IMAGE=mysql-57-centos7 \
# MYSQL_USER=testu \
# MYSQL_PASSWORD=testp \
# MYSQL_DATABASE=testdb
function ct_os_deploy_template_image() {
local template="${1}" ; shift
oc process -f "${template}" "$@" | oc create -f -
# let openshift cluster to sync to avoid some race condition errors
sleep 3
}
# _ct_os_get_uniq_project_name
# --------------------
# Returns a uniq name of the OpenShift project.
function _ct_os_get_uniq_project_name() {
local r
while true ; do
r=${RANDOM}
mkdir /var/tmp/sclorg-test-${r} &>/dev/null && echo sclorg-test-${r} && break
done
}
# ct_os_new_project [PROJECT]
# --------------------
# Creates a new project in the openshfit using 'os' command.
# Arguments: project - project name, uses a new random name if omitted
# Expects 'os' command that is properly logged in to the OpenShift cluster.
# Not using mktemp, because we cannot use uppercase characters.
function ct_os_new_project() {
if [ "${CT_SKIP_NEW_PROJECT:-false}" == 'true' ] ; then
echo "Creating project skipped."
return
fi
local project_name="${1:-$(_ct_os_get_uniq_project_name)}" ; shift || :
oc new-project ${project_name}
# let openshift cluster to sync to avoid some race condition errors
sleep 3
}
# ct_os_delete_project [PROJECT]
# --------------------
# Deletes the specified project in the openshfit
# Arguments: project - project name, uses the current project if omitted
function ct_os_delete_project() {
if [ "${CT_SKIP_NEW_PROJECT:-false}" == 'true' ] ; then
echo "Deleting project skipped, cleaning objects only."
ct_delete_all_objects
return
fi
local project_name="${1:-$(oc project -q)}" ; shift || :
oc delete project "${project_name}"
}
# ct_delete_all_objects
# -----------------
# Deletes all objects within the project.
# Handy when we have one project and want to run more tests.
function ct_delete_all_objects() {
for x in bc builds dc is isimage istag po pv pvc rc routes secrets svc ; do
oc delete $x --all
done
# for some objects it takes longer to be really deleted, so a dummy sleep
# to avoid some races when other test can see not-yet-deleted objects and can fail
sleep 10
}
# ct_os_docker_login
# --------------------
# Logs in into docker daemon
# Uses global REGISRTY_ADDRESS environment variable for arbitrary registry address.
# Does not do anything if REGISTRY_ADDRESS is set.
function ct_os_docker_login() {
[ -n "${REGISTRY_ADDRESS:-}" ] && "REGISTRY_ADDRESS set, not trying to docker login." && return 0
# docker login fails with "404 page not found" error sometimes, just try it more times
for i in `seq 12` ; do
docker login -u developer -p $(oc whoami -t) ${REGISRTY_ADDRESS:-172.30.1.1:5000} && return 0 || :
sleep 5
done
return 1
}
# ct_os_upload_image IMAGE [IMAGESTREAM]
# --------------------
# Uploads image from local registry to the OpenShift internal registry.
# Arguments: image - image name to upload
# Arguments: imagestream - name and tag to use for the internal registry.
# In the format of name:tag ($image_name:latest by default)
# Uses global REGISRTY_ADDRESS environment variable for arbitrary registry address.
function ct_os_upload_image() {
local input_name="${1}" ; shift
local image_name=${input_name##*/}
local imagestream=${1:-$image_name:latest}
local output_name="${REGISRTY_ADDRESS:-172.30.1.1:5000}/$(oc project -q)/$imagestream"
ct_os_docker_login
docker tag ${input_name} ${output_name}
docker push ${output_name}
}
# ct_os_install_in_centos
# --------------------
# Installs os cluster in CentOS
function ct_os_install_in_centos() {
yum install -y centos-release-openshift-origin
yum install -y wget git net-tools bind-utils iptables-services bridge-utils\
bash-completion origin-clients docker origin-clients
}
# ct_os_cluster_up [DIR, IS_PUBLIC, CLUSTER_VERSION]
# --------------------
# Runs the local OpenShift cluster using 'oc cluster up' and logs in as developer.
# Arguments: dir - directory to keep configuration data in, random if omitted
# Arguments: is_public - sets either private or public hostname for web-UI,
# use "true" for allow remote access to the web-UI,
# "false" is default
# Arguments: cluster_version - version of the OpenShift cluster to use, empty
# means default version of `oc`; example value: 3.7;
# also can be specified outside by OC_CLUSTER_VERSION
function ct_os_cluster_up() {
ct_os_cluster_running && echo "Cluster already running. Nothing is done." && return 0
ct_os_logged_in && echo "Already logged in to a cluster. Nothing is done." && return 0
mkdir -p /var/tmp/openshift
local dir="${1:-$(mktemp -d /var/tmp/openshift/os-data-XXXXXX)}" ; shift || :
local is_public="${1:-'false'}" ; shift || :
local default_cluster_version=${OC_CLUSTER_VERSION:-}
local cluster_version=${1:-${default_cluster_version}} ; shift || :
if ! grep -qe '--insecure-registry.*172\.30\.0\.0' /etc/sysconfig/docker ; then
sed -i "s|OPTIONS='|OPTIONS='--insecure-registry 172.30.0.0/16 |" /etc/sysconfig/docker
fi
systemctl stop firewalld || :
setenforce 0
iptables -F
systemctl restart docker
local cluster_ip="127.0.0.1"
[ "${is_public}" == "true" ] && cluster_ip=$(ct_get_public_ip)
if [ -n "${cluster_version}" ] ; then
# if $cluster_version is not set, we simply use oc that is available
ct_os_set_path_oc "${cluster_version}"
fi
mkdir -p ${dir}/{config,data,pv}
case $(oc version| head -n 1) in
"oc v3.1"?.*)
oc cluster up --base-dir="${dir}/data" --public-hostname="${cluster_ip}"
;;
"oc v3."*)
oc cluster up --host-data-dir="${dir}/data" --host-config-dir="${dir}/config" \
--host-pv-dir="${dir}/pv" --use-existing-config --public-hostname="${cluster_ip}"
;;
*)
echo "ERROR: Unexpected oc version." >&2
return 1
;;
esac
oc version
oc login -u system:admin
oc project default
ct_os_wait_rc_ready docker-registry 180
ct_os_wait_rc_ready router 30
oc login -u developer -p developer
# let openshift cluster to sync to avoid some race condition errors
sleep 3
}
# ct_os_cluster_down
# --------------------
# Shuts down the local OpenShift cluster using 'oc cluster down'
function ct_os_cluster_down() {
oc cluster down
}
# ct_os_cluster_running
# --------------------
# Returns 0 if oc cluster is running
function ct_os_cluster_running() {
oc cluster status &>/dev/null
}
# ct_os_logged_in
# ---------------
# Returns 0 if logged in to a cluster (remote or local)
function ct_os_logged_in() {
oc whoami >/dev/null
}
# ct_os_set_path_oc OC_VERSION
# --------------------
# This is a trick that helps using correct version of the `oc`:
# The input is version of the openshift in format v3.6.0 etc.
# If the currently available version of oc is not of this version,
# it first takes a look into /usr/local/oc-<ver>/bin directory,
# and if not found there it downloads the community release from github.
# In the end the PATH variable is changed, so the other tests can still use just 'oc'.
# Arguments: oc_version - X.Y part of the version of OSE (e.g. 3.9)
function ct_os_set_path_oc() {
local oc_version=$(ct_os_get_latest_ver $1)
local oc_path
if oc version | grep -q "oc ${oc_version%.*}." ; then
echo "Binary oc found already available in version ${oc_version}: `which oc` Doing noting."
return 0
fi
# first check whether we already have oc available in /usr/local
local installed_oc_path="/usr/local/oc-${oc_version%.*}/bin"
if [ -x "${installed_oc_path}/oc" ] ; then
oc_path="${installed_oc_path}"
echo "Binary oc found in ${installed_oc_path}" >&2
else
# oc not available in /usr/local, try to download it from github (community release)
oc_path="/tmp/oc-${oc_version}-bin"
ct_os_download_upstream_oc "${oc_version}" "${oc_path}"
fi
if [ -z "${oc_path}/oc" ] ; then
echo "ERROR: oc not found installed, nor downloaded" >&1
return 1
fi
export PATH="${oc_path}:${PATH}"
if ! oc version | grep -q "oc ${oc_version%.*}." ; then
echo "ERROR: something went wrong, oc located at ${oc_path}, but oc of version ${oc_version} not found in PATH ($PATH)" >&1
return 1
else
echo "PATH set correctly, binary oc found in version ${oc_version}: `which oc`"
fi
}
# ct_os_get_latest_ver VERSION_PART_X
# --------------------
# Returns full version (vX.Y.Z) from part of the version (X.Y)
# Arguments: vxy - X.Y part of the version
# Returns vX.Y.Z variant of the version
function ct_os_get_latest_ver(){
local vxy="v$1"
for vz in {3..0} ; do
curl -sif "https://github.com/openshift/origin/releases/tag/${vxy}.${vz}" >/dev/null && echo "${vxy}.${vz}" && return 0
done
echo "ERROR: version ${vxy} not found in https://github.com/openshift/origin/tags" >&2
return 1
}
# ct_os_download_upstream_oc OC_VERSION OUTPUT_DIR
# --------------------
# Downloads a particular version of openshift-origin-client-tools from
# github into specified output directory
# Arguments: oc_version - version of OSE (e.g. v3.7.2)
# Arguments: output_dir - output directory
function ct_os_download_upstream_oc() {
local oc_version=$1
local output_dir=$2
# check whether we already have the binary in place
[ -x "${output_dir}/oc" ] && return 0
mkdir -p "${output_dir}"
# using html output instead of https://api.github.com/repos/openshift/origin/releases/tags/${oc_version},
# because API is limited for number of queries if not authenticated
tarball=$(curl -si "https://github.com/openshift/origin/releases/tag/${oc_version}" | grep -o -e "openshift-origin-client-tools-${oc_version}-[a-f0-9]*-linux-64bit.tar.gz" | head -n 1)
# download, unpack the binaries and then put them into output directory
echo "Downloading https://github.com/openshift/origin/releases/download/${oc_version}/${tarball} into ${output_dir}/" >&2
curl -sL https://github.com/openshift/origin/releases/download/${oc_version}/"${tarball}" | tar -C "${output_dir}" -xz
mv -f "${output_dir}"/"${tarball%.tar.gz}"/* "${output_dir}/"
rmdir "${output_dir}"/"${tarball%.tar.gz}"
}
# ct_os_test_s2i_app_func IMAGE APP CONTEXT_DIR CHECK_CMD [OC_ARGS]
# --------------------
# Runs [image] and [app] in the openshift and optionally specifies env_params
# as environment variables to the image. Then check the container by arbitrary
# function given as argument (such an argument may include <IP> string,
# that will be replaced with actual IP).
# Arguments: image - prefix or whole ID of the pod to run the cmd in (compulsory)
# Arguments: app - url or local path to git repo with the application sources (compulsory)
# Arguments: context_dir - sub-directory inside the repository with the application sources (compulsory)
# Arguments: check_command - CMD line that checks whether the container works (compulsory; '<IP>' will be replaced with actual IP)
# Arguments: oc_args - all other arguments are used as additional parameters for the `oc new-app`
# command, typically environment variables (optional)
function ct_os_test_s2i_app_func() {
local image_name=${1}
local app=${2}
local context_dir=${3}
local check_command=${4}
local oc_args=${5:-}
local import_image=${6:-}
local image_name_no_namespace=${image_name##*/}
local service_name="${image_name_no_namespace}-testing"
local image_tagged="${image_name_no_namespace}:${VERSION}"
if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
echo "ERROR: ct_os_test_s2i_app_func() requires at least 4 arguments that cannot be emtpy." >&2
return 1
fi
ct_os_new_project
# Create a specific imagestream tag for the image so that oc cannot use anything else
if [ "${CT_SKIP_UPLOAD_IMAGE:-false}" == 'true' ] ; then
if [ -n "${import_image}" ] ; then
echo "Importing image ${import_image} as ${image_name}:${VERSION}"
oc import-image ${image_name}:${VERSION} --from ${import_image} --confirm
else
echo "Uploading and importing image skipped."
fi
else
if [ -n "${import_image}" ] ; then
echo "Warning: Import image ${import_image} requested, but uploading image ${image_name} instead."
fi
ct_os_upload_image "${image_name}" "${image_tagged}"
fi
local app_param="${app}"
if [ -d "${app}" ] ; then
# for local directory, we need to copy the content, otherwise too smart os command
# pulls the git remote repository instead
app_param=$(ct_obtain_input "${app}")
fi
ct_os_deploy_s2i_image "${image_tagged}" "${app_param}" \
--context-dir="${context_dir}" \
--name "${service_name}" \
${oc_args}
if [ -d "${app}" ] ; then
# in order to avoid weird race seen sometimes, let's wait shortly
# before starting the build explicitly
sleep 5
oc start-build "${service_name}" --from-dir="${app_param}"
fi
ct_os_wait_pod_ready "${service_name}" 300
local ip=$(ct_os_get_service_ip "${service_name}")
local check_command_exp=$(echo "$check_command" | sed -e "s/<IP>/$ip/g")
echo " Checking APP using $check_command_exp ..."
local result=0
eval "$check_command_exp" || result=1
if [ $result -eq 0 ] ; then
echo " Check passed."
else
echo " Check failed."
fi
ct_os_delete_project
return $result
}
# ct_os_test_s2i_app IMAGE APP CONTEXT_DIR EXPECTED_OUTPUT [PORT, PROTOCOL, RESPONSE_CODE, OC_ARGS, ... ]
# --------------------
# Runs [image] and [app] in the openshift and optionally specifies env_params
# as environment variables to the image. Then check the http response.
# Arguments: image - prefix or whole ID of the pod to run the cmd in (compulsory)
# Arguments: app - url or local path to git repo with the application sources (compulsory)
# Arguments: context_dir - sub-directory inside the repository with the application sources (compulsory)
# Arguments: expected_output - PCRE regular expression that must match the response body (compulsory)
# Arguments: port - which port to use (optional; default: 8080)
# Arguments: protocol - which protocol to use (optional; default: http)
# Arguments: response_code - what http response code to expect (optional; default: 200)
# Arguments: oc_args - all other arguments are used as additional parameters for the `oc new-app`
# command, typically environment variables (optional)
function ct_os_test_s2i_app() {
local image_name=${1}
local app=${2}
local context_dir=${3}
local expected_output=${4}
local port=${5:-8080}
local protocol=${6:-http}
local response_code=${7:-200}
local oc_args=${8:-}
local import_image=${9:-}
if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
echo "ERROR: ct_os_test_s2i_app() requires at least 4 arguments that cannot be emtpy." >&2
return 1
fi
ct_os_test_s2i_app_func "${image_name}" \
"${app}" \
"${context_dir}" \
"ct_os_test_response_internal '${protocol}://<IP>:${port}' '${response_code}' '${expected_output}'" \
"${oc_args}" "${import_image}"
}
# ct_os_test_template_app_func IMAGE APP IMAGE_IN_TEMPLATE CHECK_CMD [OC_ARGS]
# --------------------
# Runs [image] and [app] in the openshift and optionally specifies env_params
# as environment variables to the image. Then check the container by arbitrary
# function given as argument (such an argument may include <IP> string,
# that will be replaced with actual IP).
# Arguments: image_name - prefix or whole ID of the pod to run the cmd in (compulsory)
# Arguments: template - url or local path to a template to use (compulsory)
# Arguments: name_in_template - image name used in the template
# Arguments: check_command - CMD line that checks whether the container works (compulsory; '<IP>' will be replaced with actual IP)
# Arguments: oc_args - all other arguments are used as additional parameters for the `oc new-app`
# command, typically environment variables (optional)
# Arguments: other_images - some templates need other image to be pushed into the OpenShift registry,
# specify them in this parameter as "<image>|<tag>", where "<image>" is a full image name
# (including registry if needed) and "<tag>" is a tag under which the image should be available
# in the OpenShift registry.
function ct_os_test_template_app_func() {
local image_name=${1}
local template=${2}
local name_in_template=${3}
local check_command=${4}
local oc_args=${5:-}
local other_images=${6:-}
local import_image=${7:-}
if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
echo "ERROR: ct_os_test_template_app_func() requires at least 4 arguments that cannot be emtpy." >&2
return 1
fi
local service_name="${name_in_template}-testing"
local image_tagged="${name_in_template}:${VERSION}"
ct_os_new_project
# Create a specific imagestream tag for the image so that oc cannot use anything else
if [ "${CT_SKIP_UPLOAD_IMAGE:-false}" == 'true' ] ; then
if [ -n "${import_image}" ] ; then
echo "Importing image ${import_image} as ${image_name}:${VERSION}"
oc import-image ${image_name}:${VERSION} --from ${import_image} --confirm
else
echo "Uploading and importing image skipped."
fi
else
if [ -n "${import_image}" ] ; then
echo "Warning: Import image ${import_image} requested, but uploading image ${image_name} instead."
fi
ct_os_upload_image "${image_name}" "${image_tagged}"
# upload also other images, that template might need (list of pairs in the format <image>|<tag>
local images_tags_a
local i_t
for i_t in ${other_images} ; do
echo "${i_t}"
IFS='|' read -ra image_tag_a <<< "${i_t}"
docker pull "${image_tag_a[0]}"
ct_os_upload_image "${image_tag_a[0]}" "${image_tag_a[1]}"
done
fi
local local_template=$(ct_obtain_input "${template}")
local namespace=${CT_NAMESPACE:-$(oc project -q)}
oc new-app ${local_template} \
--name "${name_in_template}" \
-p NAMESPACE="${namespace}" \
${oc_args}
ct_os_wait_pod_ready "${service_name}" 300
local ip=$(ct_os_get_service_ip "${service_name}")
local check_command_exp=$(echo "$check_command" | sed -e "s/<IP>/$ip/g")
echo " Checking APP using $check_command_exp ..."
local result=0
eval "$check_command_exp" || result=1
if [ $result -eq 0 ] ; then
echo " Check passed."
else
echo " Check failed."
fi
ct_os_delete_project
return $result
}
# params:
# ct_os_test_template_app IMAGE APP IMAGE_IN_TEMPLATE EXPECTED_OUTPUT [PORT, PROTOCOL, RESPONSE_CODE, OC_ARGS, ... ]
# --------------------
# Runs [image] and [app] in the openshift and optionally specifies env_params
# as environment variables to the image. Then check the http response.
# Arguments: image_name - prefix or whole ID of the pod to run the cmd in (compulsory)
# Arguments: template - url or local path to a template to use (compulsory)
# Arguments: name_in_template - image name used in the template
# Arguments: expected_output - PCRE regular expression that must match the response body (compulsory)
# Arguments: port - which port to use (optional; default: 8080)
# Arguments: protocol - which protocol to use (optional; default: http)
# Arguments: response_code - what http response code to expect (optional; default: 200)
# Arguments: oc_args - all other arguments are used as additional parameters for the `oc new-app`
# command, typically environment variables (optional)
# Arguments: other_images - some templates need other image to be pushed into the OpenShift registry,
# specify them in this parameter as "<image>|<tag>", where "<image>" is a full image name
# (including registry if needed) and "<tag>" is a tag under which the image should be available
# in the OpenShift registry.
function ct_os_test_template_app() {
local image_name=${1}
local template=${2}
local name_in_template=${3}
local expected_output=${4}
local port=${5:-8080}
local protocol=${6:-http}
local response_code=${7:-200}
local oc_args=${8:-}
local other_images=${9:-}
local import_image=${10:-}
if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
echo "ERROR: ct_os_test_template_app() requires at least 4 arguments that cannot be emtpy." >&2
return 1
fi
ct_os_test_template_app_func "${image_name}" \
"${template}" \
"${name_in_template}" \
"ct_os_test_response_internal '${protocol}://<IP>:${port}' '${response_code}' '${expected_output}'" \
"${oc_args}" \
"${other_images}" \
"${import_image}"
}
# ct_os_test_image_update IMAGE_NAME OLD_IMAGE ISTAG CHECK_FUNCTION OC_ARGS
# --------------------
# Runs an image update test with [image] uploaded to [is] imagestream
# and checks the services using an arbitrary function provided in [check_function].
# Arguments: image_name - prefix or whole ID of the pod to run the cmd in (compulsory)
# Arguments: old_image - valid name of the image from the registry
# Arguments: istag - imagestream to upload the images into (compulsory)
# Arguments: check_function - command to be run to check functionality of created services (compulsory)
# Arguments: oc_args - arguments to use during oc new-app (compulsory)
ct_os_test_image_update() {
local image_name=$1; shift
local old_image=$1; shift
local istag=$1; shift
local check_function=$1; shift
local service_name=${image_name##*/}
local ip="" check_command_exp=""
echo "Running image update test for: $image_name"
ct_os_new_project
# Get current image from repository and create an imagestream
docker pull "$old_image:latest" 2>/dev/null
ct_os_upload_image "$old_image" "$istag"
# Setup example application with curent image
oc new-app "$@" --name "$service_name"
ct_os_wait_pod_ready "$service_name" 60
# Check application output
ip=$(ct_os_get_service_ip "$service_name")
check_command_exp=${check_function//<IP>/$ip}
ct_assert_cmd_success "$check_command_exp"
# Tag built image into the imagestream and wait for rebuild
ct_os_upload_image "$image_name" "$istag"
ct_os_wait_pod_ready "${service_name}-2" 60
# Check application output
ip=$(ct_os_get_service_ip "$service_name")
check_command_exp=${check_function//<IP>/$ip}
ct_assert_cmd_success "$check_command_exp"
ct_os_delete_project
}
# ct_os_deploy_cmd_image IMAGE_NAME
# --------------------
# Runs a special command pod, a pod that does nothing, but includes utilities for testing.
# A typical usage is a mysql pod that includes mysql commandline, that we need for testing.
# Running commands inside this command pod is done via ct_os_cmd_image_run function.
# The pod is not run again if already running.
# Arguments: image_name - image to be used as a command pod
function ct_os_deploy_cmd_image() {
local image_name=${1}
oc get pod command-app &>/dev/null && echo "command POD already running" && return 0
echo "command POD not running yet, will start one called command-app"
oc create -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: command-app
spec:
containers:
- name: command-container
image: "${image_name}"
command: ["sleep"]
args: ["3h"]
restartPolicy: OnFailure
EOF
SECONDS=0
echo -n "Waiting for command POD ."
while [ $SECONDS -lt 180 ] ; do
sout="$(ct_os_cmd_image_run 'echo $((11*11))')"
grep -q '^121$' <<< "$sout" && echo "DONE" && return 0 || :
sleep 3
echo -n "."
done
echo "FAIL"
return 1
}
# ct_os_cmd_image_run CMD [ ARG ... ]
# --------------------
# Runs a command CMD inside a special command pod
# Arguments: cmd - shell command with args to run in a pod
function ct_os_cmd_image_run() {
oc exec command-app -- bash -c "$@"
}
# ct_os_test_response_internal
# ----------------
# Perform GET request to the application container, checks output with
# a reg-exp and HTTP response code.
# That all is done inside an image in the cluster, so the function is used
# typically in clusters that are not accessible outside.
# The interanal image is a python image that should include the most of the useful commands.
# The check is repeated until timeout.
# Argument: url - request URL path
# Argument: expected_code - expected HTTP response code
# Argument: body_regexp - PCRE regular expression that must match the response body
# Argument: max_attempts - Optional number of attempts (default: 20), three seconds sleep between
# Argument: ignore_error_attempts - Optional number of attempts when we ignore error output (default: 10)
ct_os_test_response_internal() {
local url="$1"
local expected_code="$2"
local body_regexp="$3"
local max_attempts=${4:-20}
local ignore_error_attempts=${5:-10}
: " Testing the HTTP(S) response for <${url}>"
local sleep_time=3
local attempt=1
local result=1
local status
local response_code
local response_file=$(mktemp /tmp/ct_test_response_XXXXXX)
local util_image_name='python:3.6'
ct_os_deploy_cmd_image "${util_image_name}"
while [ ${attempt} -le ${max_attempts} ]; do
ct_os_cmd_image_run "curl --connect-timeout 10 -s -w '%{http_code}' '${url}'" >${response_file} && status=0 || status=1
if [ ${status} -eq 0 ]; then
response_code=$(cat ${response_file} | tail -c 3)
if [ "${response_code}" -eq "${expected_code}" ]; then
result=0
fi
cat ${response_file} | grep -qP -e "${body_regexp}" || result=1;
# Some services return 40x code until they are ready, so let's give them
# some chance and not end with failure right away
# Do not wait if we already have expected outcome though
if [ ${result} -eq 0 -o ${attempt} -gt ${ignore_error_attempts} -o ${attempt} -eq ${max_attempts} ] ; then
break
fi
fi
attempt=$(( ${attempt} + 1 ))
sleep ${sleep_time}
done
rm -f ${response_file}
return ${result}
}
# ct_os_get_image_from_pod
# ------------------------
# Print image identifier from an existing pod to stdout
# Argument: pod_prefix - prefix or full name of the pod to get image from
ct_os_get_image_from_pod() {
local pod_prefix=$1 ; shift
local pod_name=$(ct_os_get_pod_name $pod_prefix)
oc get "po/${pod_name}" -o yaml | sed -ne 's/^\s*image:\s*\(.*\)\s*$/\1/ p' | head -1
}
# ct_os_check_cmd_internal
# ----------------
# Runs a specified command, checks exit code and compares the output with expected regexp.
# That all is done inside an image in the cluster, so the function is used
# typically in clusters that are not accessible outside.
# The check is repeated until timeout.
# Argument: util_image_name - name of the image in the cluster that is used for running the cmd
# Argument: service_name - kubernetes' service name to work with (IP address is taken from this one)
# Argument: check_command - command that is run within the util_image_name container
# Argument: expected_content_match - regexp that must be in the output (use .* to ignore check)
# Argument: timeout - number of seconds to wait till the check succeeds
function ct_os_check_cmd_internal() {
local util_image_name=$1 ; shift
local service_name=$1 ; shift
local check_command=$1 ; shift
local expected_content_match=${1:-.*} ; shift
local timeout=${1:-60} ; shift || :
: " Service ${service_name} check ..."
local output
local ret
local ip=$(ct_os_get_service_ip "${service_name}")
local check_command_exp=$(echo "$check_command" | sed -e "s/<IP>/$ip/g")
ct_os_deploy_cmd_image $(ct_os_get_image_from_pod "${util_image_name}" | head -n 1)
SECONDS=0
echo -n "Waiting for ${service_name} service becoming ready ..."
while true ; do
output=$(ct_os_cmd_image_run "$check_command_exp")
ret=$?
echo "${output}" | grep -qe "${expected_content_match}" || ret=1
if [ ${ret} -eq 0 ] ; then
echo " PASS"
return 0
fi
echo -n "."
[ ${SECONDS} -gt ${timeout} ] && break
sleep 3
done
echo " FAIL"
return 1
}