Skip to content

Commit

Permalink
feat: add support for finalize plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
axtloss committed Aug 9, 2024
1 parent 99c107c commit ff825a3
Show file tree
Hide file tree
Showing 21 changed files with 1,366 additions and 48 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ on:
jobs:

build:
name: Build
runs-on: ubuntu-latest
container:
image: ghcr.io/vanilla-os/pico:main

steps:
- uses: actions/checkout@v4
Expand All @@ -19,6 +22,11 @@ jobs:
with:
go-version: 1.22

- name: Install Build Dependencies
run: |
apt-get update
apt-get install -y libbtrfs-dev libdevmapper-dev pkg-config build-essential
- name: Build vib
run: |
go get ./...
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
jobs:
build:
runs-on: ubuntu-latest
container:
image: ghcr.io/vanilla-os/pico:main
permissions:
contents: write # to upload assets to releases
attestations: write # to upload assets attestation for build provenance
Expand All @@ -21,6 +23,11 @@ jobs:
with:
go-version: 1.22

- name: Install Build Dependencies
run: |
apt-get update
apt-get install -y libbtrfs-dev libdevmapper-dev pkg-config build-essential
- name: Build
run: |
go get ./...
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ build:
build-plugins: FORCE
mkdir -p build/plugins
$(MAKE) -C plugins/
$(MAKE) -C finalize-plugins/

install:
install: build
install -Dm755 -t ${DESTDIR}/${PREFIX}/bin/ ./build/${BINARY_NAME}

install-plugins:
install-plugins: build-plugins
install -Dm644 -t ${DESTDIR}/${PREFIX}/share/vib/plugins/ ./build/plugins/*.so

clean:
Expand Down
24 changes: 24 additions & 0 deletions api/finalize-scopes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package api

// / Get the final tagged Image name
var IMAGENAME int32 = 1

// / Get the final tagged Image ID
var IMAGEID int32 = 2

// / Get the build recipe
var RECIPE int32 = 4

// Get the used build runtime
var RUNTIME int32 = 8

// / Get a read-only filesystem of the Image
var FS int32 = 16

type ScopeData struct {
ImageName string
ImageID string
Recipe Recipe
Runtime string
FS string
}
13 changes: 13 additions & 0 deletions api/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Recipe struct {
SourcesPath string
PluginPath string
Containerfile string
Finalize []interface{}
}

type Stage struct {
Expand All @@ -40,6 +41,18 @@ type Stage struct {
Entrypoint Entrypoint
}

type PluginType int

const (
BuildPlugin PluginType = iota
FinalizePlugin
)

type PluginInfo struct {
Name string
Type PluginType
}

type Copy struct {
From string
SrcDst map[string]string
Expand Down
2 changes: 1 addition & 1 deletion core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func BuildModule(recipe *api.Recipe, moduleInterface interface{}) (string, error
}
commands = commands + " && " + command
} else {
command, err := LoadPlugin(module.Type, moduleInterface, recipe)
command, err := LoadBuildPlugin(module.Type, moduleInterface, recipe)
if err != nil {
return "", err
}
Expand Down
14 changes: 14 additions & 0 deletions core/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"

"github.com/mitchellh/mapstructure"
"github.com/vanilla-os/vib/api"
)

Expand Down Expand Up @@ -32,6 +33,19 @@ func CompileRecipe(recipePath string, runtime string) error {
return fmt.Errorf("no runtime specified and the prometheus library is not implemented yet")
}

for _, finalizeInterface := range recipe.Finalize {
var module Finalize

err := mapstructure.Decode(finalizeInterface, &module)
if err != nil {
return err
}
err = LoadFinalizePlugin(module.Type, finalizeInterface, &recipe, runtime)
if err != nil {
return err
}
}

fmt.Printf("Image %s built successfully using %s\n", recipe.Id, runtime)

return nil
Expand Down
95 changes: 95 additions & 0 deletions core/finalize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package core

import (
"fmt"
cstorage "github.com/containers/storage"
"os/exec"
"strings"
)

type StorageConf struct {
Driver string
Runroot string
Graphroot string
}

func GetContainerStorage(runtime string) (cstorage.Store, error) {
storageconfig := &StorageConf{}
if runtime == "podman" {
podmanPath, err := exec.LookPath("podman")
out, err := exec.Command(
podmanPath, "info", "-f json").Output()
if err != nil {
fmt.Println("Failed to get podman info")
} else {
driver := strings.Split(strings.Split(string(out), "\"graphDriverName\": \"")[1], "\",")[0]
storageconfig.Driver = driver

graphRoot := strings.Split(strings.Split(string(out), "\"graphRoot\": \"")[1], "\",")[0]
storageconfig.Graphroot = graphRoot

runRoot := strings.Split(strings.Split(string(out), "\"runRoot\": \"")[1], "\",")[0]
storageconfig.Runroot = runRoot
}

}
if storageconfig.Runroot == "" {
storageconfig.Runroot = "/var/lib/vib/runroot"
storageconfig.Graphroot = "/var/lib/vib/graphroot"
storageconfig.Driver = "overlay"
}
store, err := cstorage.GetStore(cstorage.StoreOptions{
RunRoot: storageconfig.Runroot,
GraphRoot: storageconfig.Graphroot,
GraphDriverName: storageconfig.Driver,
})
if err != nil {
return store, err
}

return store, err
}

func GetImageID(name string, store cstorage.Store) (string, error) {
images, err := store.Images()
if err != nil {
return "", err
}
for _, img := range images {
for _, imgname := range img.Names {
if imgname == name {
return img.ID, nil
}
}
}
return "", fmt.Errorf("image not found")
}

func GetTopLayerID(imageid string, store cstorage.Store) (string, error) {
images, err := store.Images()
if err != nil {
return "", err
}
for _, img := range images {
if img.ID == imageid {
return img.TopLayer, nil
}
}
return "", fmt.Errorf("no top layer for id %s found", imageid)
}

func MountImage(imagename string, imageid string, runtime string) (string, error) {
store, err := GetContainerStorage(runtime)
if err != nil {
return "", err
}
topLayerID, err := GetTopLayerID(imageid, store)
if err != nil {
return "", err
}
mountDir, err := store.Mount(topLayerID, "")
if err != nil {
return "", err
}
return mountDir, err
}
Loading

0 comments on commit ff825a3

Please sign in to comment.