Browse Source

Create Docker image and build in Zuul

Change-Id: I9de51069c8302ac44711f08f0d396d09389b0067
Story: 2001694
Topic: 29771
Dobroslaw Zybort 1 month ago
parent
commit
71817e2ede
7 changed files with 414 additions and 0 deletions
  1. 1
    0
      .zuul.yaml
  2. 44
    0
      docker/Dockerfile
  3. 112
    0
      docker/README.rst
  4. 150
    0
      docker/build_image.sh
  5. 35
    0
      docker/health_check.py
  6. 61
    0
      docker/start.sh
  7. 11
    0
      docker/tempest.conf.j2

+ 1
- 0
.zuul.yaml View File

@@ -11,6 +11,7 @@
11 11
         - monasca-tempest-python2-cassandra
12 12
         - monascalog-python2-tempest
13 13
         - monascalog-python3-tempest
14
+        - docker-build-monasca-service
14 15
     gate:
15 16
       queue: monasca
16 17
       jobs:

+ 44
- 0
docker/Dockerfile View File

@@ -0,0 +1,44 @@
1
+ARG DOCKER_IMAGE=monasca/tempest
2
+ARG APP_REPO=https://git.openstack.org/openstack/monasca-tempest-plugin
3
+
4
+# Branch, tag or git hash to build from.
5
+ARG REPO_VERSION=master
6
+ARG CONSTRAINTS_BRANCH=master
7
+
8
+# Extra Python3 dependencies.
9
+ARG EXTRA_DEPS="python-openstackclient"
10
+
11
+# Always start from `monasca-base` image and use specific tag of it.
12
+ARG BASE_TAG=master
13
+FROM monasca/base:$BASE_TAG
14
+
15
+# Environment variables used for our service or wait scripts.
16
+ENV \
17
+    MONASCA_URI=http://monasca:8070 \
18
+    KEYSTONE_IDENTITY_URI=http://keystone:35357 \
19
+    USE_DYNAMIC_CREDS=True \
20
+    KEYSTONE_ADMIN_USER=mini-mon \
21
+    KEYSTONE_ADMIN_PASSWORD=password \
22
+    KEYSTONE_ADMIN_PROJECT=mini-mon \
23
+    KEYSTONE_ADMIN_DOMAIN=Default \
24
+    OS_AUTH_URL=http://keystone:35357/v3 \
25
+    OS_USERNAME=mini-mon \
26
+    OS_PASSWORD=password \
27
+    OS_TENANT_NAME=mini-mon \
28
+    OS_DOMAIN_NAME=Default \
29
+    STAY_ALIVE_ON_FAILURE=false
30
+
31
+# Copy all neccessary files to proper locations.
32
+COPY tempest.conf.j2 /etc/tempest/
33
+
34
+WORKDIR /tempest
35
+
36
+# Run here all additionals steps your service need post installation.
37
+# Stay with only one `RUN` and use `&& \` for next steps to don't create
38
+# unnecessary image layers. Clean at the end to conserve space.
39
+#RUN \
40
+#    echo "Some steps to do after main installation." && \
41
+#    echo "Hello when building."
42
+
43
+# Implement start script in `start.sh` file.
44
+CMD ["/start.sh"]

+ 112
- 0
docker/README.rst View File

