Skip to content

Commit

Permalink
Use custom innerloop scripts for java and nodejs (redhat-developer#27)
Browse files Browse the repository at this point in the history
* preparation for language specific scripts

* add dev-* scripts for nodejs and java

* remove ODO_DEPLOYMENT_BACKUP_DIR

* Remove custom dev-* scripts for java images

assemble and run scripts in most java images are already "innerloop friendly"

* getlanguage.go: add constants for env variables

* nodejs: enable debug by default and add hot reload support

* add dev-assemble and dev-run scripts for Java with remote debugging enabled

* small comment fix

* fix rebase errors
  • Loading branch information
kadel authored and openshift-merge-robot committed Sep 3, 2019
1 parent a3afbfd commit bf47608
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 10 deletions.
26 changes: 18 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
# This is an "initContainer" image used by odo to inject required tools for odo to work properly.
#

# Build SupervisordD
FROM registry.svc.ci.openshift.org/openshift/release:golang-1.11 AS supervisordbuilder
WORKDIR /go/src/github.com/ochinchina/supervisord
# Build Go stuff (SupervisorD and getlanguage)

FROM registry.svc.ci.openshift.org/openshift/release:golang-1.11 AS gobuilder

RUN mkdir -p /go/src/github.com/ochinchina/supervisord
COPY vendor/supervisord /go/src/github.com/ochinchina/supervisord
ADD vendor/supervisord /go/src/github.com/ochinchina/supervisord
WORKDIR /go/src/github.com/ochinchina/supervisord
RUN go build -o /tmp/supervisord

RUN mkdir -p /go/src/github.com/openshift/odo-supervisord-image
ADD get-language /go/src/github.com/openshift/odo-supervisord-image/get-language/
WORKDIR /go/src/github.com/openshift/odo-supervisord-image/get-language
RUN go build -o /tmp/getlanguage getlanguage.go


# Build dumb-init
FROM registry.access.redhat.com/ubi7/ubi AS dumbinitbuilder
WORKDIR /tmp/dumb-init-src
Expand All @@ -31,16 +39,18 @@ RUN chmod +x ${ODO_TOOLS_DIR}/bin/dumb-init
# SupervisorD
RUN mkdir -p ${ODO_TOOLS_DIR}/conf ${ODO_TOOLS_DIR}/bin
COPY supervisor.conf ${ODO_TOOLS_DIR}/conf/
COPY --from=supervisordbuilder /tmp/supervisord ${ODO_TOOLS_DIR}/bin/supervisord
COPY --from=gobuilder /tmp/supervisord ${ODO_TOOLS_DIR}/bin/supervisord

# wrapper scrips
# Wrapper scripts
COPY assemble-and-restart ${ODO_TOOLS_DIR}/bin
COPY run ${ODO_TOOLS_DIR}/bin
COPY s2i-setup ${ODO_TOOLS_DIR}/bin
COPY setup-and-run ${ODO_TOOLS_DIR}/bin
COPY vendor/fix-permissions /usr/bin/fix-permissions

COPY language-scripts ${ODO_TOOLS_DIR}/language-scripts/
COPY --from=gobuilder /tmp/getlanguage ${ODO_TOOLS_DIR}/bin/getlanguage

RUN chgrp -R 0 ${ODO_TOOLS_DIR} && \
chmod -R g+rwX ${ODO_TOOLS_DIR} && \
chmod -R 666 ${ODO_TOOLS_DIR}/conf/* && \
chmod 775 ${ODO_TOOLS_DIR}/bin/supervisord
chmod -R 666 ${ODO_TOOLS_DIR}/conf/*
16 changes: 14 additions & 2 deletions assemble-and-restart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
set -x
set -eo pipefail

export ODO_UTILS_DIR=/opt/odo
export ODO_IMAGE_MAPPINGS_FILE=${ODO_UTILS_DIR}/language-scripts/image-mappings.json

IMAGE_LANG=`${ODO_UTILS_DIR}/bin/getlanguage`

if [ ! -z "${IMAGE_LANG}" ]; then
${ODO_UTILS_DIR}/language-scripts/${IMAGE_LANG}/dev-assemble
${ODO_UTILS_DIR}/bin/supervisord ctl stop run; ${ODO_UTILS_DIR}/bin/supervisord ctl start run
exit 0
fi


# If WorkingDir is injected as an env from odo and destination path is not equal to WorkingDir(if not copying directly to working dir) and deployment dir is not working dir
if [ ! -z "${ODO_S2I_WORKING_DIR}" ] && [ "${ODO_S2I_SRC_BIN_PATH}" != "${ODO_S2I_WORKING_DIR}" ]; then

Expand Down Expand Up @@ -66,5 +78,5 @@ fi


# Restart supervisord in order to actualy run the application
# This is a dumb way to start as supervisord does not have a restart function
/opt/odo/bin/supervisord ctl stop run; /opt/odo/bin/supervisord ctl start run
# This is a dumb way to restart as supervisord does not have a restart function
${ODO_UTILS_DIR}/bin/supervisord ctl stop run; ${ODO_UTILS_DIR}/bin/supervisord ctl start run
68 changes: 68 additions & 0 deletions get-language/getlanguage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
)

// imageMappingsFileEnv is env variable that points to image-mappings.json file
const imageMappingsFileEnv string = "ODO_IMAGE_MAPPINGS_FILE"

// builderImageNameEnv in env variable with name of the image ("name" label on the image)
const builderImageNameEnv string = "ODO_S2I_BUILDER_IMG"

// Lang stores list of image names associated to language
type Lang struct {
Language string
Images []string
}

// ImageMappings hold mappings of language to image name
// the structure represents image-mappings.json file
type ImageMappings []Lang

// GetLanguage returns language name name for associated to imageName
func (im ImageMappings) GetLanguage(imageName string) string {
for _, lang := range im {
for _, image := range lang.Images {
if image == imageName {
return lang.Language
}
}
}
return ""
}

func main() {
// json file where all the mappings of language to image name is stored
// see ImageMappings for json structure
imageMappingFile := os.Getenv(imageMappingsFileEnv)

// name of the current image, we will try to find what language this is based on ImageMappings
imageName := os.Getenv(builderImageNameEnv)

var imageMappings ImageMappings

file, err := os.Open(imageMappingFile)
defer file.Close()
if err != nil {
log.Fatalln(err)
}

mappings, err := ioutil.ReadAll(file)
if err != nil {
log.Fatalln(err)
}

err = json.Unmarshal(mappings, &imageMappings)
if err != nil {
log.Fatalln(err)
}

lang := imageMappings.GetLanguage(imageName)

fmt.Print(lang)
}
18 changes: 18 additions & 0 deletions language-scripts/image-mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"language": "nodejs",
"images": [
"rhscl/nodejs-8-rhel7",
"rhoar-nodejs/nodejs-8",
"rhoar-nodejs/nodejs-10"
]
},
{
"language": "java",
"images": [
"redhat-openjdk-18/openjdk18-openshift",
"openjdk/openjdk-11-rhel8",
"openjdk/openjdk-11-rhel7"
]
}
]
6 changes: 6 additions & 0 deletions language-scripts/java/dev-assemble
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e
set -x

# run normal assemble script
exec "${ODO_S2I_SCRIPTS_URL}/assemble"
15 changes: 15 additions & 0 deletions language-scripts/java/dev-run
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e
set -x

DEBUG_PORT="${DEBUG_PORT:=49200}"

# DEBUG_MODE is true by defualt, which means that the application will be started with remote debugging enabled.
DEBUG_MODE="${DEBUG_MODE:=true}"

if [ $DEBUG_MODE != "false" ]; then
export JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=${DEBUG_PORT},suspend=n"
fi

# run normal run s2i script
exec "${ODO_S2I_SCRIPTS_URL}/run"
78 changes: 78 additions & 0 deletions language-scripts/nodejs/dev-assemble
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash
set -e
set -x

cd /tmp/src

NODE_ENV="${NODE_ENV:=development}"

REFRESH_WORKING_DIR=/tmp/refresh/
PACKAGE_SHA_FILE=$REFRESH_WORKING_DIR/package.json.sha1

mkdir -p $REFRESH_WORKING_DIR


if [ ! -z $HTTP_PROXY ]; then
echo "---> Setting npm http proxy to $HTTP_PROXY"
npm config set proxy $HTTP_PROXY
fi

if [ ! -z $http_proxy ]; then
echo "---> Setting npm http proxy to $http_proxy"
npm config set proxy $http_proxy
fi

if [ ! -z $HTTPS_PROXY ]; then
echo "---> Setting npm https proxy to $HTTPS_PROXY"
npm config set https-proxy $HTTPS_PROXY
fi

if [ ! -z $https_proxy ]; then
echo "---> Setting npm https proxy to $https_proxy"
npm config set https-proxy $https_proxy
fi

if [ ! -z $NO_PROXY ]; then
echo "---> Setting npm no proxy config to $NO_PROXY"
npm config set no-proxy $NO_PROXY
fi

if [ ! -z $no_proxy ]; then
echo "---> Setting npm no proxy config to $no_proxy"
npm config set no-proxy $no_proxy
fi

# Change the npm registry mirror if provided
if [ ! -z "$NPM_MIRROR" ]; then
echo "---> Setting the npm package mirror to $NPM_MIRROR"
npm config set registry $NPM_MIRROR
fi

echo "---> Building your Node application from source"


# don't do anything if package.json is the same as the last time this script was executed
if [ -f $PACKAGE_SHA_FILE ]; then
out=$(sha1sum -c $PACKAGE_SHA_FILE || true)
if [ "$out" == "package.json: OK" ]; then
echo "---> package.json is the same as last time, no changes needed."
exit 0
fi
fi

echo "---> package.json modified"
sha1sum package.json > $PACKAGE_SHA_FILE


if [ ! -z "$YARN_ENABLED" ]; then
echo "---> Using 'yarn install' with YARN_ARGS"
npx yarn install $YARN_ARGS
else
echo "---> Installing dependencies"
echo "---> Using 'npm install'"
npm install

#do not fail when there is no build script
echo "---> Building in development mode"
npm run build --if-present
fi
32 changes: 32 additions & 0 deletions language-scripts/nodejs/dev-run
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
set -e
set -x

cd /tmp/src

NODE_ENV="${NODE_ENV:=development}"
DEBUG_PORT="${DEBUG_PORT:=9229}"

# DEBUG_MODE is true by defualt, which means that the application will be started with remote debugging enabled.
DEBUG_MODE="${DEBUG_MODE:=true}"

export GIT_COMMITTER_NAME="unknown"
export and GIT_COMMITTER_EMAIL="unknown@localhost"
git --version

echo -e "Using Node.js version: $(node --version)"
echo -e "Environment:"
echo -e " NODE_ENV=${NODE_ENV}"
echo -e " DEBUG_PORT=${DEBUG_PORT}"
echo -e " DEBUG_MODE=${DEBUG_MODE}"
echo -e "Running as user $(id)"

echo "Launching via npm..."

if [ $DEBUG_MODE == "false" ]; then
exec npm run -d start
else
# TODO: if users app doesn't have nodemon as a dev dependency it will take a long time to start it via npx
exec npx nodemon --inspect=$DEBUG_PORT
fi

9 changes: 9 additions & 0 deletions run
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
set -x
set -eo pipefail

export ODO_UTILS_DIR=/opt/odo

export ODO_IMAGE_MAPPINGS_FILE=${ODO_UTILS_DIR}/language-scripts/image-mappings.json

IMAGE_LANG=`${ODO_UTILS_DIR}/bin/getlanguage`

if [ ! -z "${IMAGE_LANG}" ]; then
exec ${ODO_UTILS_DIR}/language-scripts/${IMAGE_LANG}/dev-run
fi

# We now run the run script. If there is a custom one written in the
# source files, we use that instead.
Expand Down

0 comments on commit bf47608

Please sign in to comment.