Skip to content

Commit

Permalink
Merge e2a28ed into afb3ed1
Browse files Browse the repository at this point in the history
  • Loading branch information
rgooch committed Oct 3, 2019
2 parents afb3ed1 + e2a28ed commit 79b5c44
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 50 deletions.
2 changes: 1 addition & 1 deletion cmd/builder-tool/buildRaw.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func buildRawFromManifest(manifestDir, rawFilename string,
}
defer syscall.Unmount(rootDir, 0)
err = builder.UnpackImageAndProcessManifest(srpcClient, manifestDir,
rootDir, buildLog)
rootDir, bindMounts, buildLog)
if err != nil {
fmt.Fprintf(os.Stderr, "Error processing manifest: %s\n", err)
io.Copy(os.Stderr, buildLog)
Expand Down
3 changes: 3 additions & 0 deletions cmd/builder-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
var (
alwaysShowBuildLog = flag.Bool("alwaysShowBuildLog", false,
"If true, show build log even for successful builds")
bindMounts flagutil.StringList
imaginatorHostname = flag.String("imaginatorHostname", "localhost",
"Hostname of image build server")
imaginatorPortNum = flag.Uint("imaginatorPortNum",
Expand All @@ -40,6 +41,8 @@ var (
)

func init() {
flag.Var(&bindMounts, "bindMounts",
"Comma separated list of directories to bind mount into build workspace")
flag.Var(&rawSize, "rawSize", "Size of RAW file to create")
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/builder-tool/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func buildFromManifestSubcommand(args []string, logger log.DebugLogger) {
fmt.Fprintln(os.Stderr, "Start of build log ==========================")
}
name, err := builder.BuildImageFromManifest(srpcClient, args[0], args[1],
*expiresIn, logWriter, logger)
*expiresIn, bindMounts, logWriter, logger)
if err != nil {
if !*alwaysShowBuildLog {
fmt.Fprintln(os.Stderr,
Expand Down Expand Up @@ -58,7 +58,7 @@ func buildTreeFromManifestSubcommand(args []string, logger log.DebugLogger) {
fmt.Fprintln(os.Stderr, "Start of build log ==========================")
}
rootDir, err := builder.BuildTreeFromManifest(srpcClient, args[0],
logWriter, logger)
bindMounts, logWriter, logger)
if err != nil {
if !*alwaysShowBuildLog {
fmt.Fprintln(os.Stderr,
Expand Down Expand Up @@ -88,7 +88,8 @@ func processManifestSubcommand(args []string, logger log.DebugLogger) {
if *alwaysShowBuildLog {
fmt.Fprintln(os.Stderr, "Start of build log ==========================")
}
if err := builder.ProcessManifest(args[0], args[1], logWriter); err != nil {
err := builder.ProcessManifest(args[0], args[1], bindMounts, logWriter)
if err != nil {
if !*alwaysShowBuildLog {
fmt.Fprintln(os.Stderr,
"Start of build log ==========================")
Expand Down
23 changes: 14 additions & 9 deletions imagebuilder/builder/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type buildResultType struct {
}

type masterConfigurationType struct {
BindMounts []string `json:",omitempty"`
BootstrapStreams map[string]*bootstrapStream `json:",omitempty"`
ImageStreamsCheckInterval uint `json:",omitempty"`
ImageStreamsToAutoRebuild []string `json:",omitempty"`
Expand Down Expand Up @@ -94,6 +95,7 @@ type sourceImageInfoType struct {
}

type Builder struct {
bindMounts []string
stateDir string
imageServerAddress string
logger log.Logger
Expand Down Expand Up @@ -144,29 +146,32 @@ func (b *Builder) WriteHtml(writer io.Writer) {
}

func BuildImageFromManifest(client *srpc.Client, manifestDir, streamName string,
expiresIn time.Duration, buildLog buildLogger, logger log.Logger) (
expiresIn time.Duration, bindMounts []string, buildLog buildLogger,
logger log.Logger) (
string, error) {
_, name, err := buildImageFromManifestAndUpload(client, manifestDir,
proto.BuildImageRequest{
StreamName: streamName,
ExpiresIn: expiresIn,
},
buildLog)
bindMounts, buildLog)
return name, err
}

func BuildTreeFromManifest(client *srpc.Client, manifestDir string,
buildLog io.Writer, logger log.Logger) (string, error) {
return buildTreeFromManifest(client, manifestDir, buildLog)
bindMounts []string, buildLog io.Writer,
logger log.Logger) (string, error) {
return buildTreeFromManifest(client, manifestDir, bindMounts, buildLog)
}

func ProcessManifest(manifestDir, rootDir string, buildLog io.Writer) error {
return processManifest(manifestDir, rootDir, buildLog)
func ProcessManifest(manifestDir, rootDir string, bindMounts []string,
buildLog io.Writer) error {
return processManifest(manifestDir, rootDir, bindMounts, buildLog)
}

func UnpackImageAndProcessManifest(client *srpc.Client, manifestDir string,
rootDir string, buildLog io.Writer) error {
_, err := unpackImageAndProcessManifest(client, manifestDir, rootDir, true,
buildLog)
rootDir string, bindMounts []string, buildLog io.Writer) error {
_, err := unpackImageAndProcessManifest(client, manifestDir, rootDir,
bindMounts, true, buildLog)
return err
}
38 changes: 34 additions & 4 deletions imagebuilder/builder/bootstrapImage.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.10

package builder

import (
Expand All @@ -8,7 +10,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
"syscall"
Expand All @@ -18,6 +20,7 @@ import (
"github.com/Symantec/Dominator/lib/format"
"github.com/Symantec/Dominator/lib/image"
"github.com/Symantec/Dominator/lib/srpc"
"github.com/Symantec/Dominator/lib/wsyscall"
proto "github.com/Symantec/Dominator/proto/imaginator"
)

Expand All @@ -41,7 +44,7 @@ var environmentToSet = map[string]string{
"USER": "root",
}

func clean(rootDir string, buildLog io.Writer) error {
func cleanPackages(rootDir string, buildLog io.Writer) error {
fmt.Fprintln(buildLog, "\nCleaning packages:")
startTime := time.Now()
err := runInTarget(nil, buildLog, rootDir, packagerPathname, "clean")
Expand Down Expand Up @@ -99,7 +102,7 @@ func (stream *bootstrapStream) build(b *Builder, client *srpc.Client,
buildDuration := time.Since(startTime)
fmt.Fprintf(buildLog, "\nBuild time: %s\n",
format.Duration(buildDuration))
if err := clean(rootDir, buildLog); err != nil {
if err := cleanPackages(rootDir, buildLog); err != nil {
return nil, err
}
return packImage(client, request, rootDir,
Expand All @@ -108,7 +111,7 @@ func (stream *bootstrapStream) build(b *Builder, client *srpc.Client,
}

func (packager *packagerType) writePackageInstaller(rootDir string) error {
filename := path.Join(rootDir, packagerPathname)
filename := filepath.Join(rootDir, packagerPathname)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, cmdPerms)
if err != nil {
return err
Expand Down Expand Up @@ -192,6 +195,33 @@ func runInTarget(input io.Reader, output io.Writer, rootDir, prog string,
return cmd.Run()
}

func runInTargetWithBindMounts(input io.Reader, output io.Writer,
rootDir string, bindMounts []string, prog string, args ...string) error {
if len(bindMounts) < 1 {
return runInTarget(input, output, rootDir, prog, args...)
}
errChannel := make(chan error)
go func() {
err := func() error {
if err := wsyscall.UnshareMountNamespace(); err != nil {
return err
}
for _, bindMount := range bindMounts {
err := wsyscall.Mount(bindMount,
filepath.Join(rootDir, bindMount), "",
wsyscall.MS_BIND|wsyscall.MS_RDONLY, "")
if err != nil {
return fmt.Errorf("error bind mounting: %s: %s",
bindMount, err)
}
}
return runInTarget(input, output, rootDir, prog, args...)
}()
errChannel <- err
}()
return <-errChannel
}

func stripVariables(input []string, varsToCopy map[string]struct{}) []string {
output := make([]string, 0)
for _, nameValue := range os.Environ() {
Expand Down
17 changes: 9 additions & 8 deletions imagebuilder/builder/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (stream *imageStreamType) build(b *Builder, client *srpc.Client,
}
defer os.RemoveAll(manifestDirectory)
img, err := buildImageFromManifest(client, manifestDirectory, request,
buildLog)
b.bindMounts, buildLog)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -212,7 +212,7 @@ func runCommand(buildLog io.Writer, cwd string, args ...string) error {
}

func buildImageFromManifest(client *srpc.Client, manifestDir string,
request proto.BuildImageRequest,
request proto.BuildImageRequest, bindMounts []string,
buildLog buildLogger) (*image.Image, error) {
// First load all the various manifest files (fail early on error).
computedFilesList, err := util.LoadComputedFiles(
Expand Down Expand Up @@ -240,7 +240,7 @@ func buildImageFromManifest(client *srpc.Client, manifestDir string,
defer os.RemoveAll(rootDir)
fmt.Fprintf(buildLog, "Created image working directory: %s\n", rootDir)
manifest, err := unpackImageAndProcessManifest(client, manifestDir,
rootDir, false, buildLog)
rootDir, bindMounts, false, buildLog)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -273,9 +273,10 @@ func buildImageFromManifest(client *srpc.Client, manifestDir string,
}

func buildImageFromManifestAndUpload(client *srpc.Client, manifestDir string,
request proto.BuildImageRequest,
request proto.BuildImageRequest, bindMounts []string,
buildLog buildLogger) (*image.Image, string, error) {
img, err := buildImageFromManifest(client, manifestDir, request, buildLog)
img, err := buildImageFromManifest(client, manifestDir, request, bindMounts,
buildLog)
if err != nil {
return nil, "", err
}
Expand All @@ -287,13 +288,13 @@ func buildImageFromManifestAndUpload(client *srpc.Client, manifestDir string,
}

func buildTreeFromManifest(client *srpc.Client, manifestDir string,
buildLog io.Writer) (string, error) {
bindMounts []string, buildLog io.Writer) (string, error) {
rootDir, err := makeTempDirectory("", "tree")
if err != nil {
return "", err
}
_, err = unpackImageAndProcessManifest(client, manifestDir, rootDir, true,
buildLog)
_, err = unpackImageAndProcessManifest(client, manifestDir, rootDir,
bindMounts, true, buildLog)
if err != nil {
os.RemoveAll(rootDir)
return "", err
Expand Down
1 change: 1 addition & 0 deletions imagebuilder/builder/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func load(confUrl, variablesFile, stateDir, imageServerAddress string,
variables = make(map[string]string)
}
b := &Builder{
bindMounts: masterConfiguration.BindMounts,
stateDir: stateDir,
imageServerAddress: imageServerAddress,
logger: logger,
Expand Down
Loading

0 comments on commit 79b5c44

Please sign in to comment.