@@ -0,0 +1,112 @@
1
+===============================================
2
+Docker image for Monasca Monasca Temptest tests
3
+===============================================
4
+This image could be used for running Tempest tests on Monasca installed in any
5
+way. After providing proper environment variables container could be started
6
+and will run tests based on what endpoints it will find in configured Keystone.
7
+Supported endpoints are ``monitoring`` and ``logs`` (``Service Type`` in
8
+``openstack endpoint list`` output).
9
+
10
+
11
+Building monasca-base image
12
+===========================
13
+See https://github.com/openstack/monasca-common/tree/master/docker/README.rst
14
+
15
+
16
+Building Docker image
17
+=====================
18
+
19
+Example:
20
+  $ ./build_image.sh <repository_version> <upper_constrains_branch> <common_version>
21
+
22
+Everything after ``./build_image.sh`` is optional and by default configured
23
+to get versions from ``Dockerfile``. ``./build_image.sh`` also contain more
24
+detailed build description.
25
+
26
+
27
+How to start
28
+~~~~~~~~~~~~
29
+
30
+When using running Monasca in Docker you can connect this image to the network
31
+where Monasca is accessible and run all tests.
32
+Find network on machine with Monasca with ``docker network ls``.
33
+For example you can see similar information to:
34
+``e20533f6112c   monasca-docker_default   bridge   local``
35
+
36
+Using this network run all tempest tests with following command:
37
+
38
+``docker run -it --rm --network=monasca-docker_default monasca/tempest:master``
39
+
40
+It's important to configure all necessary connection environment variables.
41
+They are listed in the next two sections.
42
+
43
+Example command to run tempest tests with custom variables::
44
+
45
+``docker run -it --rm --network=monasca-docker_default --env-file=tempest_con.env monasca/tempest:master``
46
+
47
+In this example you configure all environment variables in ``tempest_con.env``
48
+file::
49
+
50
+    MONASCA_URI=172.17.0.1:8070
51
+    KEYSTONE_IDENTITY_URI=http://172.17.0.1:35357
52
+    USE_DYNAMIC_CREDS=True
53
+    KEYSTONE_ADMIN_USER=mini-mon
54
+    KEYSTONE_ADMIN_PASSWORD=password
55
+    KEYSTONE_ADMIN_PROJECT=mini-mon
56
+    KEYSTONE_ADMIN_DOMAIN=Default
57
+    OS_AUTH_URL=http://172.17.0.1:35357/v3
58
+    OS_USERNAME=mini-mon
59
+    OS_PASSWORD=password
60
+    OS_PROJECT_NAME=mini-mon
61
+    OS_DOMAIN_NAME=Default
62
+
63
+
64
+Environment variables
65
+~~~~~~~~~~~~~~~~~~~~~
66
+========================= ============================== ==========================================
67
+Variable                  Default                        Description
68
+========================= ============================== ==========================================
69
+USE_DYNAMIC_CREDS         True                           Create dynamic credentials for tests
70
+KEYSTONE_ADMIN_USER       mini-mon                       OpenStack administrator user name
71
+KEYSTONE_ADMIN_PASSWORD   password                       OpenStack administrator user password
72
+KEYSTONE_ADMIN_PROJECT    mini-mon                       OpenStack administrator tenant name
73
+KEYSTONE_ADMIN_DOMAIN     Default                        OpenStack administrator domain
74
+OS_AUTH_URL               http://keystone:35357/v3       Versioned Keystone URL
75
+OS_USERNAME               mini-mon                       Keystone user name
76
+OS_PASSWORD               password                       Keystone user password
77
+OS_PROJECT_NAME           mini-mon                       Keystone user project name
78
+OS_DOMAIN_NAME            Default                        Keystone user domain name
79
+IDENTITY_URI              http://keystone:35357/v2.0/    Full URI of the Keystone, v2
80
+IDENTITY_URI_V3           http://keystone:35357/v3/      Full URI of the Keystone, v3
81
+LOG_LEVEL                 INFO                           Log level for root logging
82
+STAY_ALIVE_ON_FAILURE     false                          If true, container runs 2 hours after service fail
83
+========================= ============================== ==========================================
84
+
85
+
86
+Wait scripts environment variables
87
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88
+========================= ============================== ==========================================
89
+Variable                  Default                        Description
90
+========================= ============================== ==========================================
91
+MONASCA_URI               http://monasca:8070            The metric pipeline endpoint
92
+KEYSTONE_IDENTITY_URI     http://keystone:35357          URI to Keystone admin endpoint
93
+========================= ============================== ==========================================
94
+
95
+
96
+Scripts
97
+~~~~~~~
98
+start.sh
99
+  In this starting script provide all steps that lead to the proper service
100
+  start. Including usage of wait scripts and templating of configuration
101
+  files. You also could provide the ability to allow running container after
102
+  service died for easier debugging.
103
+
104
+health_check.py
105
+  This file will be used for checking the status of the application.
106
+
107
+
108
+Links
109
+~~~~~
110
+https://docs.openstack.org/monasca-api/latest/
111
+
112
+https://github.com/openstack/monasca-api/blob/master/README.rst

