-
Notifications
You must be signed in to change notification settings - Fork 88
/
runc.go
67 lines (54 loc) · 1.47 KB
/
runc.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
package cluster
import (
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"github.com/pkg/errors"
)
// verifyRunc will ensure that a compatble version of runc is present in the runc directory
func verifyRunc(dataDir string) error {
installDir := filepath.Join(dataDir, "runc")
if _, err := os.Stat(installDir); os.IsNotExist(err) {
if err := os.MkdirAll(installDir, 0755); err != nil {
return errors.Wrapf(err, "create install dir")
}
}
currentVersion, err := getCurrentRuncVersion(installDir)
if err != nil {
return errors.Wrap(err, "get current version")
}
// TODO which versions do we support?
if currentVersion != "" {
return nil
}
packageURI := `https://github.com/opencontainers/runc/releases/download/v1.0.0-rc95/runc.amd64`
resp, err := http.Get(packageURI)
if err != nil {
return errors.Wrap(err, "download runc")
}
defer resp.Body.Close()
out, err := os.Create(filepath.Join(installDir, "runc"))
if err != nil {
return errors.Wrap(err, "create file")
}
defer out.Close()
if _, err := io.Copy(out, resp.Body); err != nil {
return errors.Wrap(err, "copy response")
}
out.Close()
if err := os.Chmod(filepath.Join(installDir, "runc"), 0755); err != nil {
return errors.Wrap(err, "chmod")
}
return nil
}
func getCurrentRuncVersion(dir string) (string, error) {
// TODO check runc
cmd := exec.Command(filepath.Join(dir, "runc"), "--version")
out, err := cmd.CombinedOutput()
if err != nil {
return "", nil
}
return string(out), nil
}