github.com/apptainer/singularity@v3.1.1+incompatible/internal/app/singularity/oci_linux.go (about)

     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 singularity
     7  
     8  import (
     9  	"encoding/json"
    10  	"fmt"
    11  	"os"
    12  
    13  	"github.com/sylabs/singularity/internal/pkg/instance"
    14  	"github.com/sylabs/singularity/internal/pkg/runtime/engines/config"
    15  	"github.com/sylabs/singularity/internal/pkg/runtime/engines/oci"
    16  	"github.com/sylabs/singularity/internal/pkg/sylog"
    17  	"github.com/sylabs/singularity/pkg/ociruntime"
    18  )
    19  
    20  // OciArgs contains CLI arguments
    21  type OciArgs struct {
    22  	BundlePath     string
    23  	LogPath        string
    24  	LogFormat      string
    25  	SyncSocketPath string
    26  	EmptyProcess   bool
    27  	PidFile        string
    28  	FromFile       string
    29  	KillSignal     string
    30  }
    31  
    32  func getCommonConfig(containerID string) (*config.Common, error) {
    33  	commonConfig := config.Common{
    34  		EngineConfig: &oci.EngineConfig{},
    35  	}
    36  
    37  	file, err := instance.Get(containerID, instance.OciSubDir)
    38  	if err != nil {
    39  		return nil, fmt.Errorf("no container found with name %s", containerID)
    40  	}
    41  
    42  	if err := json.Unmarshal(file.Config, &commonConfig); err != nil {
    43  		return nil, fmt.Errorf("failed to read %s container configuration: %s", containerID, err)
    44  	}
    45  
    46  	return &commonConfig, nil
    47  }
    48  
    49  func getEngineConfig(containerID string) (*oci.EngineConfig, error) {
    50  	commonConfig, err := getCommonConfig(containerID)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return commonConfig.EngineConfig.(*oci.EngineConfig), nil
    55  }
    56  
    57  func getState(containerID string) (*ociruntime.State, error) {
    58  	engineConfig, err := getEngineConfig(containerID)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return &engineConfig.State, nil
    63  }
    64  
    65  func exitContainer(containerID string, delete bool) {
    66  	state, err := getState(containerID)
    67  	if err != nil {
    68  		if !delete {
    69  			sylog.Errorf("%s", err)
    70  			os.Exit(1)
    71  		}
    72  		return
    73  	}
    74  
    75  	if state.ExitCode != nil {
    76  		defer os.Exit(*state.ExitCode)
    77  	}
    78  
    79  	if delete {
    80  		if err := OciDelete(containerID); err != nil {
    81  			sylog.Errorf("%s", err)
    82  		}
    83  	}
    84  }