+ 150
- 0
docker/build_image.sh View File

@@ -0,0 +1,150 @@
1
+#!/bin/bash
2
+
3
+#  Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+#  not use this file except in compliance with the License. You may obtain
5
+#  a copy of the License at
6
+#
7
+#       http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+#  Unless required by applicable law or agreed to in writing, software
10
+#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+#  License for the specific language governing permissions and limitations
13
+#  under the License.
14
+
15
+# TODO(Dobroslaw): move this script to monasca-common/docker folder
16
+# and leave here small script to download it and execute using env variables
17
+# to minimize code duplication.
18
+
19
+set -x  # Print each script step.
20
+set -eo pipefail  # Exit the script if any statement returns error.
21
+
22
+# This script is used for building Docker image with proper labels
23
+# and proper version of monasca-common.
24
+#
25
+# Example usage:
26
+#   $ ./build_image.sh <repository_version> <upper_constains_branch> <common_version>
27
+#
28
+# Everything after `./build_image.sh` is optional and by default configured
29
+# to get versions from `Dockerfile`.
30
+#
31
+# To build from master branch (default):
32
+#   $ ./build_image.sh
33
+# To build specific version run this script in the following way:
34
+#   $ ./build_image.sh stable/queens
35
+# Building from specific commit:
36
+#   $ ./build_image.sh cb7f226
37
+# When building from a tag monasca-common will be used in version available
38
+# in upper constraint file:
39
+#   $ ./build_image.sh 2.5.0
40
+# To build image from Gerrit patch sets that is targeting branch stable/queens:
41
+#   $ ./build_image.sh refs/changes/51/558751/1 stable/queens
42
+#
43
+# If you want to build image with custom monasca-common version you need
44
+# to provide it as in the following example:
45
+#   $ ./build_image.sh master master refs/changes/19/595719/3
46
+
47
+# Go to folder with Docker files.
48
+REAL_PATH=$(python -c "import os,sys; print(os.path.realpath('$0'))")
49
+cd "$(dirname "$REAL_PATH")/../docker/"
50
+
51
+[ -z "$DOCKER_IMAGE" ] && \
52
+    DOCKER_IMAGE=$(\grep DOCKER_IMAGE Dockerfile | cut -f2 -d"=")
53
+
54
+: "${REPO_VERSION:=$1}"
55
+[ -z "$REPO_VERSION" ] && \
56
+    REPO_VERSION=$(\grep REPO_VERSION Dockerfile | cut -f2 -d"=")
57
+# Let's stick to more readable version and disable SC2001 here.
58
+# shellcheck disable=SC2001
59
+REPO_VERSION_CLEAN=$(echo "$REPO_VERSION" | sed 's|/|-|g')
60
+
61
+[ -z "$APP_REPO" ] && APP_REPO=$(\grep APP_REPO Dockerfile | cut -f2 -d"=")
62
+GITHUB_REPO=$(echo "$APP_REPO" | sed 's/git.openstack.org/github.com/' | \
63
+              sed 's/ssh:/https:/')
64
+
65
+if [ -z "$CONSTRAINTS_FILE" ]; then
66
+    CONSTRAINTS_FILE=$(\grep CONSTRAINTS_FILE Dockerfile | cut -f2 -d"=") || true
67
+    : "${CONSTRAINTS_FILE:=http://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt}"
68
+fi
69
+
70
+: "${CONSTRAINTS_BRANCH:=$2}"
71
+[ -z "$CONSTRAINTS_BRANCH" ] && \
72
+    CONSTRAINTS_BRANCH=$(\grep CONSTRAINTS_BRANCH Dockerfile | cut -f2 -d"=")
73
+
74
+# When using stable version of repository use same stable constraints file.
75
+case "$REPO_VERSION" in
76
+    *stable*)
77
+        CONSTRAINTS_BRANCH_CLEAN="$REPO_VERSION"
78
+        # Get monasca-common version from stable upper constraints file.
79
+        CONSTRAINTS_TMP_FILE=$(mktemp)
80
+        wget --output-document "$CONSTRAINTS_TMP_FILE" \
81
+            "$CONSTRAINTS_FILE"?h="$CONSTRAINTS_BRANCH_CLEAN"
82
+        UPPER_COMMON=$(\grep 'monasca-common' "$CONSTRAINTS_TMP_FILE")
83
+        # Get only version part from monasca-common.
84
+        UPPER_COMMON_VERSION="${UPPER_COMMON##*===}"
85
+        rm -rf "$CONSTRAINTS_TMP_FILE"
86
+    ;;
87
+    *)
88
+        CONSTRAINTS_BRANCH_CLEAN="$CONSTRAINTS_BRANCH"
89
+    ;;
90
+esac
91
+
92
+# Monasca-common variables.
93
+if [ -z "$COMMON_REPO" ]; then
94
+    COMMON_REPO=$(\grep COMMON_REPO Dockerfile | cut -f2 -d"=") || true
95
+    : "${COMMON_REPO:=https://git.openstack.org/openstack/monasca-common}"
96
+fi
97
+: "${COMMON_VERSION:=$3}"
98
+if [ -z "$COMMON_VERSION" ]; then
99
+    COMMON_VERSION=$(\grep COMMON_VERSION Dockerfile | cut -f2 -d"=") || true
100
+    if [ "$UPPER_COMMON_VERSION" ]; then
101
+        # Common from upper constraints file.
102
+        COMMON_VERSION="$UPPER_COMMON_VERSION"
103
+    fi
104
+fi
105
+
106
+# Clone project to temporary directory for getting proper commit number from
107
+# branches and tags. We need this for setting proper image labels.
108
+# Docker does not allow to get any data from inside of system when building
109
+# image.
110
+TMP_DIR=$(mktemp -d)
111
+(
112
+    cd "$TMP_DIR"
113
+    # This many steps are needed to support gerrit patch sets.
114
+    git init
115
+    git remote add origin "$APP_REPO"
116
+    git fetch origin "$REPO_VERSION"
117
+    git reset --hard FETCH_HEAD
118
+)
119
+GIT_COMMIT=$(git -C "$TMP_DIR" rev-parse HEAD)
120
+[ -z "${GIT_COMMIT}" ] && echo "No git commit hash found" && exit 1
121
+rm -rf "$TMP_DIR"
122
+
123
+# Do the same for monasca-common.
124
+COMMON_TMP_DIR=$(mktemp -d)
125
+(
126
+    cd "$COMMON_TMP_DIR"
127
+    # This many steps are needed to support gerrit patch sets.
128
+    git init
129
+    git remote add origin "$COMMON_REPO"
130
+    git fetch origin "$COMMON_VERSION"
131
+    git reset --hard FETCH_HEAD
132
+)
133
+COMMON_GIT_COMMIT=$(git -C "$COMMON_TMP_DIR" rev-parse HEAD)
134
+[ -z "${COMMON_GIT_COMMIT}" ] && echo "No git commit hash found" && exit 1
135
+rm -rf "$COMMON_TMP_DIR"
136
+
137
+CREATION_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
138
+
139
+docker build --no-cache \
140
+    --build-arg CREATION_TIME="$CREATION_TIME" \
141
+    --build-arg GITHUB_REPO="$GITHUB_REPO" \
142
+    --build-arg APP_REPO="$APP_REPO" \
143
+    --build-arg REPO_VERSION="$REPO_VERSION" \
144
+    --build-arg GIT_COMMIT="$GIT_COMMIT" \
145
+    --build-arg CONSTRAINTS_FILE="$CONSTRAINTS_FILE" \
146
+    --build-arg CONSTRAINTS_BRANCH="$CONSTRAINTS_BRANCH_CLEAN" \
147
+    --build-arg COMMON_REPO="$COMMON_REPO" \
148
+    --build-arg COMMON_VERSION="$COMMON_VERSION" \
149
+    --build-arg COMMON_GIT_COMMIT="$COMMON_GIT_COMMIT" \
150
+    --tag "$DOCKER_IMAGE":"$REPO_VERSION_CLEAN" .

