Skip to content

Commit

Permalink
Merge pull request #6 from DaoCloud/init1
Browse files Browse the repository at this point in the history
Add support for multi os
  • Loading branch information
weizhoublue authored Apr 29, 2024
2 parents 04e337d + 82ff429 commit 12508ea
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 93 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/build-image-latest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Auto Build Latest Image

on:
push:
branches:
- main

permissions: write-all

jobs:
call-build-push:
uses: ./.github/workflows/call-image-build.yaml
with:
ref: ${{ github.ref }}
tag: latest
push: true
secrets: inherit
33 changes: 33 additions & 0 deletions .github/workflows/build-image-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Auto Build Release Image

on:
push:
tags:
- v[0-9]+.[0-9]+.[0-9]+

permissions: write-all

jobs:
get_ref:
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.get_ref.outputs.tag }}
steps:
- name: Get Ref
id: get_ref
run: |
if ${{ github.event_name != 'push' }} ; then
echo "unexpected event: ${{ github.event_name }} "
exit 1
fi
echo "tag=${GITHUB_REF##*/}" >> $GITHUB_OUTPUT
call-build-release:
needs: [get_ref]
uses: ./.github/workflows/call-image-build.yaml
with:
ref: ${{ github.ref }}
tag: ${{ needs.get_ref.outputs.tag }}
push: true
secrets: inherit

117 changes: 117 additions & 0 deletions .github/workflows/call-image-build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
name: Call Build Image

env:
ONLINE_REGISTER: ghcr.io
BUILD_PLATFORM: linux/amd64,linux/arm64
ONLINE_REGISTER_USER: ${{ github.actor }}
ONLINE_REGISTER_PASSWORD: ${{ secrets.GITHUB_TOKEN }}

on:
workflow_call:
inputs:
ref:
required: true
type: string
tag:
required: true
type: string
push:
required: false
type: boolean
ci:
required: false
type: boolean

permissions: write-all

jobs:
build_and_push_prs:
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/[email protected]

- name: Login to online register
uses: docker/[email protected]
if: ${{ inputs.push == true }}
with:
username: ${{ env.ONLINE_REGISTER_USER }}
password: ${{ env.ONLINE_REGISTER_PASSWORD }}
registry: ${{ env.ONLINE_REGISTER }}

# checkout the changed code
- name: Checkout Source Code
uses: actions/checkout@v4
with:
persist-credentials: false
ref: ${{ inputs.ref }}

- name: Getting Build Arg
id: arg
continue-on-error: false
run: |
GIT_COMMIT_VERSION=$( git show -s --format='format:%H')
GIT_COMMIT_TIME=$( git show -s --format='format:%aI')
echo ::set-output name=commit_version::${GIT_COMMIT_VERSION}
echo ::set-output name=commit_time::${GIT_COMMIT_TIME}
- name: Generate Dockerfile
id: dockerfile
continue-on-error: false
run: |
make update_dockerfile
# ========== image build
# build normal image
- name: Image Build
uses: docker/[email protected]
if: ${{ inputs.ci != true }}
continue-on-error: false
id: docker_build_master
with:
context: .
file: ./images/Dockerfile
# Only push when the event name was a GitHub push, this is to avoid
# re-pushing the image tags when we only want to re-create the Golang
# docker cache after the workflow "Image CI Cache Cleaner" was terminated.
push: ${{ inputs.push }}
provenance: false
platforms: linux/amd64,linux/arm64
github-token: ${{ secrets.GITHUB_TOKEN }}
tags: |
${{ env.ONLINE_REGISTER }}/${{ github.repository }}:${{ inputs.tag }}
build-args: |
GIT_COMMIT_VERSION=${{ steps.arg.outputs.commit_version }}
GIT_COMMIT_TIME=${{ steps.arg.outputs.commit_time }}
# ==========CI image build
# build CI image
- name: CI Image Build
if: ${{ inputs.ci == true }}
uses: docker/[email protected]
continue-on-error: false
id: docker_build_pr
with:
context: .
file: ./Dockerfile
# Only push when the event name was a GitHub push, this is to avoid
# re-pushing the image tags when we only want to re-create the Golang
# docker cache after the workflow "Image CI Cache Cleaner" was terminated.
push: false
provenance: false
platforms: linux/amd64
outputs: type=docker,dest=/tmp/image.tar
github-token: ${{ secrets.GITHUB_TOKEN }}
tags: |
${{ env.ONLINE_REGISTER }}/${{ github.repository }}:${{ inputs.tag }}
build-args: |
GIT_COMMIT_VERSION=${{ steps.arg.outputs.commit_version }}
GIT_COMMIT_TIME=${{ steps.arg.outputs.commit_time }}
- name: Upload artifact e2e image tar
if: ${{ inputs.ci == true }}
uses: actions/[email protected]
with:
name: image-e2e-tar
path: /tmp/image.tar
retention-days: 1
15 changes: 0 additions & 15 deletions Dockerfile

