File: yum.go

package info (click to toggle)
distrobuilder 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,436 kB
  • sloc: sh: 214; makefile: 76
file content (118 lines) | stat: -rw-r--r-- 2,377 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package managers

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	incus "github.com/lxc/incus/v6/shared/util"

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

type yum struct {
	common
}

func (m *yum) load() error {
	m.commands = managerCommands{
		clean:   "yum",
		install: "yum",
		refresh: "yum",
		remove:  "yum",
		update:  "yum",
	}

	m.flags = managerFlags{
		clean: []string{
			"clean", "all",
		},
		global: []string{
			"-y",
		},
		install: []string{
			"install",
		},
		remove: []string{
			"remove",
		},
		refresh: []string{
			"makecache",
		},
		update: []string{
			"update",
		},
	}

	var buf bytes.Buffer

	err := shared.RunCommand(m.ctx, nil, &buf, "yum", "--help")
	if err != nil {
		return fmt.Errorf("Failed running yum: %w", err)
	}

	scanner := bufio.NewScanner(&buf)

	for scanner.Scan() {
		if strings.Contains(scanner.Text(), "--allowerasing") {
			m.flags.global = append(m.flags.global, "--allowerasing")
			continue
		}

		if strings.Contains(scanner.Text(), "--nobest") {
			m.flags.update = append(m.flags.update, "--nobest")
		}
	}

	return nil
}

func (m *yum) manageRepository(repoAction shared.DefinitionPackagesRepository) error {
	// Run rpmdb --rebuilddb
	err := shared.RunCommand(m.ctx, nil, nil, "rpmdb", "--rebuilddb")
	if err != nil {
		return fmt.Errorf("failed to run rpmdb --rebuilddb: %w", err)
	}

	return yumManageRepository(repoAction)
}

func yumManageRepository(repoAction shared.DefinitionPackagesRepository) error {
	targetFile := filepath.Join("/etc/yum.repos.d", repoAction.Name)

	if !strings.HasSuffix(targetFile, ".repo") {
		targetFile = fmt.Sprintf("%s.repo", targetFile)
	}

	if !incus.PathExists(filepath.Dir(targetFile)) {
		err := os.MkdirAll(filepath.Dir(targetFile), 0o755)
		if err != nil {
			return fmt.Errorf("Failed to create directory %q: %w", filepath.Dir(targetFile), err)
		}
	}

	f, err := os.Create(targetFile)
	if err != nil {
		return fmt.Errorf("Failed to create file %q: %w", targetFile, err)
	}

	defer f.Close()

	_, err = f.WriteString(repoAction.URL)
	if err != nil {
		return fmt.Errorf("Failed to write to file %q: %w", targetFile, err)
	}

	// Append final new line if missing
	if !strings.HasSuffix(repoAction.URL, "\n") {
		_, err = f.WriteString("\n")
		if err != nil {
			return fmt.Errorf("Failed to write to file %q: %w", targetFile, err)
		}
	}

	return nil
}