|
| 1 | +#/bin/bash -e |
| 2 | + |
| 3 | +function usage { |
| 4 | + echo "Usage: $0 [OPTIONS]" |
| 5 | + echo " -c: command --> default [build]" |
| 6 | + echo " | delete --> Delete the container, Dockerfile isn't mandatory" |
| 7 | + echo " | build --> Build the container, Dockerfile is mandatory" |
| 8 | + echo " | compile --> Builds the container, then compiles it. Dockerfile is mandatory" |
| 9 | + echo "" |
| 10 | + echo " -n: name: Docker container name --> default [sunshine]" |
| 11 | + echo " --> all: Build/Compile/Delete all available docker containers" |
| 12 | + echo " -f: Dockerfile: The name of the docker file" |
| 13 | +} |
| 14 | + |
| 15 | +# Attempt to turn relative paths into absolute paths |
| 16 | +function absolute_path() { |
| 17 | + RELATIVE_PATH=$1 |
| 18 | + if which realpath >/dev/null 2>/dev/null |
| 19 | + then |
| 20 | + RELATIVE_PATH=$(realpath $RELATIVE_PATH) |
| 21 | + else |
| 22 | + echo "Warning: realpath is not installed on your system, ensure [$1] is absolute" |
| 23 | + fi |
| 24 | + |
| 25 | + RETURN=$RELATIVE_PATH |
| 26 | +} |
| 27 | + |
| 28 | +CONTAINER_NAME=sunshine |
| 29 | +COMMAND=BUILD |
| 30 | + |
| 31 | +function build_container() { |
| 32 | + CONTAINER_NAME=$1 |
| 33 | + DOCKER_FILE=$2 |
| 34 | + |
| 35 | + if [ ! -f "$DOCKER_FILE" ] |
| 36 | + then |
| 37 | + echo "Error: $DOCKER_FILE doesn't exist" |
| 38 | + exit 7 |
| 39 | + fi |
| 40 | + |
| 41 | + echo "docker build . -t $CONTAINER_NAME -f $DOCKER_FILE" |
| 42 | + docker build . -t "$CONTAINER_NAME" -f "$DOCKER_FILE" |
| 43 | +} |
| 44 | + |
| 45 | +function delete() { |
| 46 | + CONTAINER_NAME_UPPER=$(echo "$CONTAINER_NAME" | tr '[:lower:]' '[:upper:]') |
| 47 | + if [ "$CONTAINER_NAME_UPPER" == "ALL" ] |
| 48 | + then |
| 49 | + shopt -s nullglob |
| 50 | + for file in $(find . -maxdepth 1 -iname "Dockerfile-*" -type f) |
| 51 | + do |
| 52 | + CURRENT_CONTAINER="sunshine-$(echo $file | cut -c 14-)" |
| 53 | + |
| 54 | + if docker inspect "$CURRENT_CONTAINER" > /dev/null 2> /dev/null |
| 55 | + then |
| 56 | + echo "docker rmi $CURRENT_CONTAINER" |
| 57 | + docker rmi "$CURRENT_CONTAINER" |
| 58 | + fi |
| 59 | + done |
| 60 | + shopt -u nullglob #revert nullglob back to it's normal default state |
| 61 | + else |
| 62 | + if docker inspect "$CONTAINER_NAME" > /dev/null 2> /dev/null |
| 63 | + then |
| 64 | + echo "docker rmi $CONTAINER_NAME" |
| 65 | + docker rmi $CONTAINER_NAME |
| 66 | + fi |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +function build() { |
| 71 | + CONTAINER_NAME_UPPER=$(echo "$CONTAINER_NAME" | tr '[:lower:]' '[:upper:]') |
| 72 | + if [ "$CONTAINER_NAME_UPPER" == "ALL" ] |
| 73 | + then |
| 74 | + shopt -s nullglob |
| 75 | + for file in $(find . -maxdepth 1 -iname "Dockerfile-*" -type f) |
| 76 | + do |
| 77 | + CURRENT_CONTAINER="sunshine-$(echo $file | cut -c 14-)" |
| 78 | + build_container "$CURRENT_CONTAINER" "$file" |
| 79 | + done |
| 80 | + shopt -u nullglob #revert nullglob back to it's normal default state |
| 81 | + else |
| 82 | + if [[ -z "$DOCKER_FILE" ]] |
| 83 | + then |
| 84 | + echo "Error: if container name isn't equal to 'all', you need to specify the Dockerfile" |
| 85 | + exit 6 |
| 86 | + fi |
| 87 | + |
| 88 | + build_container "$CONTAINER_NAME" "$DOCKER_FILE" |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +function abort() { |
| 93 | + echo "$1" |
| 94 | + exit 10 |
| 95 | +} |
| 96 | + |
| 97 | +function compile() { |
| 98 | + CONTAINER_NAME_UPPER=$(echo "$CONTAINER_NAME" | tr '[:lower:]' '[:upper:]') |
| 99 | + if [ "$CONTAINER_NAME_UPPER" == "ALL" ] |
| 100 | + then |
| 101 | + shopt -s nullglob |
| 102 | + |
| 103 | + # If any docker container doesn't exist, we cannot compile all of them |
| 104 | + for file in $(find . -maxdepth 1 -iname "Dockerfile-*" -type f) |
| 105 | + do |
| 106 | + CURRENT_CONTAINER="sunshine-$(echo $file | cut -c 14-)" |
| 107 | + |
| 108 | + # If container doesn't exist --> abort. |
| 109 | + docker inspect "$CURRENT_CONTAINER" > /dev/null 2> /dev/null || abort "Error: container image [$CURRENT_CONTAINER] doesn't exist" |
| 110 | + done |
| 111 | + |
| 112 | + for file in $(find . -maxdepth 1 -iname "Dockerfile-*" -type f) |
| 113 | + do |
| 114 | + CURRENT_CONTAINER="sunshine-$(echo $file | cut -c 14-)" |
| 115 | + |
| 116 | + echo "$PWD/build-sunshine.sh -p -n $CURRENT_CONTAINER" |
| 117 | + "$PWD/build-sunshine.sh" -p -n "$CURRENT_CONTAINER" |
| 118 | + done |
| 119 | + shopt -u nullglob #revert nullglob back to it's normal default state |
| 120 | + else |
| 121 | + # If container exists |
| 122 | + if docker inspect "$CURRENT_CONTAINER" > /dev/null 2> /dev/null |
| 123 | + then |
| 124 | + echo "$PWD/build-sunshine.sh -p -n $CONTAINER_NAME" |
| 125 | + "$PWD/build-sunshine.sh" -p -n "$CONTAINER_NAME" |
| 126 | + else |
| 127 | + echo "Error: container image [$CONTAINER_NAME] doesn't exist" |
| 128 | + exit 9 |
| 129 | + fi |
| 130 | + fi |
| 131 | +} |
| 132 | + |
| 133 | +while getopts ":c:hn:f:" arg; do |
| 134 | + case ${arg} in |
| 135 | + c) |
| 136 | + COMMAND=$(echo $OPTARG | tr '[:lower:]' '[:upper:]') |
| 137 | + ;; |
| 138 | + n) |
| 139 | + echo "Container name: $OPTARG" |
| 140 | + CONTAINER_NAME="$OPTARG" |
| 141 | + ;; |
| 142 | + f) |
| 143 | + echo "Using Dockerfile [$OPTARG]" |
| 144 | + DOCKER_FILE="$OPTARG" |
| 145 | + ;; |
| 146 | + h) |
| 147 | + usage |
| 148 | + exit 0 |
| 149 | + ;; |
| 150 | + esac |
| 151 | +done |
| 152 | + |
| 153 | +echo "$0 set to $(echo $COMMAND | tr '[:upper:]' '[:lower:]')" |
| 154 | + |
| 155 | +if [[ "$COMMAND" == "BUILD" ]] |
| 156 | +then |
| 157 | + echo "Start building..." |
| 158 | + delete |
| 159 | + build |
| 160 | + echo "Done." |
| 161 | +elif [[ "$COMMAND" == "COMPILE" ]] |
| 162 | +then |
| 163 | + echo "Start compiling..." |
| 164 | + compile |
| 165 | + echo "Done." |
| 166 | +elif [[ "$COMMAND" == "DELETE" ]] |
| 167 | +then |
| 168 | + echo "Start deleting..." |
| 169 | + delete |
| 170 | + echo "Done." |
| 171 | +else |
| 172 | + echo "Unknown command [$(echo $COMMAND | tr '[:upper:]' '[:lower:]')]" |
| 173 | + exit 4 |
| 174 | +fi |
0 commit comments