This file was deleted.

44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
include ./Makefile.defs

.PHONY: update_dockerfile
update_dockerfile:
rm -f images/Dockerfile ; \
touch images/Dockerfile ;\
echo "# this file is entirly generated by makefile" >> images/Dockerfile ; \
OS_LIST=`jq -r '.[] | "\(.os)|\(.baseImage)"' ./images/smc-os-list.json`; \
echo $${OS_LIST} ; \
for OS in $${OS_LIST} ; do \
os_name=`echo $${OS} | awk -F '|' '{print $$1}'` ; \
os_image=`echo $${OS} | awk -F '|' '{print $$2}'` ; \
printf 'ARG base_image_%s=%s\n' "$${os_name}" "$${os_image}" >> images/Dockerfile ;\
done ; \
printf '\n' >> images/Dockerfile ; \
for OS in $${OS_LIST} ; do \
os_name=`echo $${OS} | awk -F '|' '{print $$1}'` ; \
printf 'FROM --platform=$${BUILDPLATFORM} $${base_image_%s} as %s_builder\n' "$${os_name}" "$${os_name}" >> images/Dockerfile ; \
printf 'ADD install-tools.sh .\nRUN chmod +x install-tools.sh && ./install-tools.sh\n' >> images/Dockerfile ;\
printf '\n' >> images/Dockerfile ; \
done ; \
printf 'FROM alpine:3\n' >> images/Dockerfile ;\
for OS in $${OS_LIST} ; do \
os_name=`echo $${OS} | awk -F '|' '{print $$1}'` ; \
printf 'RUN mkdir -p /host/%s/usr/bin && mkdir -p /host/%s/usr/lib\n' "$${os_name}" "$${os_name}" >> images/Dockerfile ; \
printf 'COPY --from=%s_builder /host/%s/usr/bin /host/%s/usr/bin\n' "$${os_name}" "$${os_name}" "$${os_name}" >> images/Dockerfile ; \
printf 'COPY --from=%s_builder /host/%s/usr/lib/libsmc-preload.so /host/%s/usr/lib\n' "$${os_name}" "$${os_name}" "$${os_name}" >> images/Dockerfile ; \
printf '\n' >> images/Dockerfile ; \
done ;\
printf 'WORKDIR /host/\n' >> images/Dockerfile ; \
printf 'ADD modules /host/modules\n' >> images/Dockerfile ; \
printf 'ADD entrypoint.sh /host/\n' >> images/Dockerfile ;\
printf 'ADD smc-os-list.json /host/\n' >> images/Dockerfile ;\
printf 'RUN chmod +x /host/entrypoint.sh\n' >> images/Dockerfile ;\
printf '\n' >> images/Dockerfile; \
printf 'ENTRYPOINT ["sh","entrypoint.sh"]\n' >> images/Dockerfile

.PHONY: build_image
build_image: update_dockerfile
$(CONTAINER_RUNTIME) buildx build --build-arg GIT_COMMIT_VERSION=$(GIT_COMMIT_VERSION) \
--build-arg GIT_COMMIT_TIME=$(GIT_COMMIT_TIME) \
--build-arg VERSION=$(GIT_COMMIT_VERSION) \
--file images/Dockerfile \
--output type=docker --tag $(IMAGE_REGISTER)/$(GIT_REPO):$(GIT_TAG) images
10 changes: 10 additions & 0 deletions Makefile.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#----------build-image--------
IMAGE_REGISTER ?= ghcr.io
GIT_REPO ?= daocloud/netmaterial

