diff --git a/README.md b/README.md index cb296e8..58f225c 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ Builds `.opkg` files and a `Packages.gz` ```bash docker run -ti --rm \ + -e GOSU_USER=`id -u`:`id -g` \ -e PACKAGES="transmission openvpn node node-npm" \ -v /path/to/custom-packages-feed:/feeds/mypackages:z \ -v /path/to/output-dir:/output:z \ @@ -88,6 +89,7 @@ Builds all the target images. ```bash docker run -ti --rm \ + -e GOSU_USER=`id -u`:`id -g` \ -e PACKAGES="-luci transmission openvpn node node-npm" \ -e CUSTOM_FEEDS="mypackages" \ -v /path/to/custom-packages-feed:/feeds/mypackages:z \ @@ -97,6 +99,63 @@ docker run -ti --rm \ ``` +## Development + +Want to build your own images or help us out? + +``` +git clone https://github.com/cusspvz/openwrt-builder.docker openwrt-builder +cd openwrt-builder/ +DOCKER_USERNAME=yourusername ./docker-images-builder.sh +``` + +NOTE: The `DOCKER_USERNAME` variable is required so the builder can check which +images are already built and available on the registry. It also sets the image +base. + +### `docker-images-builder.sh` Environment Variables + +Usage example: `DOCKER_USE_SUDO=1 FORCE=1 ./docker-images-builder.sh` + +#### `VERBOSIFY` +Description: Shows all the underlaying command's outputs + +Example: `VERBOSIFY=1` + +#### `TARGETS` +Description: List of versions and targets that are meant to be built by the +image builder. Each list item should contain each version and target concatened +with an underscore. [ `$VERSION_$TARGET` ] +Default: Defaults to all versions and targets + +Example: `TARGETS="18.0.1_omap-generic 18.0.1_brcm2708-brcm2708"` + +#### `FORCE` +Description: This script checks if the images already exists on the registry. +If this environment variable is set, it will always build and push all the +version's targets. + +This should be used whenever there's a change on the base image. + +Example: `FORCE=1` + +#### `DOCKER_USE_SUDO` +Description: If you need `sudo` to run docker on your system, this should be +set. +Example: `DOCKER_USE_SUDO=1` + +#### `DOCKER` +Description: This is needed in case you need to change your docker binary path. +Example: `DOCKER=/path/to/docker` + +#### `DOCKER_USERNAME` +Description: Sets the docker username in order to check if the image is already +present on the registry. This also is used to prefix the image name. + +#### `DOCKER_IMAGE` +Description: Allows to change the docker image name. +Note: If this needs to be altered, you still have to set `DOCKER_USERNAME` so +the caching check works properly. Unless you're setting `FORCE`. ## Donate diff --git a/base-image/Dockerfile b/base-image/Dockerfile index 63d174d..e004cff 100644 --- a/base-image/Dockerfile +++ b/base-image/Dockerfile @@ -1,4 +1,4 @@ -FROM debian:8 +FROM debian:8-slim LABEL MAINTAINER "Jose Moreira " RUN echo "deb http://ftp.debian.org/debian jessie-backports main" >> /etc/apt/sources.list \ diff --git a/base-image/entrypoint.sh b/base-image/entrypoint.sh index 296a2dc..f31ad3d 100644 --- a/base-image/entrypoint.sh +++ b/base-image/entrypoint.sh @@ -1,15 +1,15 @@ #!/bin/sh -chown -R $GOSU_USER /src - # If GOSU_USER environment variable set to something other than 0:0 (root:root), # become user:group set within and exec command passed in args if [ "$GOSU_USER" != "0:0" ]; then + chown -R $GOSU_USER /src + # make sure a valid user exists in /etc/passwd if grep "^builder:" /etc/passwd; then sed -i "/^builder:/d" /etc/passwd fi - echo "builder:x:$GOSU_USER:LEDE builder:/lede:/bin/bash" >> /etc/passwd + echo "builder:x:$GOSU_USER:OpenWRT builder:/src:/bin/bash" >> /etc/passwd exec gosu $GOSU_USER "$@" fi diff --git a/builder.sh b/builder.sh deleted file mode 100755 index cc3367f..0000000 --- a/builder.sh +++ /dev/null @@ -1,101 +0,0 @@ -#!/bin/bash - -DOCKER_USE_SUDO=${DOCKER_USE_SUDO:-0} -DOCKER_USERNAME=${DOCKER_USERNAME:-cusspvz} -DOCKER_PASSWORD=${DOCKER_PASSWORD} -DOCKER_IMAGE=${DOCKER_IMAGE:-$DOCKER_USERNAME/openwrt-builder} - -DOCKER="${DOCKER:-docker}" - -FORCE=${FORCE:-0} -CLOSE_EXEC="/dev/null" -# CLOSE_EXEC=">/dev/null 2>&1" -if [ ! -z DEBUG ]; then - CLOSE_EXEC="/dev/null" -fi - -function safeexit () { - echo "$1" - [ ! -z $2 ] && exit $2 || exit 0 -} - -if [ -z $DOCKER_USERNAME ] || [ -z $DOCKER_PASSWORD ]; then - safeexit "Please make sure that you've specified the DOCKER_USERNAME and DOCKER_PASSWORD envs" 1 -fi - - -if [ "$DOCKER_USE_SUDO" != "0" ]; then - DOCKER="sudo ${DOCKER}" -fi - -function docker_tag_exists() { - [ "$FORCE" != "0" ] && return 1; - curl --silent -f -lSL https://index.docker.io/v1/repositories/$1/tags/$2; -} - -function generate_dockerfile_from() { - echo "FROM $1" - - # Print the Dockerfile, but without the FROM header - cat $2 | grep -v ^FROM -} - -# MAIN - this is where it starts - -echo "BASE - Building/Fetching base image" - -# Lets pull up the base image -$DOCKER pull "${DOCKER_IMAGE}:base"; - -# Now that we have it in cache, lets build the base image to ensure -# that it has the same output -$DOCKER build -t "${DOCKER_IMAGE}:base" ./base-image; - -# Push the docker base in case it gets changed -$DOCKER push "${DOCKER_IMAGE}:base"; - -# Now it's time to do the same -for VERSION in $(ls targets/); do -for TARGET in $(ls targets/${VERSION}/); do - echo "${VERSION} ${TARGET} - Loading up target configs" - source targets/${VERSION}/${TARGET} - - DOCKER_PACKAGE_BUILDER_TAG="package-builder_${VERSION}_${TARGET}" - DOCKER_IMAGE_BUILDER_TAG="image-builder_${VERSION}_${TARGET}" - - # Handle Package builder - if docker_tag_exists "${DOCKER_IMAGE}" "${DOCKER_PACKAGE_BUILDER_TAG}"; then - echo "${VERSION} ${TARGET} - Package Builder already exists" - else - echo "${VERSION} ${TARGET} - Building Package Builder ..." - generate_dockerfile_from "${DOCKER_IMAGE}:base" ./package-builder/Dockerfile | \ - $DOCKER build \ - -f - \ - --build-arg INSTALL_SRC="$INSTALL_PACKAGE_BUILDER" \ - -t "${DOCKER_IMAGE}:${DOCKER_PACKAGE_BUILDER_TAG}" \ - ./package-builder; - - echo "${VERSION} ${TARGET} - Pushing Package Builder ..." - $DOCKER push "${DOCKER_IMAGE}:${DOCKER_PACKAGE_BUILDER_TAG}"; - fi - - # Handle Image builder - if docker_tag_exists "${DOCKER_IMAGE}" "${DOCKER_IMAGE_BUILDER_TAG}"; then - echo "${VERSION} ${TARGET} - Image Builder already exists" - else - echo "${VERSION} ${TARGET} - Building Image Builder ..." - generate_dockerfile_from "${DOCKER_IMAGE}:base" ./image-builder/Dockerfile | \ - $DOCKER build \ - -f - \ - --build-arg INSTALL_SRC="$INSTALL_IMAGE_BUILDER" \ - -t "${DOCKER_IMAGE}:${DOCKER_IMAGE_BUILDER_TAG}" \ - ./image-builder; - - echo "${VERSION} ${TARGET} - Pushing Image Builder ..." - $DOCKER push "${DOCKER_IMAGE}:${DOCKER_IMAGE_BUILDER_TAG}"; - fi - -done; -done; - -echo "Builder is now going to rest ..." diff --git a/docker-images-builder.sh b/docker-images-builder.sh new file mode 100755 index 0000000..1f41aa3 --- /dev/null +++ b/docker-images-builder.sh @@ -0,0 +1,139 @@ +#!/bin/bash + +### BEGIN - HELPERS ### +function safeexit() { + echo "$1" && [ ! -z $2 ] && exit $2 || exit 0 +} + +function docker_tag_exists() { + [ ! -z $FORCE ] && return 1; + curl --silent -f -lSL "https://index.docker.io/v1/repositories/${1}/tags/${2}" &> $CLOSE_EXEC; +} + +function generate_dockerfile_from() { + echo "FROM $1" + + # Print the Dockerfile, but without the FROM header + cat $2 | grep -v ^FROM +} + +function should_build_target () { + [ -z $TARGETS ] && return 0; + + # lets compare the targets + for x in $TARGETS; do + # If it was specified to be built, lets return true (0) + if [ "$x" == "$1" ]; then + return 0 + fi; + done; + + # we haven't found a match, return falsy (1) + return 1 +} +### END - HELPERS ### + + +### BEGIN - VARIABLES ### +DOCKER_USE_SUDO=${DOCKER_USE_SUDO:-0} +DOCKER_USERNAME=${DOCKER_USERNAME:-cusspvz} +DOCKER_IMAGE=${DOCKER_IMAGE:-$DOCKER_USERNAME/openwrt-builder} +DOCKER="${DOCKER:-docker}" +FORCE="${FORCE}" +CLOSE_EXEC="/dev/null" +[ ! -z $VERBOSIFY ] && CLOSE_EXEC=`tty` +### END - ARIABLES ### + +### BEGIN - VALIDATION ### +if [ -z $DOCKER_USERNAME ]; then + safeexit "Please make sure that you've specified the DOCKER_USERNAME env" 1 +fi + +if [ "$DOCKER_USE_SUDO" != "0" ]; then + DOCKER="sudo ${DOCKER}" +fi +### END - VALIDATION ### + +### BEGIN - SPIT CONFIGS ### +echo "########################################" +echo "## CONFIGS ##" +echo "########################################" +echo "# TARGETS: $TARGETS" +echo "# DOCKER: $DOCKER" +echo "# VERBOSIFY: $([ -z $VERBOSIFY ] && echo "No" || echo "Yes")" +echo "# FORCE: $([ -z $FORCE ] && echo "No" || echo "Yes")" +echo +echo +### END - SPIT CONFIGS ### + +### BEGIN - BASE IMAGE BUILDING ### +echo "BASE - Building/Fetching base image" + +# Lets pull up the base image +$DOCKER pull "${DOCKER_IMAGE}:base" &> $CLOSE_EXEC; + +# Now that we have it in cache, lets build the base image to ensure +# that it has the same output +$DOCKER build -t "${DOCKER_IMAGE}:base" ./base-image &> $CLOSE_EXEC; + +# Push the docker base in case it gets changed +$DOCKER push "${DOCKER_IMAGE}:base" &> $CLOSE_EXEC; +### END - BASE IMAGE BUILDING ### + +### BEGIN - TARGETS PER VERSION BUILDING ### +for VERSION in $(ls targets/); do +for TARGET in $(ls targets/${VERSION}/); do + + # Check if we should build this target + if ! should_build_target "${VERSION}_${TARGET}"; then + echo "${VERSION} ${TARGET} X Skipping this build..." + continue + else + echo "${VERSION} ${TARGET} > Starting build ..." + fi + + echo "${VERSION} ${TARGET} -> Loading up target configs" + source targets/${VERSION}/${TARGET} + + DOCKER_PACKAGE_BUILDER_TAG="package-builder_${VERSION}_${TARGET}" + DOCKER_IMAGE_BUILDER_TAG="image-builder_${VERSION}_${TARGET}" + + # Handle Package builder + if docker_tag_exists "${DOCKER_IMAGE}" "${DOCKER_PACKAGE_BUILDER_TAG}"; then + echo "${VERSION} ${TARGET} -> Package Builder already exists" + else + echo "${VERSION} ${TARGET} -> Building Package Builder ..." + generate_dockerfile_from "${DOCKER_IMAGE}:base" ./package-builder/Dockerfile | \ + $DOCKER build \ + -f - \ + --build-arg INSTALL_SRC="$INSTALL_PACKAGE_BUILDER" \ + -t "${DOCKER_IMAGE}:${DOCKER_PACKAGE_BUILDER_TAG}" \ + ./package-builder &> $CLOSE_EXEC \ + || safeexit "${VERSION} ${TARGET} -X Error Building Package Builder" 2; + + echo "${VERSION} ${TARGET} -> Pushing Package Builder ..." + $DOCKER push "${DOCKER_IMAGE}:${DOCKER_PACKAGE_BUILDER_TAG}" &> $CLOSE_EXEC; + fi + + # Handle Image builder + if docker_tag_exists "${DOCKER_IMAGE}" "${DOCKER_IMAGE_BUILDER_TAG}"; then + echo "${VERSION} ${TARGET} -> Image Builder already exists" + else + echo "${VERSION} ${TARGET} -> Building Image Builder ..." + generate_dockerfile_from "${DOCKER_IMAGE}:base" ./image-builder/Dockerfile | \ + $DOCKER build \ + -f - \ + --build-arg INSTALL_SRC="$INSTALL_IMAGE_BUILDER" \ + -t "${DOCKER_IMAGE}:${DOCKER_IMAGE_BUILDER_TAG}" \ + ./image-builder &> $CLOSE_EXEC \ + || safeexit "${VERSION} ${TARGET} -X Error Building Image Builder" 2; + + echo "${VERSION} ${TARGET} -> Pushing Image Builder ..." + $DOCKER push "${DOCKER_IMAGE}:${DOCKER_IMAGE_BUILDER_TAG}" &> $CLOSE_EXEC; + fi + +done; +done; +### END - TARGETS PER VERSION BUILDING ### + +echo "Builder is now going to rest ..." diff --git a/image-builder/Dockerfile b/image-builder/Dockerfile index de103ff..637405f 100644 --- a/image-builder/Dockerfile +++ b/image-builder/Dockerfile @@ -2,4 +2,8 @@ FROM base LABEL MAINTAINER "Jose Moreira " ARG INSTALL_SRC -RUN /bin/bash -c "$INSTALL_SRC" \ No newline at end of file +RUN /bin/bash -c "$INSTALL_SRC" + +ADD builder.sh /builder +RUN chmod 755 /builder +CMD ["/builder"] \ No newline at end of file diff --git a/package-builder/Dockerfile b/package-builder/Dockerfile index de103ff..637405f 100644 --- a/package-builder/Dockerfile +++ b/package-builder/Dockerfile @@ -2,4 +2,8 @@ FROM base LABEL MAINTAINER "Jose Moreira " ARG INSTALL_SRC -RUN /bin/bash -c "$INSTALL_SRC" \ No newline at end of file +RUN /bin/bash -c "$INSTALL_SRC" + +ADD builder.sh /builder +RUN chmod 755 /builder +CMD ["/builder"] \ No newline at end of file diff --git a/targets/18.0.1/brcm2708-brcm2708 b/targets/18.0.1/brcm2708-brcm2708 new file mode 100644 index 0000000..21aefca --- /dev/null +++ b/targets/18.0.1/brcm2708-brcm2708 @@ -0,0 +1,10 @@ +# DEVICES: +# - Raspberry PI 1 + +OPENWRT_BASE="http://downloads.openwrt.org/releases/18.06.1/targets/brcm2708/bcm2708" +OPENWRT_CONFIG_SEED="$OPENWRT_BASE/config.seed" +OPENWRT_IMAGE_BUILDER="$OPENWRT_BASE/openwrt-imagebuilder-18.06.1-brcm2708-bcm2708.Linux-x86_64.tar.xz" +OPENWRT_PACKAGE_BUILDER="$OPENWRT_BASE/openwrt-sdk-18.06.1-brcm2708-bcm2708_gcc-7.3.0_musl_eabi.Linux-x86_64.tar.xz" + +INSTALL_IMAGE_BUILDER="curl $OPENWRT_IMAGE_BUILDER | tar xvJf - --strip-components 1 -C /src" +INSTALL_PACKAGE_BUILDER="curl $OPENWRT_PACKAGE_BUILDER | tar xvJf - --strip-components 1 -C /src" \ No newline at end of file diff --git a/targets/18.0.1/brcm2708-brcm2709 b/targets/18.0.1/brcm2708-brcm2709 new file mode 100644 index 0000000..70f3f71 --- /dev/null +++ b/targets/18.0.1/brcm2708-brcm2709 @@ -0,0 +1,10 @@ +# DEVICES: +# - Raspberry PI 2 + +OPENWRT_BASE="http://downloads.openwrt.org/releases/18.06.1/targets/brcm2708/bcm2709" +OPENWRT_CONFIG_SEED="$OPENWRT_BASE/config.seed" +OPENWRT_IMAGE_BUILDER="$OPENWRT_BASE/openwrt-imagebuilder-18.06.1-brcm2708-bcm2709.Linux-x86_64.tar.xz" +OPENWRT_PACKAGE_BUILDER="$OPENWRT_BASE/openwrt-sdk-18.06.1-brcm2708-bcm2709_gcc-7.3.0_musl_eabi.Linux-x86_64.tar.xz" + +INSTALL_IMAGE_BUILDER="curl $OPENWRT_IMAGE_BUILDER | tar xvJf - --strip-components 1 -C /src" +INSTALL_PACKAGE_BUILDER="curl $OPENWRT_PACKAGE_BUILDER | tar xvJf - --strip-components 1 -C /src" \ No newline at end of file diff --git a/targets/18.0.1/brcm2708-brcm2710 b/targets/18.0.1/brcm2708-brcm2710 index e328752..7d0dec9 100644 --- a/targets/18.0.1/brcm2708-brcm2710 +++ b/targets/18.0.1/brcm2708-brcm2710 @@ -1,4 +1,8 @@ +# DEVICES: +# - Raspberry PI 3 + OPENWRT_BASE="http://downloads.openwrt.org/releases/18.06.1/targets/brcm2708/bcm2710" +OPENWRT_CONFIG_SEED="$OPENWRT_BASE/config.seed" OPENWRT_IMAGE_BUILDER="$OPENWRT_BASE/openwrt-imagebuilder-18.06.1-brcm2708-bcm2710.Linux-x86_64.tar.xz" OPENWRT_PACKAGE_BUILDER="$OPENWRT_BASE/openwrt-sdk-18.06.1-brcm2708-bcm2710_gcc-7.3.0_musl.Linux-x86_64.tar.xz" diff --git a/targets/18.0.1/omap-generic b/targets/18.0.1/omap-generic new file mode 100644 index 0000000..d5d713f --- /dev/null +++ b/targets/18.0.1/omap-generic @@ -0,0 +1,10 @@ +# DEVICES: +# - BeagleBone Black + +OPENWRT_BASE="http://downloads.openwrt.org/releases/18.06.1/targets/omap/generic" +OPENWRT_CONFIG_SEED="$OPENWRT_BASE/config.seed" +OPENWRT_IMAGE_BUILDER="$OPENWRT_BASE/openwrt-imagebuilder-18.06.1-omap.Linux-x86_64.tar.xz" +OPENWRT_PACKAGE_BUILDER="$OPENWRT_BASE/openwrt-sdk-18.06.1-omap_gcc-7.3.0_musl_eabi.Linux-x86_64.tar.xz" + +INSTALL_IMAGE_BUILDER="curl $OPENWRT_IMAGE_BUILDER | tar xvJf - --strip-components 1 -C /src" +INSTALL_PACKAGE_BUILDER="curl $OPENWRT_PACKAGE_BUILDER | tar xvJf - --strip-components 1 -C /src" \ No newline at end of file