Skip to content

Commit

Permalink
add a util to smartly link files with prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Nianyu Shen <[email protected]>
  • Loading branch information
nianyush committed Mar 6, 2024
1 parent fa19a33 commit b2870f5
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .earthlyignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
local/
build/*
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ content-*/*
*.arg
.idea

.DS_Store
.DS_Store

build/
local/
86 changes: 72 additions & 14 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ARG https_proxy=${HTTPS_PROXY}
ARG no_proxy=${NO_PROXY}
ARG PROXY_CERT_PATH
ARG UPDATE_KERNEL=false
ARG IS_UKI=false

ARG ETCD_VERSION="v3.5.5"

Expand All @@ -54,6 +55,15 @@ IF [[ "$BASE_IMAGE" =~ "ubuntu-20-lts-arm-nvidia-jetson-agx-orin" ]]
ARG IS_JETSON=true
END

IF [ "$FIPS_ENABLED" = "true" ]
ARG STYLUS_BASE=gcr.io/spectro-images-public/stylus-framework-fips-linux-$ARCH:$PE_VERSION
ELSE
ARG STYLUS_BASE=gcr.io/spectro-images-public/stylus-framework-linux-$ARCH:$PE_VERSION
END

ARG IMAGE_PATH=$IMAGE_REGISTRY/$IMAGE_REPO:$K8S_DISTRIBUTION-$PE_VERSION


build-all-images:
IF $FIPS_ENABLED
BUILD +build-provider-images-fips
Expand Down Expand Up @@ -142,6 +152,60 @@ uki-iso:
COPY --platform=linux/${ARCH} (+build-uki-iso/ --ISO_NAME=$ISO_NAME) .
SAVE ARTIFACT /build/* AS LOCAL ./build/

uki-provider-image:
FROM scratch
WORKDIR /

COPY --platform=linux/${ARCH} +trust-boot-unpack/ /trusted-boot
COPY --platform=linux/${ARCH} +install-k8s/ /k8s
SAVE IMAGE --push $IMAGE_PATH

trust-boot-unpack:
COPY +luet/luet /usr/bin/luet
COPY --platform=linux/${ARCH} +build-provider-trustedboot-image/ /image
RUN FILE="file:/$(find /image -type f -name "*.tar" | head -n 1)" && \
luet util unpack $FILE /trusted-boot
SAVE ARTIFACT /trusted-boot/*

luet:
FROM quay.io/luet/base:latest
SAVE ARTIFACT /usr/bin/luet /luet

install-k8s:
FROM alpine
COPY +luet/luet /usr/bin/luet
ARG K8S_VERSION=1.26.4

IF [ "$K8S_DISTRIBUTION" = "kubeadm" ] || [ "$K8S_DISTRIBUTION" = "kubeadm-fips" ]
ARG BASE_K8S_VERSION=$K8S_VERSION
ELSE IF [ "$K8S_DISTRIBUTION" = "k3s" ]
ARG K8S_DISTRIBUTION_TAG=$K3S_FLAVOR_TAG
ARG BASE_K8S_VERSION=$K8S_VERSION-$K8S_DISTRIBUTION_TAG
ELSE IF [ "$K8S_DISTRIBUTION" = "rke2" ]
ARG K8S_DISTRIBUTION_TAG=$RKE2_FLAVOR_TAG
ARG BASE_K8S_VERSION=$K8S_VERSION-$K8S_DISTRIBUTION_TAG
END

WORKDIR /output
RUN mkdir -p /etc/luet/repos.conf.d && \
luet repo add spectro --type docker --url gcr.io/spectro-dev-public/luet-repo --priority 1 -y && \
luet repo update
RUN luet install -y k8s/$K8S_DISTRIBUTION@$BASE_K8S_VERSION --system-target /output && luet cleanup
RUN rm -rf /output/var/cache/*
SAVE ARTIFACT /output/*

internal-slink:
FROM alpine
COPY internal/slink/slink /slink
RUN chmod +x /slink
SAVE ARTIFACT /slink

stylus-image-pack:
COPY +luet/luet /usr/bin/luet
COPY --platform=linux/${ARCH} +stylus-image/ /stylus
RUN tar -czf /stylus.tar.gz /stylus
RUN luet util pack $STYLUS_BASE /stylus.tar.gz /stylus-image.tar
SAVE ARTIFACT stylus-image.tar

build-uki-iso:
ARG ISO_NAME
Expand All @@ -150,6 +214,7 @@ build-uki-iso:
ENV ISO_NAME=${ISO_NAME}
COPY overlay/files-iso/ /overlay/
COPY --if-exists user-data /overlay/config.yaml
COPY --platform=linux/${ARCH} +stylus-image-pack/ /overlay/data/stylus
COPY --if-exists content-*/*.zst /overlay/opt/spectrocloud/content/
#check if clusterconfig is passed in
IF [ "$CLUSTERCONFIG" != "" ]
Expand Down Expand Up @@ -256,15 +321,13 @@ provider-image:
COPY +stylus-image/etc/kairos/branding /etc/kairos/branding
COPY +stylus-image/oem/stylus_config.yaml /etc/kairos/branding/stylus_config.yaml
COPY +stylus-image/etc/elemental/config.yaml /etc/elemental/config.yaml
IF [ "$K8S_DISTRIBUTION" = "kubeadm" ]
RUN luet install -y container-runtime/containerd
END

