|
| 1 | +// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved. |
| 2 | +// This software is licensed under a 3-clause BSD license. Please consult the |
| 3 | +// LICENSE.md file distributed with the sources of this project regarding your |
| 4 | +// rights to use or distribute this software. |
| 5 | + |
| 6 | +package types |
| 7 | + |
| 8 | +import ( |
| 9 | + "io/ioutil" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + |
| 13 | + ocitypes "github.com/containers/image/types" |
| 14 | + "github.com/sylabs/singularity/internal/pkg/sylog" |
| 15 | +) |
| 16 | + |
| 17 | +// Bundle is the temporary build environment used during the image |
| 18 | +// building process. A Bundle is the programmatic representation of |
| 19 | +// the directory structure which will constitute this environmenb. |
| 20 | +// /tmp/...: |
| 21 | +// fs/ - A chroot filesystem |
| 22 | +// .singularity.d/ - Container metadata (from 2.x image format) |
| 23 | +// config.json (optional) - Contain information for OCI image bundle |
| 24 | +// etc... - The Bundle dir can theoretically contain arbitrary directories, |
| 25 | +// files, etc... which can be interpreted by the Chef |
| 26 | +type Bundle struct { |
| 27 | + // FSObjects is a map of the filesystem objects contained in the Bundle. An object |
| 28 | + // will be built as one section of a SIF file. |
| 29 | + // |
| 30 | + // Known FSObjects labels: |
| 31 | + // * rootfs -> root file system |
| 32 | + // * .singularity.d -> .singularity.d directory (includes image exec scripts) |
| 33 | + // * data -> directory containing data files |
| 34 | + FSObjects map[string]string `json:"fsObjects"` |
| 35 | + JSONObjects map[string][]byte `json:"jsonObjects"` |
| 36 | + Recipe Definition `json:"rawDeffile"` |
| 37 | + BindPath []string `json:"bindPath"` |
| 38 | + Path string `json:"bundlePath"` |
| 39 | + Opts Options `json:"opts"` |
| 40 | +} |
| 41 | + |
| 42 | +// Options defines build time behavior to be executed on the bundle |
| 43 | +type Options struct { |
| 44 | + // sections are the parts of the definition to run during the build |
| 45 | + Sections []string `json:"sections"` |
| 46 | + // TmpDir specifies a non-standard temporary location to perform a build |
| 47 | + TmpDir string |
| 48 | + // LibraryURL contains URL to library where base images can be pulled |
| 49 | + LibraryURL string `json:"libraryURL"` |
| 50 | + // LibraryAuthToken contains authentication token to access specified library |
| 51 | + LibraryAuthToken string `json:"libraryAuthToken"` |
| 52 | + // contains docker credentials if specified |
| 53 | + DockerAuthConfig *ocitypes.DockerAuthConfig |
| 54 | + // noTest indicates if build should skip running the test script |
| 55 | + NoTest bool `json:"noTest"` |
| 56 | + // force automatically deletes an existing container at build destination while performing build |
| 57 | + Force bool `json:"force"` |
| 58 | + // update detects and builds using an existing sandbox container at build destination |
| 59 | + Update bool `json:"update"` |
| 60 | + // noHTTPS |
| 61 | + NoHTTPS bool `json:"noHTTPS"` |
| 62 | + // NoCleanUp allows a user to prevent a bundle from being cleaned up after a failed build |
| 63 | + // useful for debugging |
| 64 | + NoCleanUp bool `json:"noCleanUp"` |
| 65 | +} |
| 66 | + |
| 67 | +// NewBundle creates a Bundle environment |
| 68 | +func NewBundle(bundleDir, bundlePrefix string) (b *Bundle, err error) { |
| 69 | + b = &Bundle{} |
| 70 | + b.JSONObjects = make(map[string][]byte) |
| 71 | + |
| 72 | + if bundlePrefix == "" { |
| 73 | + bundlePrefix = "sbuild-" |
| 74 | + } |
| 75 | + |
| 76 | + // Bundle path must be predictable |
| 77 | + b.Path = "/tmp/sbuild" |
| 78 | + err = os.MkDir(b.Path) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + sylog.Debugf("Created temporary directory for bundle %v\n", b.Path) |
| 83 | + |
| 84 | + b.FSObjects = map[string]string{ |
| 85 | + "rootfs": "fs", |
| 86 | + } |
| 87 | + |
| 88 | + for _, fso := range b.FSObjects { |
| 89 | + if err = os.MkdirAll(filepath.Join(b.Path, fso), 0755); err != nil { |
| 90 | + return |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return b, nil |
| 95 | +} |
| 96 | + |
| 97 | +// Rootfs give the path to the root filesystem in the Bundle |
| 98 | +func (b *Bundle) Rootfs() string { |
| 99 | + return filepath.Join(b.Path, b.FSObjects["rootfs"]) |
| 100 | +} |
| 101 | + |
| 102 | +// RunSection iterates through the sections specified in a bundle |
| 103 | +// and returns true if the given string, s, is a section of the |
| 104 | +// definition that should be executed during the build process |
| 105 | +func (b Bundle) RunSection(s string) bool { |
| 106 | + for _, section := range b.Opts.Sections { |
| 107 | + if section == "none" { |
| 108 | + return false |
| 109 | + } |
| 110 | + if section == "all" || section == s { |
| 111 | + return true |
| 112 | + } |
| 113 | + } |
| 114 | + return false |
| 115 | +} |
0 commit comments