github.com/criyle/go-sandbox@v0.10.3/container/container_cmd_linux.go (about)

     1  package container
     2  
     3  import (
     4  	"bufio"
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/criyle/go-sandbox/pkg/unixsocket"
    12  )
    13  
    14  func (c *containerServer) handlePing() error {
    15  	return c.sendReply(reply{}, unixsocket.Msg{})
    16  }
    17  
    18  func (c *containerServer) handleConf(conf *confCmd) error {
    19  	if conf != nil {
    20  		c.containerConfig = conf.Conf
    21  		if err := initContainer(conf.Conf); err != nil {
    22  			return err
    23  		}
    24  		if c.ContainerUID == 0 {
    25  			c.ContainerUID = containerUID
    26  		}
    27  		if c.ContainerGID == 0 {
    28  			c.ContainerGID = containerGID
    29  		}
    30  		env, err := readDotEnv()
    31  		if err != nil {
    32  			return err
    33  		}
    34  		c.defaultEnv = env
    35  	}
    36  	return c.sendReply(reply{}, unixsocket.Msg{})
    37  }
    38  
    39  func (c *containerServer) handleOpen(open []OpenCmd) error {
    40  	if len(open) == 0 {
    41  		return c.sendErrorReply("open: no open parameter received")
    42  	}
    43  
    44  	// open files
    45  	fds := make([]int, 0, len(open))
    46  	fileToClose := make([]*os.File, 0, len(open)) // let sendMsg close these files
    47  	for _, o := range open {
    48  		outFile, err := os.OpenFile(o.Path, o.Flag, o.Perm)
    49  		if err != nil {
    50  			return c.sendErrorReply("open: %v", err)
    51  		}
    52  		fileToClose = append(fileToClose, outFile)
    53  		fds = append(fds, int(outFile.Fd()))
    54  	}
    55  
    56  	return c.sendReplyFiles(reply{}, unixsocket.Msg{Fds: fds}, fileToClose)
    57  }
    58  
    59  func (c *containerServer) handleDelete(delete *deleteCmd) error {
    60  	if delete == nil {
    61  		return c.sendErrorReply("delete: no parameter provided")
    62  	}
    63  	if err := os.Remove(delete.Path); err != nil {
    64  		return c.sendErrorReply("delete: %v", err)
    65  	}
    66  	return c.sendReply(reply{}, unixsocket.Msg{})
    67  }
    68  
    69  func (c *containerServer) handleReset() error {
    70  	for _, m := range c.Mounts {
    71  		if !m.IsTmpFs() {
    72  			continue
    73  		}
    74  		if err := removeContents(filepath.Join("/", m.Target)); err != nil {
    75  			return c.sendErrorReply("reset: %v %v", m.Target, err)
    76  		}
    77  	}
    78  	return c.sendReply(reply{}, unixsocket.Msg{})
    79  }
    80  
    81  // readDotEnv attempts to read /.env file and save as default environment variables
    82  func readDotEnv() ([]string, error) {
    83  	f, err := os.Open("/.env")
    84  	if err != nil {
    85  		if errors.Is(err, os.ErrNotExist) {
    86  			return nil, nil
    87  		}
    88  	}
    89  	defer f.Close()
    90  
    91  	var ret []string
    92  	scanner := bufio.NewScanner(f)
    93  	for scanner.Scan() {
    94  		line := strings.TrimSpace(scanner.Text())
    95  		if len(line) == 0 || strings.HasPrefix(line, "#") {
    96  			continue
    97  		}
    98  		if !strings.Contains(line, "=") {
    99  			return nil, fmt.Errorf("dotenv: invalid line %s", line)
   100  		}
   101  		ret = append(ret, line)
   102  	}
   103  	return ret, nil
   104  }