+ 35
- 0
docker/health_check.py View File

@@ -0,0 +1,35 @@
1
+#!/usr/bin/env python
2
+# coding=utf-8
3
+
4
+# (C) Copyright 2018 FUJITSU LIMITED
5
+#
6
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
+# not use this file except in compliance with the License. You may obtain
8
+# a copy of the License at
9
+#
10
+# http://www.apache.org/licenses/LICENSE-2.0
11
+#
12
+# Unless required by applicable law or agreed to in writing, software
13
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
+# License for the specific language governing permissions and limitations
16
+# under the License.
17
+
18
+"""Health check will returns 0 when service is working properly."""
19
+
20
+import logging
21
+import os
22
+
23
+
24
+LOG_LEVEL = logging.getLevelName(os.environ.get('LOG_LEVEL', 'INFO'))
25
+logging.basicConfig(level=LOG_LEVEL)
26
+logger = logging.getLogger(__name__)
27
+
28
+
29
+def main():
30
+    logger.debug('No health check for tempest')
31
+    return 0
32
+
33
+
34
+if __name__ == '__main__':
35
+    main()

+ 61
- 0
docker/start.sh View File

@@ -0,0 +1,61 @@
1
+#!/bin/sh
2
+
3
+#  Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+#  not use this file except in compliance with the License. You may obtain
5
+#  a copy of the License at
6
+#
7
+#       http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+#  Unless required by applicable law or agreed to in writing, software
10
+#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+#  License for the specific language governing permissions and limitations
13
+#  under the License.
14
+
15
+# Starting script.
16
+# All checks and configuration templating you need to do before service
17
+# could be safely started should be added in this file.
18
+
19
+set -x
20
+
21
+set -eo pipefail  # Exit the script if any statement returns error.
22
+
23
+# Test services we need before starting our service.
24
+echo "Start script: waiting for needed services"
25
+/wait_for.sh "$MONASCA_URI"
26
+/wait_for.sh "$KEYSTONE_IDENTITY_URI"
27
+
28
+# Template all config files before start, it will use env variables.
29
+# Read usage examples: https://pypi.org/project/Templer/
30
+echo "Start script: creating config files from templates"
31
+templer -v -f /etc/tempest/tempest.conf.j2 /etc/tempest/tempest.conf
32
+
33
+# Start our service.
34
+echo "Start script: starting tempest"
35
+cd /tempest/
36
+# Initialize only when folder is empty.
37
+if [ ! "$(ls -A /tempest/)" ]; then
38
+  tempest init
39
+fi
40
+tempest list-plugins
41
+
42
+if openstack endpoint list --column "Service Type" -f value | grep -q monitoring
43
+then
44
+ tempest run -r monasca_tempest_tests.tests.api
45
+else
46
+ true
47
+fi
48
+if openstack endpoint list --column "Service Type" -f value | grep -q logs
49
+then
50
+  tempest run -r monasca_tempest_tests.tests.log_api
51
+else
52
+  true
53
+fi
54
+
55
+# Allow server to stay alive in case of failure for 2 hours for debugging.
56
+RESULT=$?
57
+if [ $RESULT != 0 ] && [ "$STAY_ALIVE_ON_FAILURE" = "true" ]; then
58
+  echo "Service died, waiting 120 min before exiting"
59
+  sleep 7200
60
+fi
61
+exit $RESULT

+ 11
- 0
docker/tempest.conf.j2 View File

@@ -0,0 +1,11 @@
1
+[identity]
2
+auth_version = v3
3
+uri = {{ AUTH_URI | default(KEYSTONE_IDENTITY_URI + "/v2.0/") }}
4
+uri_v3 = {{ AUTH_URI_V3 | default(KEYSTONE_IDENTITY_URI + "/v3/") }}
5
+
6
+[auth]
7
+use_dynamic_credentials = {{ USE_DYNAMIC_CREDS }}
8
+admin_username = {{ KEYSTONE_ADMIN_USER }}
9
+admin_password = {{ KEYSTONE_ADMIN_PASSWORD }}
10
+admin_project_name = {{ KEYSTONE_ADMIN_PROJECT }}
11
+admin_domain_name = {{ KEYSTONE_ADMIN_DOMAIN }}

Loading…
Cancel
Save