Allow installation of local modules

This commit is contained in:
Malachy Byrne 2023-05-06 23:23:48 +01:00
parent 069aed2c7a
commit b747ad94f6
Signed by: malmal200
GPG Key ID: EC21443030A655D9
2 changed files with 18 additions and 14 deletions

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
//"bytes" //"bytes"
"net/http" "net/http"
@ -51,17 +50,17 @@ func getSchema(c echo.Context) error {
key := c.Param("container") key := c.Param("container")
requestURL := fmt.Sprintf("http://%s/schema", key) requestURL := fmt.Sprintf("http://%s:8080/schema", key)
resp, err := http.Get(requestURL) resp, err := http.Get(requestURL)
if err != nil { if err != nil {
log.Fatalf("Request to container failed: %v", err) panic(err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatalf("Failed to print Body: %v", err) panic(err)
} }
return c.JSONBlob(resp.StatusCode, body) return c.JSONBlob(resp.StatusCode, body)
@ -74,14 +73,14 @@ func getComponent(c echo.Context) error {
requestURL := fmt.Sprintf("http://%s:8080/component/%s", container, component) requestURL := fmt.Sprintf("http://%s:8080/component/%s", container, component)
resp, err := http.Get(requestURL) resp, err := http.Get(requestURL)
if err != nil { if err != nil {
log.Fatalf("Request to container failed: %v", err) panic(err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatalf("Failed to print Body: %v", err) panic(err)
} }
return c.JSONBlob(http.StatusOK, body) return c.JSONBlob(http.StatusOK, body)
@ -94,14 +93,14 @@ func postComponent(c echo.Context) error {
requestURL := fmt.Sprintf("http://%s:8080/component/%s", container, component) requestURL := fmt.Sprintf("http://%s:8080/component/%s", container, component)
resp, err := http.Post(requestURL, "application/json", c.Request().Body) resp, err := http.Post(requestURL, "application/json", c.Request().Body)
if err != nil { if err != nil {
log.Fatalf("Request to container failed: %v", err) panic(err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatalf("Failed to print Body: %v", err) panic(err)
} }
return c.JSONBlob(http.StatusOK, body) return c.JSONBlob(http.StatusOK, body)
@ -155,14 +154,14 @@ func postFromModule(c echo.Context) error {
requestURL := fmt.Sprintf("http://%s", url) requestURL := fmt.Sprintf("http://%s", url)
resp, err := http.Post(requestURL, "application/json", c.Request().Body) resp, err := http.Post(requestURL, "application/json", c.Request().Body)
if err != nil { if err != nil {
log.Fatalf("Request to container failed: %v", err) panic(err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatalf("Failed to print Body: %v", err) panic(err)
} }
return c.JSONBlob(http.StatusOK, body) return c.JSONBlob(http.StatusOK, body)
@ -258,6 +257,7 @@ func CreateModule(c echo.Context) error {
s, _ := json.MarshalIndent(module, "", " ") s, _ := json.MarshalIndent(module, "", " ")
fmt.Println(string(s)) fmt.Println(string(s))
mod_id := docker.CreateModule(module) mod_id := docker.CreateModule(module)
fmt.Println(mod_id)
stmt, err := db.Prepare(`INSERT INTO Module (name, container_name) VALUES ($1, $2);`) stmt, err := db.Prepare(`INSERT INTO Module (name, container_name) VALUES ($1, $2);`)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -74,17 +74,18 @@ func CreateModule(module Module) string {
ContainerConfig(module, &container, &host, &network) ContainerConfig(module, &container, &host, &network)
image_out, err := cli.ImagePull(ctx, module.Image, types.ImagePullOptions{}) image_out, err := cli.ImagePull(ctx, module.Image, types.ImagePullOptions{})
if err != nil { if err == nil {
panic(err)
}
defer image_out.Close() defer image_out.Close()
io.Copy(ioutil.Discard, image_out) io.Copy(ioutil.Discard, image_out)
}
fmt.Println("test1")
resp, err := cli.ContainerCreate(ctx, &container, &host, &network, nil, module.Name) resp, err := cli.ContainerCreate(ctx, &container, &host, &network, nil, module.Name)
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("test2")
if _, err := cli.NetworkInspect(ctx, module.ID, types.NetworkInspectOptions{}); err != nil { if _, err := cli.NetworkInspect(ctx, module.ID, types.NetworkInspectOptions{}); err != nil {
networkConfig := types.NetworkCreate{ networkConfig := types.NetworkCreate{
@ -94,14 +95,17 @@ func CreateModule(module Module) string {
panic(err) panic(err)
} }
} }
fmt.Println("test3")
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err) panic(err)
} }
fmt.Println("test3")
if err := cli.NetworkConnect(ctx, module.ID, os.Getenv("hostname"), &networktypes.EndpointSettings{}); err != nil { if err := cli.NetworkConnect(ctx, module.ID, os.Getenv("hostname"), &networktypes.EndpointSettings{}); err != nil {
panic(err) panic(err)
} }
fmt.Println("test4")
return resp.ID return resp.ID
} }