-
Notifications
You must be signed in to change notification settings - Fork 132
/
instance_stop_linux.go
140 lines (120 loc) · 3.74 KB
/
instance_stop_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2021 Apptainer a Series of LF Projects LLC
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2018-2020, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package cli
import (
"errors"
"os"
"syscall"
"time"
"github.com/apptainer/apptainer/docs"
"github.com/apptainer/apptainer/internal/app/apptainer"
"github.com/apptainer/apptainer/internal/pkg/util/signal"
"github.com/apptainer/apptainer/pkg/cmdline"
"github.com/apptainer/apptainer/pkg/sylog"
"github.com/spf13/cobra"
)
func init() {
addCmdInit(func(cmdManager *cmdline.CommandManager) {
cmdManager.RegisterFlagForCmd(&instanceStopUserFlag, instanceStopCmd)
cmdManager.RegisterFlagForCmd(&instanceStopAllFlag, instanceStopCmd)
cmdManager.RegisterFlagForCmd(&instanceStopForceFlag, instanceStopCmd)
cmdManager.RegisterFlagForCmd(&instanceStopSignalFlag, instanceStopCmd)
cmdManager.RegisterFlagForCmd(&instanceStopTimeoutFlag, instanceStopCmd)
})
}
// -u|--user
var instanceStopUser string
var instanceStopUserFlag = cmdline.Flag{
ID: "instanceStopUserFlag",
Value: &instanceStopUser,
DefaultValue: "",
Name: "user",
ShortHand: "u",
Usage: "if running as root, stop instances belonging to user",
Tag: "<username>",
EnvKeys: []string{"USER"},
}
// -a|--all
var instanceStopAll bool
var instanceStopAllFlag = cmdline.Flag{
ID: "instanceStopAllFlag",
Value: &instanceStopAll,
DefaultValue: false,
Name: "all",
ShortHand: "a",
Usage: "stop all user's instances",
EnvKeys: []string{"ALL"},
}
// -f|--force
var instanceStopForce bool
var instanceStopForceFlag = cmdline.Flag{
ID: "instanceStopForceFlag",
Value: &instanceStopForce,
DefaultValue: false,
Name: "force",
ShortHand: "F",
Usage: "force kill instance",
EnvKeys: []string{"FORCE"},
}
// -s|--signal
var instanceStopSignal string
var instanceStopSignalFlag = cmdline.Flag{
ID: "instanceStopSignalFlag",
Value: &instanceStopSignal,
DefaultValue: "",
Name: "signal",
ShortHand: "s",
Usage: "signal sent to the instance",
Tag: "<signal>",
EnvKeys: []string{"SIGNAL"},
}
// -t|--timeout
var instanceStopTimeout int
var instanceStopTimeoutFlag = cmdline.Flag{
ID: "instanceStopTimeoutFlag",
Value: &instanceStopTimeout,
DefaultValue: 10,
Name: "timeout",
ShortHand: "t",
Usage: "force kill non stopped instances after X seconds",
}
// apptainer instance stop
var instanceStopCmd = &cobra.Command{
Args: cobra.RangeArgs(0, 1),
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 && !instanceStopAll {
return errors.New("invalid command")
}
uid := os.Getuid()
if instanceStopUser != "" && uid != 0 {
sylog.Fatalf("Only root user can stop user's instances")
}
sig := syscall.SIGINT
if instanceStopSignal != "" {
var err error
sig, err = signal.Convert(instanceStopSignal)
if err != nil {
sylog.Fatalf("Could not convert stop signal: %s", err)
}
}
if instanceStopForce {
sig = syscall.SIGKILL
}
name := "*"
if len(args) > 0 {
name = args[0]
}
timeout := time.Duration(instanceStopTimeout) * time.Second
return apptainer.StopInstance(name, instanceStopUser, sig, timeout)
},
Use: docs.InstanceStopUse,
Short: docs.InstanceStopShort,
Long: docs.InstanceStopLong,
Example: docs.InstanceStopExample,
}