/
build.go
165 lines (139 loc) · 5.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
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
package components
import (
"context"
"database/sql"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/builder/dockerignore"
docker "github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
)
// DockerImagePrefix is the prefix that shnorky attaches to each docker image name
var DockerImagePrefix = "shnorky/"
// ErrEmptyComponentID signifies that a caller attempted to create build or execution metadata in
// which the ComponentID string was the empty string
var ErrEmptyComponentID = errors.New("ComponentID must be a non-empty string")
// BuildMetadata - the metadata about a component build that gets stored in the state database
type BuildMetadata struct {
ID string `json:"id"`
ComponentID string `json:"component_id"`
CreatedAt time.Time `json:"created_at"`
}
// GenerateBuildMetadata creates a BuildMetadata instance representing a fresh (as yet unbuilt)
// build of the component specified by the given componentID.
func GenerateBuildMetadata(componentID string) (BuildMetadata, error) {
if componentID == "" {
return BuildMetadata{}, ErrEmptyComponentID
}
createdAt := time.Now()
buildID := fmt.Sprintf("%s%s:%d", DockerImagePrefix, componentID, createdAt.Unix())
return BuildMetadata{ID: buildID, ComponentID: componentID, CreatedAt: createdAt}, nil
}
// CreateBuild creates a new build for the component with the given componentID
func CreateBuild(ctx context.Context, db *sql.DB, dockerClient *docker.Client, outstream io.Writer, componentID string) (BuildMetadata, error) {
componentMetadata, err := SelectComponentByID(db, componentID)
if err != nil {
return BuildMetadata{}, err
}
buildMetadata, err := GenerateBuildMetadata(componentMetadata.ID)
if err != nil {
return BuildMetadata{}, err
}
specFile, err := os.Open(componentMetadata.SpecificationPath)
if err != nil {
return buildMetadata, fmt.Errorf("Could not open specification file (%s): %s", componentMetadata.SpecificationPath, err.Error())
}
defer specFile.Close()
specification, err := ReadSingleSpecification(specFile)
if err != nil {
return buildMetadata, fmt.Errorf("Could not parse specification from specification file (%s): %s", componentMetadata.SpecificationPath, err.Error())
}
context := filepath.Join(componentMetadata.ComponentPath, specification.Build.Context)
tarOptions := archive.TarOptions{
Compression: archive.Uncompressed,
}
dockerignoreFilePath := filepath.Join(context, ".dockerignore")
dockerignoreInfo, dockerignoreErr := os.Stat(dockerignoreFilePath)
if !os.IsNotExist(dockerignoreErr) {
if dockerignoreErr != nil {
return buildMetadata, fmt.Errorf("Error checking dockerignore file (%s): %s", dockerignoreFilePath, err.Error())
}
if !dockerignoreInfo.IsDir() {
dockerignoreFile, err := os.Open(dockerignoreFilePath)
if err != nil {
return buildMetadata, fmt.Errorf("Error opening dockerignore file (%s): %s", dockerignoreFilePath, err.Error())
}
defer dockerignoreFile.Close()
excludePatterns, err := dockerignore.ReadAll(dockerignoreFile)
if err != nil {
return buildMetadata, fmt.Errorf("Could not read exclude patterns from dockerignore file (%s): %s", dockerignoreFilePath, err.Error())
}
tarOptions.ExcludePatterns = excludePatterns
}
}
buildContext, err := archive.TarWithOptions(context, &tarOptions)
if err != nil {
return buildMetadata, fmt.Errorf("Could not archive context: %s", err.Error())
}
defer buildContext.Close()
tags := []string{buildMetadata.ID}
imageIDComponents := strings.Split(buildMetadata.ID, ":")
if len(imageIDComponents) > 1 {
imageIDComponents[len(imageIDComponents)-1] = "latest"
tags = append(tags, strings.Join(imageIDComponents, ":"))
}
buildOptions := dockerTypes.ImageBuildOptions{
Tags: tags,
Dockerfile: specification.Build.Dockerfile,
// Setting Remove to true means that intermediate containers for the build will be removed
// on a successful build.
Remove: true,
}
response, err := dockerClient.ImageBuild(ctx, buildContext, buildOptions)
if err != nil {
return buildMetadata, fmt.Errorf("Error building image: %s", err.Error())
}
defer response.Body.Close()
io.Copy(outstream, response.Body)
err = InsertBuild(db, buildMetadata)
if err != nil {
return buildMetadata, fmt.Errorf("Error inserting build metadata into state database: %s", err.Error())
}
return buildMetadata, nil
}
// ListBuilds streams builds one by one from the given state database into the given builds channel.
// This function closes the builds channel when it is finished.
func ListBuilds(db *sql.DB, builds chan<- BuildMetadata, componentID string) error {
defer close(builds)
var rows *sql.Rows
var err error
if componentID != "" {
rows, err = db.Query(selectBuildsByComponentID, componentID)
} else {
rows, err = db.Query(selectBuilds)
}
if err != nil {
return err
}
defer rows.Close()
var id, rowComponentID string
var createdAt int64
for rows.Next() {
err = rows.Scan(&id, &rowComponentID, &createdAt)
if err != nil {
return err
}
builds <- BuildMetadata{
ID: id,
ComponentID: rowComponentID,
CreatedAt: time.Unix(createdAt, 0),
}
}
return nil
}