-
Notifications
You must be signed in to change notification settings - Fork 1
/
manifests.go
122 lines (103 loc) · 2.58 KB
/
manifests.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
package server
import (
"io"
"log"
"net/http"
"os"
"path"
"github.com/gorilla/mux"
)
func getManifest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
log.Printf("Get Manifest: %#v\n", vars)
imageName, ok := vars["imagename"]
if !ok {
w.WriteHeader(404)
return
}
tagName, ok := vars["tagname"]
if !ok {
w.WriteHeader(404)
return
}
manifestPath := path.Join(imagesDirectory, imageName, tagName)
manifestFile, err := os.Open(manifestPath)
if err != nil {
errStatus := 500
if os.IsNotExist(err) {
errStatus = 404
}
log.Printf("unable to open manifest file %q: %s\n", manifestPath, err)
w.WriteHeader(errStatus)
return
}
bytesCopied, err := io.Copy(w, manifestFile)
if err != nil {
log.Printf("unable to copy manifest file %q: %s\n", manifestPath, err)
w.WriteHeader(500)
} else {
log.Printf("copied %d bytes from manifest file %q\n", bytesCopied, manifestPath)
}
}
func putManifest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
log.Printf("Put Manifest: %#v\n", vars)
imageName, ok := vars["imagename"]
if !ok {
w.WriteHeader(404)
return
}
tagName, ok := vars["tagname"]
if !ok {
w.WriteHeader(404)
return
}
manifestDir := path.Join(imagesDirectory, imageName)
err := os.MkdirAll(manifestDir, os.FileMode(0755))
if err != nil {
log.Printf("unable to create manifest directory %q: %s\n", manifestDir, err)
w.WriteHeader(500)
return
}
manifestPath := path.Join(manifestDir, tagName)
manifestFile, err := os.OpenFile(manifestPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0644))
if err != nil {
log.Printf("unable to open manifest file %q: %s\n", manifestPath, err)
w.WriteHeader(500)
return
}
bytesCopied, err := io.Copy(manifestFile, r.Body)
if err != nil {
log.Printf("unable to copy request body to manifest file %q: %s\n", manifestPath, err)
w.WriteHeader(500)
} else {
log.Printf("copied %d bytes from request body to manifest file %q\n", bytesCopied, manifestPath)
}
w.WriteHeader(201)
}
func deleteManifest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
log.Printf("Delete Manifest: %#v\n", vars)
imageName, ok := vars["imagename"]
if !ok {
w.WriteHeader(404)
return
}
tagName, ok := vars["tagname"]
if !ok {
w.WriteHeader(404)
return
}
manifestPath := path.Join(imagesDirectory, imageName, tagName)
err := os.Remove(manifestPath)
if err != nil {
errStatus := 500
if os.IsNotExist(err) {
errStatus = 404
}
log.Printf("unable to remove manifest file %q: %s\n", manifestPath, err)
w.WriteHeader(errStatus)
return
}
w.WriteHeader(204)
}