/
docker.go
153 lines (124 loc) · 4.12 KB
/
docker.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
// Copyright (c) 2020-2022 TU Delft & Valentijn van de Beek <v.d.vandebeek@student.tudelft.nl> All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package api
import (
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"os/exec"
"strconv"
"github.com/baas-project/baas/pkg/model/images"
"github.com/baas-project/baas/pkg/model/user"
"github.com/baas-project/baas/pkg/fs"
log "github.com/sirupsen/logrus"
)
func extractPart(r *http.Request) (*multipart.Part, error) {
// Get the reader and writer of the multireader
mr, err := r.MultipartReader()
if err != nil {
log.Errorf("cannot parse POST form: %v", err)
return nil, err
}
p, err := mr.NextPart()
if err != nil {
log.Errorf("cannot fetch image from database: %v", err)
return nil, err
}
return p, nil
}
func runDockerCommand(dir string) error {
cmd := exec.Command("utils/docker2image.sh", dir)
stderr, err := cmd.StdoutPipe()
if err != nil {
log.Errorf("Failed to open a standard output pipe: %v", err)
return err
}
if err := cmd.Run(); err != nil {
log.Errorf("Cannot run the docker2image script: %v", err)
// If we error out here we probably don't care
slurp, _ := io.ReadAll(stderr)
log.Errorf("Output: %s", slurp)
return err
}
return nil
}
func renameFiles(dir string, diskpath string, filepathfmt string, uniqueID string, version uint64) error {
if err := os.Rename(dir+"/"+"image.img",
fmt.Sprintf(diskpath+filepathfmt, uniqueID, version)); err != nil {
log.Errorf("Failed to move image file: %v", err)
return err
}
newPath := fmt.Sprintf(diskpath+"/%s/Dockerfile-%d", uniqueID, version)
if err := os.Rename(dir+"/"+"Dockerfile", newPath); err != nil {
log.Errorf("Failed to move dockerfile: %v", err)
return err
}
return nil
}
// RunDocker takes a Dockerfile and generates a bootable OS image
// Request request: /image/{uuid}/docker
func (api_ *API) RunDocker(w http.ResponseWriter, r *http.Request) {
uniqueID, err := GetTag("uuid", w, r)
if err != nil {
return
}
version, err := CreateNewVersion(uniqueID, api_.store)
if err != nil {
http.Error(w, "cannot fetch the image from the database", http.StatusNotFound)
log.Errorf("cannot fetch image from database: %v", err)
return
}
p, err := extractPart(r)
if err != nil {
http.Error(w, "cannot parse POST form", http.StatusBadRequest)
return
}
// One liner which closes the file at the end of the call.
defer func() {
if err = p.Close(); err != nil {
log.Errorf("Cannot close upload file: %v", err)
}
}()
dir := api_.diskpath + "/" + uniqueID
// Delete the directory at the end of the program
// defer os.RemoveAll(dir)
// Write the docker file to the directory
f, err := os.OpenFile(dir+"/Dockerfile", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
http.Error(w, "Cannot compile docker image", http.StatusInternalServerError)
log.Errorf("Cannot write to DockerImage file: %v", err)
return
}
defer f.Close()
err = fs.CopyStream(p, f)
if err != nil {
http.Error(w, "Cannot compile docker image", http.StatusInternalServerError)
log.Errorf("Cannot write to DockerImage file: %v", err)
return
}
// Run a shell script which creates the image
// It is written in shell for convience, it could be rewritten in Go if someone really cares.
if runDockerCommand(dir) != nil {
http.Error(w, "cannot compile docker image", http.StatusInternalServerError)
return
}
if renameFiles(dir, api_.diskpath, images.FilePathFmt, uniqueID, version.Version) != nil {
http.Error(w, "cannot compile docker image", http.StatusInternalServerError)
return
}
http.Error(w, "Successfully uploaded image: "+strconv.FormatUint(version.Version, 10), http.StatusOK)
}
// RegisterImageDockerHandlers sets the metadata for each of the routes and registers them to the global handler
func (api_ *API) RegisterImageDockerHandlers() {
api_.Routes = append(api_.Routes, Route{
URI: "/image/{uuid}/docker",
Permissions: []user.UserRole{user.Moderator, user.Admin},
UserAllowed: true,
Handler: api_.RunDocker,
Method: http.MethodPost,
Description: "Uploads a new version of the image",
})
}