Skip to content

Commit

Permalink
Prepare for running in release container
Browse files Browse the repository at this point in the history
  • Loading branch information
shapovalovts committed Aug 31, 2024
1 parent 264a2e0 commit 271972a
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 101 deletions.
20 changes: 14 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

COG = cog.py
REBAR = ./rebar3
BUILD_CONTAINER = scripts/build-dev-container.sh
START_CONTAINER = scripts/start-dev-container.sh
BUILD_DEBUG_CONTAINER = scripts/build-debug-container.sh
START_DEBUG_CONTAINER = scripts/start-debug-container.sh -i
BUILD_RELEASE_CONTAINER = scripts/build-release-container.sh
START_RELEASE_CONTAINER = scripts/start-release-container.sh -i

VERSION = $(shell scripts/version)

Expand All @@ -31,11 +33,17 @@ all: gen compile porter format
help: ## Show this help
@perl -e '$(HELP_FUN)' $(MAKEFILE_LIST)

cb: ##@CONTAINERS build container image
$(BUILD_CONTAINER)
build-debug-container: ##@CONTAINERS build development container image
$(BUILD_DEBUG_CONTAINER)

build-release-container: ##@CONTAINERS build release container image
$(BUILD_RELEASE_CONTAINER)

start-release-container: ##@CONTAINERS start release container
$(START_RELEASE_CONTAINER)

cr: ##@CONTAINERS run or attach to running container
$(START_CONTAINER)
$(START_DEBUG_CONTAINER)

