-
Notifications
You must be signed in to change notification settings - Fork 37
/
build.go
304 lines (268 loc) · 9.54 KB
/
build.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright © 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"os"
"runtime"
"github.com/clearlinux/mixer-tools/builder"
"github.com/clearlinux/mixer-tools/helpers"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type buildCmdFlags struct {
format string
increment bool
minVersion int
noSigning bool
prefix string
noPublish bool
keepChroot bool
template string
numFullfileWorkers int
numDeltaWorkers int
numBundleWorkers int
}
var buildFlags buildCmdFlags
func setWorkers(b *builder.Builder) {
var workers int
workers = buildFlags.numFullfileWorkers
if workers < 1 {
workers = runtime.NumCPU()
}
b.NumFullfileWorkers = workers
workers = buildFlags.numDeltaWorkers
if workers < 1 {
workers = runtime.NumCPU()
}
b.NumDeltaWorkers = workers
workers = buildFlags.numBundleWorkers
if workers < 1 {
workers = runtime.NumCPU()
}
b.NumBundleWorkers = workers
}
// buildCmd represents the base build command when called without any subcommands
var buildCmd = &cobra.Command{
Use: "build",
Short: "Build various pieces of OS content",
}
func buildBundles(builder *builder.Builder, signflag bool) error {
// Create the signing and validation key/cert
if _, err := os.Stat(builder.Config.Builder.Cert); os.IsNotExist(err) {
fmt.Println("Generating certificate for signature validation...")
privkey, err := helpers.CreateKeyPair()
if err != nil {
return errors.Wrap(err, "Error generating OpenSSL keypair")
}
template := helpers.CreateCertTemplate()
err = builder.BuildBundles(template, privkey, signflag)
if err != nil {
return errors.Wrap(err, "Error building bundles")
}
} else {
err := builder.BuildBundles(nil, nil, true)
if err != nil {
return errors.Wrap(err, "Error building bundles")
}
}
return nil
}
var buildBundlesCmd = &cobra.Command{
Use: "bundles",
Aliases: []string{"chroots"},
Short: "Build the bundles for your mix",
Long: `Build the bundles for your mix`,
Run: func(cmd *cobra.Command, args []string) {
b, err := builder.NewFromConfig(config)
if err != nil {
fail(err)
}
setWorkers(b)
err = buildBundles(b, buildFlags.noSigning)
if err != nil {
fail(err)
}
},
}
var buildUpdateCmd = &cobra.Command{
Use: "update",
Short: "Build the update content for your mix",
Long: `Build the update content for your mix`,
Run: func(cmd *cobra.Command, args []string) {
b, err := builder.NewFromConfig(config)
if err != nil {
fail(err)
}
setWorkers(b)
err = b.BuildUpdate(buildFlags.prefix, buildFlags.minVersion, buildFlags.format, buildFlags.noSigning, !buildFlags.noPublish, buildFlags.keepChroot)
if err != nil {
failf("couldn't build update: %s", err)
}
if buildFlags.increment {
if err = b.UpdateMixVer(); err != nil {
failf("Couldn't update Mix Version")
}
}
},
}
var buildAllCmd = &cobra.Command{
Use: "all",
Short: "Build all content for mix with default options",
Long: `Build all content for mix with default options`,
Run: func(cmd *cobra.Command, args []string) {
b, err := builder.NewFromConfig(config)
if err != nil {
fail(err)
}
setWorkers(b)
rpms, err := helpers.ListVisibleFiles(b.Config.Mixer.LocalRPMDir)
if err == nil {
err = b.AddRPMList(rpms)
if err != nil {
failf("couldn't add the RPMs: %s", err)
}
}
err = buildBundles(b, buildFlags.noSigning)
if err != nil {
failf("couldn't build bundles: %s", err)
}
err = b.BuildUpdate(buildFlags.prefix, buildFlags.minVersion, buildFlags.format, buildFlags.noSigning, !buildFlags.noPublish, buildFlags.keepChroot)
if err != nil {
failf("couldn't build update: %s", err)
}
err = b.UpdateMixVer()
if err != nil {
failf("Couldn't update Mix Version")
}
},
}
var buildImageCmd = &cobra.Command{
Use: "image",
Short: "Build an image from the mix content",
Long: `Build an image from the mix content`,
Run: func(cmd *cobra.Command, args []string) {
b, err := builder.NewFromConfig(config)
if err != nil {
fail(err)
}
setWorkers(b)
err = b.BuildImage(buildFlags.format, buildFlags.template)
if err != nil {
failf("couldn't build image: %s", err)
}
},
}
var buildDeltaPacksCmd = &cobra.Command{
Use: "delta-packs",
Short: "Build packs used to optimize update between versions",
Long: `Build packs used to optimize update between versions
When a swupd client updates a bundle, it looks for a pack file from
its current version to the new version. If not available, the client
will download the individual files necessary for the update. If a
bundle haven't changed between two versions, no pack need to be
generated.
To generate the packs to optimize update from VER to the current mix
version use
mixer build delta-packs --from VER
Alternatively, to generate packs for a set of NUM previous versions,
each one to the current mix version, instead of --from use
mixer build delta-packs --previous-versions NUM
To change the target version (by default the current version), use the
flag --to. The target version must be larger than the --from version.
`,
RunE: runBuildDeltaPacks,
}
var buildDeltaPacksFlags struct {
previousVersions uint32
from uint32
to uint32
report bool
}
func runBuildDeltaPacks(cmd *cobra.Command, args []string) error {
fromChanged := cmd.Flags().Changed("from")
prevChanged := cmd.Flags().Changed("previous-versions")
if fromChanged == prevChanged {
return errors.Errorf("either --from or --previous-versions must be set, but not both")
}
b, err := builder.NewFromConfig(config)
if err != nil {
fail(err)
}
setWorkers(b)
if fromChanged {
err = b.BuildDeltaPacks(buildDeltaPacksFlags.from, buildDeltaPacksFlags.to, buildDeltaPacksFlags.report)
} else {
err = b.BuildDeltaPacksPreviousVersions(buildDeltaPacksFlags.previousVersions, buildDeltaPacksFlags.to, buildDeltaPacksFlags.report)
}
if err != nil {
fail(err)
}
return nil
}
func setUpdateFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&buildFlags.format, "format", "", "Supply format to use")
cmd.Flags().BoolVar(&buildFlags.increment, "increment", false, "Automatically increment the mixversion post build")
cmd.Flags().IntVar(&buildFlags.minVersion, "min-version", 0, "Supply minversion to build update with")
cmd.Flags().BoolVar(&buildFlags.noSigning, "no-signing", false, "Do not generate a certificate and do not sign the Manifest.MoM")
cmd.Flags().StringVar(&buildFlags.prefix, "prefix", "", "Supply prefix for where the swupd binaries live")
cmd.Flags().BoolVar(&buildFlags.noPublish, "no-publish", false, "Do not update the latest version after update")
cmd.Flags().BoolVar(&buildFlags.keepChroot, "keep-chroots", false, "Keep individual chroots created and not just consolidated 'full'")
}
var buildCmds = []*cobra.Command{
buildBundlesCmd,
buildUpdateCmd,
buildAllCmd,
buildImageCmd,
buildDeltaPacksCmd,
}
func init() {
for _, cmd := range buildCmds {
buildCmd.AddCommand(cmd)
cmd.Flags().StringVarP(&config, "config", "c", "", "Builder config to use")
}
buildCmd.PersistentFlags().IntVar(&buildFlags.numFullfileWorkers, "fullfile-workers", 0, "Number of parallel workers when creating fullfiles, 0 means number of CPUs")
buildCmd.PersistentFlags().IntVar(&buildFlags.numDeltaWorkers, "delta-workers", 0, "Number of parallel workers when creating deltas, 0 means number of CPUs")
buildCmd.PersistentFlags().IntVar(&buildFlags.numBundleWorkers, "bundle-workers", 0, "Number of parallel workers when building bundles, 0 means number of CPUs")
RootCmd.AddCommand(buildCmd)
buildBundlesCmd.Flags().BoolVar(&buildFlags.noSigning, "no-signing", false, "Do not generate a certificate to sign the Manifest.MoM")
unusedBoolFlag := false
buildBundlesCmd.Flags().BoolVar(&unusedBoolFlag, "new-chroots", false, "")
_ = buildBundlesCmd.Flags().MarkHidden("new-chroots")
_ = buildBundlesCmd.Flags().MarkDeprecated("new-chroots", "new functionality is now the standard behavior, this flag is obsolete and no longer used")
buildImageCmd.Flags().StringVar(&buildFlags.format, "format", "", "Supply the format used for the Mix")
buildImageCmd.Flags().StringVar(&buildFlags.template, "template", "", "Path to template file to use")
buildDeltaPacksCmd.Flags().Uint32Var(&buildDeltaPacksFlags.from, "from", 0, "Generate packs from a specific version")
buildDeltaPacksCmd.Flags().Uint32Var(&buildDeltaPacksFlags.previousVersions, "previous-versions", 0, "Generate packs for multiple previous versions")
buildDeltaPacksCmd.Flags().Uint32Var(&buildDeltaPacksFlags.to, "to", 0, "Generate packs targeting a specific version")
buildDeltaPacksCmd.Flags().BoolVar(&buildDeltaPacksFlags.report, "report", false, "Report reason each file in to manifest was packed or not")
setUpdateFlags(buildUpdateCmd)
setUpdateFlags(buildAllCmd)
externalDeps[buildBundlesCmd] = []string{
"rpm",
"dnf",
}
externalDeps[buildUpdateCmd] = []string{
"hardlink",
"openssl",
"xz",
}
externalDeps[buildImageCmd] = []string{
"ister.py",
}
externalDeps[buildAllCmd] = append(
externalDeps[buildBundlesCmd],
append(externalDeps[buildUpdateCmd],
externalDeps[buildImageCmd]...)...)
}