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

Add a flatpak plugin #80

Merged
merged 2 commits into from
Aug 12, 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
13 changes: 0 additions & 13 deletions core/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"fmt"
"math"
"os"
"os/exec"
"syscall"
Expand Down Expand Up @@ -72,12 +71,6 @@ func compileDocker(recipe api.Recipe, gid int, uid int) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = recipe.ParentPath
cmd.SysProcAttr = &syscall.SysProcAttr{}
if uid > 0 && uid <= math.MaxUint32 && gid > 0 && gid <= math.MaxUint32 {
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}
} else {
return fmt.Errorf("uid and gid exceed uint32")
}

return cmd.Run()
}
Expand All @@ -97,12 +90,6 @@ func compilePodman(recipe api.Recipe, gid int, uid int) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = recipe.ParentPath
cmd.SysProcAttr = &syscall.SysProcAttr{}
if uid > 0 && uid <= math.MaxUint32 && gid > 0 && gid <= math.MaxUint32 {
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}
} else {
return fmt.Errorf("uid and gid exceed uint32")
}

return cmd.Run()
}
145 changes: 145 additions & 0 deletions plugins/flatpak.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package main

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

"github.com/vanilla-os/vib/api"
)
import (
"os"
"path"
"strings"
)

type innerFlatpakModule struct {
Repourl string `json:"repo-url"`
Reponame string `json:"repo-name"`
Install []string `json:"install"`
Remove []string `json:"remove"`
}

type FlatpakModule struct {
Name string `json:"name"`
Type string `json:"type"`
System innerFlatpakModule `json:"system"`
User innerFlatpakModule `json:"user"`
}

var SystemService string = `
[Unit]
Description=Manage system flatpaks
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/bin/system-flatpak-setup
Restart=on-failure
RestartSec=30

[Install]
WantedBy=default.target
`

var UserService string = `
[Unit]
Description=Configure Flatpaks for current user
Wants=network-online.target
After=system-flatpak-setup.service

[Service]
Type=simple
ExecStart=/usr/bin/user-flatpak-setup
Restart=on-failure
RestartSec=30

[Install]
WantedBy=default.target
`

//export PlugInfo
func PlugInfo() *C.char {
plugininfo := &api.PluginInfo{Name: "flatpak", 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))
}

func createRepo(module innerFlatpakModule, isSystem bool) string {
fmt.Println("Adding remote ", isSystem, " ", module)
command := "flatpak remote-add --if-not-exists"
if isSystem {
command = fmt.Sprintf("%s --system", command)
} else {
command = fmt.Sprintf("%s --user", command)
}
return fmt.Sprintf("%s %s %s", command, module.Reponame, module.Repourl)
}

//export BuildModule
func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {
var module *FlatpakModule
var recipe *api.Recipe

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(recipeInterface)), &recipe)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

os.MkdirAll(path.Join(recipe.ParentPath, "includes.container/usr/bin/"), 0o775)
if module.System.Reponame != "" {
syscommands := "#!/usr/bin/env sh"
if module.System.Repourl != "" {
syscommands = fmt.Sprintf("%s\n%s", syscommands, createRepo(module.System, true))
fmt.Println(syscommands)
}
if len(module.System.Install) != 0 {
syscommands = fmt.Sprintf("%s\nflatpak install --system --noninteractive %s %s", syscommands, module.System.Reponame, strings.Join(module.System.Install, " "))
}
if len(module.System.Remove) != 0 {
syscommands = fmt.Sprintf("%s\nflatpak uninstall --system --noninteractive %s %s", syscommands, module.User.Reponame, strings.Join(module.System.Remove, " "))
}

syscommands = fmt.Sprintf("%s\nsystemctl disable flatpak-system-setup.service", syscommands)
err := os.WriteFile(path.Join(recipe.ParentPath, "includes.container/usr/bin/system-flatpak-setup"), []byte(syscommands), 0o777)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

}
if module.User.Reponame != "" {
usercommands := "#!/usr/bin/env sh"
if module.User.Repourl != "" {
usercommands = fmt.Sprintf("%s\n%s", usercommands, createRepo(module.User, false))
fmt.Println(usercommands)
}
if len(module.User.Install) != 0 {
usercommands = fmt.Sprintf("%s\nflatpak install --user --noninteractive %s", usercommands, strings.Join(module.User.Install, " "))
}
if len(module.User.Remove) != 0 {
usercommands = fmt.Sprintf("%s\nflatpak uninstall --user --noninteractive %s", usercommands, strings.Join(module.User.Remove, " "))
}

err := os.WriteFile(path.Join(recipe.ParentPath, "includes.container/usr/bin/user-flatpak-setup"), []byte(usercommands), 0o777)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
}
os.MkdirAll(path.Join(recipe.ParentPath, "includes.container/etc/systemd/user"), 0o775)
os.MkdirAll(path.Join(recipe.ParentPath, "includes.container/etc/systemd/system"), 0o775)
os.WriteFile(path.Join(recipe.ParentPath, "includes.container/etc/systemd/user/flatpak-user-setup.service"), []byte(UserService), 0o666)
os.WriteFile(path.Join(recipe.ParentPath, "includes.container/etc/systemd/system/flatpak-system-setup.service"), []byte(SystemService), 0o666)

return C.CString("systemctl enable --global flatpak-user-setup.service && systemctl enable --system flatpak-system-setup.service")
}

func main() { fmt.Println("This plugin is not meant to run standalone!") }
Loading