Skip to content

Commit

Permalink
Containerizing fuel ccp
Browse files Browse the repository at this point in the history
* Ability to create docker container out of current state
of the repository has been added.
* Alias to execute 'ccp' command transparently from the host system
is generated.
* Ability to create container out of remote repository.

Change-Id: Iafcad1190a63dbc2c10b129f50bf47d70d2faa65
  • Loading branch information
Dmitry Klenov committed Dec 20, 2016
1 parent d8b879f commit 29328d4
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docker/ccp/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
FROM debian:jessie
MAINTAINER MOS Microservices <[email protected]>

ENV DEBIAN_FRONTEND=noninteractive \
GOSU_VERSION=1.9

COPY entrypoint.sh /usr/local/bin/entrypoint.sh
COPY fuel-ccp.tar.gz /opt/

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl \
ca-certificates \
python \
python-dev \
gcc \
git \
# Bringing GOSU to be able to execute ccp process under current user
&& dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')" \
&& curl -L -o /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch" \
&& curl -L -o /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc" \
&& export GNUPGHOME="$(mktemp -d)" \
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \
&& gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \
&& rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \
&& chmod +x /usr/local/bin/gosu \
&& gosu nobody true \
# Installing pip and virtualenv
&& curl -O https://bootstrap.pypa.io/get-pip.py \
&& python get-pip.py --user \
&& ~/.local/bin/pip install --user virtualenv \
# Creating venv for CCP
&& ~/.local/bin/virtualenv /var/lib/ccp/venv \
# Installing Fuel CCP
&& /var/lib/ccp/venv/bin/pip install /opt/fuel-ccp.tar.gz \
&& chmod a+x /usr/local/bin/entrypoint.sh \
# Cleaning up a bit
&& apt-get purge -y --auto-remove curl gcc \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
/tmp/* /var/tmp/* \
get-pip.py \
~/.local/

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
4 changes: 4 additions & 0 deletions docker/ccp/ccprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
alias ccp='docker run --rm -t -i --name=fuel-ccp --network=host \
-v ~/:/home/`whoami`/ -v /var/run/docker.sock:/var/run/docker.sock \
-e LOCAL_USER_ID=`id -u $USER` -e LOCAL_USER_NAME=`whoami` \
fuel-ccp:latest'
91 changes: 91 additions & 0 deletions docker/ccp/dockerize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/bin/bash

# The script, which is going to create fuel-ccp docker image out of current content of the working
# tree or from specific branch of remote git repository
set -e

usage() {
cat << EOF
Usage: $0 [-u] [-r REPO] [-b BRANCH]
-h Prints this help
-u Build image from default upstream git repository
-r Repository to create image from
-b Branch to use for docker assembly
By default docker image will be created from the local source tree. If flag -u is set, sources
will be fetched from upstream git repository "https://git.openstack.org/openstack/fuel-ccp".
Arbitrary repository REPO can be specified with -r flag.
Flag -b can be used to ccp state from the branch other than master. In this case branch BRANCH will be
checked out from the selected repository before assembling docker container.
EOF
}

build_tarball() {
echo "Creating sdist tarball"
python setup.py sdist
TARBALL_NAME=`find dist -type f -iname "*.tar.gz" -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "`
}

SOURCE_REPO_DEFAULT=https://git.openstack.org/openstack/fuel-ccp
TMP_TAR=fuel-ccp.tar.gz
# Using local source tree by default.
SOURCE_REPO=`readlink -f ../../`

while getopts ":ur:b:h" opt; do
case $opt in
r)
SOURCE_REPO="${OPTARG}"
;;
b)
BRANCH="${OPTARG}"
;;
h)
usage
exit 1
;;
u)
SOURCE_REPO="${SOURCE_REPO_DEFAULT}"
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
usage
exit 1
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
usage
exit 1
;;
esac
done

function cleanup {
if [ -e "${SRC_DIR}" ]; then
rm -rf "${SRC_DIR}"
echo "Deleted temp working directory $SRC_DIR"
fi
if [ -e "${TMP_TAR}" ]; then
rm "${TMP_TAR}"
echo "Deleting sources archive ${TMP_TAR}"
fi
}
trap cleanup EXIT

SRC_DIR=`mktemp -d "/tmp/ccp_sources.XXXXXXXXX"`
pushd "${SRC_DIR}"

echo "Fetching sources from ${SOURCE_REPO}"
git clone "${SOURCE_REPO}"
cd fuel-ccp
if [ -n "${BRANCH}" ]; then
echo "Checking out branch ${BRANCH}"
git checkout -b "${BRANCH}" origin/"${BRANCH}"
fi

build_tarball
popd
cp "${SRC_DIR}"/fuel-ccp/"${TARBALL_NAME}" "${TMP_TAR}"

echo "Building Docker image"
docker build -t fuel-ccp .
14 changes: 14 additions & 0 deletions docker/ccp/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Adds a local user
# Either use the LOCAL_USER_ID and LOCAL_USER_NAME if passed in at runtime
# or user with id 9001 and name ccp as a fallback

USER_ID=${LOCAL_USER_ID:-9001}
USER_NAME=${LOCAL_USER_NAME:-ccp}

groupadd docker -g `stat -c " %g" /var/run/docker.sock`
useradd --shell /bin/bash -u "${USER_ID}" -G docker -o -m "${USER_NAME}" 2> /dev/null
export HOME=/home/"${USER_NAME}"
chown "${USER_NAME}":"${USER_NAME}" "${HOME}"
cd "${HOME}" && exec /usr/local/bin/gosu "${USER_NAME}" /var/lib/ccp/venv/bin/ccp "$@"

0 comments on commit 29328d4

Please sign in to comment.