Skip to content

Commit

Permalink
add build process using docker
Browse files Browse the repository at this point in the history
it make it easier to setup statically linking env
  • Loading branch information
edenreich committed Mar 8, 2020
1 parent 018cd5e commit 46a8af0
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 4 deletions.
61 changes: 57 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ the IP address statically (P.S this is a FREE solution, due note you could still

## Usage

Just run `cargo build --release` and use the cloureflare binary like the following:

```sh
CLOUDFLARE_ACCESS_TOKEN=[ACCESS_TOKEN] \
CLOUDFLARE_ZONE_ID=[ZONE_ID] \
Expand Down Expand Up @@ -54,12 +52,67 @@ sudo systemctl enable cloudflare
sudo systemctl start cloudflare
```

## Build

Note: compilation is done using statically linking, to make sure everything comes with the binary as is.

Build for linux:

```sh
docker build -t cloudflare/linux --target normal-build -f build/Dockerfile .
```

Build on and for ARM:

```sh
docker build -t cloudflare/linux-arm7 --target arm7-build -f build/Dockerfile .
```

Copy the binaries from the containers, for example:

```sh
docker run --name cloudflare_linux -d cloudflare/linux
docker cp cloudflare_linux:/home/rust/src/bin/cloudflare bin/cloudflare
```

For ARM run:

```sh
docker run --name cloudflare_linux-arm7 -d cloudflare/linux-arm7
docker cp cloudflare_linux-arm7:/home/rust/src/bin/cloudflare bin/cloudflare_arm7
```

## Tests

After building the binary a simple test to check it works with standard installation of ubuntu:
```sh
docker build -t cloudflare/ubuntu-test -f tests/ubuntu/Dockerfile .
docker run --rm -it cloudflare/ubuntu-test
```

## Download

You may also download the released binary and simply use it:

```sh
sudo curl -sSL "https://github.com/edenreich/cloudflare-dns-updater/releases/download/v1.0.0/cloudflare" -o /usr/local/bin/cloudflare
sudo curl -sSL "https://github.com/edenreich/cloudflare-dns-updater/releases/download/v1.0.1/cloudflare" -o /usr/local/bin/cloudflare
# or for ARM:
# sudo curl -sSL "https://github.com/edenreich/cloudflare-dns-updater/releases/download/v1.0.1/cloudflare_arm" -o /usr/local/bin/cloudflare

sudo chmod +x /usr/local/bin/cloudflare
sudo ln -s /usr/local/bin/cloudflare /usr/bin/cloudflare
```
```

## Target

This project targets linux, works on ARM as well.

## Motivation

I have a raspberry pi k3s cluster at home, and I often find it annoying that the internet provider changes the IP address.
So I thought it could be nice to have a webserver without needing to worry about the IP, that would automatically update my DNS
records on cloudflare to point to that newly dynamic IP provided by my ISP.

## Bugs Reporting

If you find any bug please submit an issue ;)
2 changes: 2 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
112 changes: 112 additions & 0 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
FROM ubuntu:18.04 as build-environment

ENV PATH=/home/rust/.cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

ENV ZLIB_VERSION=1.2.11
ENV POSTGRESQL_VERSION=11.2
ENV MDBOOK_VERSION=0.2.1

RUN apt-get update && \
apt-get install -y \
build-essential \
cmake \
curl \
file \
git \
musl-dev \
musl-tools \
libpq-dev \
libsqlite-dev \
libssl-dev \
linux-libc-dev \
pkgconf \
sudo \
xutils-dev \
gcc-multilib-arm-linux-gnueabihf \
&& \
apt-get clean && rm -rf /var/lib/apt/lists/* && \
useradd rust --user-group --create-home --shell /bin/bash --groups sudo && \
curl -LO https://github.com/rust-lang-nursery/mdBook/releases/download/v$MDBOOK_VERSION/mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz && \
tar xf mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz && \
mv mdbook /usr/local/bin/ && \
rm -f mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz

RUN sudo ln -s "/usr/bin/g++" "/usr/bin/musl-g++"

RUN echo "Building OpenSSL" && \
ls /usr/include/linux && \
sudo mkdir -p /usr/local/musl/include && \
sudo ln -s /usr/include/linux /usr/local/musl/include/linux && \
sudo ln -s /usr/include/x86_64-linux-gnu/asm /usr/local/musl/include/asm && \
sudo ln -s /usr/include/asm-generic /usr/local/musl/include/asm-generic && \
cd /tmp && \
curl -LO "https://www.openssl.org/source/openssl-1.0.2r.tar.gz" && \
tar xvzf "openssl-1.0.2r.tar.gz" && cd "openssl-1.0.2r" && \
CC=musl-gcc ./Configure no-shared no-zlib -fPIC --prefix=/usr/local/musl -DOPENSSL_NO_SECURE_MEMORY linux-x86_64 && \
C_INCLUDE_PATH=/usr/local/musl/include/ make depend && \
C_INCLUDE_PATH=/usr/local/musl/include/ make && \
sudo make install && \
sudo rm /usr/local/musl/include/linux /usr/local/musl/include/asm /usr/local/musl/include/asm-generic && \
rm -r /tmp/*

RUN echo "Building zlib" && \
cd /tmp && \
curl -LO "http://zlib.net/zlib-$ZLIB_VERSION.tar.gz" && \
tar xzf "zlib-$ZLIB_VERSION.tar.gz" && cd "zlib-$ZLIB_VERSION" && \
CC=musl-gcc ./configure --static --prefix=/usr/local/musl && \
make && sudo make install && \
rm -r /tmp/*

RUN echo "Building libpq" && \
cd /tmp && \
curl -LO "https://ftp.postgresql.org/pub/source/v$POSTGRESQL_VERSION/postgresql-$POSTGRESQL_VERSION.tar.gz" && \
tar xzf "postgresql-$POSTGRESQL_VERSION.tar.gz" && cd "postgresql-$POSTGRESQL_VERSION" && \
CC=musl-gcc CPPFLAGS=-I/usr/local/musl/include LDFLAGS=-L/usr/local/musl/lib ./configure --with-openssl --without-readline --prefix=/usr/local/musl && \
cd src/interfaces/libpq && make all-static-lib && sudo make install-lib-static && \
cd ../../bin/pg_config && make && sudo make install && \
rm -r /tmp/*

ENV OPENSSL_DIR=/usr/local/musl/ \
OPENSSL_INCLUDE_DIR=/usr/local/musl/include/ \
DEP_OPENSSL_INCLUDE=/usr/local/musl/include/ \
OPENSSL_LIB_DIR=/usr/local/musl/lib/ \
OPENSSL_STATIC=1 \
PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL=1 \
PG_CONFIG_X86_64_UNKNOWN_LINUX_GNU=/usr/bin/pg_config \
PKG_CONFIG_ALLOW_CROSS=true \
PKG_CONFIG_ALL_STATIC=true \
LIBZ_SYS_STATIC=1 \
TARGET=musl

RUN echo '%sudo ALL=(ALL:ALL) NOPASSWD:ALL' > /etc/sudoers.d/nopasswd
RUN chown -R rust:rust /home/rust
USER rust
RUN mkdir /home/rust/src
WORKDIR /home/rust/src
RUN mkdir /home/rust/libs
COPY --chown=rust:rust build/cargo-config.toml /home/rust/.cargo/config

RUN curl https://sh.rustup.rs -sSf | \
sh -s -- -y --default-toolchain stable && \
rustup target add x86_64-unknown-linux-musl && \
rustup target add armv7-unknown-linux-musleabihf

RUN cargo install -f cargo-audit && \
cargo install -f cargo-deny && \
rm -rf /home/rust/.cargo/registry/

COPY --chown=rust:rust src src
COPY --chown=rust:rust Cargo.lock Cargo.lock
COPY --chown=rust:rust Cargo.toml Cargo.toml

FROM build-environment as normal-build

RUN cargo build --release

RUN mkdir bin && cp target/x86_64-unknown-linux-musl/release/cloudflare bin/cloudflare

FROM build-environment as arm7-build

RUN sudo cargo build --release --target armv7-unknown-linux-musleabihf

RUN mkdir bin && cp target/armv7-unknown-linux-musleabihf/release/cloudflare bin/cloudflare
5 changes: 5 additions & 0 deletions build/cargo-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
target = "x86_64-unknown-linux-musl"

[target.armv7-unknown-linux-musleabihf]
linker = "arm-linux-gnueabihf-gcc"
5 changes: 5 additions & 0 deletions tests/centos/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM centos:centos7

COPY bin/cloudflare /usr/local/bin/cloudflare

CMD [ "cloudflare" ]
5 changes: 5 additions & 0 deletions tests/ubuntu/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM ubuntu:18.04

COPY bin/cloudflare /usr/local/bin/cloudflare

CMD [ "cloudflare" ]

0 comments on commit 46a8af0

Please sign in to comment.