aboutsummaryrefslogtreecommitdiff
path: root/app/main.go
blob: b3f726559b86fb6b4551fb9f1e8afbb1648d79d8 (plain)
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
183
184
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"syscall"
)

type TokenResponse struct {
	Token       string `json:"token"`
	AccessToken string `json:"access_token"`
	Expires     int    `json:"expires_in"`
	IssuedAt    string `json:"issued_at"`
}

type ManiFest struct {
	Name     string     `json:"name"`
	Tag      string     `json:"tag"`
	FSLayers []fsLayers `json:"fsLayers"`
}

type fsLayers struct {
	BlobSum string `json:"blobSum"`
}

func getToken(repo, image string) (*TokenResponse, error) {
	url := fmt.Sprintf("https://auth.docker.io/token?service=registry.docker.io&scope=repository:%s/%s:pull", repo, image)

	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	var token TokenResponse

	err = json.NewDecoder(resp.Body).Decode(&token)
	if err != nil {
		return nil, err
	}

	return &token, nil
}

func getManifest(repo, image, tag string, token string) (*ManiFest, error) {
	url := fmt.Sprintf("https://registry.hub.docker.com/v2/%s/%s/manifests/%s", repo, image, tag)

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}

	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
	req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.list.v1+json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	var manifest ManiFest

	err = json.NewDecoder(resp.Body).Decode(&manifest)
	if err != nil {
		return nil, err
	}

	return &manifest, nil
}

func downloadBlob(image, blobSum, token, tmpDir string) error {
	url := fmt.Sprintf("https://registry-1.docker.io/v2/library/%s/blobs/%s", image, blobSum)

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return err
	}

	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}

	defer resp.Body.Close()

	out, err := os.Create("/tmp/layer")
	if err != nil {
		return err
	}

	defer out.Close()

	_, err = out.ReadFrom(resp.Body)
	if err != nil {
		return err
	}

	err = exec.Command("tar", "xf", "/tmp/layer", "-C", tmpDir).Run()
	if err != nil {
		return err
	}

	return os.RemoveAll("/tmp/layer")
}

func main() {
	img := os.Args[2]
	split := strings.Split(img, ":")
	repo := "library"
	image := split[0]
	tag := "latest"
	if len(split) == 2 {
		tag = split[1]
	}

	command := os.Args[3]
	args := os.Args[4:len(os.Args)]
	tmpDir := "/tmp/mydocker"

	_ = os.Mkdir(tmpDir, 0755)
	defer os.RemoveAll(tmpDir)

	err := exec.Command("mkdir", "-p", filepath.Join(tmpDir, filepath.Dir(command))).Run()
	if err != nil {
		fmt.Printf("Err: %v", err)
		os.Exit(1)
	}

	token, err := getToken(repo, image)
	if err != nil {
		fmt.Printf("Err: %v", err)
		os.Exit(1)
	}

	manifest, err := getManifest(repo, image, tag, token.Token)
	if err != nil {
		fmt.Printf("Err: %v", err)
		os.Exit(1)
	}

	for _, layer := range manifest.FSLayers {
		if err = downloadBlob(image, layer.BlobSum, token.Token, tmpDir); err != nil {
			fmt.Printf("Err: %v", err)
			os.Exit(1)
		}
	}

	cmd := exec.Command(command, args...)

	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err = exec.Command("cp", command, filepath.Join(tmpDir, command)).Run()
	if err != nil {
		fmt.Printf("Err: %v", err)
		os.Exit(1)
	}

	cmd.SysProcAttr = &syscall.SysProcAttr{
		Chroot:     tmpDir,
		Cloneflags: syscall.CLONE_NEWPID,
	}

	err = cmd.Run()
	if err != nil {
		if exitError, ok := err.(*exec.ExitError); ok {
			os.Exit(exitError.ExitCode())
		} else {
			fmt.Printf("Err: %v", err)
			os.Exit(1)
		}
	}
}