Skip to content

Commit

Permalink
apt repo: additional settings and pinning
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Dec 17, 2024
1 parent 95a4ec4 commit 62cccde
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
64 changes: 48 additions & 16 deletions entity/aptrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ import (
)

const (
keyRingsDir = "/etc/apt/keyrings"
sourcesDir = "/etc/apt/sources.list.d"
defaultComponents = "stable"
keyRingsDir = "/etc/apt/keyrings"
sourcesDir = "/etc/apt/sources.list.d"
)

type AptRepo struct {
unless.BasicUnlessable
GPGKey string `yaml:"gpg_key"`
RepoName string `yaml:"name"`
Repo string `yaml:"repo"`
When string `yaml:"when"`
Distribution string `yaml:"distribution"`
Components string `yaml:"components"`
GPGKey string `yaml:"gpg_key"`
Pin bool `yaml:"pin"`
RepoName string `yaml:"name"`
Repo string `yaml:"repo"`
When string `yaml:"when"`
}

func (a AptRepo) DefaultVersionCmd() string {
Expand Down Expand Up @@ -135,23 +139,51 @@ func (a AptRepo) Install() error {
}
architecture := strings.TrimSpace(out.Stdout)

versionCodename, err := precheck.GetOSVersionCodename()
if err != nil {
return err
distribution := a.Distribution
if distribution == "" {
distribution, err = precheck.GetOSVersionCodename()
if err != nil {
return err
}
}

content := fmt.Sprintf("deb [arch=${architecture} signed-by=%s] %s ${codename} stable\n", keyRingFile,
a.Repo)
content = settings.Expand(content, map[string]string{
"architecture": architecture,
"codename": versionCodename,
})
repoFile := path.Join(sourcesDir, fmt.Sprintf("%s.list", a.RepoName))
components := a.Components
if components == "" {
components = defaultComponents
}

name := a.RepoName
repo := a.Repo
content := fmt.Sprintf("deb [arch=%s signed-by=%s] %s %s %s\n", architecture, keyRingFile,
repo, distribution, components)
repoFile := path.Join(sourcesDir, fmt.Sprintf("%s.list", name))

_, err = internal.WriteContent(internal.ManagedFile{
Content: content,
Path: repoFile,
})
if err != nil {
return err
}

if !a.Pin {
return nil
}

origin, _ := path.Split(repo)
fields := strings.Split(origin, "://")
if len(fields) < 2 {
return fmt.Errorf("unable to determine origin from repository %s", repo)
}

origin = strings.TrimSuffix(fields[1], "/")
pinContent := fmt.Sprintf(`Package: *
Pin: origin %s
Pin-Priority: 1000`, origin)
pinFile := fmt.Sprintf("/etc/apt/preferences.d/%s", name)
_, err = internal.WriteContent(internal.ManagedFile{
Content: pinContent,
Path: pinFile,
})
return err
}
2 changes: 1 addition & 1 deletion provision/osrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func addRepos(config entity.Config) error {
internal.Log.Infof("Adding repo %s", repo.Name())

err := repo.Install()
if err == nil && repo.UpdateCmd() != "" {
if err != nil && repo.UpdateCmd() != "" {
internal.Log.Errorf("Error installing repo %s: %v", repo.Name(), err)
updateCmds.Add(repo.UpdateCmd())
}
Expand Down

0 comments on commit 62cccde

Please sign in to comment.