File: rpmbootstrap.go

package info (click to toggle)
distrobuilder 3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,384 kB
  • sloc: sh: 204; makefile: 75
file content (98 lines) | stat: -rw-r--r-- 2,178 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package sources

import (
	"fmt"
	"os"
	"path"

	"github.com/lxc/distrobuilder/shared"
)

type rpmbootstrap struct {
	common
}

func (s *rpmbootstrap) yumordnf() (cmd string, err error) {
	// check whether yum or dnf command exists
	for _, cmd = range []string{"yum", "dnf"} {
		if err = shared.RunCommand(s.ctx, nil, nil, cmd, "--version"); err == nil {
			return
		}
	}
	cmd = ""
	err = fmt.Errorf("Command yum or dnf not found, sudo apt-get install yum or sudo apt-get install dnf and try again")
	return
}

func (s *rpmbootstrap) repodirs() (dir string, err error) {
	reposdir := path.Join(s.sourcesDir, "etc", "yum.repos.d")
	err = os.MkdirAll(reposdir, 0o755)
	if err != nil {
		return "", err
	}

	distribution := s.definition.Image.Distribution
	content := s.definition.Source.URL
	if distribution == "" || content == "" {
		err = fmt.Errorf("No valid distribution and source url specified")
		return "", err
	}

	err = os.WriteFile(path.Join(reposdir, distribution+".repo"), []byte(content), 0o644)
	if err != nil {
		return "", err
	}

	return reposdir, nil
}

// Run runs yum --installroot.
func (s *rpmbootstrap) Run() (err error) {
	cmd, err := s.yumordnf()
	if err != nil {
		return err
	}

	repodir, err := s.repodirs()
	if err != nil {
		return err
	}

	release := s.definition.Image.Release
	args := []string{
		fmt.Sprintf("--installroot=%s", s.rootfsDir),
		fmt.Sprintf("--releasever=%s", release),
		fmt.Sprintf("--setopt=reposdir=%s", repodir),
		"install", "-y",
	}

	os.RemoveAll(s.rootfsDir)
	earlyPackagesRemove := s.definition.GetEarlyPackages("remove")

	for _, pkg := range earlyPackagesRemove {
		args = append(args, fmt.Sprintf("--exclude=%s", pkg))
	}

	pkgs := []string{"yum", "dnf"}
	components := s.definition.Source.Components

	for _, pkg := range components {
		pkg, err = shared.RenderTemplate(pkg, s.definition)
		if err != nil {
			return err
		}

		pkgs = append(pkgs, pkg)
	}

	earlyPackagesInstall := s.definition.GetEarlyPackages("install")
	pkgs = append(pkgs, earlyPackagesInstall...)
	args = append(args, pkgs...)

	// Install
	if err = shared.RunCommand(s.ctx, nil, nil, cmd, args...); err != nil {
		return err
	}

	return nil
}