-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
api_updateContainer.go
145 lines (127 loc) · 4.37 KB
/
api_updateContainer.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
package docker
import (
"encoding/json"
"net/http"
"os"
"fmt"
"strings"
"github.com/azukaar/cosmos-server/src/utils"
containerType "github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/docker/docker/api/types/mount"
"github.com/gorilla/mux"
)
type ContainerForm struct {
Image string `json:"image"`
User string `json:"user"`
RestartPolicy string `json:"restartPolicy"`
Env []string `json:"envVars"`
Devices []string `json:"devices"`
Labels map[string]string `json:"labels"`
PortBindings nat.PortMap `json:"portBindings"`
Volumes []mount.Mount `json:"Volumes"`
// we make this a int so that we can ignore 0
Interactive int `json:"interactive"`
NetworkMode string `json:"networkMode"`
}
func UpdateContainerRoute(w http.ResponseWriter, req *http.Request) {
if utils.AdminOnly(w, req) != nil {
return
}
utils.Log("UpdateContainer" + "Updating container")
if req.Method == "POST" {
errD := Connect()
if errD != nil {
utils.Error("UpdateContainer", errD)
utils.HTTPError(w, "Internal server error: "+errD.Error(), http.StatusInternalServerError, "DS002")
return
}
vars := mux.Vars(req)
containerName := utils.SanitizeSafe(vars["containerId"])
if os.Getenv("HOSTNAME") != "" && containerName == os.Getenv("HOSTNAME") {
utils.Error("SecureContainerRoute - Container cannot update itself", nil)
utils.HTTPError(w, "Container cannot update itself", http.StatusBadRequest, "DS003")
return
}
container, err := DockerClient.ContainerInspect(DockerContext, containerName)
if err != nil {
utils.Error("UpdateContainer", err)
utils.HTTPError(w, "Internal server error: "+err.Error(), http.StatusInternalServerError, "DS002")
return
}
var form ContainerForm
err = json.NewDecoder(req.Body).Decode(&form)
if err != nil {
utils.Error("UpdateContainer", err)
utils.HTTPError(w, "Invalid JSON", http.StatusBadRequest, "DS003")
return
}
// Update container settings
if(form.Image != "") {
container.Config.Image = form.Image
}
if(form.RestartPolicy != "") {
// THIS IS HACK BECAUSE USER IS NULLABLE, BETTER SOLUTION TO COME
container.Config.User = form.User
container.HostConfig.RestartPolicy = containerType.RestartPolicy{Name: form.RestartPolicy}
}
if(form.Env != nil) {
container.Config.Env = form.Env
}
if(form.Devices != nil) {
container.HostConfig.Devices = []containerType.DeviceMapping{}
for _, device := range form.Devices {
deviceHost := strings.Split(device, ":")[0]
deviceCont := deviceHost
if strings.Contains(device, ":") {
deviceCont = strings.Split(device, ":")[1]
}
container.HostConfig.Devices = append(container.HostConfig.Devices, containerType.DeviceMapping{
PathOnHost: deviceHost,
PathInContainer: deviceCont,
CgroupPermissions: "rwm",
})
}
}
if(form.Labels != nil) {
container.Config.Labels = form.Labels
}
if(form.PortBindings != nil) {
utils.Debug(fmt.Sprintf("UpdateContainer: PortBindings: %v", form.PortBindings))
container.HostConfig.PortBindings = form.PortBindings
container.Config.ExposedPorts = make(map[nat.Port]struct{})
for port := range form.PortBindings {
container.Config.ExposedPorts[port] = struct{}{}
}
}
if(form.Volumes != nil) {
container.HostConfig.Mounts = form.Volumes
container.HostConfig.Binds = []string{}
}
if(form.Interactive != 0) {
container.Config.Tty = form.Interactive == 2
container.Config.OpenStdin = form.Interactive == 2
}
if(form.NetworkMode != "") {
container.HostConfig.NetworkMode = containerType.NetworkMode(form.NetworkMode)
// if not bridge, remove mac address
if form.NetworkMode != "bridge" &&
form.NetworkMode != "default" {
container.Config.MacAddress = ""
}
}
_, err = EditContainer(container.ID, container, false)
if err != nil {
utils.Error("UpdateContainer: EditContainer", err)
utils.HTTPError(w, "Internal server error: "+err.Error(), http.StatusInternalServerError, "DS004")
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
})
} else {
utils.Error("UpdateContainer: Method not allowed " + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}