Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/expand repart #96

Merged
merged 3 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions finalize-plugins/shell-final.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"C"
"encoding/json"
"fmt"
"github.com/vanilla-os/vib/api"
"os"
"os/exec"
"strings"
)

type Shell struct {
Name string `json:"name"`
Type string `json:"type"`
Commands []string `json:"commands"`
Cwd string `json:"cwd"`
}

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

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

func parsePath(path string, data *api.ScopeData) string {
path = strings.ReplaceAll(path, "$PROJROOT", data.Recipe.ParentPath)
path = strings.ReplaceAll(path, "$FSROOT", data.FS)
return path
}

func baseCommand(command string, data *api.ScopeData) string {
commandParts := strings.Split(command, " ")
if strings.Contains(commandParts[0], "/") {
return parsePath(commandParts[0], data)
} else {
command, err := exec.LookPath(commandParts[0])
if err != nil {
return commandParts[0]
}
return command
}
}

func getArgs(command string, data *api.ScopeData) []string {
commandParts := strings.Split(parsePath(command, data), " ")
return commandParts[1:]
}

func genCommand(command string, data *api.ScopeData) []string {
baseCommand := baseCommand(command, data)
args := getArgs(command, data)
return append(append(append([]string{"-c", "'"}, strings.Join(args, " ")), baseCommand), "'")
}

//export FinalizeBuild
func FinalizeBuild(moduleInterface *C.char, extraData *C.char) *C.char {
var module *Shell
var data *api.ScopeData

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

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

for _, command := range module.Commands {
fmt.Println("shell-final:: bash ", "-c ", command)

cmd := exec.Command(
"bash", "-c", parsePath(command, data),
)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = os.Environ()
if len(strings.TrimSpace(module.Cwd)) == 0 {
cmd.Dir = data.Recipe.ParentPath
} else {
cmd.Dir = parsePath(module.Cwd, data)
}

err = cmd.Run()
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
}

return C.CString("")
}

func main() {}
2 changes: 1 addition & 1 deletion finalize-plugins/sysext.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func FinalizeBuild(moduleInterface *C.char, extraData *C.char) *C.char {

var extensionRelease strings.Builder
fmt.Fprintf(&extensionRelease, "ID=%s\n", module.OSReleaseID)
fmt.Fprintf(&extensionRelease, "VersionID=%s\n", module.OSReleaseVersionID)
fmt.Fprintf(&extensionRelease, "VERSION_ID=%s\n", module.OSReleaseVersionID)

err = os.MkdirAll(filepath.Join(data.FS, "usr/lib/extension-release.d"), 0o777)
if err != nil {
Expand Down
41 changes: 33 additions & 8 deletions finalize-plugins/systemd-repart.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import (
"github.com/vanilla-os/vib/api"
"os"
"os/exec"
"strings"
)

type SystemdRepart struct {
Name string `json:"name"`
Type string `json:"type"`
Output string `json:"output"`
Size string `json:"size"`
Seed string `json:"seed"`
Name string `json:"name"`
Type string `json:"type"`
Output string `json:"output"`
Json string `json:"json"`
SpecOutput string `json:"spec_output"`
Size string `json:"size"`
Seed string `json:"seed"`
Split bool `json:"split"`
DeferPartitions []string `json:"defer_partitions"`
}

//export PlugInfo
Expand Down Expand Up @@ -51,20 +56,40 @@ func FinalizeBuild(moduleInterface *C.char, extraData *C.char) *C.char {
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
cmd := exec.Command(
repart,

if len(strings.TrimSpace(module.Json)) == 0 {
module.Json = "off"
}

args := []string{
"--definitions=definitions",
"--empty=create",
fmt.Sprintf("--size=%s", module.Size),
"--dry-run=no",
"--discard=no",
"--offline=true",
"--no-pager",
fmt.Sprintf("--split=%t", module.Split),
fmt.Sprintf("--seed=%s", module.Seed),
fmt.Sprintf("--root=%s", data.FS),
module.Output,
fmt.Sprintf("--json=%s", module.Json),
}

if len(module.DeferPartitions) > 0 {
args = append(args, fmt.Sprintf("--defer-partitions=%s", strings.Join(module.DeferPartitions, ",")))
}

cmd := exec.Command(
repart,
args...,
)
cmd.Stdout = os.Stdout
jsonFile, err := os.Create(module.SpecOutput)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
defer jsonFile.Close()
cmd.Stdout = jsonFile
cmd.Stderr = os.Stderr
cmd.Dir = data.Recipe.ParentPath

Expand Down
Loading