From 9f174e4c9e6ed68608cd855a258f631fda22d0e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Piotrowski?= Date: Thu, 16 May 2019 11:38:40 +0200 Subject: [PATCH] Create Docker image from Monasca repos The purpose of this change is to build Docker image for StatsD service Change-Id: I5f8b406ac4ff6fe9c560b0e27099fd08af54cbd7 Story: 2001694 Task: 30228 Depends-On: https://review.opendev.org/672932/ --- docker/build_image.sh | 4 + docker/statsd/Dockerfile | 33 ++++++++ docker/statsd/README.rst | 66 +++++++++++++++ docker/statsd/agent.yaml.j2 | 10 +++ docker/statsd/build_image.sh | 150 ++++++++++++++++++++++++++++++++++ docker/statsd/health_check.py | 28 +++++++ docker/statsd/start.sh | 17 ++++ 7 files changed, 308 insertions(+) create mode 100644 docker/statsd/Dockerfile create mode 100644 docker/statsd/README.rst create mode 100644 docker/statsd/agent.yaml.j2 create mode 100755 docker/statsd/build_image.sh create mode 100644 docker/statsd/health_check.py create mode 100755 docker/statsd/start.sh diff --git a/docker/build_image.sh b/docker/build_image.sh index 53168377..a84a5f33 100755 --- a/docker/build_image.sh +++ b/docker/build_image.sh @@ -56,3 +56,7 @@ cd "$(dirname "$REAL_PATH")/../docker/" printf "\n\n\n" ./forwarder/build_image.sh "$@" + +printf "\n\n\n" + +./statsd/build_image.sh "$@" diff --git a/docker/statsd/Dockerfile b/docker/statsd/Dockerfile new file mode 100644 index 00000000..275a9794 --- /dev/null +++ b/docker/statsd/Dockerfile @@ -0,0 +1,33 @@ +ARG DOCKER_IMAGE=monasca/statsd +ARG APP_REPO=https://opendev.org/openstack/monasca-agent + +# Branch, tag or git hash to build from. +ARG REPO_VERSION=master +ARG CONSTRAINTS_BRANCH=master + +# Extra Python3 dependencies. +#ARG EXTRA_DEPS="" + +# Always start from `monasca-base` image and use specific tag of it. +ARG BASE_TAG=master +FROM monasca/base:$BASE_TAG + +# Environment variables used for our service or wait scripts. +ENV \ + KEYSTONE_DEFAULTS_ENABLED=true \ + MONASCA_URL=http://monasca:8070/v2.0 \ + LOG_LEVEL=WARN \ + HOSTNAME_FROM_KUBERNETES=false \ + STAY_ALIVE_ON_FAILURE="false" + +# Copy all neccessary files to proper locations. +COPY agent.yaml.j2 /etc/monasca/agent/agent.yaml.j2 +COPY start.sh health_check.py / + +# Run here all additionals steps your service need post installation. +# Stay with only one `RUN` and use `&& \` for next steps to don't create +# unnecessary image layers. Clean at the end to conserve space. +#RUN + +# Implement start script in `start.sh` file. +CMD ["/start.sh"] \ No newline at end of file diff --git a/docker/statsd/README.rst b/docker/statsd/README.rst new file mode 100644 index 00000000..6db3d58f --- /dev/null +++ b/docker/statsd/README.rst @@ -0,0 +1,66 @@ +=============================== +Docker image for Monasca Statsd +=============================== +The Monasca Statsd image is based on the monasca-base image. + + +Building monasca-base image +=========================== +See https://github.com/openstack/monasca-common/tree/master/docker/README.rst + + +Building Monasca Statsd image +============================= + +Example: + $ ./build_image.sh + +Everything after ``./build_image.sh`` is optional and by default configured +to get versions from ``Dockerfile``. ``./build_image.sh`` also contain more +detailed build description. + +Environment variables +~~~~~~~~~~~~~~~~~~~~~ +============================== ========================= ==================================================== +Variable Default Description +============================== ========================= ==================================================== +STATSD_PORT 8125 The port for statsd +LOG_LEVEL WARN Log level for service +HOSTNAME_FROM_KUBERNETES false Determine node hostname from Kubernetes +STAY_ALIVE_ON_FAILURE false If true, container runs 2 hours after service fails +MONASCA_URL http://monasca:8070/v2.0 Versioned Monasca API URL +KEYSTONE_DEFAULTS_ENABLED true Use all OS defaults +============================== ========================= ==================================================== + + +Requirements from monasca-base image +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +health_check.py + This file will be used for checking the status of the Monasca API + application. + + +Scripts +~~~~~~~ +start.sh + In this starting script provide all steps that lead to the proper service + start. Including usage of wait scripts and templating of configuration + files. You also could provide the ability to allow running container after + service died for easier debugging. + +build_image.sh + Please read detailed build description inside the script. + + +Docker Compose +~~~~~~~~~~~~~~ +When you want to use docker-compose add it as a new service in your docker-compose.yml file +Example: + + * monasca-statsd: + * image: monasca/statsd:master + + +Links +~~~~~ +https://docs.openstack.org/designate/latest/contributor/metrics.html \ No newline at end of file diff --git a/docker/statsd/agent.yaml.j2 b/docker/statsd/agent.yaml.j2 new file mode 100644 index 00000000..83647bcf --- /dev/null +++ b/docker/statsd/agent.yaml.j2 @@ -0,0 +1,10 @@ +Main: + non_local_traffic: {{ NON_LOCAL_TRAFFIC | default(True) }} + forwarder_url: {{ FORWARDER_URL | default("http://localhost:17123") }} + +Statsd: + monasca_statsd_port : {{ STATSD_PORT | default(8125) }} + +Logging: + log_level: {{ LOG_LEVEL | default('WARN') }} + disable_file_logging: True diff --git a/docker/statsd/build_image.sh b/docker/statsd/build_image.sh new file mode 100755 index 00000000..cbc0e1ee --- /dev/null +++ b/docker/statsd/build_image.sh @@ -0,0 +1,150 @@ +#!/bin/bash + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# TODO(Dobroslaw): move this script to monasca-common/docker folder +# and leave here small script to download it and execute using env variables +# to minimize code duplication. + +set -x # Print each script step. +set -eo pipefail # Exit the script if any statement returns error. + +# This script is used for building Docker image with proper labels +# and proper version of monasca-common. +# +# Example usage: +# $ ./build_image.sh +# +# Everything after `./build_image.sh` is optional and by default configured +# to get versions from `Dockerfile`. +# +# To build from master branch (default): +# $ ./build_image.sh +# To build specific version run this script in the following way: +# $ ./build_image.sh stable/queens +# Building from specific commit: +# $ ./build_image.sh cb7f226 +# When building from a tag monasca-common will be used in version available +# in upper constraint file: +# $ ./build_image.sh 2.5.0 +# To build image from Gerrit patch sets that is targeting branch stable/queens: +# $ ./build_image.sh refs/changes/51/558751/1 stable/queens +# +# If you want to build image with custom monasca-common version you need +# to provide it as in the following example: +# $ ./build_image.sh master master refs/changes/19/595719/3 + +# Go to folder with Docker files. +REAL_PATH=$(python -c "import os,sys; print(os.path.realpath('$0'))") +cd "$(dirname "$REAL_PATH")/../statsd/" + +[ -z "$DOCKER_IMAGE" ] && \ + DOCKER_IMAGE=$(\grep "ARG DOCKER_IMAGE=" Dockerfile | cut -f2 -d"=") + +: "${REPO_VERSION:=$1}" +[ -z "$REPO_VERSION" ] && \ + REPO_VERSION=$(\grep "ARG REPO_VERSION=" Dockerfile | cut -f2 -d"=") +# Let's stick to more readable version and disable SC2001 here. +# shellcheck disable=SC2001 +REPO_VERSION_CLEAN=$(echo "$REPO_VERSION" | sed 's|/|-|g') + +[ -z "$APP_REPO" ] && APP_REPO=$(\grep "ARG APP_REPO=" Dockerfile | cut -f2 -d"=") +GITHUB_REPO=$(echo "$APP_REPO" | sed 's/opendev.org/github.com/' | \ + sed 's/ssh:/https:/') + +if [ -z "$CONSTRAINTS_FILE" ]; then + CONSTRAINTS_FILE=$(\grep "ARG CONSTRAINTS_FILE=" Dockerfile | cut -f2 -d"=") || true + : "${CONSTRAINTS_FILE:=https://opendev.org/openstack/requirements/raw/branch/master/upper-constraints.txt}" +fi + +: "${CONSTRAINTS_BRANCH:=$2}" +[ -z "$CONSTRAINTS_BRANCH" ] && \ + CONSTRAINTS_BRANCH=$(\grep "ARG CONSTRAINTS_BRANCH=" Dockerfile | cut -f2 -d"=") + +# When using stable version of repository use same stable constraints file. +case "$REPO_VERSION" in + *stable*) + CONSTRAINTS_BRANCH_CLEAN="$REPO_VERSION" + CONSTRAINTS_FILE=${CONSTRAINTS_FILE/master/$CONSTRAINTS_BRANCH_CLEAN} + # Get monasca-common version from stable upper constraints file. + CONSTRAINTS_TMP_FILE=$(mktemp) + wget --output-document "$CONSTRAINTS_TMP_FILE" \ + $CONSTRAINTS_FILE + UPPER_COMMON=$(\grep 'monasca-common' "$CONSTRAINTS_TMP_FILE") + # Get only version part from monasca-common. + UPPER_COMMON_VERSION="${UPPER_COMMON##*===}" + rm -rf "$CONSTRAINTS_TMP_FILE" + ;; + *) + CONSTRAINTS_BRANCH_CLEAN="$CONSTRAINTS_BRANCH" + ;; +esac + +# Monasca-common variables. +if [ -z "$COMMON_REPO" ]; then + COMMON_REPO=$(\grep "ARG COMMON_REPO=" Dockerfile | cut -f2 -d"=") || true + : "${COMMON_REPO:=https://opendev.org/openstack/monasca-common}" +fi +: "${COMMON_VERSION:=$3}" +if [ -z "$COMMON_VERSION" ]; then + COMMON_VERSION=$(\grep "ARG COMMON_VERSION=" Dockerfile | cut -f2 -d"=") || true + if [ "$UPPER_COMMON_VERSION" ]; then + # Common from upper constraints file. + COMMON_VERSION="$UPPER_COMMON_VERSION" + fi +fi + +# Clone project to temporary directory for getting proper commit number from +# branches and tags. We need this for setting proper image labels. +# Docker does not allow to get any data from inside of system when building +# image. +TMP_DIR=$(mktemp -d) +( + cd "$TMP_DIR" + # This many steps are needed to support gerrit patch sets. + git init + git remote add origin "$APP_REPO" + git fetch origin "$REPO_VERSION" + git reset --hard FETCH_HEAD +) +GIT_COMMIT=$(git -C "$TMP_DIR" rev-parse HEAD) +[ -z "${GIT_COMMIT}" ] && echo "No git commit hash found" && exit 1 +rm -rf "$TMP_DIR" + +# Do the same for monasca-common. +COMMON_TMP_DIR=$(mktemp -d) +( + cd "$COMMON_TMP_DIR" + # This many steps are needed to support gerrit patch sets. + git init + git remote add origin "$COMMON_REPO" + git fetch origin "$COMMON_VERSION" + git reset --hard FETCH_HEAD +) +COMMON_GIT_COMMIT=$(git -C "$COMMON_TMP_DIR" rev-parse HEAD) +[ -z "${COMMON_GIT_COMMIT}" ] && echo "No git commit hash found" && exit 1 +rm -rf "$COMMON_TMP_DIR" + +CREATION_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + +docker build --no-cache \ + --build-arg CREATION_TIME="$CREATION_TIME" \ + --build-arg GITHUB_REPO="$GITHUB_REPO" \ + --build-arg APP_REPO="$APP_REPO" \ + --build-arg REPO_VERSION="$REPO_VERSION" \ + --build-arg GIT_COMMIT="$GIT_COMMIT" \ + --build-arg CONSTRAINTS_FILE="$CONSTRAINTS_FILE" \ + --build-arg COMMON_REPO="$COMMON_REPO" \ + --build-arg COMMON_VERSION="$COMMON_VERSION" \ + --build-arg COMMON_GIT_COMMIT="$COMMON_GIT_COMMIT" \ + --tag "$DOCKER_IMAGE":"$REPO_VERSION_CLEAN" . diff --git a/docker/statsd/health_check.py b/docker/statsd/health_check.py new file mode 100644 index 00000000..d5de7dc1 --- /dev/null +++ b/docker/statsd/health_check.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# coding=utf-8 + +# (C) Copyright 2018 FUJITSU LIMITED +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Health check will returns 0 when service is working properly.""" + + +def main(): + """Health check for Monasca-agent statsd.""" + # TODO wait for health check endpoint ... + return 0 + + +if __name__ == '__main__': + main() diff --git a/docker/statsd/start.sh b/docker/statsd/start.sh new file mode 100755 index 00000000..48b1a436 --- /dev/null +++ b/docker/statsd/start.sh @@ -0,0 +1,17 @@ +#!/bin/ash +# shellcheck shell=dash +# (C) Copyright 2018 FUJITSU LIMITED + +set -x + +alias template="templer --ignore-undefined-variables --force --verbose" + +AGENT_CONF="/etc/monasca/agent" + +template $AGENT_CONF/agent.yaml.j2 $AGENT_CONF/agent.yaml +rm $AGENT_CONF/agent.yaml.j2 +cat $AGENT_CONF/agent.yaml + +# Start our service. +echo "Start script: starting container" +monasca-statsd \ No newline at end of file