GIT_COMMIT_VERSION = $(shell git show -s --format='format:%H')
GIT_COMMIT_TIME = $(shell git show -s --format='format:%aI')
GIT_BRANCH = $(shell git branch --show-current)
GIT_TAG ?= dev

CONTAINER_RUNTIME ?= docker
46 changes: 46 additions & 0 deletions images/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# this file is entirly generated by makefile
ARG base_image_ubuntu2004=ubuntu:20.04
ARG base_image_ubuntu2204=ubuntu:22.04
ARG base_image_centos8=quay.io/centos/centos:stream8
ARG base_image_centos9=quay.io/centos/centos:stream9

FROM --platform=${BUILDPLATFORM} ${base_image_ubuntu2004} as ubuntu2004_builder
ADD install-tools.sh .
RUN chmod +x install-tools.sh && ./install-tools.sh

FROM --platform=${BUILDPLATFORM} ${base_image_ubuntu2204} as ubuntu2204_builder
ADD install-tools.sh .
RUN chmod +x install-tools.sh && ./install-tools.sh

FROM --platform=${BUILDPLATFORM} ${base_image_centos8} as centos8_builder
ADD install-tools.sh .
RUN chmod +x install-tools.sh && ./install-tools.sh

FROM --platform=${BUILDPLATFORM} ${base_image_centos9} as centos9_builder
ADD install-tools.sh .
RUN chmod +x install-tools.sh && ./install-tools.sh

FROM alpine:3
RUN mkdir -p /host/ubuntu2004/usr/bin && mkdir -p /host/ubuntu2004/usr/lib
COPY --from=ubuntu2004_builder /host/ubuntu2004/usr/bin /host/ubuntu2004/usr/bin
COPY --from=ubuntu2004_builder /host/ubuntu2004/usr/lib/libsmc-preload.so /host/ubuntu2004/usr/lib

RUN mkdir -p /host/ubuntu2204/usr/bin && mkdir -p /host/ubuntu2204/usr/lib
COPY --from=ubuntu2204_builder /host/ubuntu2204/usr/bin /host/ubuntu2204/usr/bin
COPY --from=ubuntu2204_builder /host/ubuntu2204/usr/lib/libsmc-preload.so /host/ubuntu2204/usr/lib

RUN mkdir -p /host/centos8/usr/bin && mkdir -p /host/centos8/usr/lib
COPY --from=centos8_builder /host/centos8/usr/bin /host/centos8/usr/bin
COPY --from=centos8_builder /host/centos8/usr/lib/libsmc-preload.so /host/centos8/usr/lib

RUN mkdir -p /host/centos9/usr/bin && mkdir -p /host/centos9/usr/lib
COPY --from=centos9_builder /host/centos9/usr/bin /host/centos9/usr/bin
COPY --from=centos9_builder /host/centos9/usr/lib/libsmc-preload.so /host/centos9/usr/lib

WORKDIR /host/
ADD modules /host/modules
ADD entrypoint.sh /host/
ADD smc-os-list.json /host/
RUN chmod +x /host/entrypoint.sh

ENTRYPOINT ["sh","entrypoint.sh"]
68 changes: 68 additions & 0 deletions images/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/sh

set -e

check_kernel() {
printf "[Step 1] Checking the kernel version is if too old.\n"
KERNEL_VERSION=$(uname -r)
KERNEL_MAJOR_VERSION=$(echo "${KERNEL_VERSION}" | awk -F . '{print $1}')
KERNEL_MINOR_VERSION=$(echo "${KERNEL_VERSION}" | awk -F . '{print $2}')

# requeir for kernel version > 4.11
if { [ "$KERNEL_MAJOR_VERSION" -eq 4 ] && [ "$KERNEL_MINOR_VERSION" -le 11 ] ; } || [ "$KERNEL_MAJOR_VERSION" -lt 4 ] ; then
printf "[Step 1] kernel version: %s is less than 4.11, the kernel don't support smc.\n" "$KERNEL_VERSION"
exit 1
fi

printf "[Step 1] kernel version %s is ok, Done.\n" "${KERNEL_VERSION}"
}

