-
Notifications
You must be signed in to change notification settings - Fork 124
/
checkpoint.go
181 lines (144 loc) · 4.74 KB
/
checkpoint.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) Contributors to the Apptainer project, established as
// 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
// 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 (
"fmt"
"os"
"path/filepath"
"text/tabwriter"
"github.com/apptainer/apptainer/docs"
"github.com/apptainer/apptainer/internal/pkg/checkpoint/dmtcp"
"github.com/apptainer/apptainer/internal/pkg/instance"
"github.com/apptainer/apptainer/pkg/cmdline"
"github.com/apptainer/apptainer/pkg/sylog"
"github.com/spf13/cobra"
)
const listLine = "%s\n"
func init() {
addCmdInit(func(cmdManager *cmdline.CommandManager) {
cmdManager.RegisterCmd(CheckpointCmd)
cmdManager.RegisterSubCmd(CheckpointCmd, CheckpointListCmd)
cmdManager.RegisterSubCmd(CheckpointCmd, CheckpointInstanceCmd)
cmdManager.RegisterSubCmd(CheckpointCmd, CheckpointCreateCmd)
cmdManager.RegisterSubCmd(CheckpointCmd, CheckpointDeleteCmd)
cmdManager.RegisterFlagForCmd(&actionHomeFlag, CheckpointInstanceCmd)
})
}
func checkpointPreRun(cmd *cobra.Command, args []string) {
dmtcp.QuickInstallationCheck()
}
// CheckpointCmd represents the checkpoint command.
var CheckpointCmd = &cobra.Command{
Run: nil,
Use: docs.CheckpointUse,
Short: docs.CheckpointShort,
Long: docs.CheckpointLong,
Example: docs.CheckpointExample,
DisableFlagsInUseLine: true,
}
// CheckpointListCmd apptainer checkpoint list
var CheckpointListCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
PreRun: checkpointPreRun,
Run: func(cmd *cobra.Command, args []string) {
m := dmtcp.NewManager()
entries, err := m.List()
if err != nil {
if err != nil {
sylog.Fatalf("Failed to get checkpoint entries: %v", err)
}
}
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(tw, listLine, "NAME")
for _, e := range entries {
fmt.Fprintf(tw, listLine, filepath.Base(e.Path()))
}
tw.Flush()
},
Use: docs.CheckpointListUse,
Short: docs.CheckpointListShort,
Long: docs.CheckpointListLong,
Example: docs.CheckpointListExample,
DisableFlagsInUseLine: true,
}
// CheckpointCreateCmd apptainer checkpoint create
var CheckpointCreateCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
PreRun: checkpointPreRun,
Run: func(cmd *cobra.Command, args []string) {
name := args[0]
m := dmtcp.NewManager()
_, err := m.Get(name)
if err == nil {
sylog.Fatalf("Checkpoint %q already exists.", name)
}
_, err = m.Create(name)
if err != nil {
sylog.Fatalf("Failed to create checkpoint: %s", err)
}
sylog.Infof("Checkpoint %q created.", name)
},
Use: docs.CheckpointCreateUse,
Short: docs.CheckpointCreateShort,
Long: docs.CheckpointCreateLong,
Example: docs.CheckpointCreateExample,
DisableFlagsInUseLine: true,
}
// CheckpointDeleteCmd apptainer checkpoint delete
var CheckpointDeleteCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
PreRun: checkpointPreRun,
Run: func(cmd *cobra.Command, args []string) {
name := args[0]
m := dmtcp.NewManager()
err := m.Delete(name)
if err != nil {
sylog.Fatalf("Failed to delete checkpoint entries: %v", err)
}
sylog.Infof("Checkpoint %q deleted.", name)
},
Use: docs.CheckpointDeleteUse,
Short: docs.CheckpointDeleteShort,
Long: docs.CheckpointDeleteLong,
Example: docs.CheckpointDeleteExample,
DisableFlagsInUseLine: true,
}
var CheckpointInstanceCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
checkpointPreRun(cmd, args)
actionPreRun(cmd, args)
},
Run: func(cmd *cobra.Command, args []string) {
instanceName := args[0]
file, err := instance.Get(instanceName, instance.AppSubDir)
if err != nil {
sylog.Fatalf("Could not retrieve instance file: %s", err)
}
if file.Checkpoint == "" {
sylog.Fatalf("This instance was not started with checkpointing.")
}
m := dmtcp.NewManager()
e, err := m.Get(file.Checkpoint)
if err != nil {
sylog.Fatalf("Failed to get checkpoint entry: %v", err)
}
port, err := e.CoordinatorPort()
if err != nil {
sylog.Fatalf("Failed to parse port file for coordinator pord: %s", err)
}
sylog.Infof("Using checkpoint %q", e.Name())
a := append([]string{"/.singularity.d/actions/exec"}, dmtcp.CheckpointArgs(port)...)
execStarter(cmd, "instance://"+args[0], a, "")
},
Use: docs.CheckpointInstanceUse,
Short: docs.CheckpointInstanceShort,
Long: docs.CheckpointInstanceLong,
Example: docs.CheckpointInstanceExample,
DisableFlagsInUseLine: true,
}