Skip to content

Commit

Permalink
feat: add action that uses eget to fetch tools
Browse files Browse the repository at this point in the history
  • Loading branch information
oclaussen committed Nov 29, 2024
1 parent 960af60 commit 834afb4
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
cinstall "github.com/wabenet/dodfile-syntax/pkg/action/compat/install"
cscript "github.com/wabenet/dodfile-syntax/pkg/action/compat/script"
"github.com/wabenet/dodfile-syntax/pkg/action/copy"
"github.com/wabenet/dodfile-syntax/pkg/action/eget"
"github.com/wabenet/dodfile-syntax/pkg/action/env"
"github.com/wabenet/dodfile-syntax/pkg/action/fetch"
"github.com/wabenet/dodfile-syntax/pkg/action/install"
Expand Down Expand Up @@ -85,6 +86,8 @@ func getByType(t string) (Action, error) {
return &base.Action{}, nil
case copy.Type:
return &copy.Action{}, nil
case eget.Type:
return &eget.Action{}, nil
case env.Type:
return &env.Action{}, nil
case fetch.Type:
Expand Down
78 changes: 78 additions & 0 deletions pkg/action/eget/eget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package eget

import (
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/frontend/dockerfile/dockerfile2llb"
"github.com/wabenet/dodfile-syntax/pkg/state"
)

const (
Type = "eget"

defaultBaseImage = "debian"
egetInstallerURL = "https://zyedidia.github.io/eget.sh"
egetInstallerSHA = "0e64b8a3c13f531da005096cc364ac77835bda54276fedef6c62f3dbdc1ee919"
egetInstallerPath = "/tmp/eget.sh"
egetDownloadDir = "/tmp/eget/"
)

type Action struct {
Repo string `mapstructure:"repo"`

Tag string `mapstructure:"tag"`
Prerelease bool `mapstructure:"pre-release"`
ExtractFile string `mapstructure:"file"`
All bool `mapstructure:"all"`
Asset []string `mapstructure:"asset"`
VerifySHA256 string `mapstructure:"verify-sha256"`
}

func (a *Action) Type() string {
return Type
}

func (a *Action) Execute(base llb.State) (llb.State, error) {
downloader := state.From(defaultBaseImage)
downloader.Install("apt-transport-https", "curl", "ca-certificates", "tar")
downloader.Exec("/usr/bin/curl", "-o", egetInstallerPath, egetInstallerURL)
downloader.Sh("echo \"%s %s\" | sha256sum -c -", egetInstallerSHA, egetInstallerPath)
downloader.Exec("/bin/sh", egetInstallerPath)
downloader.Exec("/bin/mkdir", "-p", egetDownloadDir)
downloader.Cwd(egetDownloadDir)

egetCmd := []string{"/eget", a.Repo}

if a.Tag != "" {
egetCmd = append(egetCmd, "--tag", a.Tag)
}

if a.Prerelease {
egetCmd = append(egetCmd, "--pre-release")
}

if a.ExtractFile != "" {
egetCmd = append(egetCmd, "--file", a.ExtractFile)
}

if a.All {
egetCmd = append(egetCmd, "--all")
}

for _, asset := range a.Asset {
egetCmd = append(egetCmd, "--asset", asset)
}

if a.VerifySHA256 != "" {
egetCmd = append(egetCmd, "--verify-sha256", a.VerifySHA256)
}

downloader.Exec(egetCmd...)

s := state.FromLLB(defaultBaseImage, base)
s.CopyDir(downloader, egetDownloadDir, "/bin")

return s.Get(), nil

}

func (a *Action) UpdateImage(_ *dockerfile2llb.Image) {}
3 changes: 2 additions & 1 deletion pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"runtime"

"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
Expand All @@ -26,7 +27,7 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
st := llb.Image(defaultBaseImage)

metadata := dockerfile2llb.Image{}
metadata.Image = specs.Image{Architecture: "amd64", OS: "linux"}
metadata.Image = specs.Image{Architecture: runtime.GOARCH, OS: "linux"}
metadata.RootFS.Type = "layers"
metadata.Config.Env = []string{fmt.Sprintf("PATH=%s", system.DefaultPathEnv)}

Expand Down
3 changes: 3 additions & 0 deletions pkg/build/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
cinstall "github.com/wabenet/dodfile-syntax/pkg/action/compat/install"
cscript "github.com/wabenet/dodfile-syntax/pkg/action/compat/script"
"github.com/wabenet/dodfile-syntax/pkg/action/copy"
"github.com/wabenet/dodfile-syntax/pkg/action/eget"
"github.com/wabenet/dodfile-syntax/pkg/action/env"
"github.com/wabenet/dodfile-syntax/pkg/action/fetch"
"github.com/wabenet/dodfile-syntax/pkg/action/install"
Expand All @@ -33,6 +34,7 @@ func ParseConfig(input []byte) (Image, error) {
cscript.Type: {},
base.Type: {},
copy.Type: {},
eget.Type: {},
env.Type: {},
fetch.Type: {},
install.Type: {},
Expand Down Expand Up @@ -61,6 +63,7 @@ func ParseConfig(input []byte) (Image, error) {
sorted = append(sorted, actionsByType[user.Type]...)
sorted = append(sorted, actionsByType[cdownload.Type]...)
sorted = append(sorted, actionsByType[fetch.Type]...)
sorted = append(sorted, actionsByType[eget.Type]...)
sorted = append(sorted, actionsByType[ccopy.Type]...)
sorted = append(sorted, actionsByType[copy.Type]...)
sorted = append(sorted, actionsByType[cinstall.Type]...)
Expand Down
9 changes: 9 additions & 0 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func (s *State) Copy(src *State, srcPath string, destPath string) {
s.current = execState.AddMount("/dest", s.current)
}

func (s *State) CopyDir(src *State, srcPath string, destPath string) {
execState := llb.Image(s.baseImage).Run(llb.Args([]string{"/bin/mkdir", "-p", path.Join("/dest", path.Dir(destPath))}))
s.current = execState.AddMount("/dest", s.current)

execState = s.current.Run(llb.Args([]string{"/bin/cp", "-a", "-R", path.Join("/src", srcPath) + "/.", "-t", path.Join("/dest", destPath)}))
execState.AddMount("/src", src.current, llb.Readonly)
s.current = execState.AddMount("/dest", s.current)
}

func updateCmd() []string {
return []string{"/usr/bin/apt-get", "update"}
}
Expand Down

0 comments on commit 834afb4

Please sign in to comment.