IF [ "$K8S_DISTRIBUTION" = "kubeadm-fips" ]
RUN luet install -y container-runtime/containerd-fips
END
COPY +internal-slink/slink /usr/bin/slink
COPY +install-k8s/ /k8s
RUN slink --source /k8s/ --target /opt/k8s
RUN rm -f /usr/bin/slink
RUN rm -rf /k8s

RUN luet install -y k8s/$K8S_DISTRIBUTION@$BASE_K8S_VERSION && luet cleanup
RUN rm -f /etc/ssh/ssh_host_* /etc/ssh/moduli

COPY (+download-etcdctl/etcdctl) /usr/bin/
Expand All @@ -287,11 +350,6 @@ build-provider-trustedboot-image:
SAVE ARTIFACT /output/* AS LOCAL ./trusted-boot/

stylus-image:
IF [ "$FIPS_ENABLED" = "true" ]
ARG STYLUS_BASE=gcr.io/spectro-images-public/stylus-framework-fips-linux-$ARCH:$PE_VERSION
ELSE
ARG STYLUS_BASE=gcr.io/spectro-images-public/stylus-framework-linux-$ARCH:$PE_VERSION
END
FROM $STYLUS_BASE
SAVE ARTIFACT ./*
SAVE ARTIFACT /etc/kairos/branding
Expand Down Expand Up @@ -352,7 +410,7 @@ base-image:

RUN apt update && \
apt install --no-install-recommends kbd zstd vim iputils-ping bridge-utils curl tcpdump ethtool -y
IF [[ ! $BASE_IMG =~ "uki" ]]
IF [ "$IS_UKI" = "false" ]
IF [ "$UPDATE_KERNEL" = "false" ]
RUN if dpkg -l linux-image-generic-hwe-20.04 > /dev/null; then apt-mark hold linux-image-generic-hwe-20.04; fi && \
if dpkg -l linux-image-generic-hwe-22.04 > /dev/null; then apt-mark hold linux-image-generic-hwe-22.04; fi && \
Expand Down Expand Up @@ -442,7 +500,7 @@ base-image:
# Used to build the installer image. The installer ISO will be created from this.
iso-image:
FROM --platform=linux/${ARCH} +base-image
COPY --platform=linux/${ARCH} +stylus-image/ /
# COPY --platform=linux/${ARCH} +stylus-image/ /
COPY overlay/files/ /

RUN rm -f /etc/ssh/ssh_host_* /etc/ssh/moduli
Expand Down
15 changes: 15 additions & 0 deletions internal/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/spectrocloud/CanvOS/internal

go 1.22.0

require (
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.8.0
github.com/twpayne/go-vfs/v4 v4.3.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.5.0 // indirect
)
27 changes: 27 additions & 0 deletions internal/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/twpayne/go-vfs/v4 v4.3.0 h1:rTqFzzOQ/6ESKTSiwVubHlCBedJDOhQyVSnw8rQNZhU=
github.com/twpayne/go-vfs/v4 v4.3.0/go.mod h1:tq2UVhnUepesc0lSnPJH/jQ8HruGhzwZe2r5kDFpEIw=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Binary file added internal/slink/slink
Binary file not shown.
93 changes: 93 additions & 0 deletions internal/slink/slink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"os"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/twpayne/go-vfs/v4"
)

func slink(cmd *cobra.Command, args []string) {
source := cmd.Flag("source").Value.String()
target := cmd.Flag("target").Value.String()
logrus.Infof("Source: %s, Target: %s", source, target)
if source == "" || target == "" {
logrus.Fatal("Source and target are required")
}

sourceFS := vfs.NewPathFS(vfs.OSFS, source)

if err := vfs.Walk(sourceFS, "/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}

// Create symlink
linkname := path
targetname := filepath.Join(target, path)
logrus.Infof("Creating link %s to %s", linkname, targetname)

if err := copyDir(filepath.Dir(linkname), sourceFS, vfs.OSFS); err != nil {
logrus.Error(err)
return err
}

if err := os.Symlink(targetname, linkname); err != nil {
logrus.Error(err)
return err
}

return nil
}); err != nil {
logrus.Fatal(err)
}
}

func copyDir(path string, srcFS, dstFS vfs.FS) error {
if exists, err := Exists(dstFS, path); err != nil {
return err
} else if exists {
return nil
}

// Check if parent of path exists
if err := copyDir(filepath.Dir(path), srcFS, dstFS); err != nil {
return err
}

// Get permission of source directory
srcInfo, err := srcFS.Stat(path)
if err != nil {
return err
}
// Create directory with same permissions as source
if err := vfs.MkdirAll(dstFS, path, srcInfo.Mode()); err != nil {
return err
}
return nil
}

func Exists(fs vfs.FS, path string) (bool, error) {
_, err := fs.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func main() {
cmd := &cobra.Command{
Use: "slink",
Run: slink,
}
cmd.Flags().StringP("source", "s", "", "source directory")
cmd.Flags().StringP("target", "t", "", "target prefix")
cmd.Execute()
}

0 comments on commit b2870f5

Please sign in to comment.