load_module() {
printf "[Step 3] Try to load the kernel module.\n"
# try to load the module, exit 0 with no error.
# if any error happened, check the kernel version is
if modprobe smc 2>&1 ; then
echo "[Step 3] Success to load the kernel module, exit code 0."
return
fi

KERNEL_VERSION=$(uname -r)
OFED_VERSION=$(sh /usr/bin/ofed_info -s | awk -F 'MLNX_OFED_LINUX-' '{print $2}' | tr -d :)
echo "[Step 3] ofed driver ${OFED_VERSION} has installed, try to load compiled module."
if [ -e "modules/${KERNEL_VERSION}/${OFED_VERSION}/smc.ko" ]; then
modprobe -r smc
insmod modules/${KERNEL_VERSION}/${OFED_VERSION}/smc.ko
modprobe smc
else
echo "[Step 3] No compile smc module found: can't load smc module. exit code 1."
exit 1
fi
printf "[Step 3] Done.\n"
}

coyp_files() {
printf "[Step 2] Copy the library files and module to host.\n"
if [ -e "/usr/lib/libsmc-preload.so" ] || [ -e "/usr/lib64/libsmc-preload.so" ]; then
printf "[Step 2] libsmc-preload.so has found on host, skip copy.\n"
return
fi

OS=$(grep -E '^ID=(.*)' /host/etc/os-release | awk -F '=' '{print $2}' | tr -d '.')
VERSION_ID=$(grep -E '^VERSION_ID=(.*)' /host/etc/os-release | awk -F '=' '{print $2}' | tr -d '.')

cp /host/${OS}${VERSION_ID}/usr/lib/libsmc-preload.so /usr/lib/
ln -s /usr/lib/libsmc-preload.so /usr/lib/libsmc-preload.so.1
cp /host/${OS}${VERSION_ID}/usr/lib/libsmc-preload.so /usr/lib64/
ln -s /usr/lib64/libsmc-preload.so /usr/lib64/libsmc-preload.so.1
cp -f /host/${OS}${VERSION_ID}/usr/bin/smc* /usr/bin/
printf "[Step 2] Done.\n"
}

# 1. check the kernel version if >= 4.11, Or exit 1
check_kernel

# 2. check if the so exist. if not: copy the so from init-container to /usr/lib/xxx.
coyp_files

# 3. try to load the kernel module: modprobe smc
load_module
30 changes: 30 additions & 0 deletions images/install-tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

set -x
set -e

OS=$(grep -E '^ID=(.*)' /etc/os-release | awk -F '=' '{print $2}' | tr -d '"')
VERSION_ID=$(grep -E '^VERSION_ID=(.*)' /etc/os-release | awk -F '=' '{print $2}' | tr -d '"' | tr -d '.')
mkdir -p /host/${OS}${VERSION_ID}/usr/bin && mkdir -p /host/${OS}${VERSION_ID}/usr/lib
case "${OS}" in
ubuntu)
apt-get update
apt-get install -y kmod
apt-get install -y smc-tools
if [ "${VERSION_ID}" == "2004" ]; then
cp /usr/lib/x86_64-linux-gnu/libsmc-preload.so /host/${OS}${VERSION_ID}/usr/lib
else
cp /usr/lib/libsmc-preload.so /host/${OS}${VERSION_ID}/usr/lib
fi
;;
centos|fedora)
yum install -y smc-tools
cp /usr/lib64/libsmc-preload.so /host/${OS}${VERSION_ID}/usr/lib
;;
*)
echo "Unsupported OS: ${OS}${VERSION_ID}."
exit 1
;;
esac

cp /usr/bin/smc* /host/${OS}${VERSION_ID}/usr/bin
File renamed without changes.
Loading

0 comments on commit 12508ea

Please sign in to comment.