From 745dcb7ff8725c5abbc01130f3bdd2f922aff8dd Mon Sep 17 00:00:00 2001 From: plazmoid Date: Mon, 23 Aug 2021 22:27:41 +0500 Subject: [PATCH] get rid of in-docker cargo --- .cargo/config.toml | 3 + .gitignore | 3 +- Makefile.toml | 19 ++-- .../tests_runner.Dockerfile | 0 .../integration-tests}/u_agent.Dockerfile | 0 .../integration-tests}/u_db.Dockerfile | 0 .../integration-tests}/u_server.Dockerfile | 0 images/musl-libs.Dockerfile | 96 +++++++++++++++++++ integration/docker.py | 2 +- scripts/build_musl_libs.sh | 15 +++ scripts/cargo_musl.sh | 13 --- 11 files changed, 129 insertions(+), 22 deletions(-) create mode 100644 .cargo/config.toml rename {integration/images => images/integration-tests}/tests_runner.Dockerfile (100%) rename {integration/images => images/integration-tests}/u_agent.Dockerfile (100%) rename {integration/images => images/integration-tests}/u_db.Dockerfile (100%) rename {integration/images => images/integration-tests}/u_server.Dockerfile (100%) create mode 100644 images/musl-libs.Dockerfile create mode 100755 scripts/build_musl_libs.sh delete mode 100755 scripts/cargo_musl.sh diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..bf72e8c --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,3 @@ +[build] +rustflags = ["-L", "/home/ortem/src/rust/unki/static/lib"] +target = "x86_64-unknown-linux-musl" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 67f21e2..19d820c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ certs/* *.log echoer .env.private -*.lock \ No newline at end of file +*.lock +static/ \ No newline at end of file diff --git a/Makefile.toml b/Makefile.toml index 87cffed..7e2d27f 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -2,24 +2,29 @@ default_to_workspace = false [env] -CARGO = "./scripts/cargo_musl.sh" +CARGO = "cargo" +PREFIX = "${CARGO_MAKE_WORKING_DIRECTORY}/static" +PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL = "true" +PG_CONFIG_X86_64_UNKNOWN_LINUX_GNU = "${PREFIX}/bin/pg_config" +OPENSSL_STATIC = "true" +OPENSSL_DIR = "${PREFIX}" -[tasks.build_cargo_image] -script = "docker build -t unki/musllibs ./muslrust" +[tasks.build_static_libs] +script = "./scripts/build_musl_libs.sh" [tasks.clean] command = "${CARGO}" args = ["clean"] [tasks.debug] -dependencies = ["build_cargo_image"] +dependencies = ["build_static_libs"] command = "${CARGO}" -args = ["build"] +args = ["build", "${@}"] [tasks.release] -dependencies = ["build_cargo_image"] +dependencies = ["build_static_libs"] command = "${CARGO}" -args = ["build", "--release"] +args = ["build", "--release", "${@}"] [tasks.run] script = ''' diff --git a/integration/images/tests_runner.Dockerfile b/images/integration-tests/tests_runner.Dockerfile similarity index 100% rename from integration/images/tests_runner.Dockerfile rename to images/integration-tests/tests_runner.Dockerfile diff --git a/integration/images/u_agent.Dockerfile b/images/integration-tests/u_agent.Dockerfile similarity index 100% rename from integration/images/u_agent.Dockerfile rename to images/integration-tests/u_agent.Dockerfile diff --git a/integration/images/u_db.Dockerfile b/images/integration-tests/u_db.Dockerfile similarity index 100% rename from integration/images/u_db.Dockerfile rename to images/integration-tests/u_db.Dockerfile diff --git a/integration/images/u_server.Dockerfile b/images/integration-tests/u_server.Dockerfile similarity index 100% rename from integration/images/u_server.Dockerfile rename to images/integration-tests/u_server.Dockerfile diff --git a/images/musl-libs.Dockerfile b/images/musl-libs.Dockerfile new file mode 100644 index 0000000..2441b80 --- /dev/null +++ b/images/musl-libs.Dockerfile @@ -0,0 +1,96 @@ +FROM ubuntu:xenial +LABEL maintainer="Eirik Albrigtsen " + +# Required packages: +# - musl-dev, musl-tools - the musl toolchain +# - curl, g++, make, pkgconf, cmake - for fetching and building third party libs +# - ca-certificates - openssl + curl + peer verification of downloads +# - xutils-dev - for openssl makedepend +# - libssl-dev and libpq-dev - for dynamic linking during diesel_codegen build process +# - git - cargo builds in user projects +# - linux-headers-amd64 - needed for building openssl 1.1 (stretch only) +# - file - needed by rustup.sh install +# - automake autoconf libtool - support crates building C deps as part cargo build +# recently removed: +# cmake (not used), nano, zlib1g-dev +RUN apt-get update && apt-get install -y \ + musl-dev \ + musl-tools \ + git \ + file \ + openssh-client \ + make \ + g++ \ + curl \ + pkgconf \ + ca-certificates \ + xutils-dev \ + libssl-dev \ + libpq-dev \ + automake \ + autoconf \ + libtool \ + python3 \ + --no-install-recommends && \ + rm -rf /var/lib/apt/lists/* + +# Convenience list of versions and variables for compilation later on +# This helps continuing manually if anything breaks. +ENV SSL_VER="1.0.2u" \ + CURL_VER="7.77.0" \ + ZLIB_VER="1.2.11" \ + PQ_VER="11.12" \ + SQLITE_VER="3350500" \ + CC=musl-gcc \ + PREFIX=/musl \ + PATH=/usr/local/bin:/root/.cargo/bin:$PATH \ + PKG_CONFIG_PATH=/usr/local/lib/pkgconfig \ + LD_LIBRARY_PATH=$PREFIX + +# Set up a prefix for musl build libraries, make the linker's job of finding them easier +# Primarily for the benefit of postgres. +# Lastly, link some linux-headers for openssl 1.1 (not used herein) +RUN mkdir $PREFIX && \ + echo "$PREFIX/lib" >> /etc/ld-musl-x86_64.path && \ + ln -s /usr/include/x86_64-linux-gnu/asm /usr/include/x86_64-linux-musl/asm && \ + ln -s /usr/include/asm-generic /usr/include/x86_64-linux-musl/asm-generic && \ + ln -s /usr/include/linux /usr/include/x86_64-linux-musl/linux + +# Build zlib (used in openssl and pq) +RUN curl -sSL https://zlib.net/zlib-$ZLIB_VER.tar.gz | tar xz && \ + cd zlib-$ZLIB_VER && \ + CC="musl-gcc -fPIC -pie" LDFLAGS="-L$PREFIX/lib" CFLAGS="-I$PREFIX/include" ./configure --static --prefix=$PREFIX && \ + make -j$(nproc) && make install && \ + cd .. && rm -rf zlib-$ZLIB_VER + +# Build openssl (used in curl and pq) +# Would like to use zlib here, but can't seem to get it to work properly +# TODO: fix so that it works +RUN curl -sSL https://www.openssl.org/source/old/1.0.2/openssl-$SSL_VER.tar.gz | tar xz && \ + cd openssl-$SSL_VER && \ + ./Configure no-zlib no-shared -fPIC --prefix=$PREFIX --openssldir=$PREFIX/ssl linux-x86_64 && \ + env C_INCLUDE_PATH=$PREFIX/include make depend 2> /dev/null && \ + make -j$(nproc) && make install && \ + cd .. && rm -rf openssl-$SSL_VER + +# Build curl (needs with-zlib and all this stuff to allow https) +# curl_LDFLAGS needed on stretch to avoid fPIC errors - though not sure from what +RUN curl -sSL https://curl.se/download/curl-$CURL_VER.tar.gz | tar xz && \ + cd curl-$CURL_VER && \ + CC="musl-gcc -fPIC -pie" LDFLAGS="-L$PREFIX/lib" CFLAGS="-I$PREFIX/include" ./configure \ + --enable-shared=no --with-zlib --enable-static=ssl --enable-optimize --prefix=$PREFIX \ + --with-ca-path=/etc/ssl/certs/ --with-ca-bundle=/etc/ssl/certs/ca-certificates.crt --without-ca-fallback \ + --with-openssl && \ + make -j$(nproc) curl_LDFLAGS="-all-static" && make install && \ + cd .. && rm -rf curl-$CURL_VER + +# Build libpq +RUN curl -sSL https://ftp.postgresql.org/pub/source/v$PQ_VER/postgresql-$PQ_VER.tar.gz | tar xz && \ + cd postgresql-$PQ_VER && \ + CC="musl-gcc -fPIE -pie" LDFLAGS="-L$PREFIX/lib" CFLAGS="-I$PREFIX/include" ./configure \ + --without-readline \ + --with-openssl \ + --prefix=$PREFIX --host=x86_64-unknown-linux-musl && \ + cd src/interfaces/libpq make -s -j$(nproc) all-static-lib && make -s install install-lib-static && \ + cd ../../bin/pg_config && make -j $(nproc) && make install && \ + cd .. && rm -rf postgresql-$PQ_VER diff --git a/integration/docker.py b/integration/docker.py index 68abd69..0e1c163 100644 --- a/integration/docker.py +++ b/integration/docker.py @@ -1,7 +1,7 @@ import subprocess from utils import * -BASE_IMAGE_DIR = 'images' +BASE_IMAGE_DIR = '../images/integration-tests' DOCKERFILES = { 'u_agent': { diff --git a/scripts/build_musl_libs.sh b/scripts/build_musl_libs.sh new file mode 100755 index 0000000..1cf612c --- /dev/null +++ b/scripts/build_musl_libs.sh @@ -0,0 +1,15 @@ +#!/bin/bash +set -ex +source $(dirname $0)/rootdir.sh #set ROOTDIR +ARGS=$@ +STATIC_LIBS=static +DOCKER_EXCHG=/musl-share +IMAGE=unki/musllibs +mkdir -p $STATIC_LIBS +cd $ROOTDIR/images && docker build -t $IMAGE . -f musl-libs.Dockerfile +docker run \ + -v $ROOTDIR/$STATIC_LIBS:$DOCKER_EXCHG \ + -w /volume \ + -it \ + $IMAGE \ + bash -c "[[ \$(ls -A $DOCKER_EXCHG) ]] || cp /musl/. $DOCKER_EXCHG -r" diff --git a/scripts/cargo_musl.sh b/scripts/cargo_musl.sh deleted file mode 100755 index e4149ef..0000000 --- a/scripts/cargo_musl.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -set -ex -source $(dirname $0)/rootdir.sh #set ROOTDIR -ARGS=$@ -docker run \ - --env-file $ROOTDIR/.env \ - --env-file $ROOTDIR/.env.private \ - -v $ROOTDIR:/volume \ - -v cargo-cache:/root/.cargo/registry \ - -w /volume \ - -it \ - unki/musllibs \ - bash -c "umask 0000; cargo $ARGS"