-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change dnf repo provisioner to include apt repos
- Loading branch information
Showing
10 changed files
with
246 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package entity | ||
|
||
import ( | ||
"fmt" | ||
"github.com/femnad/fup/base/settings" | ||
"github.com/femnad/fup/internal" | ||
"github.com/femnad/fup/precheck" | ||
"github.com/femnad/fup/precheck/unless" | ||
"github.com/femnad/fup/remote" | ||
marecmd "github.com/femnad/mare/cmd" | ||
"io" | ||
"os/exec" | ||
"path" | ||
) | ||
|
||
const ( | ||
keyRingsDir = "/etc/apt/keyrings" | ||
sourcesDir = "/etc/apt/sources.list.d" | ||
) | ||
|
||
type AptRepo struct { | ||
GPGKey string `yaml:"gpg_key"` | ||
RepoName string `yaml:"name"` | ||
Repo string `yaml:"repo"` | ||
When string `yaml:"when"` | ||
} | ||
|
||
func (a AptRepo) DefaultVersionCmd() string { | ||
return "" | ||
} | ||
|
||
func (a AptRepo) GetUnless() unless.Unless { | ||
return unless.Unless{ | ||
Stat: path.Join(sourcesDir, fmt.Sprintf("%s.list", a.RepoName)), | ||
} | ||
} | ||
|
||
func (AptRepo) GetVersion() string { | ||
return "" | ||
} | ||
|
||
func (AptRepo) HasPostProc() bool { | ||
return false | ||
} | ||
|
||
func (a AptRepo) Name() string { | ||
return a.RepoName | ||
} | ||
|
||
func (a AptRepo) RunWhen() string { | ||
return a.When | ||
} | ||
|
||
func (AptRepo) UpdateCmd() string { | ||
return "apt update" | ||
} | ||
|
||
func (AptRepo) ensureKeyFile(keyUrl, keyRingFile string) error { | ||
key, err := remote.ReadResponseBytes(keyUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
gpgCmd := exec.Command("gpg", "--dearmor", "-o", keyRingFile) | ||
stdin, err := gpgCmd.StdinPipe() | ||
if err != nil { | ||
return err | ||
} | ||
defer stdin.Close() | ||
|
||
stdout, err := gpgCmd.StdoutPipe() | ||
if err != nil { | ||
return err | ||
} | ||
defer stdout.Close() | ||
|
||
if err = gpgCmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
_, err = stdin.Write(key) | ||
if err != nil { | ||
return err | ||
} | ||
stdin.Close() | ||
|
||
out, err := io.ReadAll(stdout) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = gpgCmd.Wait(); err != nil { | ||
return err | ||
} | ||
|
||
_, err = internal.WriteContent(internal.ManagedFile{ | ||
Content: string(out), | ||
Path: keyRingFile, | ||
Mode: 0o644, | ||
User: "root", | ||
Group: "root", | ||
}) | ||
return err | ||
} | ||
|
||
func (a AptRepo) Install() error { | ||
err := internal.EnsureDir(keyRingsDir) | ||
if err != nil { | ||
return err | ||
} | ||
keyRingFile := path.Join(keyRingsDir, fmt.Sprintf("%s.gpg", a.RepoName)) | ||
|
||
err = a.ensureKeyFile(a.Repo, keyRingFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out, err := marecmd.RunFormatError(marecmd.Input{Command: "dpkg --print-architecture"}) | ||
if err != nil { | ||
return err | ||
} | ||
architecture := out.Stdout | ||
|
||
versionCodename, err := precheck.GetOSVersionCodename() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
content := fmt.Sprintf("deb [arch=${architecture} signed-by=%s] %s ${codename} stable", keyRingFile, a.Repo) | ||
content = settings.Expand(content, map[string]string{ | ||
"architecture": architecture, | ||
"codename": versionCodename, | ||
}) | ||
repoFile := path.Join(sourcesDir, fmt.Sprintf("%s.list", a.RepoName)) | ||
|
||
_, err = internal.WriteContent(internal.ManagedFile{ | ||
Content: content, | ||
Path: repoFile, | ||
}) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package entity | ||
|
||
import ( | ||
"github.com/femnad/fup/precheck/unless" | ||
"github.com/femnad/fup/precheck/when" | ||
) | ||
|
||
type OSRepo interface { | ||
unless.Unlessable | ||
when.Whenable | ||
Install() error | ||
UpdateCmd() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package provision | ||
|
||
import ( | ||
"errors" | ||
marecmd "github.com/femnad/mare/cmd" | ||
|
||
mapset "github.com/deckarep/golang-set/v2" | ||
|
||
"github.com/femnad/fup/base" | ||
"github.com/femnad/fup/entity" | ||
"github.com/femnad/fup/internal" | ||
"github.com/femnad/fup/precheck/unless" | ||
"github.com/femnad/fup/precheck/when" | ||
) | ||
|
||
func runUpdateCmds(cmds mapset.Set[string]) []error { | ||
var errs []error | ||
|
||
isRoot, err := internal.IsUserRoot() | ||
if err != nil { | ||
return []error{err} | ||
} | ||
|
||
cmds.Each(func(cmd string) bool { | ||
input := marecmd.Input{Command: cmd, Sudo: !isRoot} | ||
_, err = marecmd.RunFormatError(input) | ||
errs = append(errs, err) | ||
return false | ||
}) | ||
|
||
return errs | ||
} | ||
|
||
func addRepos(config base.Config) error { | ||
var errs []error | ||
|
||
var repos []entity.OSRepo | ||
for _, repo := range config.AptRepos { | ||
repos = append(repos, repo) | ||
} | ||
for _, repo := range config.DnfRepos { | ||
repos = append(repos, repo) | ||
} | ||
|
||
updateCmds := mapset.NewSet[string]() | ||
for _, repo := range repos { | ||
if !when.ShouldRun(repo) { | ||
continue | ||
} | ||
|
||
if unless.ShouldSkip(repo, config.Settings) { | ||
continue | ||
} | ||
|
||
err := repo.Install() | ||
if err == nil && repo.UpdateCmd() != "" { | ||
updateCmds.Add(repo.UpdateCmd()) | ||
} | ||
errs = append(errs, err) | ||
} | ||
|
||
updateErrs := runUpdateCmds(updateCmds) | ||
errs = append(errs, updateErrs...) | ||
|
||
return errors.Join(errs...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters