Skip to content

Commit

Permalink
chore: update Dockerfile to include lpac builder stage and improve bu…
Browse files Browse the repository at this point in the history
…ild process
  • Loading branch information
damonto committed Jun 19, 2024
1 parent 4311b2e commit be133a7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 103 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.git
.github
.vscode
.editorconfig
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Publish Docker Image
runs-on: ubuntu-latest
permissions:
contents: write
contents: read
packages: write
steps:
- name: Checkout code
Expand Down Expand Up @@ -56,8 +56,11 @@ jobs:
uses: docker/build-push-action@v5
with:
build-args: |
BUILDKIT_CONTEXT_KEEP_GIT_DIR=true
VERSION=${{ steps.meta.outputs.version }}
context: .
platforms: linux/amd64,linux/arm64
push: ${{ startsWith(github.ref, 'refs/tags/v') }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
annotations: ${{ steps.meta.outputs.annotations }}
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lpac"]
path = lpac
url = git@github.com:estkme-group/lpac.git
29 changes: 22 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
FROM golang:1.22-alpine as builder
# Build: lpac
FROM alpine:3.20 AS lpac-builder

WORKDIR /app

RUN apk add --no-cache git gcc cmake make musl-dev curl-dev

COPY . .

RUN set -ex \
&& cd lpac \
&& cmake . -DLPAC_WITH_APDU_PCSC=off -DLPAC_WITH_APDU_AT=off \
&& make -j$(nproc)

# Build: estkme-cloud
FROM golang:1.22-alpine as estkme-cloud-builder

WORKDIR /app

ARG VERSION

COPY . .

RUN set -ex \
&& CGO_ENABLED=0 go build -trimpath -ldflags="-w -s -X main.Version=${VERSION}" -o estkme-cloud main.go

FROM alpine:latest
# Production
FROM alpine:3.20 as production

WORKDIR /app

COPY --from=builder /app/estkme-cloud /app/estkme-cloud
COPY --from=lpac-builder /app/lpac/output/lpac /app/lpac
COPY --from=estkme-cloud-builder /app/estkme-cloud /app/estkme-cloud

RUN set -ex \
&& apk add --no-cache gcompat ca-certificates pcsc-lite-libs libcurl \
&& update-ca-certificates \
&& chmod +x /app/estkme-cloud
&& apk add --no-cache libcurl

EXPOSE 1888

CMD ["/app/estkme-cloud"]
CMD ["/app/estkme-cloud", "--dont-download", "--dir=/app"]
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import (
var Version string

func init() {
dir, err := os.MkdirTemp("", "estkme-cloud")
if err != nil {
if err := os.MkdirAll("/tmp/estkme-cloud", 0755); err != nil {
panic(err)
}

flag.StringVar(&config.C.ListenAddress, "listen-address", ":1888", "eSTK.me cloud enhance server listen address")
flag.StringVar(&config.C.Dir, "dir", dir, "the directory to store lpac")
flag.StringVar(&config.C.Dir, "dir", "/tmp/estkme-cloud", "the directory to store lpac")
flag.StringVar(&config.C.Version, "version", "v2.0.2", "the version of lpac to download")
flag.BoolVar(&config.C.DontDownload, "dont-download", false, "don't download lpac")
flag.StringVar(&config.C.Advertising, "advertising", "", "advertising message to show on the server (max: 100 characters)")
Expand Down
122 changes: 32 additions & 90 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,63 +29,18 @@ if [ "$(lsb_release -cs)" == "bullseye" ]; then
fi

# Install dependencies
apt-get update -y && apt-get install -y unzip cmake pkg-config libcurl4-openssl-dev libpcsclite-dev zip curl

# Get the latest release version
get_latest_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/' |
sed 's/v//'
}
apt-get update -y && apt-get install -y git unzip cmake pkg-config libcurl4-openssl-dev zip curl

DST_DIR="/opt/estkme-cloud"

LPAC_VERSION=$(get_latest_release "estkme-group/lpac")
if [ -z "$LPAC_VERSION" ]; then
echo "Invalid LPAC version"
exit 1
fi

function download_binary {
LPAC_BINARY_URL="https://github.com/estkme-group/lpac/releases/download/v$LPAC_VERSION/lpac-linux-x86_64.zip"
curl -L -o $DST_DIR/lpac.zip $LPAC_BINARY_URL
unzip -o $DST_DIR/lpac.zip -d $DST_DIR/data
chmod +x $DST_DIR/data/lpac
rm -f $DST_DIR/lpac.zip
}

function build_from_source {
BUILD_DIR=$(mktemp -d)
LPAC_SOURCE_CODE="https://github.com/estkme-group/lpac/archive/refs/tags/v$LPAC_VERSION.zip"
echo "Downloading LPAC source code from $LPAC_SOURCE_CODE"
# Download the source code
mkdir -p $BUILD_DIR
curl -L -o $BUILD_DIR/lpac.zip $LPAC_SOURCE_CODE
unzip -o $BUILD_DIR/lpac.zip -d $BUILD_DIR
cd $BUILD_DIR/lpac-$LPAC_VERSION
mkdir -p build && cd build
# Build the source code
cmake .. && make -j$(nproc)
cp -rf $BUILD_DIR/lpac-$LPAC_VERSION/build/output/lpac $DST_DIR/data
chmod +x $DST_DIR/data/lpac
rm -rf $BUILD_DIR
}

echo "Downloading LPAC version $LPAC_VERSION"
mkdir -p $DST_DIR/data
if [ "$(uname -m)" == "x86_64" ]; then
download_binary
else
build_from_source
fi

ESTKME_CLOUD_VERSION=$(get_latest_release "damonto/estkme-cloud")
if [ -z "$ESTKME_CLOUD_VERSION" ]; then
echo "Invalid eSTK.me Cloud Enhance Server version"
exit 1
fi

# Build lpac
LPAC_VERSION=$(curl -Ls https://api.github.com/repos/estkme-group/lpac/releases/latest | grep tag_name | cut -d '"' -f 4)
curl -L -o lpac.zip https://github.com/estkme-group/lpac/archive/refs/tags/${LPAC_VERSION}.zip
unzip lpac.zip && rm -f lpac.zip && cd lpac-*
cmake . -DLPAC_WITH_APDU_PCSC=off -DLPAC_WITH_APDU_AT=off && make -j $(nproc)
cp output/lpac $DST_DIR && cd .. && rm -rf lpac-*

# Download estkme-cloud
ESTKME_CLOUD_VERSION=$(curl -Ls https://api.github.com/repos/damonto/estkme-cloud/releases/latest | grep tag_name | cut -d '"' -f 4)
if [ "$(uname -m)" == "x86_64" ]; then
ESTKME_CLOUD_BINARY="estkme-cloud-linux-amd64"
elif [ "$(uname -m)" == "aarch64" ]; then
Expand All @@ -94,16 +49,23 @@ else
echo "Unsupported architecture"
exit 1
fi
ESTKME_CLOUD_BINARY_URL="https://github.com/damonto/estkme-cloud/releases/download/v$ESTKME_CLOUD_VERSION/$ESTKME_CLOUD_BINARY"
ESTKME_CLOUD_BINARY_URL="https://github.com/damonto/estkme-cloud/releases/download/$ESTKME_CLOUD_VERSION/$ESTKME_CLOUD_BINARY"
if [ -x "$(command -v systemctl)" ] && [ "$(systemctl is-active estkme-cloud.service)" == "active" ]; then
systemctl stop estkme-cloud.service
elif [ -x "$(command -v supervisorctl)" ] && [ "$(supervisorctl status estkme-cloud | awk '{print $2}')" == "RUNNING" ]; then
supervisorctl stop estkme-cloud
fi
curl -L -o $DST_DIR/estkme-cloud $ESTKME_CLOUD_BINARY_URL
chmod +x $DST_DIR/estkme-cloud

START_CMD="/opt/estkme-cloud/estkme-cloud --dir=/opt/estkme-cloud/data --dont-download"
START_CMD="/opt/estkme-cloud/estkme-cloud --dir=/opt/estkme-cloud --dont-download"
if [ -n "$1" ]; then
START_CMD="$START_CMD --advertising='$1'"
fi

SYSTEMED_UNIT="estkme-cloud.service"
SYSTEMED_UNIT_PATH="/etc/systemd/system/$SYSTEMED_UNIT"
SYSTEMED_FILE="
if [ -x "$(command -v systemctl)" ]; then
echo "Deploying eSTK.me Cloud Enhance Server to systemd"
tee /etc/systemd/system/estkme-cloud.service << CONFIG
[Unit]
Description=eSTK.me Cloud Enhance Server
After=network.target
Expand All @@ -117,10 +79,13 @@ TimeoutStopSec=30s
[Install]
WantedBy=multi-user.target
"

SUPRVISOR_PROGRAM="estkme-cloud"
SUPRVISOR_FILE="
CONFIG
systemctl daemon-reload
systemctl enable estkme-cloud.service
systemctl start estkme-cloud.service
else
echo "Deploying eSTK.me Cloud Enhance Server to supervisor"
tee /etc/supervisor/supervisord.conf << CONFIG
[supervisord]
nodaemon=true
logfile=/dev/null
Expand All @@ -136,41 +101,18 @@ file=/tmp/supervisor.sock
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
[program:$SUPRVISOR_PROGRAM]
[program:estkme-cloud]
command="$START_CMD"
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
"

if [ -x "$(command -v systemctl)" ] && [ "$(systemctl is-active $SYSTEMED_UNIT)" == "active" ]; then
echo "Stopping eSTK.me Cloud Enhance Server in systemd"
systemctl stop $SYSTEMED_UNIT
elif [ -x "$(command -v supervisorctl)" ] && [ "$(supervisorctl status $SUPRVISOR_PROGRAM | awk '{print $2}')" == "RUNNING" ]; then
echo "Stopping eSTK.me Cloud Enhance Server in supervisor"
supervisorctl stop $SUPRVISOR_PROGRAM
fi

# Download eSTK.me Cloud Enhance Server
echo "Downloading eSTK.me Cloud Enhance Server version $ESTKME_CLOUD_VERSION"
curl -L -o $DST_DIR/estkme-cloud $ESTKME_CLOUD_BINARY_URL
chmod +x $DST_DIR/estkme-cloud

if [ -x "$(command -v systemctl)" ]; then
echo "Deploying eSTK.me Cloud Enhance Server to systemd"
echo "$SYSTEMED_FILE" > $SYSTEMED_UNIT_PATH
systemctl daemon-reload
systemctl enable $SYSTEMED_UNIT
systemctl start $SYSTEMED_UNIT
else
echo "Deploying eSTK.me Cloud Enhance Server to supervisor"
echo "$SUPRVISOR_FILE" > /etc/supervisor/supervisord.conf
CONFIG
supervisorctl reread
supervisorctl update
supervisorctl reload
fi

echo "eSTK.me Cloud Server deployed successfully!"
echo "eSTK.me Cloud Server has been deployed!"

0 comments on commit be133a7

Please sign in to comment.