github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/build/sources/conveyorPacker_scratch.go (about)

     1  // Copyright (c) 2018, 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 sources
     7  
     8  import (
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  
    14  	"github.com/sylabs/singularity/pkg/build/types"
    15  )
    16  
    17  // ScratchConveyor only needs to hold the conveyor to have the needed data to pack
    18  type ScratchConveyor struct {
    19  	b *types.Bundle
    20  }
    21  
    22  // ScratchConveyorPacker only needs to hold the conveyor to have the needed data to pack
    23  type ScratchConveyorPacker struct {
    24  	ScratchConveyor
    25  }
    26  
    27  // Get just stores the source
    28  func (c *ScratchConveyor) Get(b *types.Bundle) (err error) {
    29  	c.b = b
    30  
    31  	return nil
    32  }
    33  
    34  // Pack puts relevant objects in a Bundle!
    35  func (cp *ScratchConveyorPacker) Pack() (b *types.Bundle, err error) {
    36  	err = cp.insertBaseEnv()
    37  	if err != nil {
    38  		return nil, fmt.Errorf("While inserting base environment: %v", err)
    39  	}
    40  
    41  	err = cp.insertRunScript()
    42  	if err != nil {
    43  		return nil, fmt.Errorf("While inserting runscript: %v", err)
    44  	}
    45  
    46  	return cp.b, nil
    47  }
    48  
    49  func (c *ScratchConveyor) insertBaseEnv() (err error) {
    50  	if err = makeBaseEnv(c.b.Rootfs()); err != nil {
    51  		return
    52  	}
    53  	return nil
    54  }
    55  
    56  func (cp *ScratchConveyorPacker) insertRunScript() (err error) {
    57  	ioutil.WriteFile(filepath.Join(cp.b.Rootfs(), "/.singularity.d/runscript"), []byte("#!/bin/sh\n"), 0755)
    58  	if err != nil {
    59  		return
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  // CleanUp removes any tmpfs owned by the conveyorPacker on the filesystem
    66  func (c *ScratchConveyor) CleanUp() {
    67  	os.RemoveAll(c.b.Path)
    68  }