/
nerdctl.go
170 lines (150 loc) · 5.07 KB
/
nerdctl.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
package cmd
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/abiosoft/colima/cli"
"github.com/abiosoft/colima/cmd/root"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/environment/container/containerd"
"github.com/abiosoft/colima/util/fsutil"
"github.com/abiosoft/colima/util/osutil"
"github.com/spf13/cobra"
)
var nerdctlCmdArgs struct {
force bool
path string
usrBinWriteable bool
isColimaScript bool
}
// nerdctlCmd represents the nerdctl command
var nerdctlCmd = &cobra.Command{
Use: "nerdctl",
Aliases: []string{"nerd", "n"},
Short: "run nerdctl (requires containerd runtime)",
Long: `Run nerdctl to interact with containerd.
This requires containerd runtime.
It is recommended to specify '--' to differentiate from Colima flags.
`,
RunE: func(cmd *cobra.Command, args []string) error {
app := newApp()
r, err := app.Runtime()
if err != nil {
return err
}
if r != containerd.Name {
return fmt.Errorf("nerdctl only supports %s runtime", containerd.Name)
}
nerdctlArgs := append([]string{"sudo", "nerdctl"}, args...)
return app.SSH(nerdctlArgs...)
},
}
// nerdctlLinkFunc represents the nerdctl command
var nerdctlLinkFunc = func() *cobra.Command {
return &cobra.Command{
Use: "install",
Short: "install nerdctl alias script on the host",
Long: `Install nerdctl alias script on the host. The script will be installed at ` + nerdctlDefaultInstallPath + `.`,
Args: cobra.NoArgs,
PreRun: func(cmd *cobra.Command, args []string) {
// check if /usr/local/bin is writeable and no need for sudo
// if the path is user-specified, ignore.
if nerdctlCmdArgs.path != nerdctlDefaultInstallPath {
return
}
// attempt writing to the /usr/local/bin
tmpFile := filepath.Join(filepath.Dir(nerdctlDefaultInstallPath), "colima.tmp")
if err := os.WriteFile(tmpFile, []byte("tmp"), 0777); err == nil {
nerdctlCmdArgs.usrBinWriteable = true
_ = os.Remove(tmpFile)
}
// check if the current file (if exists) is generated by colima
// in such case no need for confirmation before overwrite
// TODO: this is too basic, should be better
if b, err := os.ReadFile(nerdctlCmdArgs.path); err == nil {
if strings.Contains(string(b), "colima nerdctl ") {
nerdctlCmdArgs.isColimaScript = true
}
}
},
RunE: func(cmd *cobra.Command, args []string) error {
exists := false
if _, err := os.Stat(nerdctlCmdArgs.path); err == nil && !nerdctlCmdArgs.force && !nerdctlCmdArgs.isColimaScript {
return fmt.Errorf("%s exists, use --force to replace", nerdctlCmdArgs.path)
} else if err == nil {
exists = true
}
t, err := template.New("").Parse(nerdctlScript)
if err != nil {
return fmt.Errorf("error parsing nerdctl script template: %w", err)
}
var values = struct {
ColimaApp string
Profile string
}{
ColimaApp: osutil.Executable(),
Profile: config.CurrentProfile().ShortName,
}
var buf bytes.Buffer
if err := t.Execute(&buf, values); err != nil {
return fmt.Errorf("error applying nerdctl script template: %w", err)
}
// /usr/local/bin writeable i.e. sudo not needed
// or user-specified install path, we assume user specified path is writeable
if nerdctlCmdArgs.usrBinWriteable || nerdctlCmdArgs.path != nerdctlDefaultInstallPath {
if exists {
if err := os.Rename(nerdctlCmdArgs.path, nerdctlCmdArgs.path+".moved"); err != nil {
return fmt.Errorf("error backing up existing file: %w", err)
}
}
if err := fsutil.MkdirAll("/usr/local/bin", 0755); err != nil {
return nil
}
return os.WriteFile(nerdctlCmdArgs.path, buf.Bytes(), 0755)
}
// sudo is needed for the default path
log.Println("/usr/local/bin not writable, sudo password required to install nerdctl binary")
if exists && !nerdctlCmdArgs.isColimaScript {
c := cli.CommandInteractive("sudo", "mv", nerdctlCmdArgs.path, nerdctlCmdArgs.path+".moved")
if err := c.Run(); err != nil {
return fmt.Errorf("error backing up existing file: %w", err)
}
}
// prepare dir
{
c := cli.CommandInteractive("sudo", "mkdir", "-p", "/usr/local/bin")
if err := c.Run(); err != nil {
return err
}
}
// install script
{
c := cli.CommandInteractive("sudo", "sh", "-c", "cat > "+nerdctlCmdArgs.path)
c.Stdin = &buf
if err := c.Run(); err != nil {
return err
}
}
// ensure it is executable
if err := cli.Command("sudo", "chmod", "+x", nerdctlCmdArgs.path).Run(); err != nil {
return err
}
return nil
},
}
}
const nerdctlDefaultInstallPath = "/usr/local/bin/nerdctl"
const nerdctlScript = `#!/usr/bin/env sh
{{.ColimaApp}} nerdctl --profile {{.Profile}} -- "$@"
`
func init() {
root.Cmd().AddCommand(nerdctlCmd)
nerdctlLink := nerdctlLinkFunc()
nerdctlCmd.AddCommand(nerdctlLink)
nerdctlLink.Flags().BoolVarP(&nerdctlCmdArgs.force, "force", "f", false, "replace "+nerdctlDefaultInstallPath+" (if exists)")
nerdctlLink.Flags().StringVar(&nerdctlCmdArgs.path, "path", nerdctlDefaultInstallPath, "path to install nerdctl binary")
}