/
build.go
113 lines (95 loc) · 3.18 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
// Copyright 2022 Chainguard, Inc.
//
// 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 cli
import (
"context"
"log"
"os"
"strconv"
"time"
"chainguard.dev/apko/pkg/build"
"chainguard.dev/apko/pkg/build/oci"
"chainguard.dev/apko/pkg/build/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func Build() *cobra.Command {
var useProot bool
var buildDate string
cmd := &cobra.Command{
Use: "build",
Short: "Build an image from a YAML configuration file",
Long: `Build an image from a YAML configuration file.
The generated image is in a format which can be used with the "docker load"
command, e.g.
# docker load < output.tar`,
Example: ` apko build <config.yaml> <tag> <output.tar>`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
return BuildCmd(cmd.Context(), args[0], args[1], args[2], useProot, buildDate)
},
}
cmd.Flags().BoolVar(&useProot, "use-proot", false, "use proot to simulate privileged operations")
cmd.Flags().StringVar(&buildDate, "build-date", "now", "date used for the timestamps of the files inside the image")
return cmd
}
func BuildCmd(ctx context.Context, configFile string, imageRef string, outputTarGZ string, useProot bool, buildDate string) error {
log.Printf("building image '%s' from config file '%s'", imageRef, configFile)
ic := types.ImageConfiguration{}
err := ic.Load(configFile)
if err != nil {
return errors.Wrap(err, "failed to load image configuration")
}
wd, err := os.MkdirTemp("", "apko-*")
if err != nil {
return errors.Wrap(err, "failed to create working directory")
}
defer os.RemoveAll(wd)
bc := build.Context{
ImageConfiguration: ic,
WorkDir: wd,
UseProot: useProot,
SourceDataEpoch: time.Now(),
}
if v, ok := os.LookupEnv("SOURCE_DATE_EPOCH"); ok {
// The value MUST be an ASCII representation of an integer
// with no fractional component, identical to the output
// format of date +%s.
sec, err := strconv.ParseInt(v, 10, 64)
if err != nil {
// If the value is malformed, the build process
// SHOULD exit with a non-zero error code.
return err
}
bc.SourceDataEpoch = time.Unix(sec, 0)
} else if buildDate == "now" {
bc.SourceDataEpoch = time.Now()
} else {
sec, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return err
}
bc.SourceDataEpoch = time.Unix(sec, 0)
}
layerTarGZ, err := bc.BuildLayer()
if err != nil {
return errors.Wrap(err, "failed to build layer image")
}
defer os.Remove(layerTarGZ)
err = oci.BuildImageTarballFromLayer(imageRef, layerTarGZ, outputTarGZ, bc.ImageConfiguration, bc.SourceDataEpoch)
if err != nil {
return errors.Wrap(err, "failed to build OCI image")
}
return nil
}