gen: ##@SKYPORT Generate entity files
$(COG) -U -z -d -e -c -o ./src/lib/wm_entity.hrl ./src/lib/wm_entity.hrl.cog
Expand All @@ -61,7 +69,7 @@ release: ##@SKYPORT Build release tar.gz package
sed -i'' 's/SWM_VERSION=.*/SWM_VERSION=$(VERSION)/' _build/default/rel/swm/scripts/swm.env
mkdir -p _build/packages
rm -f _build/packages/swm-$(VERSION).tar.gz
tar --transform 's,^\.,$(VERSION),' -czf _build/packages/swm-$(VERSION).tar.gz _build/default/rel/swm/*
tar --transform 's,^\.,$(VERSION),' -czf _build/packages/swm-$(VERSION).tar.gz -C _build/default/rel/swm .

run-ghead: gen compile porter ##@RUN Run grid head node (1st hier level)
scripts/run-in-shell.sh -g
Expand Down
File renamed without changes.
File renamed without changes.
57 changes: 57 additions & 0 deletions priv/container/release/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
############################################################################
# This Dockerfile can be used to prepare a Docker image that runs swm-core #
# with cloud gate in a release mode a production machine. #
############################################################################

FROM ubuntu:22.04

ARG PYTHON_VERSION=3.10
ARG PYTHON_VERSION_FULL=3.10.14
ARG SWM_VERSION=$SWM_VERSION
ARG SWM_GATE_PACKAGE

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get clean
RUN apt-get update
RUN apt-get update --fix-missing
RUN apt-get install openssl -y
RUN apt-get install openssh-client -y # for host certificates
RUN apt-get install python3 -y # for setup
RUN apt-get install rlwrap -y # for swmctl during setup
RUN apt-get install vim less mc -y # for debug
RUN apt-get install -y supervisor

# Install python version we currently require:
RUN apt-get install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev
RUN apt-get install -y libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
RUN cd /tmp && \
wget https://www.python.org/ftp/python/$PYTHON_VERSION_FULL/Python-$PYTHON_VERSION_FULL.tgz && \
tar -xf Python-$PYTHON_VERSION_FULL.tgz && \
cd Python-$PYTHON_VERSION_FULL && \
./configure --enable-optimizations && \
make -j $(nproc) && \
make altinstall && \
ln -s /usr/local/bin/python$PYTHON_VERSION /usr/local/bin/python && \
ln -s /usr/local/bin/pip$PYTHON_VERSION /usr/local/bin/pip

RUN mkdir -p /opt/swm/spool

COPY $SWM_GATE_PACKAGE /opt/swm/
RUN pip install /opt/swm/$(basename "$SWM_GATE_PACKAGE")

COPY _build/packages/swm-$SWM_VERSION.tar.gz /opt/swm/swm.tar.gz
RUN tar zfx /opt/swm/swm.tar.gz -C /opt/swm/ && ln -s /opt/swm/$SWM_VERSION /opt/swm/current

# To run multiple processes in one container:
COPY priv/container/release/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# swm-core to swm-core connection port:
EXPOSE 10001

# HTTP client to swm-core port:
EXPOSE 8443

VOLUME /opt/swm

CMD ["/usr/bin/supervisord"]
2 changes: 1 addition & 1 deletion priv/prod/README.md → priv/container/release/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Build container image with docker
---------------------------------

```console
scripts/build-prod-container.sh -v 0.2.0
scripts/build-prod-container.sh -v 0.3.0
```

Pull container image with dockerhub
Expand Down
16 changes: 16 additions & 0 deletions priv/container/release/supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[supervisord]
nodaemon=true

[program:swm-core]
command=/bin/sh /opt/swm/current/scripts/run-swm-in-container.sh"
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/swm-core.log

[program:swm-gate]
command=python -m swmcloudgate.app:start
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/swm-cloud-gate.log
31 changes: 0 additions & 31 deletions priv/prod/Dockerfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
DOCKER=docker
IMAGE_NAME=swm-build:24.2

${DOCKER} build -t ${IMAGE_NAME} -f ./priv/build/Dockerfile .
${DOCKER} build -t ${IMAGE_NAME} -f ./priv/container/debug/Dockerfile .
echo "------------------------------------"
${DOCKER} images ${IMAGE_NAME}
38 changes: 0 additions & 38 deletions scripts/build-prod-container.sh

This file was deleted.

34 changes: 34 additions & 0 deletions scripts/build-release-container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#/bin/bash

#set -x

ME=$( readlink -f "$0" )
ROOT_DIR=$( dirname "$( dirname "$ME" )" )
DOCKERFILE=${ROOT_DIR}/priv/container/release/Dockerfile

SWM_VERSION=$($ROOT_DIR/scripts/version)
if [ $SWM_VERSION == "" ]; then
echo "ERROR: cannot fetch version from git, check script scripts/version"
exit 1
fi

echo "Build container image: version=${SWM_VERSION}, dockerfile=${DOCKERFILE}"

DOCKER=docker
IMAGE_NAME=skyport
TAG=latest

GATE_PACKAGE_NAME=swmcloudgate-0.1.0-py3-none-any.whl
GATE_PACKAGE_PATH_OLD=$ROOT_DIR/../swm-cloud-gate/dist/$GATE_PACKAGE_NAME
GATE_PACKAGE_PATH_NEW=_build/packages/$GATE_PACKAGE_NAME
cp -f $GATE_PACKAGE_PATH_OLD $GATE_PACKAGE_DESTINATION_NEW

${DOCKER} build --tag ${IMAGE_NAME}:${SWM_VERSION} \
--build-arg SWM_VERSION=${SWM_VERSION} \
--build-arg SWM_GATE_PACKAGE=$GATE_PACKAGE_PATH_NEW \
--file ${DOCKERFILE} .
${DOCKER} tag ${IMAGE_NAME}:${SWM_VERSION} ${IMAGE_NAME}:${TAG}

echo "------------------------------------"
echo "Created image:"
${DOCKER} images ${IMAGE_NAME}
4 changes: 2 additions & 2 deletions scripts/ping.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
%%! -smp enable

%% To run:
%% export PATH=/opt/swm/0.2.0/erts-12.2/bin:$PATH
%% export SWM_LIB=/opt/swm/current/lib/swm-0.2.0+build.191.ref0586b0d/ebin
%% export PATH=/opt/swm/current/erts-12.2/bin:$PATH
%% export SWM_LIB=/opt/swm/current/lib/swm-0.3.0+build.191.ref0586b0d/ebin
%% ./ping.erl localhost 1001

main([Hostname, Port]) ->
Expand Down
4 changes: 0 additions & 4 deletions scripts/run-swm-foreground.sh

This file was deleted.

16 changes: 16 additions & 0 deletions scripts/run-swm-in-container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
#
# The script should run in container with mounted spool directory
# If spool directory is empty then the script runs setup first
#

SPOOL=/opt/swm/spool

if [ -z "$(ls -A "$SPOOL")" ]; then
echo "The directory '$SPOOL' is empty => initiate setup and exit"
/opt/swm/current/scripts/setup-skyport.linux
exit 0
fi

source /opt/swm/current/scripts/swm.env
/opt/swm/current/bin/swm foreground
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ case "$(uname -s)" in
Darwin*) USER=root;;
esac

JUPUTER_HUB_API_PORT=8081
JUPUTER_HUB_PORT=8000
#JUPUTER_HUB_API_PORT=8081
#JUPUTER_HUB_PORT=8000
RUNNING=$(${DOCKER} inspect -f '{{.State.Running}}' ${CONTAINER_NAME})
if [ "$?" = "1" ]; then
${DOCKER} run\
Expand All @@ -35,12 +35,12 @@ if [ "$?" = "1" ]; then
--workdir ${PWD}\
--tty\
--interactive\
--net bridge\
-p 10000:10000\
-p 10011:10011\
-p 8443:8443\
-p $JUPUTER_HUB_PORT:$JUPUTER_HUB_PORT\
-p $JUPUTER_HUB_API_PORT:$JUPUTER_HUB_API_PORT\
--net host\
#-p 10000:10000\
#-p 10011:10011\
#-p 8443:8443\
#-p $JUPUTER_HUB_PORT:$JUPUTER_HUB_PORT\
#-p $JUPUTER_HUB_API_PORT:$JUPUTER_HUB_API_PORT\
${IMAGE_NAME}\
runuser -u ${USER} /bin/bash
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# if the container already exists, can start a new interactive bash session
# in the container.

set -x

function print_help() {
echo "Usage: $0 [-i] [-s]"
echo " 1) To run production swm-core container in background:"
echo " 1) To run production skyport container in background:"
echo " $0"
echo " 2) To start swm-core container and run bash in it interactively and mount scrips directory:"
echo " 2) To start skyport container and run bash in it interactively and mount scrips directory:"
echo " $0 -i -s"
echo
}
Expand All @@ -24,14 +26,13 @@ done

SWM_API_PORT=10001
SWM_HTTP_PORT=8443
CONTAINER_NAME=swm-core
IMAGE_NAME=swm-core:latest
CONTAINER_NAME=skyport
IMAGE_NAME=skyport:latest
DOCKER=docker
DOCKER_SOCKET=/var/run/docker.sock
X11_SOCKET=/tmp/.X11-unix
HOST_SPOOL=${HOME}/.swm-spool
HOST_SPOOL=${HOME}/.swm/spool

HOST_IP=$(ip addr list ${BRIDGE} |grep "inet " |cut -d' ' -f6|cut -d/ -f1)
RUNNING=$(${DOCKER} inspect -f '{{.State.Running}}' ${CONTAINER_NAME})
INSPECTATION_CODE=$?

Expand All @@ -44,7 +45,7 @@ if [ ! -e ${HOST_SPOOL} ]; then
fi

if [ $INTERACTIVE ]; then
if [ "$INSPECTATION_CODE" = "1" ]; then
if [ "$INSPECTATION_CODE" != "0" ]; then
${DOCKER} run\
${EXTRA_MOUNTS}\
-v /opt/swm/current\
Expand All @@ -56,7 +57,6 @@ if [ $INTERACTIVE ]; then
--name ${CONTAINER_NAME}\
--hostname $(hostname)\
--domainname=openworkload.org\
--add-host host:${HOST_IP}\
--workdir ${PWD}\
--tty\
--interactive\
Expand Down
2 changes: 0 additions & 2 deletions scripts/version
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ git status 1>/dev/null 2>/dev/null
if [ $? == "0" ]; then
VERSION=$(git describe --tags --abbrev=0 --always)
VERSION=${VERSION:1:${#VERSION}}
else
VERSION=0.2.0 #TODO: update automatically
fi
echo -n ${VERSION}

0 comments on commit 271972a

Please sign in to comment.