forked from osbuild/images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
163 lines (139 loc) · 4.9 KB
/
main.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
// Standalone executable for building a test image.
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/osbuild/images/internal/buildconfig"
"github.com/osbuild/images/internal/cmdutil"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/distrofactory"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/manifestgen"
"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/reporegistry"
"github.com/osbuild/images/pkg/rhsm/facts"
"github.com/osbuild/images/pkg/rpmmd"
)
func save(ms manifest.OSBuildManifest, fpath string) error {
b, err := json.MarshalIndent(ms, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal data for %q: %w", fpath, err)
}
b = append(b, '\n') // add new line at end of file
fp, err := os.Create(fpath)
if err != nil {
return fmt.Errorf("failed to create output file %q: %w", fpath, err)
}
defer fp.Close()
if _, err := fp.Write(b); err != nil {
return fmt.Errorf("failed to write output file %q: %w", fpath, err)
}
return nil
}
func u(s string) string {
return strings.Replace(s, "-", "_", -1)
}
func run() error {
// common args
var outputDir, osbuildStore, rpmCacheRoot, repositories string
flag.StringVar(&outputDir, "output", ".", "artifact output directory")
flag.StringVar(&osbuildStore, "store", ".osbuild", "osbuild store for intermediate pipeline trees")
flag.StringVar(&rpmCacheRoot, "rpmmd", "/tmp/rpmmd", "rpm metadata cache directory")
flag.StringVar(&repositories, "repositories", "test/data/repositories", "path to repository file or directory")
// osbuild checkpoint arg
var checkpoints cmdutil.MultiValue
flag.Var(&checkpoints, "checkpoints", "comma-separated list of pipeline names to checkpoint (passed to osbuild --checkpoint)")
// image selection args
var distroName, imgTypeName, configFile string
flag.StringVar(&distroName, "distro", "", "distribution (required)")
flag.StringVar(&imgTypeName, "type", "", "image type name (required)")
flag.StringVar(&configFile, "config", "", "build config file (required)")
flag.Parse()
if distroName == "" || imgTypeName == "" || configFile == "" {
flag.Usage()
os.Exit(1)
}
distroFac := distrofactory.NewDefault()
config, err := buildconfig.New(configFile)
if err != nil {
return err
}
if err := os.MkdirAll(outputDir, 0777); err != nil {
return fmt.Errorf("failed to create target directory: %w", err)
}
distribution := distroFac.GetDistro(distroName)
if distribution == nil {
return fmt.Errorf("invalid or unsupported distribution: %q", distroName)
}
archName := arch.Current().String()
arch, err := distribution.GetArch(archName)
if err != nil {
return fmt.Errorf("invalid arch name %q for distro %q: %w", archName, distroName, err)
}
buildName := fmt.Sprintf("%s-%s-%s-%s", u(distroName), u(archName), u(imgTypeName), u(config.Name))
buildDir := filepath.Join(outputDir, buildName)
if err := os.MkdirAll(buildDir, 0777); err != nil {
return fmt.Errorf("failed to create target directory: %w", err)
}
imgType, err := arch.GetImageType(imgTypeName)
if err != nil {
return fmt.Errorf("invalid image type %q for distro %q and arch %q: %w", imgTypeName, distroName, archName, err)
}
var reporeg *reporegistry.RepoRegistry
var customRepos []rpmmd.RepoConfig
if st, err := os.Stat(repositories); err == nil && !st.Mode().IsRegular() {
repoConfig, err := rpmmd.LoadRepositoriesFromFile(repositories)
if err != nil {
return fmt.Errorf("failed to load repositories from %q: %w", repositories, err)
}
customRepos = repoConfig[archName]
} else {
reporeg, err = reporegistry.New([]string{repositories})
if err != nil {
return fmt.Errorf("failed to load repositories from %q: %w", repositories, err)
}
}
fmt.Printf("Generating manifest for %s: ", config.Name)
var mf bytes.Buffer
manifestOpts := manifestgen.Options{
Output: &mf,
Cachedir: filepath.Join(rpmCacheRoot, archName+distribution.Name()),
WarningsOutput: os.Stderr,
CustomRepos: customRepos,
}
// add RHSM fact to detect changes
config.Options.Facts = &facts.ImageOptions{
APIType: facts.TEST_APITYPE,
}
mg, err := manifestgen.New(reporeg, &manifestOpts)
if err != nil {
return err
}
if err := mg.Generate(config.Blueprint, distribution, imgType, arch, &config.Options); err != nil {
return err
}
fmt.Print("DONE\n")
manifestPath := filepath.Join(buildDir, "manifest.json")
if err := save(mf.Bytes(), manifestPath); err != nil {
return err
}
fmt.Printf("Building manifest: %s\n", manifestPath)
jobOutput := filepath.Join(outputDir, buildName)
_, err = osbuild.RunOSBuild(mf.Bytes(), osbuildStore, jobOutput, imgType.Exports(), checkpoints, nil, false, os.Stderr)
if err != nil {
return err
}
fmt.Printf("Jobs done. Results saved in\n%s\n", outputDir)
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}