# /bin/bash function main() { set -e set -u thispkg="${PWD##*$GOPATH/}" thisexec="$(basename "$thispkg")" function cleanup() { rm "$dockfile" } trap cleanup EXIT if [ "$(uname -s)" == "Darwin" ]; then dockfile="$(mktemp -t . tmpXXXX | tail -n 1)" echo "$dockfile" else dockfile="$(mktemp -p . tmpXXXX)" fi if [ -n "$(ls ./*.go 2>/dev/null )" ]; then echo "PACKING GO..." go_dockerfiles elif [ -n "$(ls ./*.rb 2> /dev/null)" ]; then echo "PACKING RUBY..." rb_dockerfiles elif [ -n "$(ls ./*.py 2>/dev/null )" ]; then if [ "$(ls ./main.py 2>/dev/null )" != "" ] && [[ "$(head -n 1 ./main.py)" == *python2* ]]; then echo "PACKING PYTHON2..." py2_dockerfiles else echo "PACKING PYTHON3..." py3_dockerfiles fi fi if [ "$#" -lt 1 ]; then args="-t ${thispkg##*/}:latest" else args="$*" fi docker build $args -f "$dockfile" . } function py3_dockerfiles() { echo "PY3" img="${1:-python}" if [[ "$(ls ./*req*.txt 2>/dev/null)" == "" ]]; then echo "NEED requirements.txt" exit 1 fi reqFile="$(ls ./*req*.txt | head -n 1)" if [[ "$img" == "python" ]]; then from="python:3.7.0-alpine3.8" elif [[ "$img" == "pypy" ]]; then from="pypy:3-6.0.0-slim-jessie" else echo "ERR py3_dockerfile can be '', 'python', or 'pypy', not ${img}" fi if [ "$(uname -s)" == "Darwin" ]; then from="${from}" fi f=() f+=('') f+=('FROM alpine as certs') f+=('RUN apk update && apk add --no-cache ca-certificates') f+=('') f+=('#========================') f+=('') f+=('FROM '"${from}"'') f+=('WORKDIR /main/') f+=('COPY --from=certs /etc/ssl/certs /etc/ssl/certs') f+=('') if [[ "$img" == "python" ]]; then echo "" #f+=('RUN apk add linux-headers') #f+=('RUN apk add g++ gcc make') else echo "" #f+=('RUN apt-get update') #f+=('RUN apt-get install -y linux-headers') #f+=('RUN apt-get install -y g++ gcc make') fi f+=('') f+=('COPY ./'"$(basename ${reqFile})"' /main/req.txt') f+=('RUN pip install -r /main/req.txt') f+=('') f+=('COPY . .') if [[ "$img" == "python" ]]; then f+=('ENTRYPOINT ["python3", "/main/main.py"]') else f+=('ENTRYPOINT ["pypy3", "/main/main.py"]') fi f+=('') DOCKERFILE="" for i in "${f[@]}"; do DOCKERFILE+="${i}"$'\n' done echo "$DOCKERFILE" > "$dockfile" } function py2_dockerfiles() { echo "PY2" if [[ "$(ls ./*req*.txt 2>/dev/null)" == "" ]]; then echo "NEED requirements.txt" exit 1 fi reqFile="$(ls ./*req*.txt | head -n 1)" from="python:2.7.15-alpine3.8" if [ "$(uname -s)" == "Darwin" ]; then from="${from}" fi f=() f+=('') f+=('FROM alpine as certs') f+=('RUN apk update && apk add --no-cache ca-certificates') f+=('') f+=('#========================') f+=('') f+=('FROM '"${from}"'') f+=('WORKDIR /main/') f+=('COPY --from=certs /etc/ssl/certs /etc/ssl/certs') f+=('COPY ./'"$(basename ${reqFile})"' /main/req.txt') f+=('RUN pip2.7 install -r /main/req.txt') f+=('COPY . .') f+=('ENTRYPOINT ["python2.7", "/main/main.py"]') f+=('') DOCKERFILE="" for i in "${f[@]}"; do DOCKERFILE+="${i}"$'\n' done echo "$DOCKERFILE" > "$dockfile" } function go_dockerfiles() { from="golang:1.10-alpine" if [ "$(uname -s)" == "Darwin" ]; then from="registry-app.eng.qops.net:5001/imported/alpine/golang:1.10-alpine" fi DOCKERFILE=' FROM '"$from"' as builder MAINTAINER breel@qualtrics.com RUN apk add --no-cache git WORKDIR /go/'"$thispkg"' COPY . . RUN /bin/sh -c "CGO_ENABLED=0 go build -o /go/bin/'"$thisexec"' -a -installsuffix cgo" FROM alpine as certs RUN apk update && apk add --no-cache ca-certificates #======================== FROM busybox:glibc COPY --from=builder /go/'"$thispkg"' /main COPY --from=builder /go/bin /main RUN mkdir -p /var/log WORKDIR /main ENV GOPATH="" ENV MNT="/mnt/" #COPY /etc/ssl/certs /etc/ssl COPY --from=certs /etc/ssl/certs /etc/ssl/certs ENTRYPOINT ["/main/'"$thisexec"'"] CMD [] ' echo "$DOCKERFILE" > "$dockfile" } function rb_dockerfiles() { from="ruby:2.6.2-alpine3.9" if [ "$(uname -s)" == "Darwin" ]; then from="registry-app.eng.qops.net:5001/imported/alpine/ruby:alpine" fi cp=( $(find . -mindepth 1 -maxdepth 1 -type f | grep -v "$dockfile" | grep -v '\.[^\.]*\.sw[a-z]$') ) cpd=( $(find . -mindepth 1 -maxdepth 1 -type d | grep -v \\.git || true) ) gf="" if [ -e ./Gemfile ]; then gf=' COPY ./Gemfile /main/Gemfile RUN BUNDLE_FORCE_RUBY_PLATFORM=1 bundler install --gemfile=/main/Gemfile ' fi main="$(find . -maxdepth 1 -name main.rb || find . -maxdepth 1 -name "*.rb" | head -n 1)" DOCKERFILE=' FROM alpine as certs RUN apk update && apk add --no-cache ca-certificates #======================== FROM '"$from"' MAINTAINER breel@qualtrics.com RUN mkdir -p /var/log WORKDIR /main ENV HOME=/main ENV MNT=/mnt/ COPY --from=certs /etc/ssl/certs /etc/ssl/certs RUN apk add --no-cache ruby ruby-bundler alpine-sdk '"$gf"' COPY '"${cp[@]}"' /main/ '"$(for dir in ${cpd[@]}; do printf "COPY $dir /main/${dir##*/}\n"; done)"' ENTRYPOINT ["ruby", "/main/'"${main##*/}"'"] CMD [] ' echo "$DOCKERFILE" > "$dockfile" } main $@