-
Notifications
You must be signed in to change notification settings - Fork 0
/
createcontainer.go
119 lines (96 loc) · 2.75 KB
/
createcontainer.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
package handler
import (
"fmt"
"net/http"
"github.com/bnema/gordon/internal/server"
"github.com/bnema/gordon/internal/templating/cmdparams"
"github.com/bnema/gordon/internal/templating/render"
"github.com/bnema/gordon/pkg/docker"
"github.com/bnema/gordon/pkg/sanitize"
"github.com/labstack/echo/v4"
)
// FromShortIDToImageID converts a short image ID to a full image ID
func FromShortIDToImageID(ShortID string) (string, error) {
imageID, exists := safelyInteractWithIDMap(Fetch, ShortID)
if exists {
return imageID, nil
}
return "", fmt.Errorf("image ID not found")
}
// CreateContainerRoute is the route for creating a new container
func CreateContainerGET(c echo.Context, a *server.App) error {
// Retreive the ShortID of the image from the URL
ShortID := c.Param("ID")
// Convert the ShortID to a full image ID
imageID, err := FromShortIDToImageID(ShortID)
if err != nil {
return sendError(c, err)
}
// Get the image info
imageInfo, err := docker.GetImageInfo(imageID)
if err != nil {
return sendError(c, err)
}
// Extract image name
var imageName string
if len(imageInfo.RepoTags) > 0 {
imageName = imageInfo.RepoTags[0]
}
data := map[string]interface{}{
"Title": "Create a new container",
"ShortID": ShortID,
"ImageID": imageID,
"ImageName": imageName,
}
rendererData, err := render.GetHTMLRenderer("html/fragments", "createcontainer.gohtml", a.TemplateFS, a)
if err != nil {
return sendError(c, err)
}
renderedHTML, err := rendererData.Render(data, a)
if err != nil {
return sendError(c, err)
}
return c.HTML(200, renderedHTML)
}
// CreateContainerPOST handles the create container form submission
func CreateContainerPOST(c echo.Context, a *server.App) error {
// Retreive the ShortID of the image from the URL
ShortID := c.Param("ID")
// Convert the ShortID to a full image ID
imageID, err := FromShortIDToImageID(ShortID)
if err != nil {
return sendError(c, err)
}
// Get the image info
_, err = docker.GetImageInfo(imageID)
if err != nil {
return sendError(c, err)
}
// Retrieve all form parameters
formParams, err := c.FormParams()
if err != nil {
return sendError(c, err)
}
// Initialize a map to store sanitized values
sanitizedInputs := make(map[string]string)
// Iterate over all form parameters and sanitize them
for k, v := range formParams {
if len(v) > 0 {
sanitized, err := sanitize.SanitizeHTML(v[0])
if err != nil {
return sendError(c, err)
}
sanitizedInputs[k] = sanitized
}
}
cmdParams, err := cmdparams.FromInputsToCmdParams(sanitizedInputs, a)
if err != nil {
return sendError(c, err)
}
// Create the container
_, err = docker.CreateContainer(cmdParams)
if err != nil {
return sendError(c, err)
}
return c.HTML(http.StatusOK, ActionSuccess(a))
}