forked from osbuild/image-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distribution.go
168 lines (146 loc) · 3.94 KB
/
distribution.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package common
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/labstack/echo/v4"
"github.com/osbuild/image-builder/internal/cloudapi"
)
type DistributionItem struct {
Description string `json:"description"`
Name string `json:"name"`
}
type Distributions []DistributionItem
type ArchitectureItem struct {
Arch string `json:"arch"`
ImageTypes []string `json:"image_types"`
}
type Architectures []ArchitectureItem
type DistributionFile struct {
ModulePlatformID string `json:"module_platform_id"`
Distribution DistributionItem `json:"distribution"`
ArchX86 *X86_64 `json:"x86_64,omitempty"`
}
type X86_64 struct {
ImageTypes []string `json:"image_types"`
Repositories []cloudapi.Repository `json:"repositories"`
}
type Package struct {
Name string `json:"name"`
Summary string `json:"summary"`
}
type PackagesFile struct {
Data []Package `json:"data"`
}
func ReadDistributions(distsDir, distro string) ([]DistributionFile, error) {
// note: last value is because tests' pwd is not the repository root !!!
var distributions []DistributionFile
err := filepath.Walk(distsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore non-json and the packages files in the distribution dir
if filepath.Ext(path) != ".json" || strings.HasSuffix(path, "packages.json") {
return nil
}
if distro != "" && strings.TrimSuffix(info.Name(), ".json") != distro {
return nil
}
cleanPath := filepath.Clean(path)
f, err := os.Open(cleanPath)
if err != nil {
return err
}
// nosec because of https://github.com/securego/gosec/issues/714
/* #nosec G307 */
defer func() {
err := f.Close()
if err != nil {
fmt.Printf("Error closing file: %v", err)
}
}()
var d DistributionFile
err = json.NewDecoder(f).Decode(&d)
if err != nil {
return err
}
distributions = append(distributions, d)
return nil
})
if err != nil {
return nil, err
}
if len(distributions) == 0 {
return nil, fmt.Errorf("No distributions found, is %v populated with json files?", distsDir)
}
return distributions, nil
}
func RepositoriesForImage(distsDir, distro, arch string) ([]cloudapi.Repository, error) {
distributions, err := ReadDistributions(distsDir, distro)
if err != nil {
return nil, err
}
switch arch {
case "x86_64":
return distributions[0].ArchX86.Repositories, nil
default:
return nil, echo.NewHTTPError(http.StatusBadRequest, "Architecture not supported")
}
}
func AvailableDistributions(distsDir string) (Distributions, error) {
distributions, err := ReadDistributions(distsDir, "")
if err != nil {
return nil, err
}
var availableDistributions Distributions
for _, distro := range distributions {
availableDistributions = append(availableDistributions, distro.Distribution)
}
return availableDistributions, nil
}
func ArchitecturesForImage(distsDir, distro string) (Architectures, error) {
distributions, err := ReadDistributions(distsDir, distro)
if err != nil {
return nil, err
}
d := distributions[0]
var archs Architectures
if d.ArchX86 != nil {
archs = append(archs, ArchitectureItem{
Arch: "x86_64",
ImageTypes: d.ArchX86.ImageTypes,
})
}
return archs, nil
}
func FindPackages(distsDir, distro, arch, search string) ([]Package, error) {
cleanPath := filepath.Clean(path.Join(distsDir, fmt.Sprintf("%v-%v-packages.json", distro, arch)))
f, err := os.Open(cleanPath)
if err != nil {
return nil, err
}
// nosec because of https://github.com/securego/gosec/issues/714
/* #nosec G307 */
defer func() {
err := f.Close()
if err != nil {
fmt.Printf("Error closing file: %v", err)
}
}()
var p PackagesFile
err = json.NewDecoder(f).Decode(&p)
if err != nil {
return nil, err
}
var filtPkgs []Package
for _, p := range p.Data {
if strings.Contains(p.Name, search) {
filtPkgs = append(filtPkgs, p)
}
}
return filtPkgs, nil
}