Skip to content

Commit

Permalink
Merge pull request #150 from tri-adam/launch-script
Browse files Browse the repository at this point in the history
Add OptCreateWithLaunchScript
  • Loading branch information
tri-adam authored Sep 13, 2021
2 parents 9192acd + 26fd648 commit cf8945b
Show file tree
Hide file tree
Showing 36 changed files with 36 additions and 4 deletions.
4 changes: 3 additions & 1 deletion internal/app/siftool/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func readableSize(size int64) string {
func writeHeader(w io.Writer, f *sif.FileImage) error {
tw := tabwriter.NewWriter(w, 0, 0, 0, ' ', 0)

fmt.Fprintln(tw, "Launch Script:\t", strings.TrimSuffix(f.LaunchScript(), "\n"))
if s := f.LaunchScript(); s != "" {
fmt.Fprintln(tw, "Launch Script:\t", strings.TrimSuffix(s, "\n"))
}
fmt.Fprintln(tw, "Version:\t", f.Version())
fmt.Fprintln(tw, "Primary Architecture:\t", f.PrimaryArch())
fmt.Fprintln(tw, "ID:\t", f.ID())
Expand Down
24 changes: 23 additions & 1 deletion pkg/sif/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (f *FileImage) writeHeader() error {

// createOpts accumulates container creation options.
type createOpts struct {
launchScript [hdrLaunchLen]byte
id uuid.UUID
descriptorsOffset int64
descriptorCapacity int64
Expand All @@ -126,6 +127,23 @@ type createOpts struct {
// CreateOpt are used to specify container creation options.
type CreateOpt func(*createOpts) error

var errLaunchScriptLen = errors.New("launch script too large")

// OptCreateWithLaunchScript specifies s as the launch script.
func OptCreateWithLaunchScript(s string) CreateOpt {
return func(co *createOpts) error {
b := []byte(s)

if len(b) >= len(co.launchScript) {
return errLaunchScriptLen
}

copy(co.launchScript[:], b)

return nil
}
}

// OptCreateWithID specifies id as the unique ID.
func OptCreateWithID(id string) CreateOpt {
return func(co *createOpts) error {
Expand Down Expand Up @@ -175,6 +193,7 @@ func createContainer(rw ReadWriter, co createOpts) (*FileImage, error) {
rdsSize := int64(binary.Size(rds))

h := header{
LaunchScript: co.launchScript,
Arch: hdrArchUnknown,
ID: co.id,
CreatedAt: co.t.Unix(),
Expand All @@ -185,7 +204,6 @@ func createContainer(rw ReadWriter, co createOpts) (*FileImage, error) {
DescriptorsSize: rdsSize,
DataOffset: co.descriptorsOffset + rdsSize,
}
copy(h.LaunchScript[:], hdrLaunch)
copy(h.Magic[:], hdrMagic)
copy(h.Version[:], CurrentVersion.bytes())

Expand Down Expand Up @@ -221,6 +239,8 @@ func createContainer(rw ReadWriter, co createOpts) (*FileImage, error) {
//
// By default, the image will support a maximum of 48 descriptors. To change this, consider using
// OptCreateWithDescriptorCapacity.
//
// A launch script can optionally be set using OptCreateWithLaunchScript.
func CreateContainer(rw ReadWriter, opts ...CreateOpt) (*FileImage, error) {
id, err := uuid.NewRandom()
if err != nil {
Expand Down Expand Up @@ -257,6 +277,8 @@ func CreateContainer(rw ReadWriter, opts ...CreateOpt) (*FileImage, error) {
//
// By default, the image will support a maximum of 48 descriptors. To change this, consider using
// OptCreateWithDescriptorCapacity.
//
// A launch script can optionally be set using OptCreateWithLaunchScript.
func CreateContainerAtPath(path string, opts ...CreateOpt) (*FileImage, error) {
fp, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/sif/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func TestCreateContainer(t *testing.T) {
OptCreateWithTime(testTime),
},
},
{
name: "LaunchScript",
opts: []CreateOpt{
OptCreateWithID(testID),
OptCreateWithTime(testTime),
OptCreateWithLaunchScript("#!/usr/bin/env launch-script\n"),
},
},
{
name: "EmptyCloseOnUnload",
opts: []CreateOpt{
Expand Down
1 change: 0 additions & 1 deletion pkg/sif/sif.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ import (

// SIF header constants and quantities.
const (
hdrLaunch = "#!/usr/bin/env run-singularity\n"
hdrLaunchLen = 32 // len("#!/usr/bin/env... ")
hdrMagic = "SIF_MAGIC"
hdrMagicLen = 10 // len("SIF_MAGIC")
Expand Down
2 changes: 1 addition & 1 deletion pkg/sif/sif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestHeader_GetIntegrityReader(t *testing.T) {
CreatedAt: 1504657553,
ModifiedAt: 1504657653,
}
copy(h.LaunchScript[:], hdrLaunch)
copy(h.LaunchScript[:], "#!/usr/bin/env run-singularity\n")
copy(h.Magic[:], hdrMagic)
copy(h.Version[:], CurrentVersion.bytes())

Expand Down
Binary file modified pkg/sif/testdata/TestAddObject/Empty.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestAddObject/EmptyAligned.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestAddObject/EmptyNotAligned.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestAddObject/ErrInsufficientCapacity.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestAddObject/ErrPrimaryPartition.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestAddObject/NotEmpty.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestAddObject/NotEmptyAligned.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestAddObject/NotEmptyNotAligned.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestCreateContainer/Empty.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestCreateContainer/EmptyCloseOnUnload.golden
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified pkg/sif/testdata/TestCreateContainer/OneDescriptor.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestCreateContainer/TwoDescriptors.golden
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified pkg/sif/testdata/TestCreateContainerAtPath/Empty.golden
Binary file not shown.
Binary file not shown.
Binary file modified pkg/sif/testdata/TestCreateContainerAtPath/OneDescriptor.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestCreateContainerAtPath/TwoDescriptors.golden
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified pkg/sif/testdata/TestDeleteObject/Compact.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestDeleteObject/ErrObjectNotFound.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestDeleteObject/PrimaryPartition.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestDeleteObject/Zero.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestDeleteObject/ZeroCompact.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestSetPrimPart/ErrObjectNotFound.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestSetPrimPart/One.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestSetPrimPart/Two.golden
Binary file not shown.
1 change: 1 addition & 0 deletions test/gen_sifs.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func generateImages() error {
sif.OptCreateWithID(image.id),
sif.OptCreateWithTime(image.createdAt),
sif.OptCreateWithDescriptors(dis...),
sif.OptCreateWithLaunchScript("#!/usr/bin/env run-singularity\n"),
)
if err != nil {
return err
Expand Down

0 comments on commit cf8945b

Please sign in to comment.