github.com/apptainer/singularity@v3.1.1+incompatible/internal/app/singularity/oci_delete_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  	"fmt"
    10  
    11  	"github.com/sylabs/singularity/internal/pkg/instance"
    12  	"github.com/sylabs/singularity/internal/pkg/sylog"
    13  	"github.com/sylabs/singularity/internal/pkg/util/exec"
    14  	"github.com/sylabs/singularity/pkg/ociruntime"
    15  )
    16  
    17  // OciDelete deletes container resources
    18  func OciDelete(containerID string) error {
    19  	engineConfig, err := getEngineConfig(containerID)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	switch engineConfig.State.Status {
    25  	case ociruntime.Running:
    26  		return fmt.Errorf("cannot delete '%s', the state of the container must be created or stopped", containerID)
    27  	case ociruntime.Stopped:
    28  	case ociruntime.Created:
    29  		if err := OciKill(containerID, "SIGTERM", 2); err != nil {
    30  			return err
    31  		}
    32  		engineConfig, err = getEngineConfig(containerID)
    33  		if err != nil {
    34  			return err
    35  		}
    36  	}
    37  
    38  	hooks := engineConfig.OciConfig.Hooks
    39  	if hooks != nil {
    40  		for _, h := range hooks.Poststop {
    41  			if err := exec.Hook(&h, &engineConfig.State.State); err != nil {
    42  				sylog.Warningf("%s", err)
    43  			}
    44  		}
    45  	}
    46  
    47  	// remove instance files
    48  	file, err := instance.Get(containerID, instance.OciSubDir)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	return file.Delete()
    53  }