github.com/puellanivis/breton@v0.2.16/masher/godoc (about) 1 #!/bin/bash 2 3 if ! which docker > /dev/null 2>&1 ; then 4 echo Please install docker >&2 5 exit 1 6 fi 7 8 if [[ -z ${MASHER_ROOT} ]]; then 9 echo Please setup build environment >&2 10 exit 1 11 fi 12 13 if [[ -n ${bamboo_DOCKER_REGISTRY} ]]; then 14 DOCKER_REGISTRY=${bamboo_DOCKER_REGISTRY} 15 PROD="true" 16 fi 17 18 if [[ -n ${bamboo_docker_registry} ]]; then 19 DOCKER_REGISTRY=${bamboo_docker_registry} 20 PROD="true" 21 fi 22 23 PROJECT=${PWD##*/} 24 REGISTRY=${DOCKER_REGISTRY} 25 26 RUNAS="${UID}" 27 if [[ -n ${DOCKER_MACHINE_NAME} ]]; then 28 RUNAS=1000 29 fi 30 31 PORT="8080" 32 33 while [[ $# -gt 0 ]]; do 34 key="$1" 35 val="${key#*=}" 36 37 case $key in 38 --image=*) 39 IMAGE="$val" 40 ;; 41 --registry=*) 42 REGISTRY="$val" 43 ;; 44 --port=*) 45 PORT="$val" 46 ;; 47 48 --play) 49 PLAY="-play" 50 ;; 51 --index) 52 INDEX="-index" 53 ;; 54 55 --shell) 56 ESCAPE="--shell" 57 FLAGS="-it" 58 ;; 59 --) 60 shift 61 break 62 ;; 63 --*) 64 echo "unknown flag $1" >&2 65 exit 1 66 ;; 67 *) 68 break 69 ;; 70 esac 71 shift 72 done 73 74 if [[ -n $REGISTRY ]]; then 75 REGISTRY="${REGISTRY%/}/" 76 fi 77 78 if [[ -z $IMAGE ]]; then 79 IMAGE="${USER}/masher" 80 81 if ! which git > /dev/null 2>&1 ; then 82 echo Please install git >&2 83 exit 1 84 fi 85 86 HEAD=$(git symbolic-ref HEAD 2>> /dev/null || git rev-parse HEAD) 87 BRANCH=$(basename ${HEAD}) 88 89 IMAGE="${IMAGE}:latest" 90 fi 91 92 MASHER_ROOT=${MASHER_ROOT%/} 93 PRJPATH="${PWD##*${MASHER_ROOT}/}" 94 BASE="${PWD%$PRJPATH}" 95 96 if [[ -z $BASE ]] ; then 97 BASE="${PWD}" 98 PRJPATH="" 99 fi 100 101 case $BASE in 102 */) ;; 103 *) BASE="$BASE/" 104 esac 105 106 NAME="masher-godoc" 107 108 echo "NAME='$NAME'" 109 echo "BASE='$BASE'" 110 echo "PRJPATH='$PRJPATH'" 111 echo "PROJECT='$PROJECT'" 112 echo "BRANCH='$BRANCH'" 113 114 TIMESTAMP=$(date -u +"%Y%m%d.%H%M%S") 115 116 echo Using image: $IMAGE 117 echo godoc $ESCAPE $PLAY $INDEX --timestamp=$TIMESTAMP $@ 118 119 DOCKER_VERSION="$(docker --version)" 120 DOCKER_VERSION=${DOCKER_VERSION%%,*} 121 DOCKER_VERSION=${DOCKER_VERSION##* } 122 echo Found Docker Version: $DOCKER_VERSION 123 124 case $DOCKER_VERSION in 125 1.?|1.?.*) ;; 126 *) 127 READONLY="--read-only" 128 TMPFS="--tmpfs /tmp:size=1G,exec" 129 ;; 130 esac 131 132 [[ -n $MASHER_PREFIX ]] && PREFIX="/${MASHER_PREFIX#/}" 133 SRCROOT="/go/src${PREFIX}/${MASHER_ROOT%/}" 134 echo "SRCROOT='$SRCROOT'" 135 136 [[ -n $MASHER_LIBS ]] || MASHER_LIBS="lib" 137 LIBS="" 138 139 SAVE_IFS=$IFS 140 IFS=':' 141 for p in $MASHER_LIBS; do 142 l=$(echo $p | cut -d= -f1) 143 t=$(echo $p | cut -d= -f2) 144 145 [[ -n $t ]] || t="$l" 146 147 l="${BASE}$l" 148 t="/go/src/$t" 149 150 if [[ -d $l ]]; then 151 echo "$l => $t" 152 LIBS="$LIBS --volume $l:$t" 153 else 154 echo "couldn’t find $l!" >&2 155 exit 1 156 fi 157 done 158 IFS=$SAVE_IFS 159 160 NET="-net host" 161 case ${OSTYPE} in 162 linux*) 163 ;; 164 *) 165 NET="-p $PORT:$PORT" 166 ;; 167 esac 168 169 exec docker run ${FLAGS} --rm \ 170 -e "GOPATH=$SRCROOT" \ 171 --name "${NAME}" \ 172 -p "$PORT:8080" \ 173 ${READONLY} \ 174 ${TMPFS} \ 175 --user "${RUNAS}" \ 176 --volume "${BASE}:${SRCROOT}/src:ro" \ 177 $LIBS \ 178 --workdir "${SRCROOT}" \ 179 ${IMAGE} \ 180 godoc -goroot ${SRCROOT} -http ":$PORT" $INDEX $PLAY "$@" || exit 1