Skip to content

Commit

Permalink
feat: add initial support for finalize plugins
Browse files Browse the repository at this point in the history
waiter waiter! more breaking changes please!! (this is a breaking
change)
  • Loading branch information
axtloss committed Aug 9, 2024
1 parent e1c5822 commit 079709e
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 8 deletions.
19 changes: 19 additions & 0 deletions api/finalize-scopes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package api

// / Get the final tagged Image name
var IMAEGNAME = 1

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

// / Get the build recipe
var RECIPE = 4

// / Get a read-write filesystem of the Image
var RWFS = 8

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

// / Prepare the filesystem to be chrooted into, requires either RWFilesystem or ROFilesystem
var CHROOTFS = 32
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 := LoadPlugin(module.Type, api.BuildPlugin, moduleInterface, recipe)
if err != nil {
return "", err
}
Expand Down
34 changes: 32 additions & 2 deletions core/plugins.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "os"

var openedPlugins map[string]Plugin

func LoadPlugin(name string, module interface{}, recipe *api.Recipe) (string, error) {
func LoadPlugin(name string, plugintype api.PluginType, module interface{}, recipe *api.Recipe) (string, error) {
if openedPlugins == nil {
openedPlugins = make(map[string]Plugin)
}
Expand All @@ -26,7 +26,7 @@ func LoadPlugin(name string, module interface{}, recipe *api.Recipe) (string, er

localPluginPath := fmt.Sprintf("%s/%s.so", recipe.PluginPath, name)

globalPluginPath := fmt.Sprintf("$INSTALLPREFIX$/share/vib/plugins/%s.so", name)
globalPluginPath := fmt.Sprintf("%INSTALLPREFIX%/share/vib/plugins/%s.so", name)

// Prefer local plugins before global ones
var loadedPlugin uintptr
Expand All @@ -43,6 +43,36 @@ func LoadPlugin(name string, module interface{}, recipe *api.Recipe) (string, er
}
}

infoLoc, err := purego.Dlsym(loadedPlugin, "PlugInfo")
if err != nil && !strings.Contains(err.Error(), "undefined symbol: PlugInfo") {
fmt.Println(err)
return "", err
}

pluginInfo := &api.PluginInfo{}

if infoLoc == 0 {
fmt.Println("== WARN ==")
fmt.Printf("Plugin %s does not contain function PlugInfo, assuming old BuildPlugin type\n", name)
fmt.Printf("Please update the plugin or request the developer of the plugin to update it!\n")
fmt.Println("== WARN ==")
pluginInfo.Name = name
pluginInfo.Type = api.BuildPlugin
} else {
var pluginInfoFunc func() string
purego.RegisterLibFunc(&pluginInfoFunc, loadedPlugin, "PlugInfo")
json.Unmarshal([]byte(pluginInfoFunc()), &pluginInfo)
}

if pluginInfo.Type != plugintype {
if plugintype == api.BuildPlugin {
fmt.Printf("ERROR: Plugin %s is not of type BuildPlugin", name)
os.Exit(1)
} else if plugintype == api.FinalizePlugin {
fmt.Printf("ERROR: Plugin %s is not of type FinalizePlugin", name)
os.Exit(1)
}
}
var buildFunction func(*C.char, *C.char) string
purego.RegisterLibFunc(&buildFunction, loadedPlugin, "BuildModule")
buildModule.BuildFunc = buildFunction
Expand Down
37 changes: 37 additions & 0 deletions finalize-plugins/docker-remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"encoding/json"
"fmt"

"C"

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

type DockerRemote struct {
Name string `json:"name"`
Type string `json:"type"`
OCIRegistry string `json:"registry"`
ImageName string `json:"imagename"`
ImageTag string `json:"imagetag"`
}

//export PluginScope
func PluginScope() int32 { // int32 is defined as GoInt32 in cgo which is the same as a C int
return int32(api.IMAEGNAME)
}

//export FinalizeBuild
func FinalizeBuild(moduleInterface *C.char, extraData *C.char) *C.char {
var module *DockerRemote

err := json.Unmarshal([]byte(C.GoString(moduleInterface)), &module)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

return C.CString("ERROR: failed to upload image")
}

func main() {}
10 changes: 10 additions & 0 deletions plugins/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ type AptOptions struct {
FixBroken bool `json:"fix_broken"`
}

//export PlugInfo
func PlugInfo() *C.char {
plugininfo := &api.PluginInfo{Name: "apt", Type: api.BuildPlugin}
pluginjson, err := json.Marshal(plugininfo)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
return C.CString(string(pluginjson))
}

// BuildAptModule builds a module that installs packages
// using the apt package manager
//
Expand Down
10 changes: 10 additions & 0 deletions plugins/cmake.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ type CMakeModule struct {
Source api.Source
}

//export PlugInfo
func PlugInfo() *C.char {
plugininfo := &api.PluginInfo{Name: "cmake", Type: api.BuildPlugin}
pluginjson, err := json.Marshal(plugininfo)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
return C.CString(string(pluginjson))
}

// BuildCMakeModule builds a module that builds a CMake project
//
//export BuildModule
Expand Down
18 changes: 13 additions & 5 deletions plugins/dpkg-buildpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"C"
"encoding/json"
"fmt"
"path/filepath"
"encoding/json"

"github.com/vanilla-os/vib/api"
)
Expand All @@ -15,14 +15,24 @@ type DpkgBuildModule struct {
Source api.Source
}

//export PlugInfo
func PlugInfo() *C.char {
plugininfo := &api.PluginInfo{Name: "dpkg-buildpackage", Type: api.BuildPlugin}
pluginjson, err := json.Marshal(plugininfo)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
return C.CString(string(pluginjson))
}

// BuildDpkgModule builds a module that builds a dpkg project
// and installs the resulting .deb package
//
//export BuildModule
func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {
var module *DpkgBuildModule
var recipe *api.Recipe


err := json.Unmarshal([]byte(C.GoString(moduleInterface)), &module)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
Expand Down Expand Up @@ -55,6 +65,4 @@ func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {
return C.CString(cmd)
}



func main() { fmt.Println("This plugin is not meant to run standalone!"); }
func main() { fmt.Println("This plugin is not meant to run standalone!") }
10 changes: 10 additions & 0 deletions plugins/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ type GoModule struct {
BuildFlags string
}

//export PlugInfo
func PlugInfo() *C.char {
plugininfo := &api.PluginInfo{Name: "go", Type: api.BuildPlugin}
pluginjson, err := json.Marshal(plugininfo)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
return C.CString(string(pluginjson))
}

// BuildGoModule builds a module that builds a Go project
// buildVars are used to customize the build command
// like setting the output binary name and location
Expand Down
10 changes: 10 additions & 0 deletions plugins/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ type MakeModule struct {
Source api.Source
}

//export PlugInfo
func PlugInfo() *C.char {
plugininfo := &api.PluginInfo{Name: "make", Type: api.BuildPlugin}
pluginjson, err := json.Marshal(plugininfo)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
return C.CString(string(pluginjson))
}

// BuildMakeModule builds a module that builds a Make project
//
//export BuildModule
Expand Down
10 changes: 10 additions & 0 deletions plugins/meson.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ type MesonModule struct {
Source api.Source
}

//export PlugInfo
func PlugInfo() *C.char {
plugininfo := &api.PluginInfo{Name: "meson", Type: api.BuildPlugin}
pluginjson, err := json.Marshal(plugininfo)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
return C.CString(string(pluginjson))
}

// BuildMesonModule builds a module that builds a Meson project
//
//export BuildModule
Expand Down

0 comments on commit 079709e

Please sign in to comment.