-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathmain.go
More file actions
182 lines (155 loc) · 4.45 KB
/
Copy pathmain.go
File metadata and controls
182 lines (155 loc) · 4.45 KB
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
182
/*
Copyright The Accelerated Container Image Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/containerd/accelerated-container-image/pkg/snapshot"
"github.com/containerd/accelerated-container-image/pkg/types"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var (
commitID string = "unknown"
)
func main() {
app := &cli.App{
Name: "overlaybd-attacher",
Usage: "A CLI tool to attach/detach overlaybd devices",
Version: commitID,
Commands: []*cli.Command{
{
Name: "attach",
Usage: "Attach an overlaybd device with given ID and configuration",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "Device ID (snapshot ID)",
Required: true,
},
&cli.StringFlag{
Name: "config",
Usage: "Path to overlaybd configuration file (config.v1.json)",
Required: true,
},
&cli.BoolFlag{
Name: "verbose",
Usage: "Enable verbose logging",
Aliases: []string{"v"},
},
&cli.BoolFlag{
Name: "with-devid",
Usage: "Include devid in dev_config",
Value: false,
},
},
Action: attachAction,
},
{
Name: "detach",
Usage: "Detach an overlaybd device with given ID",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "Device ID (snapshot ID)",
Required: true,
},
&cli.BoolFlag{
Name: "verbose",
Usage: "Enable verbose logging",
Aliases: []string{"v"},
},
},
Action: detachAction,
},
},
}
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func attachAction(c *cli.Context) error {
if c.Bool("verbose") {
logrus.SetLevel(logrus.DebugLevel)
}
id := c.String("id")
tenant := -1
configPath := c.String("config")
retryCount := 5
withDevID := c.Bool("with-devid")
if _, err := os.Stat(configPath); err != nil {
return fmt.Errorf("config file does not exist: %s: %w", configPath, err)
}
data, err := os.ReadFile(configPath)
if err != nil {
return fmt.Errorf("failed to read config file: %w", err)
}
var config types.OverlayBDBSConfig
if err := json.Unmarshal(data, &config); err != nil {
return fmt.Errorf("invalid JSON config file: %w", err)
}
absConfigPath, err := filepath.Abs(configPath)
if err != nil {
return fmt.Errorf("failed to get absolute path for config: %w", err)
}
var resultFile string
if config.ResultFile != "" {
if filepath.IsAbs(config.ResultFile) {
resultFile = config.ResultFile
} else {
resultFile = filepath.Join(filepath.Dir(absConfigPath), config.ResultFile)
}
} else {
resultFile = filepath.Join(filepath.Dir(absConfigPath), "init-debug.log")
}
ctx := context.Background()
devID := ""
if withDevID {
devID = id
}
params := snapshot.NewAttachDeviceParams(id, tenant, absConfigPath, resultFile, devID)
logrus.Infof("attaching device: id=%s, tenant=%d, config=%s, resultFile=%s, withDevID=%t", id, tenant, absConfigPath, resultFile, withDevID)
var devName string
var lastErr error
for attempt := 1; attempt <= retryCount; attempt++ {
if attempt > 1 {
logrus.Warnf("retry attempt %d/%d after error: %v", attempt, retryCount, lastErr)
time.Sleep(1 * time.Second)
}
devName, lastErr = snapshot.AttachDevice(ctx, params)
if lastErr == nil {
fmt.Println(devName)
logrus.Infof("device attached successfully: %s", devName)
return nil
}
}
return fmt.Errorf("failed to attach device after %d attempts: %w", retryCount, lastErr)
}
func detachAction(c *cli.Context) error {
if c.Bool("verbose") {
logrus.SetLevel(logrus.DebugLevel)
}
id := c.String("id")
tenant := -1
ctx := context.Background()
logrus.Infof("detaching device: id=%s, tenant=%d", id, tenant)
if err := snapshot.DetachDevice(ctx, id, tenant); err != nil {
return fmt.Errorf("failed to detach device: %w", err)
}
logrus.Infof("device detached successfully")
return nil
}